UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

131 lines (130 loc) • 6.7 kB
import { n as isPostgresSqlConnection } from "./sql-connection-Beiyg5Dt.mjs"; import { c as withSqlitePostCommitPublications } from "./sqlite-post-commit-BrUVQObC.mjs"; import { i as executeSqliteQuerySync, p as sqliteStringSet, s as getNodeSqliteKysely } from "./kysely-sync-YJo2lmGV.mjs"; import { a as tableExists } from "./openclaw-state-db-schema-helpers-nImZNMq_.mjs"; import { a as runSqliteImmediateTransactionSync } from "./sqlite-transaction-BHHu6P5-.mjs"; import { n as loadSqliteVecExtensionFromPath } from "./sqlite-vec-2t4h9sP9.mjs"; import { t as bindSqliteWorkerDatabase } from "./sqlite-worker-binding-B4BB5u5i.mjs"; import { n as readMemoryEntryOriginsInDatabase, r as recordMemoryEntryOriginsInDatabase, t as ensureMemoryEntryOriginsSchema } from "./memory-core-host-engine-storage-DcaZNQQh.mjs"; import "./sqlite-worker-runtime-CKocuU_N.mjs"; import { t as selectedMemoryLineageIdentity } from "./memory-entry-origins-task-B6bHy0HK.mjs"; import { t as advanceMemoryIndexRevision } from "./memory-index-revision.worker-CQcqIl0f.mjs"; import { r as recordMemorySessionTombstonesInDatabase, t as ensureMemorySessionTombstones } from "./memory-session-tombstones-D5Byk7o_.mjs"; import { a as extractPromotionKeys } from "./short-term-promotion-memory-write-Bh1sTkan.mjs"; //#region extensions/memory-core/src/memory-entry-origins-delete.ts /** Forget retains its supplied connection and original transaction boundary. */ function deleteMemoryEntryOriginsInDatabase(e, t, n) { if (t.entryKeys.length === 0 || t.sessionIds?.length === 0 || !tableExists(e, "memory_entry_origins")) return 0; return runSqliteImmediateTransactionSync(e, () => { n?.onBegin(); let r = getNodeSqliteKysely(e).deleteFrom("memory_entry_origins").where("agent_id", "=", t.agentId).where("entry_key", "in", sqliteStringSet(t.entryKeys)); if (t.sessionIds) r = r.where("session_id", "in", sqliteStringSet(t.sessionIds)); return Number(executeSqliteQuerySync(e, r).numAffectedRows ?? 0n); }, n && { withCommit: n.withCommit }); } //#endregion //#region extensions/memory-core/src/memory-forget-kernel.ts function checkLineage(e, t) { const n = tableExists(e, "memory_entry_origins") ? readMemoryEntryOriginsInDatabase(e, { agentId: t.agentId }) : []; return selectedMemoryLineageIdentity(n, new Set(t.sessionIds), new Set(t.entryKeys)) === t.identity ? { current: true } : { current: false, origins: n }; } /** The binding owns this transaction and its commit grant. */ function markMemoryForgotten(e, t) { const n = checkLineage(e, t); if (!n.current) return n; if (recordMemorySessionTombstonesInDatabase(e, t) === 0) advanceMemoryIndexRevision(e); return n; } /** The earlier tombstone commit survives any failure of this separate purge. */ function purgeForgottenMemory(e, t) { const n = checkLineage(e, t); if (!n.current) return n; const r = getNodeSqliteKysely(e); if (t.chunkIds.length > 0) { if (t.hasVectorTable && !isPostgresSqlConnection(e)) executeSqliteQuerySync(e, r.deleteFrom("memory_index_chunks_vec").where("id", "in", t.chunkIds)); executeSqliteQuerySync(e, r.deleteFrom("memory_index_chunks").where("id", "in", isPostgresSqlConnection(e) ? sqliteStringSet(t.chunkIds) : t.chunkIds)); } deleteMemoryIndexSources(e, t.sources); if (tableExists(e, "memory_embedding_cache")) executeSqliteQuerySync(e, r.deleteFrom("memory_embedding_cache")); if (t.chunkIds.length > 0 || t.sources.length > 0) advanceMemoryIndexRevision(e); return n; } function deleteMemoryIndexSources(e, t) { const n = getNodeSqliteKysely(e); for (let r = 0; r < t.length;) { const i = t[r]; let a = r + 1; while (a < t.length && t[a].source === i.source) a += 1; executeSqliteQuerySync(e, n.deleteFrom("memory_index_sources").where("path", "in", sqliteStringSet(t.slice(r, a).map((e) => e.path))).where("source", "=", i.source)); r = a; } } //#endregion //#region extensions/memory-core/src/memory-entry-origins.worker.ts /** The existing agent executor owns this connection and its native lifetime. */ function bindSqliteWorkerBackend(e, t) { const n = t.database; const r = { onBegin: () => t.admit("transaction"), withCommit: (e) => { t.admit("commit"); e(); } }; const transact = (e) => runSqliteImmediateTransactionSync(n, () => { r.onBegin(); return e(); }, { withCommit: r.withCommit }); if (e.kind === "forget") { if (e.extensionPath && !isPostgresSqlConnection(n)) try { loadSqliteVecExtensionFromPath(n, e.extensionPath); } catch (e) { throw new Error(`memory forget cannot purge vector index: ${e instanceof Error ? e.message : String(e)}`, { cause: e }); } if (e.prepareTombstones && !isPostgresSqlConnection(n) && !tableExists(n, "memory_session_tombstones")) withSqlitePostCommitPublications(n, () => transact(() => ensureMemorySessionTombstones(n))); } else if (e.kind === "origin" && !isPostgresSqlConnection(n) && !tableExists(n, "memory_entry_origins")) withSqlitePostCommitPublications(n, () => transact(() => ensureMemoryEntryOriginsSchema(n))); return bindSqliteWorkerDatabase(n, { execute(e) { if (e.type === "record") return transact(() => recordMemoryEntryOriginsInDatabase(n, e.input)); if (e.type === "reserve") { if (!tableExists(n, "memory_entry_origins")) return []; return transact(() => { const { agentId: t, operations: r } = e.input; const i = readMemoryEntryOriginsInDatabase(n, { agentId: t, entryKeys: [...new Set(r.flatMap((e) => e.parentKeys))] }); return r.flatMap(({ entryKey: e, parentKeys: r }) => { const a = new Set(r); const o = i.filter((e) => a.has(e.entryKey)); const s = o.length ? recordMemoryEntryOriginsInDatabase(n, { agentId: t, entryKey: e, origins: o }) : []; return s.length ? [{ agentId: t, entryKeys: [e], sessionIds: s.map((e) => e.sessionId) }] : []; }); }); } if (e.type === "prune") { if (!tableExists(n, "memory_entry_origins")) return 0; return transact(() => { const t = new Set(executeSqliteQuerySync(n, getNodeSqliteKysely(n).selectFrom("memory_index_chunks").select("text").where("source", "=", "memory").where("text", "like", "%openclaw-memory-promotion:%")).rows.flatMap(({ text: e }) => extractPromotionKeys(e))); return deleteMemoryEntryOriginsInDatabase(n, { agentId: e.input.agentId, entryKeys: e.input.entryKeys.filter((e) => !t.has(e)) }); }); } if (e.type === "delete") return deleteMemoryEntryOriginsInDatabase(n, e.input, r); return transact(() => e.type === "forget.mark" ? markMemoryForgotten(n, e.input) : purgeForgottenMemory(n, e.input)); } }); } //#endregion export { bindSqliteWorkerBackend as t };