UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 5.85 kB
import{o as e}from"./git-operation.worker-fs-safe-2XqdJR5K.mjs";import{S as t,h as n,x as r}from"./git-operation.worker-map-size-CypexNQj.mjs";import{$n as i,On as a,Xn as o,Yn as s,er as c,qn as l,rn as u}from"./git-operation.worker.js";import{a as d,c as f,d as p,i as m,l as h,n as g,o as _,r as v,s as y,u as b}from"./git-operation.worker-exec-runner-D4RJRnE_.mjs";import{s as x}from"./git-operation.worker-runtime-process-bootstrap.generated-NdDxMbTB.mjs";var S=/* @__PURE__ */ e({runCommandBuffered:()=>runCommandBuffered,runCommandWithTimeout:()=>g,runExec:()=>runExec});function decodeExecOutput(e){return u({buffer:Buffer.from(e.buffer,e.byteOffset,e.byteLength)})}async function runExec(e,n,r=1e4){let i=typeof r==`number`?{timeoutMs:r}:r,o=typeof i.timeoutMs==`number`?t(i.timeoutMs,1):void 0,c=i.maxBuffer??1048576;if(i.input!==void 0&&i.stdinFileDescriptor!==void 0)throw Error(`runExec accepts either input or stdinFileDescriptor, not both`);let l=!0,u=!0,f=!1,releaseCancellation=()=>{};try{let t=_([e,...n],{baseEnv:i.baseEnv,cancelSignal:i.signal,cwd:i.cwd,encoding:`buffer`,executionTimeoutMs:o,env:i.env,forceKillAfterDelay:300,...i.input===void 0?{}:{input:i.input},maxBuffer:c,reject:!0,...i.stdinFileDescriptor===void 0?{stdin:i.input===void 0?`ignore`:void 0}:{stdin:i.stdinFileDescriptor},stripFinalNewline:!1}),r=t.nodeChildProcess instanceof x&&t.pid===void 0?a():void 0,p=m(i.signal),g=!0,S,stopCommand=e=>{if(!g)return;if(releaseCancellation(),e===`timeout`){if(h(t.nodeChildProcess))return;f=!0,t.kill()}if(!u||!r)return;l=!1;let n={failed:!0,timedOut:e===`timeout`,isCanceled:e===`signal`,isMaxBuffer:!1,isTerminated:!1},i=s(n);r.reject(Object.assign(i,n,{shortMessage:i.message,stdout:``,stderr:``,cleanup:`uncertain`}))},onAbort=()=>stopCommand(`signal`);releaseCancellation=()=>{g=!1,S?.clear(),p?.removeEventListener(`abort`,onAbort)},p?.addEventListener(`abort`,onAbort,{once:!0}),o!==void 0&&(S=v(()=>stopCommand(`timeout`),o)),p?.aborted&&onAbort();let C=(async()=>{t.pid===void 0&&await y(t),u=!1;let e=b(t.nodeChildProcess),n=l?i.onOutputChunk:void 0,observe=(e,t)=>{try{n?.(e,t)}catch{n=void 0}},onStdout=e=>observe(e,`stdout`),onStderr=e=>observe(e,`stderr`);n&&(t.nodeChildProcess.stdout?.on(`data`,onStdout),t.nodeChildProcess.stderr?.on(`data`,onStderr));let r=await t.finally(()=>{releaseCancellation(),e(),t.nodeChildProcess.stdout?.off(`data`,onStdout),t.nodeChildProcess.stderr?.off(`data`,onStderr)});if(f){let e=s({timedOut:!0});throw Object.assign(e,r,{failed:!0,timedOut:!0,shortMessage:e.message})}let{stdout:a,stderr:o}=r,c=decodeExecOutput(a),d=decodeExecOutput(o);if(l&&i.logOutput!==!1){let[{shouldLogVerbose:e},{logDebug:t,logError:n}]=await Promise.all([import(`./git-operation.worker-globals-DNufH9BR.mjs`),import(`./git-operation.worker-logger-BbGLFbQp.mjs`)]);e()&&(c.trim()&&t(c.trim()),d.trim()&&n(d.trim()))}return{stdout:c,stderr:d}})();return d(C.then(()=>void 0,()=>void 0)),await(r?Promise.race([C,r.promise]):C)}catch(t){if(releaseCancellation(),t&&typeof t==`object`){let e=t;if(f||e.timedOut){let e=s({timedOut:!0}).message;t instanceof Error&&(t.stack=t.stack?.replace(t.message,e)),Object.assign(t,{failed:!0,timedOut:!0,message:e,shortMessage:e})}e.code===void 0&&typeof e.exitCode==`number`&&(e.code=e.exitCode),e.stdout instanceof Uint8Array&&(e.stdout=decodeExecOutput(e.stdout)),e.stderr instanceof Uint8Array&&(e.stderr=decodeExecOutput(e.stderr))}if(i.logOutput!==!1){let t=await Promise.all([import(`./git-operation.worker-globals-DNufH9BR.mjs`),import(`./git-operation.worker-logger-BbGLFbQp.mjs`)]).catch(()=>void 0);if(t){let[{danger:n,shouldLogVerbose:r},{logError:i}]=t;r()&&i(n(`Command failed: ${e}`))}}throw t}finally{l=!1,releaseCancellation()}}async function runCommandBuffered(e,t={}){if(t.signal?.aborted)return{stdout:Buffer.alloc(0),stderr:Buffer.alloc(0),code:null,signal:null,killed:!1,termination:`signal`,...t.signal.reason instanceof Error?{error:t.signal.reason}:{}};let a={stdout:[],stderr:[]},s={stdout:0,stderr:0},u=r(n(t.maxCombinedOutputBytes),{min:1}),d,appendChunk=(e,n)=>{if(t.discardOutput?.[n])return!0;let r=f(t.maxOutputBytes,n),i=s.stdout+s.stderr,o=u===void 0?1/0:Math.max(0,u-i),c=Math.max(0,Math.min(r-s[n],o));if(c>0){let t=e.byteLength>c?p(e.subarray(0,c)):e;a[n].push(t),s[n]+=t.byteLength}return e.byteLength>c?(d??=n,!1):!0},capturedOutput=e=>Buffer.concat(a[e],s[e]);try{let n=await g(e,{executionIntent:t.executionIntent,executionScopeKey:t.executionScopeKey,baseEnv:t.baseEnv,preparedEnv:t.preparedEnv,cwd:t.cwd,env:t.env,input:t.input,killProcessTree:t.killProcessTree??!0,killGraceMs:t.killGraceMs,onOutputChunk:appendChunk,outputCapture:`discard`,signal:t.signal,timeoutMs:t.timeoutMs,noOutputTimeoutMs:t.noOutputTimeoutMs,tolerateOutputError:{stdout:t.discardOutput?.stdout||t.tolerateOutputError?.stdout,stderr:t.discardOutput?.stderr||t.tolerateOutputError?.stderr},terminateOnOutputError:t.terminateOnOutputError}),r=n.outputLimitExceeded?`output-limit`:n.termination,a=i(n),o=a?await a.then(e=>e===`uncertain`?new l:void 0,e=>new l({cause:e})):n.cleanup===`uncertain`?new l:void 0;return{stdout:capturedOutput(`stdout`),stderr:capturedOutput(`stderr`),code:r===`exit`?n.code:null,signal:n.signal,killed:n.killed,termination:r,...d?{outputLimitStream:d}:{},...n.outputErrorStream?{errorStream:n.outputErrorStream}:{},...o?{error:o}:{}}}catch(e){let t=e instanceof Error?e:/* @__PURE__ */ Error(`Command execution failed`),n=t,r=n.outputErrorStream===`stdout`||n.outputErrorStream===`stderr`?n.outputErrorStream:void 0;return{stdout:capturedOutput(`stdout`),stderr:capturedOutput(`stderr`),code:typeof n.exitCode==`number`?n.exitCode:null,signal:null,killed:!1,termination:`error`,...r?{errorStream:r}:{},error:c(t)?.cleanup===`uncertain`&&!o(t)?new l({cause:t}):t}}}export{runCommandBuffered as n,runExec as r,S as t};