UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

167 lines (166 loc) • 6.1 kB
import { n as normalizeAgentId, t as isValidAgentId } from "./agent-id-CQjtp2ft.mjs"; import "./artifacts-D7SzhQlr.mjs"; import "./paths-DvQ5AJvD.mjs"; import { n as createLazyRuntimeMethodBinder } from "./lazy-runtime-zGjszCtt.mjs"; import "./config-env-vars-ByeGN-9N.mjs"; import "./transcript-events-BHwlF03q.mjs"; import "./sdk-deprecation-D5N9acnu.mjs"; import { o as resolveSessionStorePathForScope } from "./session-store-path-BIJyO4vM.mjs"; import { u as withSessionStoreReaderInWorker } from "./session-entry-read-runtime-1Fp1sybj.mjs"; import { a as captureSessionEntryReadScope } from "./session-entry-read-ordered-DPv30g3R.mjs"; import "./archive-compression-sRFuVDRH.mjs"; import "./session-accessor-CizTbDQj.mjs"; import "./openclaw-runtime-paths-DuD50SoI.mjs"; import { n as withSessionActorStorage } from "./session-actor-storage-acquisition-bj-qIczw.mjs"; import "./config-CMA-J084.mjs"; import { t as captureSessionActorTranscriptRead } from "./session-actor-transcript-read-CHpAgP2z.mjs"; import "./session-cold-storage-read-DHL_Hyuu.mjs"; import "./session-store-runtime-D5peOAR8.mjs"; import { n as captureMemoryOwner } from "./session-memory-authority-C31Twnth.mjs"; import path from "node:path"; //#region src/config/sessions/session-transcript-stats-batch.ts /** Preserve input order while reading each durable store in one worker request. */ async function readTranscriptStatsBatchReadOnlyAsync(e) { const t = e.map(() => null); const n = /* @__PURE__ */ new Map(); for (const [r, i] of e.entries()) { const e = captureSessionActorTranscriptRead(i); if (e) { if (e.missing) { e.assertCurrent(); t[r] = { eventCount: 0, maxSeq: 0, sizeBytes: 0 }; } else t[r] = await e.read("session.history.stats", {}); continue; } const { scope: a, agentId: o } = captureSessionEntryReadScope({ ...i, sessionKey: i.sessionKey ?? "" }); const s = resolveSessionStorePathForScope(a); const c = { ...a, agentId: o, storePath: s }; const l = JSON.stringify([ o, s, c.env ]); const u = n.get(l) ?? { scope: c, sessionIds: [], indexes: [] }; u.sessionIds.push(i.sessionId); u.indexes.push(r); n.set(l, u); } await Promise.all([...n.values()].map(async ({ scope: e, sessionIds: n, indexes: r }) => { const i = await withSessionStoreReaderInWorker(e, ({ reader: t, database: r }) => t.readTranscriptStatsBatch({ sessionIds: n, env: r.env ?? e.env }), { backing: true, dataOnly: true }); for (const [e, n] of r.entries()) t[n] = i[e] ?? null; })); return t; } //#endregion //#region packages/memory-host-sdk/src/host/openclaw-runtime-session.ts /** Capture the memory namespace once; reads never create a missing owner or session. */ function captureIncognitoMemoryReader(e) { const t = captureMemoryOwner(e); if (!t) return; const n = t.binding?.agentId === t.agentId ? t.binding : void 0; const read = async (r, i) => { if (!t.owner && !n) { t.authority.assertCurrent(); return i; } return await withSessionActorStorage({ ...e, agentId: t.agentId, storePath: t.path, sessionActor: n }, { authority: t.authority, lifetime: { assertCurrent: () => t.authority.assertCurrent(), assertReadable: () => t.authority.assertCurrent() } }, r) ?? i; }; const r = { sessionId: e.sessionId ?? e.sessionEntry?.sessionId, sessionKey: e.sessionKey }; return { memoryEntry(e, t, n) { return read(async (i) => { const { buildSessionEntryFromSnapshot: a } = await import("./session-files-C0Indxc1.mjs"); const o = await i.actor.storage.read({ type: "session.memory.entry", input: { ...r, includeMessages: Boolean(t.onTranscriptMessage) } }, i.authority); if (!o) return null; return a(e, { ...t, agentId: i.agentId, storePath: i.path, sessionId: o.sessionId, sessionKey: o.sessionKey }, o, () => { i.actor.snapshot(i.authority); }, n); }, null); }, memoryResetRecall() { return read((e) => e.actor.storage.read({ type: "session.memory.resetRecall", input: r }, e.authority), { state: "invalid" }); } }; } /** Keep worker launch machinery behind the memory host's existing lazy runtime bridge. */ const bindTranscriptRead = createLazyRuntimeMethodBinder(() => import("./session-transcript-read-worker-runtime-klXTspOx.mjs")); const bindTranscriptInventory = createLazyRuntimeMethodBinder(() => import("./session-transcript-inventory-runtime-DFCyicOB.mjs")); const streamSessionEntriesInWorker = bindTranscriptRead((e) => e.streamSessionEntriesInWorker); const readSessionTranscriptCorpusInWorker = bindTranscriptInventory((e) => e.readSessionTranscriptCorpusInWorker); /** Extracts the agent id from a canonical `agents/<id>/sessions` directory path. */ function extractAgentIdFromSessionsDir(e) { const t = path.normalize(path.resolve(e)).split(path.sep).filter(Boolean); const n = t.at(-1); const r = t.at(-2); const i = t.at(-3); const a = process.platform === "win32"; if (!n || !r || !i || (a ? n.toLowerCase() : n) !== "sessions" || (a ? i.toLowerCase() : i) !== "agents" || a && (r !== r.trim() || !isValidAgentId(r))) return null; return a ? normalizeAgentId(r) : r; } /** Finds the nearest canonical sessions owner without escaping its directory. */ function extractAgentIdFromSessionPath(e) { let t = path.dirname(path.resolve(e)); while (true) { const e = path.basename(t); if ((process.platform === "win32" ? e.toLowerCase() : e) === "sessions") { const e = extractAgentIdFromSessionsDir(t); if (e) return e; } const n = path.dirname(t); if (n === t) return null; t = n; } } const readSessionResetRecallCutoffInWorker = bindTranscriptRead((e) => e.readSessionResetRecallCutoffInWorker); //#endregion export { readSessionTranscriptCorpusInWorker as a, readSessionResetRecallCutoffInWorker as i, extractAgentIdFromSessionPath as n, streamSessionEntriesInWorker as o, extractAgentIdFromSessionsDir as r, readTranscriptStatsBatchReadOnlyAsync as s, captureIncognitoMemoryReader as t };