openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
195 lines (194 loc) • 6.37 kB
JavaScript
import { w as root } from "../../fs-safe-BbWvmdVn.mjs";
import "../../file-access-runtime-BD2Fjfbw.mjs";
import { t as createFileWatchNotifier } from "../../file-watch-notifier-T_dGgm7F.mjs";
import { i as buildMultimodalChunkForIndexing, n as readMemoryFile, r as buildFileEntry, s as listMemoryFiles } from "../../read-file-_FfN9cuw.mjs";
import "../../memory-core-host-engine-fs-Bgn0bHTy.mjs";
import path from "node:path";
import fs from "node:fs/promises";
import { AsyncLocalStorage } from "node:async_hooks";
import { createInterface } from "node:readline";
//#region extensions/memory-core/src/remote/memory-files-worker.ts
async function serveLocalMemoryWatches(e) {
await (await import("../../memory-watch.worker-CVdtIOW6.mjs")).serveLocalMemoryWatches(e);
}
/** Same-version file IPC; the provisioned adapter owns admission and configured roots. */
async function serveMemoryFiles(e) {
if (e.watch) {
const t = AsyncLocalStorage.snapshot();
const { MemoryFileWatcher: n } = await import("../../file-watcher-6iBX-ODt.mjs");
const r = createInterface({
input: e.input,
crlfDelay: Infinity
});
let i;
let a;
let o;
let s;
let c = false;
const close = () => {
c = true;
a ??= i?.close();
o ??= s?.close();
o?.catch(() => void 0);
a?.catch(() => void 0);
};
s = createFileWatchNotifier(e.output, () => {
close();
r.close();
});
e.input.on("end", close).on("close", close);
const l = [];
try {
for await (const a of r) {
if (c) break;
if (i) throw new Error("Unexpected message on the Memory watch subscription");
const r = JSON.parse(a);
const notify = (e) => s?.send(e);
i = new n({
runInBackgroundContext: t,
workspaceDir: path.resolve(e.workspace),
agentId: r.agentId,
settings: r.settings,
onChange: () => notify("change"),
onUnavailable: () => notify("unavailable")
});
if (e.input.readableEnded || e.input.destroyed) {
close();
break;
}
await i.start();
}
} catch (e) {
l.push(e);
} finally {
e.input.off("end", close).off("close", close);
r.close();
close();
const t = await Promise.allSettled([a, o]);
l.push(...t.flatMap((e) => e.status === "rejected" ? [e.reason] : []));
s = void 0;
}
if (l.length) throw new AggregateError(l, "Memory watch retirement failed");
return;
}
const t = [];
const n = e.input;
for await (const e of n) {
if (typeof e !== "string" && !(e instanceof Uint8Array)) throw new Error("Memory file request must be bytes");
t.push(Buffer.from(e));
}
const r = JSON.parse(Buffer.concat(t).toString("utf8"));
if (!r || typeof r !== "object" || Array.isArray(r)) throw new Error("Memory file request must be an object");
const i = r;
const a = path.resolve(e.workspace);
let o;
let s;
try {
switch (i.operation) {
case "maintenance":
o = await runMaintenance(i, a);
break;
case "list": {
const e = [];
o = {
files: await listMemoryFiles(a, i.extraPaths, i.multimodal, (t) => e.push(t)),
skipped: e
};
break;
}
case "inspect":
o = await buildFileEntry(i.filePath, a, i.multimodal);
break;
case "read":
o = await readMemoryFile({
...i.params,
workspaceDir: a
});
break;
case "readForIndexing": {
const e = await (await root(path.dirname(i.filePath), {
hardlinks: "allow",
symlinks: "reject",
maxBytes: Number.MAX_SAFE_INTEGER
})).read(path.basename(i.filePath));
o = {
content: e.buffer.toString("utf8"),
canonicalRelativePath: path.relative(await fs.realpath(a), e.realPath).replaceAll(path.sep, "/")
};
break;
}
case "multimodal":
o = await buildMultimodalChunkForIndexing(i.entry);
break;
default: throw new Error("Unknown Memory file operation");
}
} catch (e) {
s = {
message: e instanceof Error ? e.message : String(e),
...e instanceof Error ? { name: e.name } : {},
...e && typeof e === "object" && "publication" in e && (e.publication === "uncertain" || e.publication === "committed") ? { publication: e.publication } : {},
...e && typeof e === "object" && "code" in e && typeof e.code === "string" ? { code: e.code } : {}
};
}
await new Promise((t, n) => {
e.output.write(JSON.stringify(s ? { error: s } : { result: o }), (e) => e ? n(e) : t());
});
}
async function runMaintenance(e, t) {
switch (e.method) {
case "readFile": return (await fs.readFile(...e.args)).toString("base64");
case "stat": {
const [t, n] = e.args;
const r = n ? await fs.stat(t) : await fs.lstat(t);
return {
isFile: r.isFile(),
isDirectory: r.isDirectory(),
isSymbolicLink: r.isSymbolicLink(),
size: r.size,
mtimeMs: r.mtimeMs,
mode: r.mode
};
}
case "listDirectory": return (await fs.readdir(e.args[0], { withFileTypes: true })).map((e) => ({
name: e.name,
isFile: e.isFile(),
isDirectory: e.isDirectory(),
isSymbolicLink: e.isSymbolicLink()
}));
case "mkdir":
await fs.mkdir(e.args[0], { recursive: true });
return null;
case "rename": return await fs.rename(...e.args);
case "resolveWritePath": {
const { resolveMemoryWritePath: t } = await import("../../short-term-promotion-memory-write-BLI1OFKM.mjs");
return await t(...e.args);
}
case "commitContent": {
const { commitMemoryContent: t } = await import("../../short-term-promotion-memory-write-BLI1OFKM.mjs");
return await t(...e.args);
}
case "resolveDreamsPath": {
const { resolveDreamsPath: e } = await import("../../dreaming-dreams-file-BctGEbWr.mjs");
return await e(t);
}
case "readDreams": {
const { readDreamsFile: t } = await import("../../dreaming-dreams-file-BctGEbWr.mjs");
return await t(...e.args);
}
case "writeDreams": {
const { writeDreamsFileAtomic: t } = await import("../../dreaming-dreams-file-BctGEbWr.mjs");
return await t(...e.args);
}
case "replaceReport": {
const { replaceDreamingMarkdownFile: t } = await import("../../dreaming-markdown-D3J-JrnK.mjs");
return await t(...e.args);
}
case "appendCorpus": {
const { appendSessionCorpusText: t } = await import("../../session-ingestion-1gFn1oIe.mjs");
return await t(...e.args);
}
default: throw new Error("Unknown Memory maintenance operation");
}
}
//#endregion
export { serveLocalMemoryWatches, serveMemoryFiles };