UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 6.32 kB
import{M as e}from"./transcript-chat-message-content-Cx-VPFHv.mjs";import{i as t}from"./transcript-global-singleton-DIJgobpU.mjs";import{o as n,s as r}from"./transcript-openclaw-agent-search-schema-BQwqsKhe.mjs";import{t as i}from"./transcript-detached-async-context-BKXaSw-L.mjs";import{i as a,t as o}from"./transcript-subsystem-er2pCebD.mjs";import{n as s}from"./transcript-sql-connection-CRz-dHXM.mjs";import{p as c}from"./transcript-sqlite-schema-facts-C4Tsn8VX.mjs";import{n as l}from"./transcript-agent-id-DmscQLD6.mjs";import{E as u,ot as d}from"./transcript-openclaw-agent-db-schema-read-0j86l7zG.mjs";import{i as f,n as p,r as m}from"./transcript-openclaw-agent-db-identity-BwKDyjmN.mjs";import{n as h,o as g}from"./transcript-openclaw-agent-db-validation-cache-CPj39b9d.mjs";import{a as _,i as v,n as y,r as b,t as x}from"./transcript-openclaw-agent-db-readonly-open-Cx-tx28d.mjs";import{AsyncLocalStorage as S}from"node:async_hooks";var C=/* @__PURE__ */ e({OpenClawAgentDatabaseReadOnlyScope:()=>OpenClawAgentDatabaseReadOnlyScope,closeIdleOpenClawAgentDatabaseReadOnly:()=>closeIdleOpenClawAgentDatabaseReadOnly,invalidateOpenClawAgentReadOnlyProjections:()=>invalidateOpenClawAgentReadOnlyProjections,retainCachedOpenClawAgentDatabaseReadOnly:()=>retainCachedOpenClawAgentDatabaseReadOnly,runInScopedOpenClawAgentReadSnapshot:()=>runInScopedOpenClawAgentReadSnapshot,withScopedOpenClawAgentDatabaseReadOnly:()=>withScopedOpenClawAgentDatabaseReadOnly});const w=new S,T=o(`state/agent-db`),E=t(Symbol.for(`openclaw.agentDatabaseReadOnlyScopes`),()=>({paths:/* @__PURE__ */ new Map,active:/* @__PURE__ */ new Set}));var OpenClawAgentDatabaseReadOnlyScope=class{constructor(e=!1){this.cached=e,this.borrowers=0}get hasRetainedConnection(){return this.database!==void 0}invalidateProjection(e,t){this.database&&m(this.database)?.identity===e&&t(this.database.db)}closeIfIdle(){this.borrowers!==0||this.database?.db.isOpen&&this.database.db.isTransaction||this.discardConnection()}close(){clearTimeout(this.idleTimer),this.idleTimer=void 0,this.database?.close(),this.database=void 0,this.retainedValidation=void 0,this.borrowers=0,this.target&&E.paths.get(this.target.path)===this&&E.paths.delete(this.target.path),this.target=void 0,this.unregisterResource?.(),this.unregisterResource=void 0,E.active.delete(this),E.active.size===0&&(E.unregisterExit?.(),E.unregisterExit=void 0)}discardConnection(){let e=this.target;this.close(),this.cached||(this.target=e)}touch(){this.database&&(this.idleTimer?this.idleTimer.refresh():(this.idleTimer=i(()=>setTimeout(()=>{this.idleTimer=void 0;try{this.closeIfIdle()}catch(e){T.warn(`Idle agent read-only database cleanup failed`,{path:this.database?.path,error:e})}finally{this.touch()}},r)),this.idleTimer.unref()))}run(e,t){return(this.target?.agentId!==e.agentId||this.target.path!==e.path)&&this.close(),this.target=e,w.run(this,t)}matches(e,t){return this.target?.agentId===e&&this.target.path===t}async runInSnapshot(e){let t={};this.snapshotHold=t;try{return await e()}finally{this.snapshotHold=void 0,this.endSnapshotHold(t)}}endSnapshotHold(e){let{database:t,end:n}=e;e.database=e.end=void 0,t&&(n?.(),this.releaseBorrow(t))}acquire(e){let finish=t=>{let n=l(e.agentId);if(t.agentId!==n)throw Error(`OpenClaw agent database ${t.path} belongs to agent ${t.agentId}; requested agent ${n}.`);return d(this.unregisterResource),this.touch(),{found:!0,database:t}};if(this.database&&(this.retainedValidation&&Atomics.load(new Int32Array(this.retainedValidation.valid),0)!==1||!f(this.database))&&this.discardConnection(),this.database&&_(this.database.path),!this.database){let t=x(e);if(!t.found)return this.discardConnection(),t;this.database=t.database,this.target={...this.target,agentId:this.database.agentId,path:this.database.path};try{this.unregisterResource=u({agentId:this.target.agentId,path:this.target.path,revoke:()=>this.close(),close:()=>this.close()}),E.active.add(this),this.cached&&E.paths.set(this.database.path,this),E.unregisterExit??=a(()=>{for(let e of E.active)e.close()})}catch(e){throw this.discardConnection(),e}}let t=this.database,n=this.target?.validation;if(n&&!h(t,n))throw Error(`Session reader validation does not match its current physical owner`);return this.retainedValidation??=g(t),finish(t)}releaseBorrow(e){this.database===e&&(this.borrowers--,this.borrowers===0&&s(e.db)&&e.db.releaseIdleTransport(),this.cached&&this.borrowers===0&&this.database&&(!this.database.db.isOpen||this.database.db.isTransaction)?this.discardConnection():this.touch())}retain(e){this.target?.assertSourceCurrent?.();let t=this.database?.db.isOpen&&this.database.db.isTransaction?x(e):this.acquire(e);if(!t.found)return t;let{database:n}=t,r=n===this.database;return r&&this.borrowers++,{found:!0,database:n,claim:p(n,()=>{r?this.releaseBorrow(n):n.close()})}}read(e,t,r={}){this.target?.assertSourceCurrent?.();let i=this.snapshotHold;if(this.database?.db.isOpen&&this.database.db.isTransaction&&i?.database!==this.database)return v(e,t,r);let a=this.acquire(t);if(!a.found)return a;let{database:o}=a;this.borrowers++;try{return i&&i.database!==o&&s(o.db)&&(this.endSnapshotHold(i),i.end=n(o.db),i.database=o,this.borrowers++),r.snapshot?b(o,e):c(o.db,()=>y(o,e))}catch(e){throw i?.database===o&&this.endSnapshotHold(i),this.cached&&this.borrowers===1&&this.discardConnection(),e}finally{this.releaseBorrow(o)}}};function cachedScope(e){let t=E.paths.get(e.path);return t||(t=new OpenClawAgentDatabaseReadOnlyScope(!0),t.run(e,()=>{}),E.paths.set(e.path,t)),t}function invalidateOpenClawAgentReadOnlyProjections(e,t){for(let n of E.active)n.invalidateProjection(e,t)}function closeIdleOpenClawAgentDatabaseReadOnly(e){E.paths.get(e)?.closeIfIdle()}function retainCachedOpenClawAgentDatabaseReadOnly(e){let t=w.getStore();return(t?.matches(e.agentId,e.path)?t:cachedScope(e)).retain(e)}function runInScopedOpenClawAgentReadSnapshot(e){return w.getStore()?.runInSnapshot(e)??e()}function withScopedOpenClawAgentDatabaseReadOnly(e,t,n={}){if(n.allowExtension)return v(e,t,n);let r=w.getStore();return(r?.matches(t.agentId,t.path)?r:cachedScope(t)).read(e,t,n)}export{withScopedOpenClawAgentDatabaseReadOnly as a,retainCachedOpenClawAgentDatabaseReadOnly as i,closeIdleOpenClawAgentDatabaseReadOnly as n,C as r,OpenClawAgentDatabaseReadOnlyScope as t};