UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 8.79 kB
import{Cn as e,zt as t}from"./openclaw-state-read-subsystem-Dwoo5bcz.mjs";import{It as n,a as r,v as i}from"./openclaw-state-read-sqlite-database-admission-CrkztOzk.mjs";import"./openclaw-state-read-pool-B6DWjH6F.mjs";import{O as a}from"./openclaw-state-read-openclaw-state-db-cache-DyomEqgV.mjs";import{C as o,s,x as c}from"./openclaw-state-read-sqlite-post-commit-CsXPYVOo.mjs";import{r as l}from"./openclaw-state-read-openclaw-state-db.paths-7XLpcdiq.mjs";import{a as u}from"./openclaw-state-read-runtime-worker-generation-DIZIqsAo.mjs";import{f as d}from"./openclaw-state-read-gateway-supervision-CAmdOi7r.mjs";import"./openclaw-state-read.worker.js";import{i as f,o as p,r as m,t as h}from"./openclaw-state-read-openclaw-state-db-readonly-Bv32Owin.mjs";import{t as g}from"./openclaw-state-read-session-row-changes-C0E3v5wT.mjs";import{a as _}from"./openclaw-state-read-openclaw-agent-db-identity-EXR5NQy4.mjs";import{r as v}from"./openclaw-state-read-openclaw-agent-db-registry.read-DCfsbJGg.mjs";import{lstatSync as y,statSync as b}from"node:fs";import x from"node:path";import{AsyncLocalStorage as S}from"node:async_hooks";function registeredPathKey(e){return{name:`agent.registry:${JSON.stringify([e.agentId,x.resolve(e.path),e.identity,e.schemaVersion??38])}`,read:e=>typeof e==`boolean`?e:void 0}}function hasRegisteredOpenClawAgentDatabasePath(e,t){let{identity:n}=_(e);return r(l(t),registeredPathKey({...e,identity:`file:${n}`}),()=>void 0)===!0}const C=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}};C.publications.add(n),g.emit(n,t)}function activateRegisteredAgentDatabasesMemo(e){let t=l(e);return C.memo?.pathname!==t&&(C.memo={pathname:t,token:Symbol(t)}),C.memo}function invalidateRegisteredAgentDatabasesMemo(e){return advanceRegisteredAgentDatabasesMemo(l(e))}function advanceRegisteredAgentDatabasesMemo(e,t){t?.phase===`begin`?C.pending.set(t.operation,{...t,pathname:e}):t?.phase===`finish`&&C.pending.delete(t.operation);let n=C.memo;if(n?.pathname!==e)return;let r={pathname:e,token:Symbol(e),transition:t};return n.next=r,C.memo=r,{previous:n.token,current:r.token}}function captureRegistryMutation(e,t){try{let r=[];for(let e of t){let t=e.capturedIdentity??n(e.path);if(!t)return;r.push({agentId:e.agentId,path:x.resolve(e.path),physicalPath:t.canonicalPath,identity:t.key,schemaVersion:e.schemaVersion??38})}return{kind:e,sources:r}}catch{return}}function recordOpenClawAgentDatabaseRegistryMutation(e,t,n){let r=Symbol(`agent-registry-mutation`),a=captureRegistryMutation(t,n);for(let n of a?.sources??[])i(e.db,registeredPathKey(n),t===`upsert`);let advance=t=>advanceRegisteredAgentDatabasesMemo(e.path,{operation:r,mutation:a,phase:t});if(!s(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:c([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,s=!1,publishChange=()=>{try{(e.assertPublicationCurrent??e.admission.assertCurrent)()}catch(e){if(u(e))return;throw e}emitOpenClawAgentDatabaseRegistryChange(e.agentId)};return{begin(){if(s)throw Error(`Agent database registration admission is closed`);i||(i=!0,advance(`begin`))},recordCommitted(n){if(s||!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),u(e))return;throw e}advance(`commit`)},finish(r){if(s)return;s=!0;let c=i&&!a&&r?.kind===`unknown`,l=[];try{try{if(c){let n=invalidateRegisteredAgentDatabasesMemo(t);n&&e.onRegistryChange?.(n)}e.admission.assertCurrent()}catch(e){if(u(e))return;throw e}finally{C.pending.delete(n)}i&&!c&&advance(`finish`)}catch(e){l.push(e)}finally{if(a||c)try{publishChange()}catch(e){l.push(e)}o(l,`Agent registration settlement failed`)}}}}function cloneRegisteredAgentDatabases(e,t){let n=e.map(e=>({...e}));return t.includeIncompatibleSchemaVersions?n:n.filter(e=>e.schemaVersion===38)}function hasUnavailableMissingSqlitePath(e){for(let t of d(e))try{return y(t),!0}catch(e){if(e.code!==`ENOENT`)return!0}let t=x.dirname(e);for(;;){try{let e=y(t);if(!e.isSymbolicLink())return!e.isDirectory();try{return!b(t).isDirectory()}catch{return!0}}catch(e){if(e.code!==`ENOENT`)return!0}let e=x.dirname(t);if(e===t)return!1;t=e}}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=l(e),read=({db:e})=>v(e,n,t),finish=t=>{if(t===void 0){if(hasUnavailableMissingSqlitePath(n))throw Error(`OpenClaw state database ${n} is unavailable.`);return[]}return e.includeIncompatibleSchemaVersions?t:t.filter(e=>e.schemaVersion===38)};return t?m(read,e).then(finish):finish(f(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=S.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(()=>h(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&&hasUnavailableMissingSqlitePath(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={},n){try{let r=a(e.env??process.env);r.OPENCLAW_STATE_DIR=t(r);let i={...e,env:r,path:l({...e,env:r})},o=p(i.path).admission.assertCurrent,captureWitness=()=>{let e=activateRegisteredAgentDatabasesMemo(i),t=e,r=!1,a=e.entries,s=/* @__PURE__ */ new Set,unchanged=e=>!e||!n?!1:n(e,a);return{memo:e,assertCurrent:()=>{o();let e=C.memo;if(r)throw new AgentDatabaseRegistryChangedError;if((n?[...C.pending.values()].filter(e=>e.pathname===i.path&&!s.has(e.operation)&&!unchanged(e.mutation)):[]).length>0)throw new AgentDatabaseRegistryChangedError(`Agent database registry ownership is changing during discovery`);for(;t!==e;){let e=t.next;if(!n||!e?.transition||e.transition.phase===`commit`&&!unchanged(e.transition.mutation))throw r=!0,new AgentDatabaseRegistryChangedError;t=e}},followRegistration:e=>{if(o(),r||C.memo?.pathname!==t.pathname)throw r=!0,Error(`Agent registration cannot replace an invalidated registry read`);let n=t.token===e.current?t:t.next;if(t.token!==e.previous&&t.token!==e.current||n?.token!==e.current){if(e.current!==C.memo.token)return;throw r=!0,Error(`Agent registration cannot replace an invalidated registry read`)}let i=n.transition?.operation;i&&s.add(i),t=n},acceptEntries(e){a=e}}},s=n?captureWitness():void 0,c=s;return{assertAdmissionCurrent:o,assertCurrent:()=>(c?.assertCurrent??o)(),followRegistration(e){if(!c)throw Error(`Agent registration requires a captured registry witness`);c.followRegistration(e)},read:createRegistrySnapshotReader(i,s,()=>c=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};