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 • 1.72 kB
JavaScript
import{w as e}from"./sdk-device.js";import{_ as t,l as n}from"./host2.js";import{o as r}from"./runner-session-types.js";import i from"node:net";function a(e,t){if(e.state!==`ready`||!s(e)||e.listenerWatch)return;let r=i.connect(e.endpoint.port,`127.0.0.1`);r.unref();let a,o=new Promise(e=>{a=e}),c=setTimeout(()=>r.destroy(),500);c.unref();let l={lost:!1,ready:o,close(){clearTimeout(c),l.lost=!0,a(!1),r.removeAllListeners(),r.on(`error`,()=>{}),r.destroy()}};e.listenerWatch=l,r.on(`connect`,()=>{clearTimeout(c),a(!0)}),r.on(`error`,()=>{}),r.on(`close`,()=>{clearTimeout(c),l.lost=!0,a(!1),t()?.catch(t=>{n({level:`warn`,phase:`ios_runner_warm_stop_failed`,data:{deviceId:e.deviceId,sessionId:e.sessionId,error:String(t)}})})})}async function o(e,i,a=t().env){if(e.state!==`ready`||r(e)||e.commandCharges.hasOutstandingCharges)return!1;if(s(e)){let t=e.listenerWatch;if(!t||!await t.ready||t.lost)return!1}if(e.state!==`ready`||r(e)||e.commandCharges.hasOutstandingCharges)return!1;e.retention?.cancel();let o,l={cancel(){clearTimeout(o),e.retention===l&&(e.retention=void 0)}};e.retention=l;let u=c(a);return u>0&&(o=setTimeout(()=>{i(l,`idle_timeout`).catch(t=>{n({level:`warn`,phase:`ios_runner_idle_stop_failed`,data:{deviceId:e.deviceId,sessionId:e.sessionId,error:String(t)}})})},u),o.unref(),n({level:`debug`,phase:`ios_runner_idle_stop_scheduled`,data:{deviceId:e.deviceId,sessionId:e.sessionId,idleMs:u}})),!0}function s(t){return t.device.kind===`simulator`&&e(t.device)}function c(e=t().env){let n=e.AGENT_DEVICE_IOS_RUNNER_IDLE_STOP_MS?.trim();if(n){let e=Number(n);if(Number.isFinite(e)&&e>=0)return Math.min(Math.floor(e),2**31-1)}return 3e5}export{a as observeRunnerListener,o as retainRunnerSession};