UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 4.13 kB
import{a as throwIfSettledRejected}from"../../error-graph-internal-Bt2pFJBM.mjs";import{t as createDeferredCore}from"../../deferred-QsZCoVH-.mjs";import{t as captureRuntimeConfigAsyncReader}from"../../io.runtime-BvSeVFIY.mjs";import"../../config-DkZ8y8Gv.mjs";import{a as runCliWithExitFinalization,n as requestExitAfterOneShotOutput}from"../../one-shot-exit-LLy48Ehj.mjs";import{a as listRegisteredNodeHostCapsAndCommands,o as notifyRegisteredNodeHostCommandDisconnect,r as invokeRegisteredNodeHostCommand,s as watchRegisteredNodeHostCommandAvailability,t as ensureNodeHostPluginRegistry}from"../../plugin-node-host-CIwABrQV.mjs";import{n as parseComputerHostInput}from"../../computer-protocol-BWphQiwZ.mjs";import{createInterface}from"node:readline"; //!src/gateway/desktop/computer.worker.ts async function runComputerHost(){const e=createInterface({input:process.stdin,crlfDelay:Infinity});const t=/* @__PURE__ */ new Map;const n=/* @__PURE__ */ new Set;const r=createDeferredCore();const write=e=>process.stdout.write(`${JSON.stringify(e)}\n`);const log=e=>process.stderr.write(`${e}\n`);let i;let a;let o;const stop=s=>{if(o)return o;o=r.promise;if(!s)for(const e of t.values())e.abort(/* @__PURE__ */ new Error(`Computer host is stopping`));(async()=>{await i?.catch(()=>{});await Promise.allSettled(n);try{const e=await Promise.allSettled([(async()=>{if(s)await invokeRegisteredNodeHostCommand(`computer.act`,JSON.stringify({action:`__close_execution`,...s}));await notifyRegisteredNodeHostCommandDisconnect()})(),Promise.resolve().then(()=>a?.())]);throwIfSettledRejected(e,`Computer host cleanup failed`)}finally{e.close();requestExitAfterOneShotOutput()}})().then(r.resolve,r.reject);return o};const stopAfterError=e=>{write({type:`error`,message:e instanceof Error?e.message:String(e)});stop()};e.on(`line`,e=>{if(o)return;let r;try{r=parseComputerHostInput(JSON.parse(e))}catch{stopAfterError(/* @__PURE__ */ new Error(`Invalid computer host request`));return}if(r.type===`stop`){stop(r.execution);return}if(r.type===`cancel`){t.get(r.id)?.abort(/* @__PURE__ */ new Error(`Computer invocation cancelled`));return}if(r.type===`start`){if(i){stopAfterError(/* @__PURE__ */ new Error(`Computer host was already started`));return}const e=r.pluginIds;i=(async()=>{const t={config:await captureRuntimeConfigAsyncReader()(),env:process.env};await ensureNodeHostPluginRegistry({...t,onlyPluginIds:e,commandAllowlist:/* @__PURE__ */ new Set([`screen.snapshot`,`computer.act`]),logger:{info:log,warn:log,error:log,debug:log}});if(o)return;const n=listRegisteredNodeHostCapsAndCommands(t);if(!n.computerUse||!n.commands.includes(`screen.snapshot`))throw new Error(`COMPUTER_DRIVER_UNAVAILABLE: the Gateway desktop provider is unavailable`);a=watchRegisteredNodeHostCommandAvailability(t,()=>{const e=listRegisteredNodeHostCapsAndCommands(t);if(!e.computerUse||!e.commands.includes(`screen.snapshot`))stopAfterError(/* @__PURE__ */ new Error(`COMPUTER_DRIVER_UNAVAILABLE: the desktop provider closed`))});write({type:`ready`,computerUse:n.computerUse})})();i.catch(stopAfterError);return}const s=r;if(!i||t.has(s.id)){stopAfterError(/* @__PURE__ */ new Error(`Computer host request is not admitted`));return}const c=new AbortController;t.set(s.id,c);const l=(async()=>{try{await i;c.signal.throwIfAborted();if(o)throw new Error(`Computer host is stopping`);const e=await invokeRegisteredNodeHostCommand(s.command,s.paramsJSON,void 0,{signal:c.signal,sessionKey:s.sessionKey,sendNodeEvent:async()=>{throw new Error(`Computer host does not publish node events`)}});if(e===null)throw new Error(`COMPUTER_DRIVER_UNAVAILABLE: computer command is unavailable`);write({type:`result`,id:s.id,payload:e})}catch(e){write({type:`error`,id:s.id,message:e instanceof Error?e.message:String(e)})}finally{t.delete(s.id)}})();n.add(l);l.finally(()=>n.delete(l))});e.on(`close`,()=>void stop());process.once(`SIGINT`,()=>void stop());process.once(`SIGTERM`,()=>void stop());await r.promise}runCliWithExitFinalization({run:runComputerHost,onError:e=>{process.stderr.write(`${e instanceof Error?e.message:String(e)}\n`);process.exitCode=1}});export{};