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.

2 lines • 7.13 kB
import{v as e}from"./sdk-contracts.js";import{a as t,o as n,s as r}from"./sdk-batch-runner.js";import{H as i,K as a,Q as o,St as s,Z as c}from"./sdk-io.js";import"./diagnostics.js";import{T as l,w as u}from"./sdk-device.js";import{n as d,t as f}from"./launch-console.js";import"./observability.js";import"./command.js";import"./retry.js";import{k as p,o as m}from"./host-file.js";import{n as h,p as g,x as _}from"./config.js";import{c as v,s as y}from"./tool-provider.js";import{t as b}from"./physical-device-control.js";import{r as x}from"./simctl.js";import{t as S}from"./runner-client.js";import{t as C}from"./simulator.js";import{a as w,l as T,u as E}from"./app-resolution.js";import D from"node:path";function O(t){if(!(t instanceof e)||t.code!==`COMMAND_FAILED`)return!1;let n=t.details??{};if(n.exitCode!==4)return!1;let r=String(n.stderr??``).toLowerCase();return r.includes(`fbsopenapplicationserviceerrordomain`)&&r.includes(`the request to open`)}async function k(e,t){let n=await v(x(e,[`get_app_container`,e.id,t]),{allowFailure:!0});if(n.exitCode!==0)return{installed:!1};let r=n.stdout.trim();if(!r)return{installed:!1};let i=await y(`plutil`,[`-extract`,`CFBundleExecutable`,`raw`,`-o`,`-`,`${r}/Info.plist`],{allowFailure:!0});if(i.exitCode!==0||!i.stdout.trim())return{installed:!0};let a=`${r}/${i.stdout.trim()}`,o=await y(`otool`,[`-l`,a],{allowFailure:!0});if(o.exitCode!==0)return{installed:!0};let s=o.stdout.toLowerCase();return{installed:!0,simulatorCompatible:s.includes(`iossimulator`)||s.includes(`platform 7`)}}function A(e){return e.installed?e.simulatorCompatible===!1?`ARCH_MISMATCH`:`PERSISTENT_LAUNCH_FAIL`:`APP_NOT_INSTALLED`}function j(e){switch(e){case`ARCH_MISMATCH`:return`The app binary was not built for the simulator platform. Rebuild with a simulator destination or use a physical device.`;case`APP_NOT_INSTALLED`:return`The app bundle is not installed on this simulator. Run install before open.`;case`PERSISTENT_LAUNCH_FAIL`:return`The simulator repeatedly refused to launch the app. Inspect crash logs in Console.app or ~/Library/Logs/DiagnosticReports/ and consider reinstalling the app.`;default:return`The simulator failed to launch the app. Read the diagnostics log for the launch output.`}}const M=25e3;async function N(i,a,o){let s=o?.launchConsole?.trim(),c=o?.launchArgs;if(s&&(!u(i)||i.kind!==`simulator`))throw new e(`UNSUPPORTED_OPERATION`,d);if(l(i)){if(c&&c.length>0)throw new e(`UNSUPPORTED_OPERATION`,`--launch-args is not supported on macOS; launch arguments are currently iOS-only.`);await E(i,a,o);return}let p=o?.url?.trim();if(p){if(s)throw new e(`INVALID_ARGS`,f);if(!t(p))throw new e(`INVALID_ARGS`,`open <app> <url> requires a valid URL target`);if(i.kind===`simulator`){let e=!!c||n(p);if(o?.terminateRunningApp||e){let t=o?.appBundleId??await w(i,a);e?await z(i,t,{...c?{launchArgs:c}:{},...o?.terminateRunningApp?{terminateRunningApp:!0}:{}}):await R(i,t)}await P(i,p,void 0,o?.runnerOptions);return}let l=o?.appBundleId??await w(i,a),u=r(l,p);if(!u)throw new e(`INVALID_ARGS`,`Deep link open on iOS devices requires an active app bundle ID. Open the app first, then open the URL.`);await W(i,u,{payloadUrl:p,launchArgs:c,runnerOptions:o?.runnerOptions});return}let m=a.trim();if(t(m)){if(s)throw new e(`INVALID_ARGS`,f);if(i.kind===`simulator`){await P(i,m,c,o?.runnerOptions);return}let t=r(o?.appBundleId,m);if(!t)throw new e(`INVALID_ARGS`,`Deep link open on iOS devices requires an active app bundle ID. Open the app first, then open the URL.`);await W(i,t,{payloadUrl:m,launchArgs:c,runnerOptions:o?.runnerOptions});return}let h=o?.appBundleId??await w(i,a);if(i.kind===`simulator`){await z(i,h,{...s?{launchConsole:s}:{},...c?{launchArgs:c}:{},...o?.terminateRunningApp?{terminateRunningApp:!0}:{}});return}await W(i,h,{launchArgs:c,runnerOptions:o?.runnerOptions})}async function P(t,n,r,i){if(r&&r.length>0)throw new e(`INVALID_ARGS`,`--launch-args is not supported with iOS simulator URL opens (simctl openurl ignores launch args). Launch the app first with --launch-args, then issue the URL open in a separate call.`);await C(t);let{prepareIosSimulatorAccessibility:a}=await import(`./ios-simulator-accessibility.js`);await a(t);let s=x(t,[`openurl`,t.id,n]);try{await v(s,{timeoutMs:g,...i?.signal?{signal:i.signal}:{}})}catch(n){throw o(n)?new e(`COMMAND_FAILED`,`xcrun simctl openurl did not answer within ${g}ms.`,{...n.details,reason:`ios-simulator-openurl-timeout`,timeoutMs:g,deviceId:t.id,hint:`The Simulator may still have received the URL; CoreSimulator is slow to answer. Retry the open, or answer any "Open in" prompt with alert accept.`}):n}}async function F(e){l(e)||e.kind===`simulator`&&await C(e)}async function I(e,t,n,r){if(l(e)){await T(e,t,r);return}let i=await w(e,t);if(e.kind===`simulator`){await R(e,i);return}await L(i),await b(e).terminateApp(e,i,{runnerOptions:n,runRunnerCommand:S})}async function L(t){let{iosSystemSurfaceHost:n,iosSystemSurfaceOpenRefusal:r}=await import(`./ios-system-surface.js`).then(e=>e.a);if(n(t))throw new e(`UNSUPPORTED_OPERATION`,r(t),{reason:`system-surface-host-not-openable`,appBundleId:t})}async function R(t,n){await L(n),await C(t);let r=x(t,[`terminate`,t.id,n]),i=await v(r,{allowFailure:!0,timeoutMs:_});if(i.exitCode!==0&&!i.stderr.toLowerCase().includes(`found nothing to terminate`))throw new e(`COMMAND_FAILED`,`xcrun exited with code ${i.exitCode}`,c(i,{cmd:`xcrun`,args:r}))}async function z(t,n,r){await L(n),await C(t);let{prepareIosSimulatorAccessibility:o}=await import(`./ios-simulator-accessibility.js`);await o(t);let s=0,l=i.fromTimeoutMs(h);try{await a(async({deadline:i})=>{if(i?.isExpired())throw new e(`COMMAND_FAILED`,`App launch deadline exceeded`,{timeoutMs:h});let a=x(t,B(t.id,n,r)),o=r?.launchConsole?await V(a,r.launchConsole):await v(a,{allowFailure:!0});if(o.exitCode!==0)throw new e(`COMMAND_FAILED`,`xcrun exited with code ${o.exitCode}`,c(o,{cmd:`xcrun`,args:a}))},{maxAttempts:10,baseDelayMs:1e3,maxDelayMs:5e3,jitter:.2,shouldRetry(e){return O(e)?(s+=1,s<3):!1}},{deadline:l})}catch(e){if(O(e)){let r=e,i=A(await k(t,n));r.details={...r.details,hint:j(i)}}throw e}}function B(e,t,n){let r=[`launch`];return n?.launchConsole&&r.push(`--console-pty`),n?.terminateRunningApp&&r.push(`--terminate-running-process`),r.push(e,t),n?.launchArgs&&n.launchArgs.length>0&&r.push(...n.launchArgs),r}async function V(t,n){await m(D.dirname(n));try{let e=await v(t,{allowFailure:!0,timeoutMs:M});return await H(n,e.stdout,e.stderr),e}catch(t){let r=(t instanceof e?t:void 0)?.details;if(r?.timeoutMs===M){let e=typeof r.stdout==`string`?r.stdout:``,t=typeof r.stderr==`string`?r.stderr:``;return await H(n,e,t),s({level:`warn`,phase:`ios_simulator_launch_console_capture_timeout`,data:{timeoutMs:M,logPath:n,stdoutBytes:Buffer.byteLength(e),stderrBytes:Buffer.byteLength(t)}}),{stdout:e,stderr:t,exitCode:0}}throw t}}async function H(e,t,n){await p(e,U(t,n))}function U(e,t){return!e||!t||e.endsWith(` `)||e.endsWith(`\r`)?`${e}${t}`:`${e}\n${t}`}async function W(e,t,n){await L(t),await b(e).launchApp(e,t,{...n,runRunnerCommand:S})}export{N as n,F as r,I as t};