UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 11.2 kB
import"./openclaw-state-read-map-size-DJbMcZYM.mjs";import{v as e}from"./openclaw-state-read-home-dir-BQZX1pD2.mjs";import"./openclaw-state-read-pool-1Cp_Z7yz.mjs";import"./openclaw-state-read-openclaw-state-db-cache-BVY8IYkn.mjs";import{v as t}from"./openclaw-state-read-read-open-flags-CtNL1rJs.mjs";import{s as n}from"./openclaw-state-read-openclaw-state-db.paths-CFemYLpA.mjs";import{a as r}from"./openclaw-state-read-record-coerce-D1s3ReAs.mjs";import"./openclaw-state-read-sqlite-worker-operation-settlement-ogK5IsJy.mjs";import{u as i}from"./openclaw-state-read-sqlite-post-commit-CEHMNIsB.mjs";import{a,c as o,d as s,o as c,r as l,t as u,u as d}from"./openclaw-state-read-kysely-sync-DQyYiAw5.mjs";import"./openclaw-state-read-sqlite-worker-operation-admission-DTfv_SKu.mjs";import"./openclaw-state-read-sqlite-transaction-V1rK3HH2.mjs";import{r as f}from"./openclaw-state-read-openclaw-state-db-schema-helpers-BkqUYEFY.mjs";import{d as p,f as m,n as h,p as g,u as _}from"./openclaw-state-read-sqlite-snapshot-staging-owner-B9a1YbDW.mjs";import"./openclaw-state-read-openclaw-state-db-DnrW7QY9.mjs";import{a as v,i as y}from"./openclaw-state-read-agent-id-DmscQLD6.mjs";import{l as b}from"./openclaw-state-read-openclaw-state-db-schema-migration-required-D_rQ-tLc.mjs";import"./openclaw-state-read-openclaw-state-db-readonly-CDG18cNK.mjs";import"./openclaw-state-read-openclaw-state-db-handle-D9nuaODy.mjs";import{c as x,n as S}from"./openclaw-state-read-json-values-CH7dRPGy.mjs";import{o as C}from"./openclaw-state-read-sqlite-integrity-6ijoQyXy.mjs";import{n as w,t as T}from"./openclaw-state-read-sqlite-number-CpW6Oa2f.mjs";import{t as E}from"./openclaw-state-read-state-domain-publication-CN6b3HGd.mjs";import"./openclaw-state-read-session-entry-current-admission.worker-ClXb-bLj.mjs";import{n as D}from"./openclaw-state-read-openclaw-state-read-handlers-pzSw4079.mjs";import"node:v8";import{AsyncLocalStorage as O}from"node:async_hooks";import{toUSVString as k}from"node:util";const A=e(Symbol.for(`openclaw.pluginStatePublication`),()=>({reads:new O,pending:/* @__PURE__ */ new Map,unavailable:/* @__PURE__ */ new Set})),j=E({domain:`plugin-state`,isFact:(e,t)=>t.kind===`absent`||t.kind===`postimage`&&isRow(t.value)&&pluginStatePublicationKey(t.value)===e,invalidReceiptMessage:`Plugin state commit receipt is invalid`,installFailureMessage:`Plugin state fact installation failed`,mutationDuringInstallMessage:`Plugin state cannot mutate during fact installation`,closedMessage:`Plugin state publication is closed`,changedOwnerMessage:`Plugin state receipt changed owner`});function pluginStatePublicationKey(e){return JSON.stringify([e.plugin_id,e.namespace,k(e.entry_key)])}const M=e(Symbol.for(`openclaw.pluginStatePublicationReconciliation`),()=>({epochs:/* @__PURE__ */ new Map,active:/* @__PURE__ */ new Map}));j.subscribeFacts(e=>{e.kind===`unknown`&&(A.unavailable.add(e.identity),M.epochs.set(e.identity,(M.epochs.get(e.identity)??0)+1))}),j.subscribeFacts;function isRow(e){return r(e)&&typeof e.plugin_id==`string`&&typeof e.namespace==`string`&&typeof e.entry_key==`string`&&typeof e.value_json==`string`&&(typeof e.created_at==`number`||typeof e.created_at==`bigint`)&&(e.expires_at===null||typeof e.expires_at==`number`||typeof e.expires_at==`bigint`)}function rowToEntry(e,t,n){let r=w(e.expires_at);return{key:e.entry_key,value:m(e.value_json,t,n),createdAt:w(e.created_at)??0,...r==null?{}:{expiresAt:r}}}function getPluginStateKysely(e){return a(e)}function selectLivePluginStateRows(e,t){let n=getPluginStateKysely(e).selectFrom(`plugin_state_entries`).where(`plugin_id`,`=`,t.pluginId);return t.namespace!==void 0&&(n=n.where(`namespace`,`=`,t.namespace)),n.where(e=>e.or([e(`expires_at`,`is`,null),e(`expires_at`,`>`,t.now)]))}function assertPluginStateKey(e,t,n){if((typeof e==`boolean`?e:i(e))&&t.includes(`\0`))throw p({code:`PLUGIN_STATE_INVALID_INPUT`,operation:n,message:`PostgreSQL plugin state keys cannot contain NUL characters.`})}u(e=>o(e,t=>getPluginStateKysely(e).insertInto(`plugin_state_entries`).values({plugin_id:t(e=>e.plugin_id),namespace:t(e=>e.namespace),entry_key:t(e=>e.entry_key),value_json:t(e=>e.value_json),created_at:t(e=>e.created_at),expires_at:t(e=>e.expires_at)}).onConflict(e=>e.columns([`plugin_id`,`namespace`,`entry_key`]).doUpdateSet({value_json:e=>e.ref(`excluded.value_json`),created_at:e=>e.ref(`excluded.created_at`),expires_at:e=>e.ref(`excluded.expires_at`)})).returningAll())),u(e=>o(e,t=>getPluginStateKysely(e).insertInto(`plugin_state_entries`).values({plugin_id:t(e=>e.plugin_id),namespace:t(e=>e.namespace),entry_key:t(e=>e.entry_key),value_json:t(e=>e.value_json),created_at:t(e=>e.created_at),expires_at:t(e=>e.expires_at)}).onConflict(e=>e.columns([`plugin_id`,`namespace`,`entry_key`]).doUpdateSet({value_json:e=>e.ref(`excluded.value_json`),created_at:e=>e.ref(`excluded.created_at`),expires_at:e=>e.ref(`excluded.expires_at`)}).where(`plugin_state_entries.expires_at`,`<=`,t(e=>e.now))).returningAll())),u(e=>o(e,t=>{let n=t(e=>e.pluginId),r=t(e=>e.namespace),i=t(e=>e.key);return selectLivePluginStateRows(e,{pluginId:n,namespace:r,now:t(e=>e.now)}).select(`entry_key`).where(`entry_key`,`=`,i)}));const N=u(e=>o(e,t=>{let n=t(e=>e.pluginId),r=t(e=>e.namespace),i=t(e=>e.key);return selectLivePluginStateRows(e,{pluginId:n,namespace:r,now:t(e=>e.now)}).select([`entry_key`,`value_json`,`created_at`,`expires_at`]).where(`entry_key`,`=`,i)}));function selectPluginStateEntry(e,t){return assertPluginStateKey(e,t.key,`lookup`),N(e)(t).rows[0]}function iteratePluginStateEntries(e,t){return c(e,selectLivePluginStateRows(e,t).select([`entry_key`,`value_json`,`created_at`,`expires_at`]).orderBy(`created_at`,`asc`).orderBy(`entry_key`,`asc`))}function selectPluginStateEntriesInKeyRange(e,t){assertPluginStateKey(e,t.keyStartInclusive,`entries`),assertPluginStateKey(e,t.keyEndExclusive,`entries`);let n=selectLivePluginStateRows(e,t).select([`entry_key`,`value_json`,`created_at`,`expires_at`]).where(`entry_key`,t.exclusiveStart?`>`:`>=`,t.keyStartInclusive).where(`entry_key`,`<`,t.keyEndExclusive),r=t.valueFilter;return r&&(n=n.where(t=>t(x(e,t.ref(`value_json`),t.val(r.path)),`=`,r.equals))),l(e,n.orderBy(`entry_key`,t.order).limit(t.limit)).rows}u(e=>o(e,t=>getPluginStateKysely(e).selectFrom(`plugin_state_entries`).select(`expires_at`).where(`expires_at`,`is not`,null).where(`expires_at`,`<=`,t(e=>e)).limit(1)));const P=u(e=>o(e,t=>selectLivePluginStateRows(e,{pluginId:t(e=>e.pluginId),namespace:t(e=>e.namespace),now:t(e=>e.now)}).select(e=>e.fn.countAll().as(`count`))));function countLivePluginStateNamespaceEntries(e,t){let n=P(e)(t).rows[0];return T(n?.count??0)}function lookupPluginStateEntry(e,t){let n=selectPluginStateEntry(e.db,{...t,now:Date.now()});return n?m(n.value_json,`lookup`,e.path):void 0}function wrapPluginStateError(e,t,r,i,a=n(process.env)){if(e instanceof _)return e;let o=i;return r===`PLUGIN_STATE_OPEN_FAILED`&&(b(e)?o+=` The state database uses a newer schema. Run an OpenClaw build that supports it.`:e instanceof Error&&C(e)&&(o+=` Database integrity verification failed. Restore or repair the state database, then run openclaw doctor --fix.`)),p({code:r,operation:t,message:o,path:a,cause:e})}function isMissingPluginStateTableError(e){return e instanceof Error&&(t(e,`ERR_SQLITE_ERROR`)&&e.message===`no such table: plugin_state_entries`||t(e,`42P01`)&&e.message===`relation "plugin_state_entries" does not exist`)}function lookupPluginStateEntries(e,t){for(let n of t.keys)assertPluginStateKey(e.db,n,`lookup`);let n=Date.now(),r=l(e.db,selectLivePluginStateRows(e.db,{...t,now:n}).select([`entry_key`,`value_json`]).where(`entry_key`,`in`,d(t.keys))).rows,i=new Map(r.map(e=>[e.entry_key,e.value_json]));return decodePluginStateLookups(t.keys,e=>i.get(e),e.path)}function decodePluginStateLookups(e,t,n){return e.map(e=>{let r=t(k(e));try{return v(r===void 0?void 0:m(r,`lookup`,n))}catch(e){if(e instanceof _&&e.code===`PLUGIN_STATE_CORRUPT`)return y(e);throw e}})}function listPluginStateEntries(e,t){let n=iteratePluginStateEntries(e.db,{...t,now:Date.now()}),r=[],i;for(let t of n)if(!i)try{r.push(rowToEntry(t,`entries`,e.path))}catch(e){i={error:e}}if(i)throw i.error;return r}function listPluginStateEntriesInKeyRange(e,t){return selectPluginStateEntriesInKeyRange(e.db,{...t,order:t.order??`asc`,now:Date.now()}).map(t=>rowToEntry(t,`entries`,e.path))}u(e=>o(e,t=>getPluginStateKysely(e).selectFrom(s(t(e=>e.entriesJson)).as(`requested`)).crossJoin(`plugin_state_entries`).where(`plugin_id`,`=`,t(e=>e.pluginId)).where(t=>t(`namespace`,`=`,S(e,t.ref(`requested.value`),0))).where(t=>t(`entry_key`,`=`,S(e,t.ref(`requested.value`),1))).select([`requested.key as position`,`entry_key`,`value_json`,`created_at`,`expires_at`]).where(e=>e.or([e(`expires_at`,`is`,null),e(`expires_at`,`>`,t(e=>e.now))])))),u(e=>o(e,t=>selectLivePluginStateRows(e,{pluginId:t(e=>e.pluginId),now:t(e=>e.now)}).select(e=>e.fn.countAll().as(`count`))));function selectPluginStateRetention(e,t){return getPluginStateKysely(e).selectFrom(`plugin_state_entries`).select(e=>[e.fn.sum(e.case().when(e.or([e(`expires_at`,`is`,null),e(`expires_at`,`>`,t)])).then(1).else(0).end()).as(`namespace_count`),e.fn.min(e.case().when(`expires_at`,`>`,t).then(e.ref(`expires_at`)).else(null).end()).as(`next_expiry`),e.fn.min(`expires_at`).as(`first_expiry`)])}u(e=>o(e,t=>selectPluginStateRetention(e,t(e=>e.now)).where(`plugin_id`,`=`,t(e=>e.pluginId)).where(`namespace`,`=`,t(e=>e.namespace)))),Array.from({length:4},(e,t)=>u(e=>o(e,n=>{let select=t=>selectPluginStateRetention(e,n(e=>e.now)).select(n(e=>e.namespaces[t]).as(`namespace`)).where(`plugin_id`,`=`,n(e=>e.pluginId)).where(`namespace`,`=`,n(e=>e.namespaces[t])),r=select(0);for(let e=1;e<=t;e++)r=r.unionAll(select(e));return r}))),new TextEncoder;function capturePluginStateWorkerFailure(e){let t=g(e.cause,e=>h(e,{includeOrdinary:e instanceof AggregateError}));return{message:e.message,code:e.code,operation:e.operation,owner:e.owner,...e.path===void 0?{}:{path:e.path},...t?{cause:t}:{}}}function readPluginStateCommand(e,t){let n=D[t.type];try{switch(t.type){case`pluginState.lookup`:return{type:t.type,result:v(lookupPluginStateEntry(e,t.input))};case`pluginState.lookupMany`:return{type:t.type,result:v(lookupPluginStateEntries(e,t.input).map(e=>e.ok?e:y(capturePluginStateWorkerFailure(e.error))))};case`pluginState.entries`:return{type:t.type,result:v(listPluginStateEntries(e,t.input))};case`pluginState.entriesInKeyRange`:return{type:t.type,result:v(listPluginStateEntriesInKeyRange(e,t.input))};default:return{type:t.type,result:v(countLivePluginStateNamespaceEntries(e.db,{...t.input,now:Date.now()}))}}}catch(r){if(isMissingPluginStateTableError(r)&&!f(e.db))switch(t.type){case`pluginState.lookup`:return{type:t.type,result:v(void 0)};case`pluginState.count`:return{type:t.type,result:v(0)};case`pluginState.lookupMany`:return{type:t.type,result:v(t.input.keys.map(()=>v(void 0)))};default:return{type:t.type,result:v([])}}return{type:t.type,result:{ok:!1,error:capturePluginStateWorkerFailure(wrapPluginStateError(r,n.operation,n.code,n.message,e.path))}}}}export{readPluginStateCommand};