UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 8.81 kB
import{v as e}from"./openclaw-state-read-home-dir-BQZX1pD2.mjs";import{Jt as t,o as n,x as r}from"./openclaw-state-read-sqlite-database-admission-Ck6VG7rE.mjs";import"./openclaw-state-read-pool-1Cp_Z7yz.mjs";import{a as i}from"./openclaw-state-read-tmp-openclaw-dir-C3Ynn93H.mjs";import{O as a}from"./openclaw-state-read-openclaw-state-db-cache-BVY8IYkn.mjs";import{c as o,u as s}from"./openclaw-state-read-sqlite-lifecycle-errors-3AcFJUep.mjs";import{r as c}from"./openclaw-state-read-openclaw-state-db.paths-CFemYLpA.mjs";import{r as l}from"./openclaw-state-read-openclaw-state-db-async-lifecycle-B_Y330HB.mjs";import{s as u}from"./openclaw-state-read-sqlite-post-commit-CEHMNIsB.mjs";import{i as d,r as f,t as p}from"./openclaw-state-read-openclaw-state-db-readonly-CDG18cNK.mjs";import"./openclaw-state-read.worker.js";import{t as m}from"./openclaw-state-read-openclaw-state-worker-context-Cvog3s4V.mjs";import{t as h}from"./openclaw-state-read-session-row-changes-C0Qzq-vO.mjs";import{a as g}from"./openclaw-state-read-openclaw-agent-db-identity-2iBH9vAR.mjs";import{i as _,t as v}from"./openclaw-state-read-openclaw-agent-db-registry.read-D1CTFqiu.mjs";import y from"node:path";import{AsyncLocalStorage as b}from"node:async_hooks";function registeredPathKey(e){return{name:`agent.registry:${JSON.stringify([e.agentId,y.resolve(e.path),e.identity,e.schemaVersion??42])}`,read:e=>typeof e==`boolean`?e:void 0}}function hasRegisteredOpenClawAgentDatabasePath(e,t){let{identity:r}=g(e);return n(c(t),registeredPathKey({...e,identity:`file:${r}`}),()=>void 0)===!0}const x=e(Symbol.for(`openclaw.agentDatabaseRegistryMemo`),()=>({pending:/* @__PURE__ */ new Map,publications:/* @__PURE__ */ new WeakSet}));function emitOpenClawAgentDatabaseRegistryChange(e,t){let n={all:!0,scope:{agentId:e,topology:!0}};x.publications.add(n),h.emit(n,t)}function activateRegisteredAgentDatabasesMemo(e){let t=c(e);return x.memo?.pathname!==t&&(x.memo={pathname:t,token:Symbol(t)}),x.memo}function invalidateRegisteredAgentDatabasesMemo(e){return advanceRegisteredAgentDatabasesMemo(c(e))}function advanceRegisteredAgentDatabasesMemo(e,t){t?.phase===`begin`?x.pending.set(t.operation,{...t,pathname:e}):t?.phase===`finish`&&x.pending.delete(t.operation);let n=x.memo;if(n?.pathname!==e)return;let r={pathname:e,token:Symbol(e),transition:t};return n.next=r,x.memo=r,{previous:n.token,current:r.token}}function captureRegistryMutation(e,n){try{let r=[];for(let e of n){let n=e.capturedIdentity??t(e.path);if(!n)return;r.push({agentId:e.agentId,path:y.resolve(e.path),physicalPath:n.canonicalPath,identity:n.key,schemaVersion:e.schemaVersion??42})}return{kind:e,sources:r}}catch{return}}function recordOpenClawAgentDatabaseRegistryMutation(e,t,n){let i=Symbol(`agent-registry-mutation`),a=captureRegistryMutation(t,n);for(let n of a?.sources??[])r(e.db,registeredPathKey(n),t===`upsert`);let advance=t=>advanceRegisteredAgentDatabasesMemo(e.path,{operation:i,mutation:a,phase:t});if(!u(e.db,{stage:()=>advance(`begin`),commit:()=>{advance(`commit`),advance(`finish`)},rollback:()=>advance(`finish`)}))throw Error(`Registry mutation requires its canonical transaction publication scope`)}async function settleAgentRegistration(e,t){let n;try{n={ok:!0,value:await t()}}catch(e){n={ok:!1,error:e}}try{e.finish(await e.nativeSettlement)}catch(e){throw n.ok?e:o([n.error,e],`Agent open and registration publication failed`,n.error)}if(!n.ok)throw n.error;return n.value}function captureOpenClawAgentDatabaseRegistration(e){let t={path:e.admission.databasePath},n=Symbol(`agent-registry-registration`),r=captureRegistryMutation(e.kind??`upsert`,[{agentId:e.agentId,path:e.agentPath,capturedIdentity:e.capturedIdentity}]),advance=i=>{let a=advanceRegisteredAgentDatabasesMemo(t.path,{operation:n,mutation:r,phase:i});a&&e.onRegistryChange?.(a)},i=!1,a=!1,o=!1,publishChange=()=>{try{(e.assertPublicationCurrent??e.admission.assertCurrent)()}catch(e){if(l(e))return;throw e}emitOpenClawAgentDatabaseRegistryChange(e.agentId)};return{begin(){if(o)throw Error(`Agent database registration admission is closed`);i||(i=!0,advance(`begin`))},recordCommitted(n){if(o||!i||n.agentId!==e.agentId||n.agentPath!==e.agentPath||n.stateDatabasePath!==e.admission.databasePath||n.stateDatabaseIdentity!==e.admission.identity.key)throw Error(`Agent registration commit differs from its captured owner`);a=!0;try{e.admission.assertCurrent()}catch(e){if(invalidateRegisteredAgentDatabasesMemo(t),l(e))return;throw e}advance(`commit`)},finish(r){if(o)return;o=!0;let c=i&&!a&&r?.kind===`unknown`,u=[];try{try{if(c){let n=invalidateRegisteredAgentDatabasesMemo(t);n&&e.onRegistryChange?.(n)}e.admission.assertCurrent()}catch(e){if(l(e))return;throw e}finally{x.pending.delete(n)}i&&!c&&advance(`finish`)}catch(e){u.push(e)}finally{if(a||c)try{publishChange()}catch(e){u.push(e)}s(u,`Agent registration settlement failed`)}}}}function cloneRegisteredAgentDatabases(e,t){let n=e.map(e=>({...e}));return t.includeIncompatibleSchemaVersions?n:n.filter(e=>e.schemaVersion===42)}var AgentDatabaseRegistryChangedError=class extends Error{constructor(e=`Agent database registry changed during discovery; retry the read.`){super(e),this.name=`AgentDatabaseRegistryChangedError`}};function readRegisteredAgentDatabases(e,t){let n=c(e),read=({db:e})=>_(e,n,t),finish=t=>{if(t===void 0){if(v(n))throw Error(`OpenClaw state database ${n} is unavailable.`);return[]}return e.includeIncompatibleSchemaVersions?t:t.filter(e=>e.schemaVersion===42)};return t?f(read,e).then(finish):finish(d(read,e))}function listOpenClawRegisteredAgentDatabases(e={}){let t=activateRegisteredAgentDatabasesMemo(e);return cloneRegisteredAgentDatabases(t.entries??=readRegisteredAgentDatabases({...e,includeIncompatibleSchemaVersions:!0},!1),e)}function createRegistrySnapshotReader(e,t,n,r){let i=b.snapshot();return async a=>{for(;;){a?.throwIfAborted();let o=n(),{memo:s,assertCurrent:c,followRegistration:l}=o,u=t?c:r;u();let d=e.fresh?void 0:s.entries;if(!d){let n=(await i(()=>p(e,{type:`agentDatabaseRegistry.read`},{signal:a,current:e.fresh})))?.result;if(!t&&s.next)continue;if(o.acceptEntries(n?.status===`available`?n.entries:[]),u(),n?.status===`unavailable`||n===void 0&&v(e.path))return{result:{status:`unavailable`},assertCurrent:c,followRegistration:l};d=e.fresh?n?.entries??[]:s.entries??=n?.entries??[]}return{result:{status:`available`,entries:cloneRegisteredAgentDatabases(d,e)},assertCurrent:c,followRegistration:l}}}}function prepareOpenClawAgentDatabaseRegistrySnapshotRead(e={},t){try{let n=a(e.env??process.env);n.OPENCLAW_STATE_DIR=i(n);let r={...e,env:n,path:c({...e,env:n})},o=m(r.path).admission.assertCurrent,captureWitness=()=>{let e=activateRegisteredAgentDatabasesMemo(r),n=e,i=!1,a=e.entries,s=/* @__PURE__ */ new Set,unchanged=e=>!e||!t?!1:t(e,a);return{memo:e,assertCurrent:()=>{o();let e=x.memo;if(i)throw new AgentDatabaseRegistryChangedError;if((t?[...x.pending.values()].filter(e=>e.pathname===r.path&&!s.has(e.operation)&&!unchanged(e.mutation)):[]).length>0)throw new AgentDatabaseRegistryChangedError(`Agent database registry ownership is changing during discovery`);for(;n!==e;){let e=n.next;if(!t||!e?.transition||e.transition.phase===`commit`&&!unchanged(e.transition.mutation))throw i=!0,new AgentDatabaseRegistryChangedError;n=e}},followRegistration:e=>{if(o(),i||x.memo?.pathname!==n.pathname)throw i=!0,Error(`Agent registration cannot replace an invalidated registry read`);let t=n;for(;t.token!==e.current&&t.next;)t=t.next;if(t.token===e.current)for(;n.token!==e.previous&&n.token!==e.current;){let e=n.next;if(!e?.transition||e.transition.phase===`commit`&&!unchanged(e.transition.mutation))throw i=!0,Error(`Agent registration cannot replace an invalidated registry read`);n=e}let r=n.token===e.current?n:n.next;if(n.token!==e.previous&&n.token!==e.current||r?.token!==e.current){if(e.current!==x.memo.token)return;throw i=!0,Error(`Agent registration cannot replace an invalidated registry read`)}let a=r.transition?.operation;a&&s.add(a),n=r},acceptEntries(e){a=e}}},s=t?captureWitness():void 0,l=s;return{assertAdmissionCurrent:o,assertCurrent:()=>(l?.assertCurrent??o)(),followRegistration(e){if(!l)throw Error(`Agent registration requires a captured registry witness`);l.followRegistration(e)},read:createRegistrySnapshotReader(r,s,()=>l=s??captureWitness(),o)}}catch(e){let rethrow=()=>{throw e};return{assertAdmissionCurrent:rethrow,assertCurrent:rethrow,followRegistration:rethrow,read:async()=>rethrow()}}}export{prepareOpenClawAgentDatabaseRegistrySnapshotRead as a,listOpenClawRegisteredAgentDatabases as i,emitOpenClawAgentDatabaseRegistryChange as n,recordOpenClawAgentDatabaseRegistryMutation as o,hasRegisteredOpenClawAgentDatabasePath as r,settleAgentRegistration as s,captureOpenClawAgentDatabaseRegistration as t};