UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

95 lines (94 loc) • 3.13 kB
//#region src/node-host/launcher-client.ts const CHILD_MARKER = Symbol.for("openclaw.node-host.launcher-child"); const MANAGED_STATE_MARKER = Symbol.for("openclaw.node-host.managed-state-path"); /** A companion retains stdin's write end; EOF also retires the node after an app crash. */ function watchNodeHostParentStdin(e) { const t = process.stdin; let n = false; const close = () => { if (!n) { n = true; e(); } }; t.once("end", close); t.once("error", close); t.once("close", close); t.resume(); if (t.readableEnded || t.destroyed) queueMicrotask(close); return () => { n = true; t.off("end", close); t.off("error", close); t.off("close", close); t.pause(); }; } function isNodeHostLauncherChild() { return Reflect.get(process, CHILD_MARKER) === true && process.connected; } function getManagedNodeHostStatePath() { if (!isNodeHostLauncherChild()) return; const e = Reflect.get(process, MANAGED_STATE_MARKER); return typeof e === "string" ? e : void 0; } function sendLauncherMessage(e) { return new Promise((t, n) => { if (!isNodeHostLauncherChild() || !process.send) { n(/* @__PURE__ */ new Error("The node runtime has no connected update supervisor.")); return; } process.send(e, (e) => e ? n(e) : t()); }); } async function notifyNodeHostLauncherReady(e) { if (isNodeHostLauncherChild()) await sendLauncherMessage({ type: "openclaw.node.ready", version: e }); } async function setNodeHostLauncherRestartArguments(e) { if (isNodeHostLauncherChild()) await sendLauncherMessage({ type: "openclaw.node.restart-args", argv: e }); } function requestNodeHostLauncherBootstrap(e) { return requestLauncherReply({ type: "openclaw.node.bootstrap", ...e }, "openclaw.node.bootstrap-result"); } function requestNodeHostLauncherRestart(e) { return requestLauncherReply({ type: "openclaw.node.restart", ...e }, "openclaw.node.restart-result"); } function requestLauncherReply(e, t) { return new Promise((n, r) => { const cleanup = () => { clearTimeout(i); process.off("message", onMessage); process.off("disconnect", onDisconnect); process.channel?.unref(); }; const fail = (e) => { cleanup(); r(e); }; const onDisconnect = () => fail(/* @__PURE__ */ new Error("The node update supervisor disconnected.")); const onMessage = (e) => { if (!e || typeof e !== "object" || !("type" in e) || e.type !== t) return; cleanup(); if ("ok" in e && e.ok === true) n(); else r(new Error("error" in e && typeof e.error === "string" ? e.error : "The node update supervisor rejected the restart.")); }; const i = setTimeout(() => fail(/* @__PURE__ */ new Error("The node update supervisor did not acknowledge the restart.")), 3e4); process.on("message", onMessage); process.once("disconnect", onDisconnect); sendLauncherMessage(e).catch(fail); }); } //#endregion export { requestNodeHostLauncherRestart as a, requestNodeHostLauncherBootstrap as i, isNodeHostLauncherChild as n, setNodeHostLauncherRestartArguments as o, notifyNodeHostLauncherReady as r, watchNodeHostParentStdin as s, getManagedNodeHostStatePath as t };