Lines 1-21javascript
2import{spawn as Ye}from"node:child_process";import{closeSync as Qe,existsSync as Ze,mkdirSync as tr,openSync as er,readFileSync as rr,rmSync as nr,writeFileSync as sr}from"node:fs";import{dirname as ir,resolve as Vt}from"node:path";import{fileURLToPath as or}from"node:url";import{randomUUID as j}from"node:crypto";var U="0.1.2";function O(){return new Date().toISOString()}function u(r){return r!==null&&typeof r=="object"&&!Array.isArray(r)}function c(r){return String(r??"").trim()}function T(r,t){let e=Number.parseInt(String(r??""),10);return Number.isFinite(e)?e:t}function k(r){return Array.is ...
3`):""}var D=class{constructor(t){this.agent=t,this.sessionCounter=0}async sessionNew(){return this.sessionCounter+=1,{sessionId:`echo-${this.agent.agentId}-${this.sessionCounter}`}}async sessionPrompt(t,e){let n=L(t.prompt);return e({sessionId:t.sessionId,update:{sessionUpdate:"agent_message_chunk",content:{type:"text",text:n?`Echo runtime received: ${n}`:"Echo runtime received an empty prompt."}}}),{stopReason:"end_turn"}}async sessionCancel(){return{ok:!0}}};import{spawn as te}from"node:child_process";var C="2.0",ee=3e4;function re(r){return{code:Number.isInteger(r?.code)?r.code:-32e3,messag ...
5`);if(e<0)return;let s=this.headerBuffer.subarray(0,e).toString("utf8").match(/content-length:\s*(\d+)/i);if(!s){h("warn","runtime.protocol","missing Content-Length header",{target:this.label}),this.headerBuffer=Buffer.
8`;this.child.stdin.write(s,e)}onNotification(t,e){let n=this.notificationHandlers.get(t)||new Set;return n.add(e),this.notificationHandlers.set(t,n),()=>{n.delete(e),n.size===0&&this.notificationHandlers.delete(t)}}onRequest(t,e){return this.requestHandlers.set(t,e),()=>this.requestHandlers.delete(t)}onExit(t){return this.exitHandlers.add(t),()=>this.exitHandlers.delete(t)}rejectAll(t){for(let e of this.pending.values())clearTimeout(e.timer),e.reject(t);this.pending.clear()}handleExit(t,e){this.rejectAll(new Error(`runtime exited before response (${t}/${e})`));for(let n of this.exitHandlers)n( ...
10${s}`:s;t.emittedTexts.add(s),n({sessionId:e,update:{sessionUpdate:"agent_message_chunk",content:{type:"text",text:i}}})}at(t,ot(r)),ve(r)&&n({sessionId:e,update:{sessionUpdate:"runtime_event",event:bt(r)}})}function At(r,t,e,n,s=Date.now()){if(!t)return{artifactCount:0
11`,E,g,i,e),At(g,X,i,e,Date.now());let v=Le(l);if(v&&!g.emittedTexts.has(v)&&(g.emittedTexts.add(v),e({sessionId:i,update:{sessionUpdate:"agent_message_chunk",content:{type:"text",text:v}}})),Ot(s,this.target)){let Q=ke(g,{target:this.target,promptText:a,startedAtMs:n,endedAtMs:Date.now()});Q.artifactCount>0&&h("info","runtime.cli","codex transcript artifacts collected",{target:this.agent.targetCode,sessionId:i,artifactCount:Q.artifactCount,transcriptCount:Q.transcriptCount})}let P=_.trim();P&&h("debug","runtime.cli","runtime stderr",{target:this.agent.targetCode,sessionId:i,stderr:P}),h("info" ...
12`}}})}}}async sessionCancel(t){let e=c(t.sessionId),n=this.active.get(e);return n?(h("info","runtime.cli","cancelling runtime child",{target:this.agent.targetCode,sessionId:e,pid:n.pid}),z(n,"SIGTERM"),this.active.delete(e),{ok:!0,cancelled:!0}):{ok:!0,cancelled:!1
13`)}];return this.prompt({...t,agentId:n,sessionId:a,prompt:d},e)}async requestPermission({agent:t,runtimeSessionId:e,request:n,emitUpdate:s}){let i=j(),o=this.runtimeSessionIndex.get(`${t.agentId}:${e}`)?.sessionId||e||j(),d={permissionId:i,agentId:t.agentId,sessionId:o,runtimeSessionId:e,request:u(n)?n:{},status:"pending",createdAt:O()};return s?.({sessionId:e,update:{sessionUpdate:"permission_request",permission:d}}),await new Promise(l=>{let p=T(n?.timeoutMs,this.config.permissionRequestTimeoutMs),m=setTimeout(()=>{this.pendingPermissions.delete(i),l({permissionId:i,outcome:"denied",respons ...
14`).replaceAll("\\r","\r").replaceAll("\\t"," ").replaceAll('\\"','"').replaceAll("\\\\","\\")}function Ve(r){let t=!1,e=!1;
15`),console.log(JSON.stringify({status:"started",pid:s.pid,pidFile:r,logFile:t}))}async function Jt({quiet:r=!1}={}){let t=pt(),e=ft(t);if(!e){r||console.log(JSON.stringify({status:"not_running",pidFile:t}));return}if(!N(e)){lt(t),r||console.log(JSON.stringify({status:"not_running",pid:e,pidFile:t}));return}process.kill(e,"SIGTERM"),!await Kt(e,1e4)&&N(e)&&(process.kill(e,"SIGKILL"),await Kt(e,3e3)),lt(t),r||console.log(JSON.stringify({status:"stopped",pid:e,pidFile:t}))}function fr(){let r=pt(),t=Yt(),e=ft(r),n=!!(e&&N(e));console.log(JSON.stringify({status:n?"running":"not_running",running:n, ...
18 run, foreground Run in the foreground. This is the default.
19 start Start in the background and write a pid file.
20 reload, restart Stop the background process and start it again.
21 stop Stop the background process.
Long lines were clipped for display.