UNPKG

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.39 kB
import{r as e}from"./rolldown-runtime.js";import{Ot as t,St as n,it as r,lt as i,rt as a}from"./sdk-io.js";import"./diagnostics.js";import{r as o}from"./version.js";import"./command.js";import{f as s}from"./host-file.js";import{n as c}from"./simctl.js";import{t as l}from"./host3.js";import{n as u}from"./source-channel.js";import{r as d}from"./errors.js";import{createHash as f}from"node:crypto";import p from"node:path";import m from"node:net";var h=e({createSnapshotSourceHost:()=>_,snapshotSourceSocketPath:()=>x});const g=65536;function _(e){return{...l(async(e,t,n)=>await a(e,t,n)),projectRoot:o,homeDirectory:s,start:(t,n,r,i)=>v(e,t,n,r,i),connect:y,emitDiagnostic:n,withDiagnosticTimer:t,readTargetProcessStartTime:u}}function v(e,t,n,a,o={}){if(o.signal?.aborted)throw d(`cancelled`,`abort-signal`);let{env:s,cwd:l}=e,u=r(`xcrun`,c(t,[`spawn`,t.udid,n,`serve`,a,`--idle-timeout`,`60`,`--exit-on-disconnect`,`false`]),{allowFailure:!0,captureOutput:!1,detached:!0,env:s,cwd:l}),f=u.child.pid??0;if(f<=0)throw d(`transport-failure`,`bridge-process-pid-missing`);let p=``;return u.child.stderr?.setEncoding(`utf8`),u.child.stderr?.on(`data`,e=>{p=b(p,String(e))}),{pid:f,wait:u.wait,isAlive:()=>u.child.exitCode===null&&u.child.signalCode===null,signal:e=>{i(f,e)||u.child.kill(e)},readLog:()=>p}}async function y(e,t){if(t.signal?.aborted)throw d(`cancelled`,`abort-signal`);let n=Math.max(1,Math.floor(t.timeoutMs));return await new Promise((r,i)=>{let a=m.createConnection({path:e}),o=!1,s=!1,c=setTimeout(()=>{h(d(`timeout`,`bridge-connect-timeout`)),a.destroy()},n),l=()=>{h(d(`cancelled`,`abort-signal`)),a.destroy()},u=()=>{o=!0,clearTimeout(c),t.signal?.removeEventListener(`abort`,l),a.setTimeout(0),r(a)},f=e=>{h(e),a.destroy()},p=()=>{a.off(`error`,f),o||h(d(`transport-failure`,`bridge-closed-before-connect`))},h=e=>{s||o||(s=!0,clearTimeout(c),t.signal?.removeEventListener(`abort`,l),a.off(`connect`,u),a.off(`error`,f),a.off(`close`,p),i(e))};a.once(`connect`,u),a.on(`error`,f),a.once(`close`,p),t.signal?.addEventListener(`abort`,l,{once:!0}),t.signal?.aborted&&l()})}function b(e,t){let n=e+t;return n.length<=g?n:n.slice(n.length-g)}function x(e,t,n){let r=f(`sha256`).update(t).digest(`hex`).slice(0,12),i=f(`sha256`).update(n).digest(`hex`).slice(0,12);return p.join(`/tmp`,`agent-device-ax-${r}-${e.processId()}-${i}`,`snapshot.sock`)}export{x as n,h as t};