UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 1.96 kB
import{n as enterAgentExecCgroup}from"../linux-exec-cgroup-kO5YhBoe.mjs";import{t as prepareTerminalPty}from"../terminal-pty-B8c3Y2mp.mjs"; //!src/process/terminal-pty-worker.ts let pty;let starting=false;let parentLost=false;let finished=false;let authorizeLaunch;function report(e,t){if(!process.connected){t?.();return}process.send?.(e,()=>t?.())}function finish(e){if(finished)return;finished=true;pty?.kill();process.stdout.end(()=>{report(e,()=>{if(process.connected)process.disconnect?.()})})}function close(){parentLost=true;pty?.kill()}process.once(`disconnect`,close);process.once(`SIGTERM`,close);process.once(`SIGINT`,close);process.stdout.on(`error`,close);process.stdout.on(`drain`,()=>pty?.resume());process.on(`message`,e=>{if(e.type===`start`||e.type===`prepare`){if(starting||parentLost)return;starting=true;if(process.versions.bun){finish({type:`error`,message:`Terminal worker requires Node; PATH resolves node to Bun.`});return}if(e.params.executionIntent===`agent-tool`)enterAgentExecCgroup(e.params.executionScopeKey);prepareTerminalPty(e.params).then(t=>{if(e.type===`start`){if(parentLost||finished)throw new Error(`Terminal host retired before launch`);return t()}return new Promise((e,n)=>{authorizeLaunch=()=>{try{if(parentLost||finished)throw new Error(`Terminal host retired before launch`);e(t())}catch(e){n(e instanceof Error?e:new Error(String(e)))}};report({type:`prepared`})})}).then(e=>{pty=e;pty.onData(t=>{if(!process.stdout.write(t))e.pause()});pty.onExit(e=>finish({type:`exit`,...e}));if(parentLost||finished)pty.kill();else report({type:`ready`,pid:pty.pid})},e=>finish({type:`error`,message:e instanceof Error?e.message:String(e)}))}else if(e.type===`launch`){const e=authorizeLaunch;authorizeLaunch=void 0;e?.()}else if(e.type===`input`)pty?.write(`data`in e?e.data:Buffer.from(e.dataBase64,`base64`));else if(e.type===`resize`)pty?.resize(e.cols,e.rows);else pty?.kill(e.signal)});report({type:`boot`});export{};