UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

124 lines (123 loc) • 2.98 kB
import { p as toErrorObject } from "./error-coercion-hDrHOSGy.mjs"; import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs"; import "./error-runtime-BM740gJj.mjs"; import { t as MemoryFileWatcher } from "./file-watcher-99Tnsb4I.mjs"; import path from "node:path"; import { AsyncLocalStorage } from "node:async_hooks"; //#region extensions/memory-core/src/remote/memory-watch.worker.ts /** Dedicated local transport; all subscriptions retain the watcher's shared-root owner. */ function serveLocalMemoryWatches(e) { const t = AsyncLocalStorage.snapshot(); const n = /* @__PURE__ */ new Map(); const r = []; let i = false; let a; return new Promise((o, s) => { const send = (t) => { if (!i) e.postMessage(t); }; const closeWatch = (e, t) => { if (e.closing) return e.closing; let i; try { i = e.watcher.close(); } catch (e) { i = Promise.reject(toErrorObject(e, "Memory file watcher close failed")); } return e.closing = i.then(() => { n.delete(e.id); send({ type: "closed", id: e.id, ...t === void 0 ? {} : { error: formatErrorMessage(t) } }); }, (t) => { r.push(t); n.delete(e.id); send({ type: "closed", id: e.id, error: String(t) }); }); }; const stop = () => { e.off("message", onMessage); return a ??= Promise.all([...n.values()].map((e) => closeWatch(e))).then(() => { const t = r.length ? new AggregateError(r, "Local Memory watcher retirement failed") : void 0; send({ type: "shutdown", ...t ? { error: String(t) } : {} }); e.off("close", onPortClose); e.close(); if (t) s(t); else o(); }); }; const onPortClose = () => { i = true; stop(); }; const onMessage = (e) => { switch (e.type) { case "open": { const { id: r, request: i } = e; const a = new MemoryFileWatcher({ runInBackgroundContext: t, workspaceDir: path.resolve(e.workspaceDir), agentId: i.agentId, settings: i.settings, onDirty: () => send({ type: "dirty", id: r }), onChange: () => send({ type: "change", id: r }), onUnavailable: () => send({ type: "unavailable", id: r }), onState: (e) => send({ type: "state", id: r, facts: e }) }); const o = { id: r, watcher: a }; n.set(r, o); a.start().then(() => { if (!o.closing) send({ type: "started", id: r, facts: { health: a.health(), capacityDegraded: a.capacityDegraded } }); }, (e) => closeWatch(o, e)); break; } case "close": { const t = n.get(e.id); if (t) closeWatch(t); else send({ type: "closed", id: e.id }); break; } case "shutdown": stop(); } }; e.on("message", onMessage); e.once("close", onPortClose); }); } //#endregion export { serveLocalMemoryWatches };