UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 1.62 kB
import{n as e}from"./fs-safe-copy.worker-terminal-pty-CrGkR3ff.mjs";import{constants as t}from"node:os";function spawnBunTerminalPty(n){let r=globalThis.Bun;if(!r)throw Error(`Bun's native terminal is unavailable in this runtime`);let i=new TextDecoder,a=/* @__PURE__ */ new Set,o=/* @__PURE__ */ new Set,s=[],c=0,l=!1,u=!1,d=!1,f=!1,p=!1,m,h,g,flush=()=>{if(!l){l=!0;try{for(;;){let e=s[c];if(u&&!d||a.size===0||e===void 0)break;c+=1;for(let t of a)t(e)}}finally{l=!1}if(c>0&&c*2>=s.length&&(s=s.slice(c),c=0),p&&m&&!(u&&s.length>0&&!d)){p=!1,h=m,s=[],c=0,v.close();for(let e of o)e(h)}}},deliver=e=>{e&&s.push(e),flush()},finish=()=>{h||p||!m||(clearTimeout(g),g=void 0,p=!0,deliver(i.decode()))},armEofGrace=()=>{!m||f||h||g||u&&!d||(g=setTimeout(finish,200))},_=r.spawn([n.file,...n.args],{cwd:n.cwd,env:n.env,terminal:{cols:n.cols,rows:n.rows,name:n.name,data:(e,t)=>deliver(i.decode(t,{stream:!0})),exit:()=>{f=!0,finish()}}});if(!_.terminal)throw _.kill(`SIGKILL`),Error(`Bun did not attach a terminal to the spawned process`);let v=_.terminal;return _.exited.then(()=>{m=_.signalCode?{exitCode:0,signal:t.signals[_.signalCode]}:{exitCode:_.exitCode??0},f?finish():armEofGrace()}),{pid:_.pid,write:e=>void v.write(e),resize:(e,t)=>v.resize(e,t),pause:()=>{u=!0,d||(v.pause(),clearTimeout(g),g=void 0)},resume:()=>{u=!1,v.resume(),flush(),armEofGrace()},onData:e=>(a.add(e),flush(),{dispose:()=>a.delete(e)}),onExit:e=>(o.add(e),h&&e(h),{dispose:()=>o.delete(e)}),kill:t=>{e(_.pid,t,e=>_.kill(e)),!(h||t!==void 0&&t!==`SIGKILL`&&t!==`SIGTERM`)&&(d=!0,v.resume(),flush(),armEofGrace())}}}export{spawnBunTerminalPty};