UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

253 lines (252 loc) • 10.9 kB
import { n as normalizeAgentId, t as isValidAgentId } from "./agent-id-D1-Q6vR4.mjs"; import "./session-key-DY3MOX4N.mjs"; import "./paths-Bonddxfk.mjs"; import "./openclaw-agent-db.paths-BbmZlEP5.mjs"; import "./config-env-vars-BzUE_dOh.mjs"; import "./artifacts-C6u8F4sP.mjs"; import "./transcript-events-DSEdnn1V.mjs"; import { t as captureSessionTranscriptStorageEnvironment } from "./transcript-target-binding-DpcuSIFy.mjs"; import "./agent-scope-bJOokm5r.mjs"; import "./config-VnHbY_QV.mjs"; import "./internal-runtime-context-D5iQhnLo.mjs"; import { r as IncognitoSessionSyncAccessError } from "./incognito-session-error-BUzgcwoZ.mjs"; import "./main-session-BOUJdkxQ.mjs"; import "./tokens-Ljy0DfNV.mjs"; import { c as withIncognitoSessionBinding, i as captureIncognitoSessionSource, n as captureIncognitoSessionHistoryBinding } from "./session-incognito-binding-Dp6RTei2.mjs"; import "./session-cold-storage-state-BRw-PqQq.mjs"; import "./heartbeat-nZ1C5oCa.mjs"; import "./archive-compression--mynoXML.mjs"; import "./input-provenance-B5YZS2Do.mjs"; import "./session-cold-storage-read-Dq7uoWUT.mjs"; import { C as readTranscriptStatsBatchReadOnlySync$1, w as readTranscriptStatsSync } from "./session-accessor.sqlite-read-UBWmiUlC.mjs"; import "./strip-inbound-meta-_xsoQtSF.mjs"; import "./session-accessor-C3ljJ974.mjs"; import "./heartbeat-filter-DruZVPZx.mjs"; import "./heartbeat-events-filter-CAoft5F2.mjs"; import "./session-store-runtime-B8fW3ciZ.mjs"; import path from "node:path"; //#region packages/memory-host-sdk/src/host/openclaw-runtime-session.ts /** Native sync compatibility stays available until production actor acquisition. */ function assertBoundIncognitoMemorySyncAccess(scope, method, replacement) { if (captureIncognitoSessionSource(scope)) throw new IncognitoSessionSyncAccessError(method, replacement); } function readTranscriptStatsBatchReadOnlySync(scopes) { for (const scope of scopes) assertBoundIncognitoMemorySyncAccess(scope, "readTranscriptStatsBatchReadOnlySync", "buildSessionEntry"); return readTranscriptStatsBatchReadOnlySync$1(scopes); } /** Capture the physical source before loading the optional compute adapter. */ function captureIncognitoMemoryReader(scope) { const shared = captureIncognitoSessionSource(scope); if (!shared) return; if ("kind" in shared) { const readAbsent = (value) => Promise.resolve().then(() => { shared.assertCurrent(); return value; }); return { memoryEntry() { return readAbsent(null); }, memoryResetRecall() { return readAbsent({ state: "invalid" }); } }; } const { actor, admissionSignal } = shared; const memorySessionId = scope.sessionId ?? scope.sessionEntry?.sessionId; const authority = { assertCurrent() { admissionSignal?.throwIfAborted(); actor.assertReadable(); } }; const capture = (sessionKey) => withIncognitoSessionBinding(shared, () => { const binding = captureIncognitoSessionHistoryBinding({ agentId: actor.agentId, storePath: actor.path, sessionKey }); if (!binding) throw new Error("Incognito Memory lost its captured binding"); return binding; }); const currentKey = scope.sessionKey ?? actor.sessions.deadlines().find((entry) => entry.sessionId === memorySessionId)?.sessionKey; const current = currentKey && actor.sessions.readSharing(currentKey)?.entry ? capture(currentKey) : void 0; const missingClaim = !current && currentKey ? actor.sessions.captureCurrent(currentKey) : void 0; const read = (operation, missing) => { let resolved = current; let selectedAbsent = false; let assertReadCurrent; const consume = async (binding, assertSource) => { const { bindIncognitoSessionComputeReader } = await import("./session-incognito-compute-read-CDF0eWtT.mjs"); assertSource(); return operation(bindIncognitoSessionComputeReader({ ...binding, memorySessionId, onMemoryRead: (assertCurrent) => { assertReadCurrent = assertCurrent; }, authority: { assertCurrent() { assertSource(); binding.authority.assertCurrent(); }, authorize: (stage, facts) => binding.authority.authorize?.(stage, facts) } })); }; return actor.sessions.withSharedState(() => current ? consume(current, () => current.authority.assertCurrent()) : actor.sessions.withCompute(authority, void 0, async (compute) => { const inventory = await compute.execute({ type: "session.compute.store.inventory", input: {} }); compute.assertCurrent(); missingClaim?.assertCurrent(); const selected = inventory.find((entry) => entry.sessionId === memorySessionId && (!currentKey || entry.sessionKey === currentKey)); if (!selected) { selectedAbsent = true; return missing; } resolved = capture(selected.sessionKey); return consume(resolved, compute.assertCurrent); }, admissionSignal)).then((result) => { authority.assertCurrent(); missingClaim?.assertCurrent(); resolved?.authority.assertCurrent(); assertReadCurrent?.(); if (selectedAbsent && actor.sessions.deadlines().some((entry) => entry.sessionId === memorySessionId && (!currentKey || entry.sessionKey === currentKey))) throw new Error("Incognito Memory transcript appeared during preparation"); return result; }); }; return { memoryEntry(absPath, options) { const { onTranscriptMessage, ...serializable } = options; const captured = { ...structuredClone(serializable), storePath: actor.path, onTranscriptMessage }; return read((reader) => reader.memoryEntry(absPath, captured), null); }, memoryResetRecall(input) { const captured = { ...structuredClone(input), storePath: actor.path }; return read((reader) => reader.memoryResetRecall(captured), { state: "invalid" }); } }; } /** Read the captured memory-only corpus without opening an absent actor. */ function readBoundIncognitoMemoryCorpus(scope, options) { const binding = captureIncognitoSessionSource({ agentId: scope.normalizedAgentId, storePath: scope.storePath }); if (!binding) return; if ("kind" in binding) return Promise.resolve().then(() => { binding.assertCurrent(); return []; }); const selected = binding.actor.sessions.deadlines(); const claims = new Map(selected.map(({ sessionKey }) => [sessionKey, binding.actor.sessions.captureCurrent(sessionKey)])); const assertSelection = () => { binding.admissionSignal?.throwIfAborted(); binding.actor.assertReadable(); const current = binding.actor.sessions.deadlines(); if (current.length !== claims.size || current.some(({ sessionKey }) => !claims.has(sessionKey))) throw new Error("Incognito Memory corpus changed during preparation"); for (const claim of claims.values()) claim.assertCurrent(); }; let assertReadCurrent; const disclose = (value) => { assertSelection(); assertReadCurrent?.(); return value; }; assertSelection(); const target = selected[0]; if (!target) return binding.actor.sessions.withCompute({ assertCurrent: assertSelection }, void 0, async (compute) => { await compute.execute({ type: "session.compute.store.inventory", input: {} }); return []; }, binding.admissionSignal).then(disclose); const history = captureIncognitoSessionHistoryBinding({ agentId: scope.normalizedAgentId, storePath: scope.storePath, ...target }); if (!history) throw new Error("Incognito Memory corpus lost its captured binding"); const captured = structuredClone({ scope: { ...scope, env: captureSessionTranscriptStorageEnvironment(scope.env) }, options }); return binding.actor.sessions.withSharedState(async () => { const { readIncognitoMemoryCorpus } = await import("./session-incognito-memory-corpus-D16R3uKf.mjs"); assertSelection(); return readIncognitoMemoryCorpus({ ...history, authority: { assertCurrent() { assertSelection(); history.authority.assertCurrent(); }, authorize: (stage, facts) => history.authority.authorize?.(stage, facts) } }, captured.scope, captured.options, binding.admissionSignal, (assertCurrent) => { assertReadCurrent = assertCurrent; }); }).then(disclose); } /** Keep worker launch machinery behind the memory host's existing lazy runtime bridge. */ async function prepareSessionEntryInWorker(...args) { const { prepareSessionEntryInWorker: prepare } = await import("./session-transcript-read-worker-runtime-CjWPhKgS.mjs"); return prepare(...args); } async function readSessionTranscriptCorpusInWorker(...args) { const { readSessionTranscriptCorpusInWorker: read } = await import("./session-transcript-inventory-runtime-ZRON-1F5.mjs"); return read(...args); } /** Returns an opaque revision that changes for every canonical transcript mutation. */ function readTranscriptContentRevisionSync(params) { const stats = readTranscriptStatsSync(params); return [ "sqlite", stats.maxSeq, stats.sizeBytes, stats.eventCount, stats.lastMutationAtMs ?? "", stats.lastObservedMutationAtMs ?? "" ].join(":"); } /** Extracts the agent id from a canonical `agents/<id>/sessions` directory path. */ function extractAgentIdFromSessionsDir(sessionsDir) { const parts = path.normalize(path.resolve(sessionsDir)).split(path.sep).filter(Boolean); const sessionsSegment = parts.at(-1); const agentId = parts.at(-2); const agentsSegment = parts.at(-3); const isWindows = process.platform === "win32"; if (!sessionsSegment || !agentId || !agentsSegment || (isWindows ? sessionsSegment.toLowerCase() : sessionsSegment) !== "sessions" || (isWindows ? agentsSegment.toLowerCase() : agentsSegment) !== "agents" || isWindows && (agentId !== agentId.trim() || !isValidAgentId(agentId))) return null; return isWindows ? normalizeAgentId(agentId) : agentId; } /** Finds the nearest canonical sessions owner without escaping its directory. */ function extractAgentIdFromSessionPath(absPath) { let currentDir = path.dirname(path.resolve(absPath)); while (true) { const currentSegment = path.basename(currentDir); if ((process.platform === "win32" ? currentSegment.toLowerCase() : currentSegment) === "sessions") { const agentId = extractAgentIdFromSessionsDir(currentDir); if (agentId) return agentId; } const parentDir = path.dirname(currentDir); if (parentDir === currentDir) return null; currentDir = parentDir; } } async function readSessionResetRecallCutoffInWorker(...args) { const { readSessionResetRecallCutoffInWorker: read } = await import("./session-transcript-read-worker-runtime-CjWPhKgS.mjs"); return read(...args); } //#endregion export { prepareSessionEntryInWorker as a, readSessionTranscriptCorpusInWorker as c, extractAgentIdFromSessionsDir as i, readTranscriptContentRevisionSync as l, captureIncognitoMemoryReader as n, readBoundIncognitoMemoryCorpus as o, extractAgentIdFromSessionPath as r, readSessionResetRecallCutoffInWorker as s, assertBoundIncognitoMemorySyncAccess as t, readTranscriptStatsBatchReadOnlySync as u };