openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 5.24 kB
JavaScript
import{i as e}from"./transcript-global-singleton-DIJgobpU.mjs";import{t}from"./transcript-detached-async-context-BKXaSw-L.mjs";import{t as n}from"./transcript-subsystem-C0UTtuvS.mjs";import{u as r}from"./transcript-sqlite-post-commit-B_pko32i.mjs";import{p as i}from"./transcript-sqlite-schema-facts-C1_zV4Yg.mjs";import{n as a}from"./transcript-agent-id-DmscQLD6.mjs";import{j as o,o as s}from"./transcript-openclaw-agent-db-resources-W6NYYBYU.mjs";import{C as c}from"./transcript-sqlite-private-snapshot-fork-CKFbjfTN.mjs";import{t as l}from"./transcript-sqlite-handle-lifecycle-fOjKc_YY.mjs";import{i as u,n as d,r as f}from"./transcript-openclaw-agent-db-identity-BPgUuROx.mjs";import{n as p,o as m}from"./transcript-openclaw-agent-db-validation-cache-CKkKL5V4.mjs";import{a as h,i as g,n as _,r as v,t as y}from"./transcript-openclaw-agent-db-readonly-open-BK-8SP7K.mjs";import{AsyncLocalStorage as b}from"node:async_hooks";const x=new b,S=n(`state/agent-db`),C=e(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&&f(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&&C.paths.get(this.target.path)===this&&C.paths.delete(this.target.path),this.target=void 0,this.unregisterResource?.(),this.unregisterResource=void 0,C.active.delete(this),C.active.size===0&&(C.unregisterExit?.(),C.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=t(()=>setTimeout(()=>{this.idleTimer=void 0;try{this.closeIfIdle()}catch(e){S.warn(`Idle agent read-only database cleanup failed`,{path:this.database?.path,error:e})}finally{this.touch()}},l)),this.idleTimer.unref()))}run(e,t){return(this.target?.agentId!==e.agentId||this.target.path!==e.path)&&this.close(),this.target=e,x.run(this,t)}matches(e,t){return this.target?.agentId===e&&this.target.path===t}acquire(e){let finish=t=>{let n=a(e.agentId);if(t.agentId!==n)throw Error(`OpenClaw agent database ${t.path} belongs to agent ${t.agentId}; requested agent ${n}.`);return o(this.unregisterResource),this.touch(),{found:!0,database:t}};if(this.database&&(this.retainedValidation&&Atomics.load(new Int32Array(this.retainedValidation.valid),0)!==1||!u(this.database))&&this.discardConnection(),this.database&&h(this.database.path),!this.database){let t=y(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=s({agentId:this.target.agentId,path:this.target.path,revoke:()=>this.close(),close:()=>this.close()}),C.active.add(this),this.cached&&C.paths.set(this.database.path,this),C.unregisterExit??=c(()=>{for(let e of C.active)e.close()})}catch(e){throw this.discardConnection(),e}}let t=this.database,n=this.target?.validation;if(n&&!p(t,n))throw Error(`Session reader validation does not match its current physical owner`);return this.retainedValidation??=m(t),finish(t)}releaseBorrow(e){this.database===e&&(this.borrowers--,this.borrowers===0&&r(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){let t=this.database?.db.isOpen&&this.database.db.isTransaction?y(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:d(n,()=>{r?this.releaseBorrow(n):n.close()})}}read(e,t,n={}){if(this.database?.db.isOpen&&this.database.db.isTransaction)return g(e,t,n);let r=this.acquire(t);if(!r.found)return r;let{database:a}=r;this.borrowers++;try{return n.snapshot?v(a,e):i(a.db,()=>_(a,e))}catch(e){throw this.cached&&this.borrowers===1&&this.discardConnection(),e}finally{this.releaseBorrow(a)}}};function cachedScope(e){let t=C.paths.get(e.path);return t||(t=new OpenClawAgentDatabaseReadOnlyScope(!0),t.run(e,()=>{}),C.paths.set(e.path,t)),t}function invalidateOpenClawAgentReadOnlyProjections(e,t){for(let n of C.active)n.invalidateProjection(e,t)}function closeIdleOpenClawAgentDatabaseReadOnly(e){C.paths.get(e)?.closeIfIdle()}function retainCachedOpenClawAgentDatabaseReadOnly(e){let t=x.getStore();return(t?.matches(e.agentId,e.path)?t:cachedScope(e)).retain(e)}function withScopedOpenClawAgentDatabaseReadOnly(e,t,n={}){if(n.allowExtension)return g(e,t,n);let r=x.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,invalidateOpenClawAgentReadOnlyProjections as r,OpenClawAgentDatabaseReadOnlyScope as t};