openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
2 lines • 2.87 kB
JavaScript
//!src/node-host/launcher-client.ts
const CHANNEL_MARKER=Symbol.for(`openclaw.node-host.launcher-channel`);const CHILD_MARKER=Symbol.for(`openclaw.node-host.launcher-child`);const MANAGED_STATE_MARKER=Symbol.for(`openclaw.node-host.managed-state-path`);function launcherChannel(){return Reflect.get(process,CHANNEL_MARKER)}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&&(launcherChannel()?.connected??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)=>{const r=launcherChannel()??process;if(!isNodeHostLauncherChild()||!r.send){n(/* @__PURE__ */ new Error(`The node runtime has no connected update supervisor.`));return}r.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 i=launcherChannel();const a=i??process;const cleanup=()=>{clearTimeout(o);a.off(`message`,onMessage);a.off(`disconnect`,onDisconnect);(i??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 o=setTimeout(()=>fail(/* @__PURE__ */ new Error(`The node update supervisor did not acknowledge the restart.`)),3e4);a.on(`message`,onMessage);a.once(`disconnect`,onDisconnect);(i??process.channel)?.ref();sendLauncherMessage(e).catch(fail)})}export{requestNodeHostLauncherRestart as a,requestNodeHostLauncherBootstrap as i,isNodeHostLauncherChild as n,setNodeHostLauncherRestartArguments as o,notifyNodeHostLauncherReady as r,watchNodeHostParentStdin as s,getManagedNodeHostStatePath as t};