UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

189 lines (188 loc) • 7.7 kB
import { i as resolveRuntimeWorkerUrl } from "./runtime-worker-url-CFqA9Dvi.mjs"; import { r as createLazyRuntimeModule } from "./lazy-runtime-CJrzFzqU.mjs"; import { d as withOpenClawAgentDatabaseRuntime } from "./openclaw-agent-db-DWI9Bgur.mjs"; import { n as runOpenClawAgentWriteAdmission } from "./openclaw-agent-write-admission-CXJiE5VB.mjs"; import { n as openOpenClawAgentSqliteWorkerStore } from "./openclaw-agent-worker-store-BE9hhRoh.mjs"; import "./sqlite-runtime-DP6ZKsOz.mjs"; import { a as extractPromotionKeys } from "./short-term-promotion-memory-write-iI2cm7Dr.mjs"; import "./process-runtime-JJRvKfGm.mjs"; import { a as readDreamsFile, t as DREAMS_FILENAMES } from "./dreaming-dreams-file-B35EYQGz.mjs"; import { n as captureMemoryAgentReadTarget, t as captureMemoryAgentDatabaseOptions } from "./memory-agent-database-CSFMSx9Y.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-X-kkaZ6s.mjs")); async function executeOriginCommand(options, command, assertOriginal) { const { memoryCpuProcessEntrypoints } = await loadMemoryCpuProcessEntrypoints(); const moduleUrl = resolveRuntimeWorkerUrl(memoryCpuProcessEntrypoints.entryOrigins); assertOriginal?.(); return runOpenClawAgentWriteAdmission(options, async (_identity, assertAdmission) => { const assertCurrent = () => { assertOriginal?.(); assertAdmission(); }; return withOpenClawAgentDatabaseRuntime(options, async ({ db }) => { const worker = await openOpenClawAgentSqliteWorkerStore(options, db, { moduleUrl, input: { kind: "origin" } }); try { return await worker.run((scope) => scope.execute(command), assertCurrent); } finally { await worker.close(); } }, assertCurrent); }, true); } async function listMemoryEntryOrigins(params, options) { if (params.sessionIds?.length === 0 || params.entryKeys?.length === 0) return []; const target = captureMemoryAgentReadTarget(options ?? captureMemoryAgentDatabaseOptions(params.agentId)); const filters = { ...params.sessionIds ? { sessionIds: [...params.sessionIds] } : {}, ...params.entryKeys ? { entryKeys: [...params.entryKeys] } : {} }; const { runMemoryOriginRead } = await loadMemoryCpuWorkerRuntime(); return (await runMemoryOriginRead({ ...target, ...filters, kind: "origin-rows" })).rows; } async function listMemorySessionTombstones(params) { if (params.sessionIds?.length === 0) return []; const target = captureMemoryAgentReadTarget(captureMemoryAgentDatabaseOptions(params.agentId)); const sessionIds = params.sessionIds ? [...params.sessionIds] : void 0; const { runMemoryOriginRead } = await loadMemoryCpuWorkerRuntime(); return (await runMemoryOriginRead({ ...target, sessionIds, kind: "session-tombstones" })).rows; } async function recordMemoryEntryOrigins(params) { if (params.origins.length === 0) return []; const input = { agentId: params.agentId, entryKey: params.entryKey, origins: params.origins.map((origin) => ({ entryKey: origin.entryKey, agentId: origin.agentId, sessionId: origin.sessionId, sessionKey: origin.sessionKey, originClass: origin.originClass, observedAt: origin.observedAt })) }; return executeOriginCommand(captureMemoryAgentDatabaseOptions(params.agentId), { type: "record", input }); } async function deleteMemoryEntryOrigins(params, options, assertOriginal) { if (params.entryKeys.length === 0 || params.sessionIds?.length === 0) return 0; assertOriginal(); const target = captureMemoryAgentReadTarget(options); const filters = { entryKeys: [...params.entryKeys], ...params.sessionIds ? { sessionIds: [...params.sessionIds] } : {} }; const { runMemoryOriginRead } = await loadMemoryCpuWorkerRuntime(); assertOriginal(); const { exists } = await runMemoryOriginRead({ ...target, ...filters, kind: "origin-exists" }); assertOriginal(); if (!exists) return 0; return executeOriginCommand(options, { type: "delete", input: params }, assertOriginal); } async function reserveMemoryEntryOrigins(params) { const previousLines = params.previousMemory.replace(/\r\n/gu, "\n").split("\n"); const operationParents = params.operations.map((operation) => { const parentKeys = /* @__PURE__ */ new Set([operation.candidateKey]); for (const entry of operation.priorEntries) { const entryIndex = previousLines.findIndex((line) => line.trim() === entry); const marker = previousLines[entryIndex - 1]?.trim(); const parentKey = /^<!--\s*openclaw-memory-promotion:([^\n]*?)\s*-->$/u.exec(marker ?? "")?.[1]?.trim(); if (parentKey) parentKeys.add(parentKey); } return { operation: { ...operation, priorEntries: [...operation.priorEntries] }, parentKeys }; }); const affectedKeys = [...new Set(operationParents.flatMap(({ parentKeys }) => [...parentKeys]))]; if (affectedKeys.length === 0) return async () => {}; const owners = [...new Set(params.agentIds)].toSorted().map(captureMemoryAgentDatabaseOptions); const reservations = []; const rollback = async () => { for (const reservation of reservations.toReversed()) await deleteMemoryEntryOrigins(reservation.params, reservation.options, reservation.assertCurrent); }; try { for (const options of owners) await runOpenClawAgentWriteAdmission(options, async (_identity, assertCurrent) => { const agentId = options.agentId; assertCurrent(); const origins = await listMemoryEntryOrigins({ agentId, entryKeys: affectedKeys }, options); assertCurrent(); for (const { operation, parentKeys } of operationParents) { const selected = origins.filter((origin) => parentKeys.has(origin.entryKey)); if (selected.length === 0) continue; const added = await executeOriginCommand(options, { type: "record", input: { agentId, origins: selected, entryKey: operation.candidateKey } }, assertCurrent); if (added.length > 0) reservations.push({ params: { agentId, entryKeys: [operation.candidateKey], sessionIds: added.map((origin) => origin.sessionId) }, options, assertCurrent }); } }, true); } catch (error) { await rollback(); throw error; } return rollback; } async function pruneMemoryEntryOrigins(params) { const entryKeys = [...new Set(params.entryKeys)].filter((key) => !params.retainedEntryKeys.has(key)); if (entryKeys.length === 0) return; const owners = [...new Set(params.agentIds)].map(captureMemoryAgentDatabaseOptions); const diaries = await Promise.all(DREAMS_FILENAMES.map((name) => readDreamsFile(path.join(params.workspaceDir, name), params.workspaceDir))); const diaryKeys = new Set(diaries.flatMap(extractPromotionKeys)); const { runMemoryOriginRead } = await loadMemoryCpuWorkerRuntime(); for (const options of owners) await runOpenClawAgentWriteAdmission(options, async (_identity, assertCurrent) => { const agentId = options.agentId; assertCurrent(); const { keys } = await runMemoryOriginRead({ ...captureMemoryAgentReadTarget(options), kind: "origin-index-keys" }); const indexed = new Set(keys); assertCurrent(); await deleteMemoryEntryOrigins({ agentId, entryKeys: entryKeys.filter((key) => !diaryKeys.has(key) && !indexed.has(key)) }, options, assertCurrent); }, true); } //#endregion export { reserveMemoryEntryOrigins as a, recordMemoryEntryOrigins as i, listMemorySessionTombstones as n, pruneMemoryEntryOrigins as r, listMemoryEntryOrigins as t };