UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

167 lines (166 loc) • 5.98 kB
import { a as resolveRuntimeWorkerUrl } from "./runtime-worker-url-DRDkoS4J.mjs"; import { r as createLazyRuntimeModule } from "./lazy-runtime-zGjszCtt.mjs"; import { r as openOpenClawAgentSqliteWorkerStoreV2 } from "./openclaw-agent-worker-store-Do41NaPe.mjs"; import "./process-runtime-DcBPEA8Y.mjs"; import "./memory-core-host-engine-storage-DcaZNQQh.mjs"; import { t as isFileMissingError } from "./fs-utils-Co3SFHW_.mjs"; import "./sqlite-runtime-lcoEVOL7.mjs"; import { r as readMemoryShadowIdentity, t as assertMemoryShadowIdentity } from "./manager-shadow-task-D4s6gAdr.mjs"; import { a as extractPromotionKeys } from "./short-term-promotion-memory-write-Bh1sTkan.mjs"; import { C as captureMemoryMutationAuthority } from "./dreaming-state-DzOG6jkz.mjs"; import { a as readDreamsFile, t as DREAMS_FILENAMES } from "./dreaming-dreams-file-Cbm7FmRe.mjs"; import { n as captureMemoryAgentReadTarget, t as captureMemoryAgentDatabaseOptions } from "./memory-agent-database-BaBNyKES.mjs"; import path from "node:path"; //#region extensions/memory-core/src/memory-entry-origins.ts const loadMemoryCpuProcessEntrypoints = createLazyRuntimeModule(() => import("./extensions/memory-core/manager-cpu-entrypoints.js")); const loadMemoryCpuWorkerRuntime = createLazyRuntimeModule(() => import("./manager-cpu-worker-runtime-BNhbvUad.mjs")); async function executeOriginCommand(e, t, n) { const { memoryCpuProcessEntrypoints: r } = await loadMemoryCpuProcessEntrypoints(); const i = resolveRuntimeWorkerUrl(r.entryOrigins); n?.(); const a = await openOpenClawAgentSqliteWorkerStoreV2(e, { version: 2, assertCurrent: () => n?.() }, { moduleUrl: i, input: { kind: t.type === "record" ? "origin" : "origin-existing" } }); try { if (t.type === "record") { await a.prepare(); return await a.run((e) => e.execute(t), () => n?.()); } return (await a.executeExisting(t, () => n?.()))?.value; } finally { await a.close(); } } async function listMemoryEntryOrigins(e, t) { if (e.sessionIds?.length === 0 || e.entryKeys?.length === 0) return []; const n = captureMemoryAgentReadTarget(t ?? captureMemoryAgentDatabaseOptions(e.agentId)); const r = { ...e.sessionIds ? { sessionIds: [...e.sessionIds] } : {}, ...e.entryKeys ? { entryKeys: [...e.entryKeys] } : {} }; const { runMemoryOriginRead: i } = await loadMemoryCpuWorkerRuntime(); return (await i({ ...n, ...r, kind: "origin-rows" })).rows; } async function findForgottenMemorySessionIds(e) { const t = /* @__PURE__ */ new Set(); if (e.sessionIds.length === 0) return t; const n = captureMemoryAgentReadTarget(captureMemoryAgentDatabaseOptions(e.agentId)); const r = [...e.sessionIds]; const { runMemoryOriginRead: i } = await loadMemoryCpuWorkerRuntime(); for (let e = 0; e < r.length; e += 256) { const a = r.slice(e, e + 256); const { indices: o } = await i({ ...n, sessionIds: a, kind: "session-tombstones" }); for (const e of o) t.add(a[e]); } return t; } async function recordMemoryEntryOrigins(e) { if (e.origins.length === 0) return []; const t = { agentId: e.agentId, entryKey: e.entryKey, origins: e.origins.map((e) => ({ entryKey: e.entryKey, agentId: e.agentId, sessionId: e.sessionId, sessionKey: e.sessionKey, originClass: e.originClass, observedAt: e.observedAt })) }; return await executeOriginCommand(captureMemoryAgentDatabaseOptions(e.agentId), { type: "record", input: t }, captureMemoryMutationAuthority()) ?? []; } async function deleteMemoryEntryOrigins(e, t, n) { if (e.entryKeys.length === 0 || e.sessionIds?.length === 0) return 0; return await executeOriginCommand(t, { type: "delete", input: e }, n) ?? 0; } async function reserveMemoryEntryOrigins(e) { const t = e.previousMemory.replace(/\r\n/gu, "\n").split("\n"); const n = e.operations.map((e) => { const n = /* @__PURE__ */ new Set([e.candidateKey]); for (const r of e.priorEntries) { const e = t.findIndex((e) => e.trim() === r); const i = t[e - 1]?.trim(); const a = /^<!--\s*openclaw-memory-promotion:([^\n]*?)\s*-->$/u.exec(i ?? "")?.[1]?.trim(); if (a) n.add(a); } return { operation: { ...e, priorEntries: [...e.priorEntries] }, parentKeys: n }; }); if ([...new Set(n.flatMap(({ parentKeys: e }) => [...e]))].length === 0) return async () => {}; const r = [...new Set(e.agentIds)].toSorted().map(captureMemoryAgentDatabaseOptions); const i = []; const rollback = async () => { for (const e of i.toReversed()) await deleteMemoryEntryOrigins(e.params, e.options, e.assertCurrent); }; try { for (const e of r) { let t; try { t = readMemoryShadowIdentity(e.path); } catch (e) { if (isFileMissingError(e)) continue; throw e; } const assertCurrent = () => assertMemoryShadowIdentity(e.path, t); const r = await executeOriginCommand(e, { type: "reserve", input: { agentId: e.agentId, operations: n.map(({ operation: e, parentKeys: t }) => ({ entryKey: e.candidateKey, parentKeys: [...t] })) } }, assertCurrent) ?? []; for (const t of r) i.push({ params: t, options: e, assertCurrent }); } } catch (e) { await rollback(); throw e; } return rollback; } async function pruneMemoryEntryOrigins(e) { const t = [...new Set(e.entryKeys)].filter((t) => !e.retainedEntryKeys.has(t)); if (t.length === 0) return; const n = [...new Set(e.agentIds)].map(captureMemoryAgentDatabaseOptions); const r = await Promise.all(DREAMS_FILENAMES.map((t) => readDreamsFile(path.join(e.workspaceDir, t), e.workspaceDir))); const i = new Set(r.flatMap(extractPromotionKeys)); for (const e of n) await executeOriginCommand(e, { type: "prune", input: { agentId: e.agentId, entryKeys: t.filter((e) => !i.has(e)) } }); } //#endregion export { reserveMemoryEntryOrigins as a, recordMemoryEntryOrigins as i, listMemoryEntryOrigins as n, pruneMemoryEntryOrigins as r, findForgottenMemorySessionIds as t };