UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 2.4 kB
import{o as resolveGlobalSingleton}from"./global-singleton-DolL9xGt.mjs";import{t as isPromiseLike}from"./promise-like-OKzBwzLG.mjs";import{a as SqliteSnapshotCleanupError,u as throwSqliteLifecycleErrors}from"./sqlite-lifecycle-errors-DMaudCZS.mjs";import{AsyncLocalStorage}from"node:async_hooks";import{isPathInside}from"@openclaw/fs-safe/path"; //!src/state/artifact-preserving-state-reads.ts const artifactPreservingReads=resolveGlobalSingleton(Symbol.for(`openclaw.artifactPreservingStateReads`),()=>new AsyncLocalStorage);const liveStateReads=resolveGlobalSingleton(Symbol.for(`openclaw.liveStateReads`),()=>new AsyncLocalStorage);function withLiveStateReads(e,t){return liveStateReads.run(t?.startupConfig?`startup-config`:`runtime`,e)}function isLiveStateRead(){return liveStateReads.getStore()!==void 0}function isStartupConfigStateRead(){return liveStateReads.getStore()===`startup-config`}function withArtifactPreservingStateReads(e,t={}){if(isArtifactPreservingStateRead(`agent`))return e();const n={agentDatabases:t.agentDatabases===true,readers:/* @__PURE__ */ new Set,privateRoots:/* @__PURE__ */ new Set,snapshots:/* @__PURE__ */ new Map};const closeReaders=e=>{for(const t of n.readers)try{t()}catch(t){e.push(t)}};const finish=(e,t=[])=>{closeReaders(t);for(const e of n.snapshots.values())try{if(!e.cleanup())throw new SqliteSnapshotCleanupError(`SQLite inspection snapshot cleanup failed.`)}catch(e){t.push(e)}throwSqliteLifecycleErrors(t,`Artifact-preserving inspection and cleanup failed.`);return e};const finishAsync=async(e,t=[])=>{closeReaders(t);for(const e of n.snapshots.values())try{if(!await e.cleanupAsync())throw new SqliteSnapshotCleanupError(`SQLite inspection snapshot cleanup failed.`)}catch(e){t.push(e)}throwSqliteLifecycleErrors(t,`Artifact-preserving inspection and cleanup failed.`);return e};let r;try{r=artifactPreservingReads.run(n,e)}catch(e){return finish(void 0,[e])}return isPromiseLike(r)?Promise.resolve(r).then(finishAsync,e=>finishAsync(void 0,[e])):finish(r)}function isArtifactPreservingStateRead(e=`shared`,t){const n=artifactPreservingReads.getStore();return Boolean(n&&(e===`shared`||n.agentDatabases)&&(!t||![...n.privateRoots].some(e=>isPathInside(e,t))))}export{withArtifactPreservingStateReads as a,isStartupConfigStateRead as i,isArtifactPreservingStateRead as n,withLiveStateReads as o,isLiveStateRead as r,artifactPreservingReads as t};