openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
132 lines (131 loc) • 5.7 kB
JavaScript
import { t as createPluginRuntimeStore } from "./runtime-store-BRjhAEZt.mjs";
import path from "node:path";
import { createHash } from "node:crypto";
import { AsyncLocalStorage } from "node:async_hooks";
import { setImmediate } from "node:timers/promises";
//#region extensions/memory-core/src/memory-mutation-authority.ts
const authority = new AsyncLocalStorage();
function captureMemoryMutationAuthority() {
return authority.getStore();
}
async function withMemoryMutationAuthority(e, t) {
const n = authority.getStore();
let r = true;
const assertActive = () => {
if (!r) throw new Error("Memory mutation authority is closed");
n?.();
e();
};
assertActive();
try {
return await authority.run(assertActive, t);
} finally {
r = false;
}
}
//#endregion
//#region extensions/memory-core/src/dreaming-state.ts
const MEMORY_CORE_PLUGIN_ID = "memory-core";
const DREAMING_DAILY_INGESTION_NAMESPACE = "dreaming-daily-ingestion";
const DREAMING_SESSION_INGESTION_FILES_NAMESPACE = "dreaming-session-ingestion-files";
const DREAMING_SESSION_INGESTION_SEEN_NAMESPACE = "dreaming-session-ingestion-seen";
const SESSION_BACKFILL_REWIND_NAMESPACE = "session-backfill-rewind";
const DREAMING_MEMORY_BACKUP_NAMESPACE = "dreaming-memory-backups";
const SHORT_TERM_RECALL_NAMESPACE = "short-term-recall";
const SHORT_TERM_PHASE_SIGNAL_NAMESPACE = "short-term-phase-signals";
const SHORT_TERM_META_NAMESPACE = "short-term-meta";
const SHORT_TERM_LOCK_NAMESPACE = "short-term-locks";
const DREAMING_WORKSPACE_STATE_MAX_ENTRIES = 5e4;
const WORKSPACE_STATE_YIELD_EVERY = 10;
const SHORT_TERM_LOCK_MAX_ENTRIES = 4096;
const dreamingState = createPluginRuntimeStore({
key: "memory-core:dreaming-state",
errorMessage: "memory-core dreaming SQLite state store is not configured"
});
const configureMemoryCoreDreamingState = dreamingState.setRuntime;
function openMemoryCoreStateStore(e) {
const t = dreamingState.getRuntime()(e);
const n = captureMemoryMutationAuthority();
if (!n) return t;
if (!t.withCurrent) throw new Error("Memory mutations require a host with action-bound SQLite stores");
return t.withCurrent({ assertCurrent: n });
}
function normalizeMemoryCoreWorkspaceKey(e) {
const t = path.resolve(e).replace(/\\/g, "/");
return process.platform === "win32" ? t.toLowerCase() : t;
}
function memoryCoreWorkspaceStateKey(e) {
return createHash("sha256").update(normalizeMemoryCoreWorkspaceKey(e)).digest("hex");
}
function memoryCoreWorkspaceEntryKey(e, t) {
return `${memoryCoreWorkspaceStateKey(e)}:${createHash("sha256").update(t).digest("hex")}`;
}
function memoryCoreStateReference(e, t) {
return `plugin-state:${MEMORY_CORE_PLUGIN_ID}/${e}/${memoryCoreWorkspaceStateKey(t)}`;
}
function openWorkspaceStore(e) {
return openMemoryCoreStateStore({
namespace: e,
maxEntries: DREAMING_WORKSPACE_STATE_MAX_ENTRIES
});
}
async function readWorkspaceStoreEntries(e, t) {
const n = `${t}:`;
if (!e.entriesInKeyRange) return (await e.entries()).filter((e) => e.key.startsWith(n));
return (await e.entriesInKeyRange({
keyStartInclusive: n,
keyEndExclusive: `${t};`,
limit: Number.MAX_SAFE_INTEGER,
order: "asc"
})).toSorted((e, t) => e.createdAt - t.createdAt);
}
async function readMemoryCoreWorkspaceEntries(e) {
const t = memoryCoreWorkspaceStateKey(e.workspaceDir);
return (await readWorkspaceStoreEntries(openWorkspaceStore(e.namespace), t)).filter((e) => e.value.workspaceKey === t).map((e) => ({
key: e.value.key,
value: e.value.value
}));
}
async function writeMemoryCoreWorkspaceEntries(e) {
const t = openWorkspaceStore(e.namespace);
const n = memoryCoreWorkspaceStateKey(e.workspaceDir);
const r = /* @__PURE__ */ new Set();
let i = 0;
for (const a of e.entries) {
const o = memoryCoreWorkspaceEntryKey(e.workspaceDir, a.key);
r.add(o);
await t.register(o, {
version: 1,
workspaceKey: n,
workspaceDir: path.resolve(e.workspaceDir),
key: a.key,
value: a.value
});
if (++i % WORKSPACE_STATE_YIELD_EVERY === 0) await setImmediate();
}
for (const e of await readWorkspaceStoreEntries(t, n)) if (!r.has(e.key)) {
await t.delete(e.key);
if (++i % WORKSPACE_STATE_YIELD_EVERY === 0) await setImmediate();
}
}
async function writeMemoryCoreWorkspaceEntry(e) {
const t = memoryCoreWorkspaceStateKey(e.workspaceDir);
await openWorkspaceStore(e.namespace).register(memoryCoreWorkspaceEntryKey(e.workspaceDir, e.key), {
version: 1,
workspaceKey: t,
workspaceDir: path.resolve(e.workspaceDir),
key: e.key,
value: e.value
});
}
async function clearMemoryCoreWorkspaceNamespace(e) {
await writeMemoryCoreWorkspaceEntries({
...e,
entries: []
});
}
async function deleteMemoryCoreWorkspaceEntry(e) {
await openWorkspaceStore(e.namespace).delete(memoryCoreWorkspaceEntryKey(e.workspaceDir, e.key));
}
//#endregion
export { captureMemoryMutationAuthority as C, writeMemoryCoreWorkspaceEntry as S, normalizeMemoryCoreWorkspaceKey as _, SESSION_BACKFILL_REWIND_NAMESPACE as a, readMemoryCoreWorkspaceEntries as b, SHORT_TERM_META_NAMESPACE as c, clearMemoryCoreWorkspaceNamespace as d, configureMemoryCoreDreamingState as f, memoryCoreWorkspaceStateKey as g, memoryCoreWorkspaceEntryKey as h, DREAMING_SESSION_INGESTION_SEEN_NAMESPACE as i, SHORT_TERM_PHASE_SIGNAL_NAMESPACE as l, memoryCoreStateReference as m, DREAMING_MEMORY_BACKUP_NAMESPACE as n, SHORT_TERM_LOCK_MAX_ENTRIES as o, deleteMemoryCoreWorkspaceEntry as p, DREAMING_SESSION_INGESTION_FILES_NAMESPACE as r, SHORT_TERM_LOCK_NAMESPACE as s, DREAMING_DAILY_INGESTION_NAMESPACE as t, SHORT_TERM_RECALL_NAMESPACE as u, openMemoryCoreStateStore as v, withMemoryMutationAuthority as w, writeMemoryCoreWorkspaceEntries as x, openWorkspaceStore as y };