UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

198 lines (197 loc) • 6.5 kB
import { r as isIncognitoSessionKey } from "./session-key-DW1jcHbM.mjs"; import { r as isIncognitoOpenClawAgentSqlitePath } from "./openclaw-agent-db.paths-DJxv80ws.mjs"; import "./openclaw-agent-db-CwXW4hGa.mjs"; import { i as buildSessionCreationStamp } from "./session-entry-provenance-01l9mXT0.mjs"; import { n as resolveInternalSessionEffectsIdentity } from "./internal-session-key-DySyL5iT.mjs"; import { i as readSessionEntryReadOnlyInWorker } from "./session-entry-read-runtime-1Fp1sybj.mjs"; import { a as readCapturedSessionActorEntry, t as captureSessionActorStorageOwner } from "./session-actor-storage-binding-DMwpDsij.mjs"; import { a as captureSessionEntryReadScope } from "./session-entry-read-ordered-DPv30g3R.mjs"; import "./session-accessor-CizTbDQj.mjs"; import { n as withSessionActorStorage } from "./session-actor-storage-acquisition-bj-qIczw.mjs"; import { d as forkSessionTranscriptFromParent, g as applySessionEntryLifecycleMutation, n as createSessionEntryWithTranscript } from "./session-accessor.entry-mutation-IJ4DYZ4a.mjs"; import { n as deleteSessionEntryLifecycle } from "./session-accessor.sqlite-lifecycle-DYu1Pygl.mjs"; //#region src/agents/internal-session-effects.ts /** Manages hidden sessions used for suppressed agent side effects. */ /** Resolves the deterministic storage target owned by one internal-effects run. */ function resolveInternalSessionEffectsTarget(e) { const t = Boolean(captureSessionActorStorageOwner(e, { assertCurrent() {}, authorize() {} })) || isIncognitoOpenClawAgentSqlitePath(e.storePath, { agentId: e.agentId }); return { agentId: e.agentId, storePath: e.storePath, ...resolveInternalSessionEffectsIdentity({ agentId: e.agentId, runId: e.runId, ...t ? { incognito: true } : {} }) }; } function toInternalSessionEffectsTarget(e, t) { return { agentId: e.agentId, sessionId: t.sessionId, sessionKey: e.sessionKey, storePath: e.storePath, sessionEntry: t, sessionFile: e.sessionKey }; } /** Creates or reopens the hidden session owned by one internal-effects run. */ async function prepareInternalSessionEffectsSession(e) { e.commitGuard?.(); const t = resolveInternalSessionEffectsTarget(e); const { env: n } = captureSessionEntryReadScope(t); const r = { ...t, env: n }; const i = { assertCurrent() { e.commitGuard?.(); }, authorize() {} }; const a = captureSessionActorStorageOwner(r, i); const prepare = async () => { const t = a ? readCapturedSessionActorEntry(a, r.sessionKey) : await readSessionEntryReadOnlyInWorker(r, e.commitGuard); e.commitGuard?.(); if (t?.sessionId === r.sessionId) return toInternalSessionEffectsTarget(r, t); if (e.source) await forkSessionTranscriptFromParent({ agentId: e.source.agentId, parentEntry: { sessionId: e.source.sessionId, updatedAt: Date.now() }, parentSessionKey: e.source.sessionKey, sessionKey: r.sessionKey, storePath: e.source.storePath, targetSessionId: r.sessionId, targetStorePath: e.storePath, commitGuard: e.commitGuard }); const n = Date.now(); const i = await createSessionEntryWithTranscript(r, () => ({ ok: true, entry: { ...buildSessionCreationStamp({ via: "internal", actor: { type: "system" } }), delivery: { kind: "internal" }, sessionId: r.sessionId, ...isIncognitoSessionKey(r.sessionKey) ? { incognito: true } : {}, sessionStartedAt: n, updatedAt: n } }), { cwd: e.cwd, commitGuard: e.commitGuard }); if (!i.ok) throw new Error(`Failed to create internal session for run ${e.runId}`); return toInternalSessionEffectsTarget(r, i.entry); }; if (!a) return prepare(); const o = await withSessionActorStorage(r, { authority: i, lifetime: { assertCurrent: () => i.assertCurrent(), assertReadable: () => i.assertCurrent() }, create: true }, prepare); if (!o) throw new Error("Internal memory session could not be acquired"); return o; } /** Tracks every hidden binding used by one run, including accepted compaction rotations. */ function createInternalSessionEffectsCleanup(e) { const t = e.enabled ? /* @__PURE__ */ new Map() : void 0; const n = e.enabled && e.storePath ? captureSessionActorStorageOwner(e, { assertCurrent() {}, authorize() {} }) : void 0; const track = (e) => { if (!t || !e?.sessionKey || !e.storePath) return; t.set(`${e.storePath}\n${e.sessionKey}`, e); }; if (t && e.storePath) track(resolveInternalSessionEffectsTarget({ agentId: e.agentId, runId: e.runId, storePath: e.storePath })); const cleanup = async () => { if (!t) return; for (const n of t.values()) try { await removeInternalSessionEffectsSession(n); } catch (t) { e.onError(t); } }; return { track, cleanup: async () => { if (n && !n.owner && !n.binding) return; n?.owner?.assertCurrent(); await cleanup(); } }; } /** Hard-deletes a run-owned hidden session and its transcript rows. */ async function removeInternalSessionEffectsSession(e, t) { if (!e?.sessionKey || !e.storePath) return; const n = captureSessionActorStorageOwner(e, { assertCurrent() {}, authorize() {} }); const { env: r } = captureSessionEntryReadScope({ ...e, sessionKey: e.sessionKey }); const i = { ...e.agentId ? { agentId: e.agentId } : {}, storePath: e.storePath, env: r }; const a = t ? n ? readCapturedSessionActorEntry(n, e.sessionKey) : await readSessionEntryReadOnlyInWorker({ ...i, sessionKey: e.sessionKey }) : void 0; if (t && (!a || a.sessionId !== e.sessionId || a.lifecycleRevision !== t.lifecycleRevision || a.activeWriterRunId !== t.activeWriterRunId)) return; if (n) { const t = e.sessionKey; await withSessionActorStorage({ ...i, sessionKey: t }, { authority: n.authority, lifetime: { assertCurrent() {}, assertReadable() {} } }, () => deleteSessionEntryLifecycle({ ...i, target: { canonicalKey: t, storeKeys: [t] }, archiveTranscript: false, deleteTranscriptWithoutArchive: true, expectedSessionId: e.sessionId, expectedEntry: a })); return; } await applySessionEntryLifecycleMutation({ ...i, removals: [{ sessionKey: e.sessionKey, ...e.sessionId ? { expectedSessionId: e.sessionId } : {}, ...a ? { expectedEntry: a } : {}, archiveRemovedTranscript: false }], skipMaintenance: true }); } //#endregion export { prepareInternalSessionEffectsSession as n, removeInternalSessionEffectsSession as r, createInternalSessionEffectsCleanup as t };