agent-device
Version:
Open source device layer for AI coding agents: a CLI, MCP server, and typed Node.js API to control mobile, web, TV, and desktop apps on devices and simulators, locally or in the cloud.
1 lines • 2.48 kB
JavaScript
import{v as e}from"./sdk-contracts.js";import{l as t,t as n}from"./host2.js";import{g as r}from"./runner-contract.js";import{n as i,r as a}from"./runner-session-types.js";import{a as o,r as s,s as c}from"./runner-io.js";import{i as l,u}from"./runner-disposal.js";import{resolveRunnerLaunchReadinessMs as d}from"./runner-start-budget.js";import{randomUUID as f}from"node:crypto";async function p(l,p,m,h,g){let _={...p.endpoint,token:r()},v=new AbortController,y=h.phase.signal?AbortSignal.any([h.phase.signal,g]):g,b=()=>v.abort(y.reason);y?.addEventListener(`abort`,b,{once:!0});let x;try{y?.throwIfAborted(),x=await p.launch(_,v.signal,h.phase.deadline)}finally{y?.removeEventListener(`abort`,b)}let S={lost:!1,ready:Promise.resolve(!0),close(){this.lost=!0}},C,w={finished:x.exited,exited:!1,output:Promise.resolve(),isRunning:()=>!w.exited,waitForExit:e=>u(x.exited,e),cleanup:async()=>{await w.output,await s(E).catch(e=>{t({level:`warn`,phase:`ios_runner_log_flush_failed`,data:{error:String(e),logPath:E}})})},readLogTail:e=>T.slice(-e),earlyExitError:async(t,n)=>new e(`COMMAND_FAILED`,`Runner did not accept connection (launcher exited early)`,{reason:`runner_process_exited`,port:t,logPath:E??n,output:w.readLogTail(16384)},w.failure),terminate:()=>C??=(async()=>{w.exited||await x.terminate(),await x.exited.catch(()=>{})})().catch(t=>{throw C=void 0,new e(`COMMAND_FAILED`,`Runner launcher could not terminate its generation`,{reason:`runner_termination_failed`,deviceId:l.id},t)})},T=``,E=c(m.logPath,l.id),D={sessionId:f(),device:l,deviceId:l.id,endpoint:_,state:`starting`,commandCharges:new i,speculative:m.speculative,launch:w,listenerWatch:S,runnerLogPath:E,launchDeadline:n.fromTimeoutMs(d(h))},O=e=>{w.exited=!0,w.failure=e,S.close(),D.retention?.cancel()};return x.exited.then(()=>O(),O),w.output=(async()=>{for await(let e of x.output)T=(T+e).slice(-16384),o(e,E,m.traceLogPath,m.verbose)})().catch(async e=>{w.failure=e,a(D,`draining`),S.close(),D.retention?.cancel(),t({level:`warn`,phase:`ios_runner_output_failed`,data:{error:String(e),deviceId:l.id}}),await w.terminate().catch(e=>{t({level:`warn`,phase:`ios_runner_termination_failed`,data:{deviceId:l.id,error:String(e)}})})}),D}async function m(t){if(t.state===`draining`)throw await l(t,{graceful:!1}),new e(`COMMAND_FAILED`,`Runner output failed during startup`,{reason:`runner_output_failed`,deviceId:t.deviceId},t.launch.failure)}export{p as launchRunnerSession,m as refuseFailedRunnerPublication};