openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 10.8 kB
JavaScript
import{Cn as e,xt as t}from"./openclaw-state-read-subsystem-Dwoo5bcz.mjs";import{Pt as n}from"./openclaw-state-read-src-RL5xtYG2.mjs";import"./openclaw-state-read-pool-B6DWjH6F.mjs";import"./openclaw-state-read-openclaw-state-db-cache-DyomEqgV.mjs";import{u as r}from"./openclaw-state-read-sqlite-post-commit-CsXPYVOo.mjs";import{s as i}from"./openclaw-state-read-openclaw-state-db.paths-7XLpcdiq.mjs";import"./openclaw-state-read-sqlite-worker-operation-settlement-DOkqmUGd.mjs";import"./openclaw-state-read-sqlite-worker-operation-admission-DdAi_0af.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-Bcu0Qb7F.mjs";import{r as f}from"./openclaw-state-read-openclaw-state-db-schema-helpers-QFZd8aJI.mjs";import{n as p,t as m}from"./openclaw-state-read-sqlite-number-5mfxKD9j.mjs";import"./openclaw-state-read-sqlite-transaction-Bt-IwVhZ.mjs";import{C as h,S as g,b as _,f as v,x as y}from"./openclaw-state-read-sqlite-snapshot-staging-owner-DKlm1W_1.mjs";import"./openclaw-state-read-openclaw-state-db-B3dGXm7m.mjs";import{a as b,i as x}from"./openclaw-state-read-agent-id-DmscQLD6.mjs";import{a as S}from"./openclaw-state-read-sqlite-user-version-B2KxorOy.mjs";import{l as C,n as w}from"./openclaw-state-read-json-values-sW-ax-AH.mjs";import"./openclaw-state-read-openclaw-state-db-handle-CTlulhMi.mjs";import{a as T}from"./openclaw-state-read-sqlite-integrity-dxMYqr_9.mjs";import{t as E}from"./openclaw-state-read-state-domain-publication-BS5chgVs.mjs";import"./openclaw-state-read-openclaw-state-db-readonly-Bv32Owin.mjs";import"./openclaw-state-read-session-entry-current-admission.worker-ChqfUASm.mjs";import{n as D}from"./openclaw-state-read-openclaw-state-read-handlers-BbXerMQy.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)])}j.subscribeFacts(e=>{e.kind===`unknown`&&A.unavailable.add(e.identity)}),j.subscribeFacts;function isRow(e){return t(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=p(e.expires_at);return{key:e.entry_key,value:g(e.value_json,t,n),createdAt:p(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:r(e))&&t.includes(`\0`))throw y({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 M=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`),M(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(C(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 N=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=N(e)(t).rows[0];return m(n?.count??0)}function lookupPluginStateEntry(e,t){let n=selectPluginStateEntry(e.db,{...t,now:Date.now()});return n?g(n.value_json,`lookup`,e.path):void 0}function wrapPluginStateError(e,t,n,r,a=i(process.env)){if(e instanceof _)return e;let o=r;return n===`PLUGIN_STATE_OPEN_FAILED`&&(S(e)?o+=`
The state database uses a newer schema. Run an OpenClaw build that supports it.`:e instanceof Error&&T(e)&&(o+=`
Database integrity verification failed. Restore or repair the state database, then run openclaw doctor --fix.`)),y({code:n,operation:t,message:o,path:a,cause:e})}function isMissingPluginStateTableError(e){return e instanceof Error&&(n(e,`ERR_SQLITE_ERROR`)&&e.message===`no such table: plugin_state_entries`||n(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 b(r===void 0?void 0:g(r,`lookup`,n))}catch(e){if(e instanceof _&&e.code===`PLUGIN_STATE_CORRUPT`)return x(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`,`=`,w(e,t.ref(`requested.value`),0))).where(t=>t(`entry_key`,`=`,w(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=h(e.cause,e=>v(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:b(lookupPluginStateEntry(e,t.input))};case`pluginState.lookupMany`:return{type:t.type,result:b(lookupPluginStateEntries(e,t.input).map(e=>e.ok?e:x(capturePluginStateWorkerFailure(e.error))))};case`pluginState.entries`:return{type:t.type,result:b(listPluginStateEntries(e,t.input))};case`pluginState.entriesInKeyRange`:return{type:t.type,result:b(listPluginStateEntriesInKeyRange(e,t.input))};default:return{type:t.type,result:b(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:b(void 0)};case`pluginState.count`:return{type:t.type,result:b(0)};case`pluginState.lookupMany`:return{type:t.type,result:b(t.input.keys.map(()=>b(void 0)))};default:return{type:t.type,result:b([])}}return{type:t.type,result:{ok:!1,error:capturePluginStateWorkerFailure(wrapPluginStateError(r,n.operation,n.code,n.message,e.path))}}}}export{readPluginStateCommand};