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 • 5.84 kB
import{v as e}from"./sdk-contracts.js";import{S as t,Z as n}from"./sdk-io.js";import"./command.js";import{D as r,b as i,m as a}from"./host-file.js";import"./config.js";import{c as o,u as s}from"./tool-provider.js";import c from"node:path";async function l(t,r,i={}){let a=[`devicectl`,...t],s=await o(a,{allowFailure:!0,signal:i.signal,timeoutMs:i.timeoutMs??2e4});if(s.exitCode===0)return;let{stdout:c,stderr:l}=s;if(!i.tolerateOutput?.(c,l))throw new e(`COMMAND_FAILED`,`Failed to ${r.action}`,n(s,{cmd:`xcrun`,args:a,stdout:c,stderr:l,deviceId:r.deviceId,hint:O(c,l)??`Ensure the iOS device is unlocked, trusted, and available in Xcode > Devices, then retry.`}))}async function u(e,t){return C(x(await g(e,{jsonPrefix:`agent-device-ios-apps`,args:[`devicectl`,`device`,`info`,`apps`,`--device`,e.id,`--include-all-apps`],failureMessage:`Failed to list iOS apps`,parseFailureMessage:`Failed to parse iOS apps list`})),t)}async function d(t){let n=await g(t,{jsonPrefix:`agent-device-ios-processes`,args:[`devicectl`,`device`,`info`,`processes`,`--device`,t.id],failureMessage:`Failed to list iOS processes`,parseFailureMessage:`Failed to parse iOS process list`,fallbackHint:T});if(!y(n))throw new e(`COMMAND_FAILED`,`Unsupported iOS process list response`,{deviceId:t.id,hint:T});return S(n)}async function f(e,t){let{appBundleUrl:n,processes:r}=await p(e,t),i=r.sort((e,t)=>v(e,n)-v(t,n))[0];i&&await g(e,{jsonPrefix:`agent-device-ios-process-terminate`,args:[`devicectl`,`device`,`process`,`terminate`,`--device`,e.id,`--pid`,String(i.pid),`--kill`],failureMessage:`Failed to terminate iOS app`,parseFailureMessage:`Failed to parse iOS process termination response`,tolerateFailurePayload:b})}async function p(t,n){let r=(await u(t,`all`)).find(e=>e.bundleId===n);if(!r)throw new e(`APP_NOT_INSTALLED`,`No iOS device app found for ${n}`,{appBundleId:n,deviceId:t.id});if(!r.url)throw new e(`COMMAND_FAILED`,`Missing app bundle URL for ${n}`,{appBundleId:n,deviceId:t.id,hint:`Installed-app metadata from devicectl did not include the bundle URL required to match running processes. Use an Xcode/CoreDevice toolchain that reports app URLs.`});let i=r.url.replace(/\/+$/,``);return{appBundleUrl:i,processes:(await d(t)).filter(e=>e.executable.startsWith(`${i}/`))}}async function m(e){let n=c.join(a(),`${e.jsonPrefix}-${t()}-${Date.now()}-${Math.random().toString(36).slice(2)}.json`),i=[...e.args,`--json-output`,n];try{let t=await o(i,{allowFailure:!0,signal:e.signal,timeoutMs:e.timeoutMs??2e4});if(t.exitCode!==0){let r=await _(n).catch(()=>void 0);return r!==void 0&&e.tolerateFailurePayload?.(r)?{ok:!0,payload:r}:{ok:!1,reason:`command-failed`,args:i,result:t,payload:r}}return await h(n,i,t)}finally{await r(n).catch(()=>{})}}async function h(t,n,r){try{return{ok:!0,payload:await _(t)}}catch(t){if(t instanceof e)throw t;return{ok:!1,reason:`unreadable-json`,args:n,result:r,cause:String(t)}}}async function g(t,r){let i=await m(r);if(i.ok)return i.payload;if(i.reason===`command-failed`){let{stdout:a,stderr:o}=i.result;throw new e(`COMMAND_FAILED`,r.failureMessage,n(i.result,{cmd:`xcrun`,args:i.args,stdout:a,stderr:o,deviceId:t.id,hint:O(a,o)??r.fallbackHint??`Ensure the iOS device is unlocked, trusted, and available in Xcode > Devices, then retry.`}))}throw new e(`COMMAND_FAILED`,r.parseFailureMessage,{deviceId:t.id,cause:i.cause})}async function _(e){return JSON.parse(await i(e))}function v(e,t){return e.executable.slice(t.length+1).split(`/`).length}function y(e){return Array.isArray(e?.result?.runningProcesses)}function b(e){let t=e?.error?.userInfo?.NSUnderlyingError?.error;return t?.domain===`NSPOSIXErrorDomain`&&t.code===3}function x(e){let t=e?.result?.apps;if(!Array.isArray(t))return[];let n=[];for(let e of t){if(!e||typeof e!=`object`)continue;let t=typeof e.bundleIdentifier==`string`?e.bundleIdentifier.trim():``;if(!t)continue;let r=typeof e.name==`string`&&e.name.trim().length>0?e.name.trim():t,i=typeof e.url==`string`&&e.url.trim().length>0?e.url.trim():void 0;n.push({bundleId:t,name:r,url:i})}return n}function S(e){let t=e?.result?.runningProcesses;if(!Array.isArray(t))return[];let n=[];for(let e of t){if(!e||typeof e!=`object`)continue;let t=typeof e.executable==`string`?e.executable.trim():``,r=typeof e.processIdentifier==`number`&&Number.isFinite(e.processIdentifier)?e.processIdentifier:NaN;!t||!Number.isFinite(r)||n.push({executable:t,pid:r})}return n}function C(e,t){return s(e,t)}const w=`Ensure the iOS device is unlocked, trusted, and available in Xcode > Devices, then retry.`,T=`This Xcode/CoreDevice toolchain must support 'devicectl device info processes' with JSON runningProcesses so agent-device can resolve app process IDs. Inspect diagnostics for the exact devicectl API failure.`,E=`Enable Developer Mode on the iOS device (Settings > Privacy & Security > Developer Mode), restart it when prompted, unlock it, then retry.`,D=`Let Xcode finish preparing this device: keep it unlocked and connected by cable, open Xcode > Settings > Platforms (or Window > Devices and Simulators), wait for device support to install, then retry.`;function O(e,t){let n=`${e}\n${t}`.toLowerCase();return n.includes(`device is busy`)&&n.includes(`connecting`)?`iOS device is still connecting. Keep it unlocked and connected by cable until it is fully available in Xcode Devices, then retry.`:n.includes(`coredeviceservice`)&&n.includes(`timed out`)?`CoreDevice service timed out. Reconnect the device and retry; if it persists restart Xcode and the iOS device.`:n.includes(`developer mode is disabled`)?E:n.includes(`developer disk image`)?D:n.includes(`must be paired`)?`Pair the iOS device with this Mac: connect it by cable, unlock it, accept the Trust prompt, and enter the device passcode, then retry.`:null}export{p as a,m as c,u as i,f as l,D as n,O as o,E as r,l as s,w as t};