UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

177 lines (176 loc) • 5.39 kB
import { r as isPathInside } from "../path-guards-TIjxtj6H.mjs"; import { t as createDeferredCore } from "../deferred-D0La5CRk.mjs"; import { n as encodeNativeWorkerFailure, t as decodeNativeWorkerFailure } from "../worker-native-error-DQmZ8Unc.mjs"; import { o as withPluginSourceCaptureStorage } from "../plugin-source-capture-context-BU9oGeml.mjs"; import { l as recordPluginNativeLoad, n as createPluginSourceCaptureRoot, t as createPluginNativeCaptureRoot } from "../plugin-source-capture-directory-iR2Zdx3F.mjs"; import fs from "node:fs"; //#region src/plugins/plugin-capture-native-resource.ts const domains = /* @__PURE__ */ new Map(); function createPluginCaptureNativeResource(e, t, n) { const r = t; let i = domains.get(r.owner); if (r.role === "owner") { if (i) throw new Error("Plugin capture owner already exists"); i = { sources: /* @__PURE__ */ new Map(), sourceParents: /* @__PURE__ */ new Map(), roots: /* @__PURE__ */ new Map(), nextRoot: 0, borrowers: 0 }; domains.set(r.owner, i); } if (!i) throw new Error("Plugin capture owner is unavailable"); const a = i; const o = n?.postMessage.bind(n); if (r.role === "execution") a.borrowers++; const s = /* @__PURE__ */ new Map(); n?.on("message", (e) => { const t = e; s.get(t.id)?.resolve(t); s.delete(t.id); }); let c = false; let l = true; e.on("close", () => { l = false; }); let u = Promise.resolve(); const execute = async ({ id: e, command: t }) => { if (c) throw new Error("Plugin capture resource has closed"); if (t.type === "recovery") { const { copyPluginRecoveryFiles: e } = await import("../plugin-recovery-source-copy-t_Mpx7DY.mjs"); const { createPluginSourceCapture: n, withPluginSourceCaptureDirectory: r } = await import("../plugin-package-metadata-capture-D4UKFdvf.mjs"); const i = t.input; const o = withPluginSourceCaptureStorage(i.storage, () => i.directory ? r(i.directory.directory, n, i.directory.managedRoot) : n()); const s = { directory: o.directory, managedRoot: o.outputRoot, release: () => o.disposeAsync() }; a.sources.set(t.captureId, s); if (t.parentCaptureId !== void 0) a.sourceParents.set(t.captureId, t.parentCaptureId); try { const n = e(i, s.directory); let r = n.next(); while (!r.done) { await fs.promises.cp(...r.value); r = n.next(); } return { root: { id: t.captureId, directory: s.directory, managedRoot: s.managedRoot } }; } catch (e) { try { await s.release(); a.sources.delete(t.captureId); a.sourceParents.delete(t.captureId); } catch (t) { throw new AggregateError([e, t], "Plugin recovery copy and cleanup failed", { cause: t }); } throw e; } } if (t.type === "source") { const e = createPluginSourceCaptureRoot(t.storage.stateDir, t.prefix, t.storage.placement); a.sources.set(t.captureId, e); return { root: { id: t.captureId, directory: e.directory, managedRoot: e.managedRoot } }; } if (t.type === "native") { if (!o) throw new Error("Plugin capture execution has no host owner"); const n = createPluginNativeCaptureRoot(t.storage.stateDir, t.storage.placement); const r = ++a.nextRoot; const i = { captureId: t.captureId, announced: false, root: n }; a.roots.set(r, i); const c = createDeferredCore(); s.set(e, c); const l = { id: e, captureId: t.captureId, root: { id: r, directory: n.directory } }; o(l); const u = await c.promise; if (!u.ok) throw decodeNativeWorkerFailure(u.error); i.announced = true; return { root: u.root }; } if (t.type === "loaded") { recordPluginNativeLoad(t.filename); return { retainBrokerUntilProcessExit: [...a.roots.values()].some(({ root: e }) => isPathInside(e.directory, t.filename)) }; } else if (t.type === "release-source") { for (const [e, n] of a.sourceParents) if (n === t.captureId) { await a.sources.get(e)?.release(); a.sources.delete(e); a.sourceParents.delete(e); } await a.sources.get(t.captureId)?.release(); a.sources.delete(t.captureId); a.sourceParents.delete(t.captureId); for (const [e, n] of a.roots) if (n.captureId === t.captureId && !n.announced) { await n.root.disposeAsync(); a.roots.delete(e); } } else if (t.type === "commit-native") { const e = a.roots.get(t.rootId); if (!e) throw new Error("Plugin native capture has closed"); e.root.commit(); } else { await a.roots.get(t.rootId)?.root.disposeAsync(); a.roots.delete(t.rootId); } return {}; }; const receive = (t) => { const n = t; u = u.then(async () => { let t; try { t = { id: n.id, ok: true, ...await execute(n) }; } catch (e) { t = { id: n.id, ok: false, error: encodeNativeWorkerFailure(e) }; } if (l) try { e.postMessage(t); } catch { l = false; } }); }; e.on("message", receive); return { async close() { if (c) return; await u; if (r.role === "owner") { if (a.borrowers || a.sources.size || a.roots.size) throw new Error("Plugin capture owner still has retained resources"); domains.delete(r.owner); } else a.borrowers--; c = true; e.off("message", receive); e.close(); n?.close(); } }; } //#endregion export { createPluginCaptureNativeResource as default };