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.04 kB
JavaScript
import{r as e}from"./rolldown-runtime.js";import{v as t}from"./sdk-contracts.js";import{a as n}from"./sdk-batch-runner.js";import{E as r,T as i,w as a}from"./sdk-device.js";import{a as o}from"./settings.js";import{t as s}from"./app-resolution-cache.js";import{o as c,u as l}from"./tool-provider.js";import{t as u}from"./physical-device-control.js";import{r as d}from"./helper.js";import{setTimeout as f}from"node:timers/promises";const p={settings:`com.apple.systempreferences`},m=/^[a-z0-9-]+\./,h={platform:`macos`,deviceId:`host`,variant:`all`},g=s();async function _(e){let n=e.trim(),i=p[n.toLowerCase()];if(i)return i;if(r(n)&&m.test(n))return n;let a=g.get(h,n);if(a)return a;let o=n.toLowerCase(),s=(await D(`all`)).filter(e=>e.name.toLowerCase()===o||e.bundleId.toLowerCase()===o),c=s[0];if(c!==void 0&&s.length===1)return g.set(h,n,c.bundleId);throw s.length>1?new t(`INVALID_ARGS`,`Multiple apps matched "${e}"`,{matches:s}):new t(`APP_NOT_INSTALLED`,`No app found matching "${e}"`)}async function v(e,t){let r=x(e);return t&&!n(t)&&S(e,await _(t)),t?{appBundleId:r.bundleId,appName:t}:{appBundleId:r.bundleId}}async function y(e,r,i){let a={background:e.hostApp!==void 0},o=r.trim();if(e.hostApp&&n(o)){await O().openBundle(e.hostApp.bundleId,o,a);return}let s=i?.url?.trim();if(s){if(!n(s))throw new t(`INVALID_ARGS`,`open <app> <url> requires a valid URL target`);let o=i?.appBundleId??await _(r);S(e,o),await O().openBundle(o,s,a);return}if(n(o)){await O().openTarget(o,a);return}let c=i?.appBundleId??await _(o);S(e,c),await O().openBundle(c,void 0,a)}async function b(e,n,r){let i=await _(n);S(e,i);let a=await d(i,r);if(a.running){if(!a.terminated&&!a.forceTerminated)throw new t(`COMMAND_FAILED`,`Failed to close macOS app ${n}`,{bundleId:i,running:a.running,terminated:a.terminated,forceTerminated:a.forceTerminated});for(let e=1;e<20;e+=1)if(await f(100),!(await d(i,r)).running)return;throw new t(`COMMAND_FAILED`,`Timed out waiting for macOS app ${n} to close`,{reason:`MACOS_APP_TERMINATION_TIMEOUT`,bundleId:i,attempts:20})}}function x(e){if(e.hostApp)return e.hostApp;throw new t(`COMMAND_FAILED`,`Expected a macOS app device.`)}function S(e,n){let r=e.hostApp;if(!(!r||r.bundleId.toLowerCase()===n.toLowerCase()))throw new t(`INVALID_ARGS`,`This session drives ${r.bundleId} as its own device, so it cannot open or close ${n}.`,{reason:`app-device-scope`,bundleId:r.bundleId,hint:`Open the other app in a new --session. To open a link in this app, pass the link alone.`})}async function C(){return await O().readClipboard()}async function w(e){await O().writeClipboard(e)}async function T(){return await O().readDarkMode()}async function E(e){let t=o(e),n=t===`toggle`?!await T():t===`dark`;await O().setDarkMode(n)}async function D(e){return await O().listApps(e)}function O(){let e=c().macosHost;if(!e)throw new t(`UNSUPPORTED_OPERATION`,`macOS host provider is not available`);return e}function k(e){return new t(`APP_NOT_INSTALLED`,`No app found matching "${e}"`)}var A=e({buildAppNotInstalledError:()=>k,detectSoleRunningIosSimulatorApp:()=>H,findIosSimulatorInstalledApp:()=>L,invalidateIosAppResolutionCache:()=>P,listIosApps:()=>Y,resolveIosApp:()=>F,resolveIosSimulatorDeepLinkBundleId:()=>W,resolveIosSimulatorExpoDevClientScheme:()=>G});const j={settings:`com.apple.Preferences`},M=s();function N(e){return{platform:`ios`,deviceId:e.id,variant:e.kind}}async function P(e,t){return await M.invalidateWhile(N(e),t)}async function F(e,t,n={}){if(i(e))return await _(t);let r=t.trim(),a=z(r),o=N(e);if(!n.installedOnly){if(r.includes(`.`))return r;if(a!==r)return a;let e=M.get(o,r);if(e)return e}let s=e.kind===`simulator`?await Q(e):await u(e).listApps(e,`all`);return M.set(o,r,I(s,t,r,a))}function I(e,n,r,i){let a=i!==r;if((a||r.includes(`.`))&&e.some(e=>e.bundleId===i))return i;if(a)throw k(n);let o=e.filter(e=>e.name.toLowerCase()===r.toLowerCase()),s=o[0];if(s!==void 0&&o.length===1)return s.bundleId;throw o.length>1?new t(`INVALID_ARGS`,`Multiple apps matched "${n}"`,{matches:o}):k(n)}async function L(e,t){if(!(!a(e)||e.kind!==`simulator`||e.booted!==!0)&&t.trim())try{return await F(e,t,{installedOnly:!0})}catch(e){if(R(e))return;throw e}}function R(e){return e instanceof t?e.code===`APP_NOT_INSTALLED`||e.details?.reason===Z||Array.isArray(e.details?.matches):!1}function z(e){let t=e.trim();return j[t.toLowerCase()]??e}const B=3e3,V=/UIKitApplication:([^[\s]+)\[/;async function H(e){if(!(!a(e)||e.kind!==`simulator`||e.booted!==!0))try{let[t,n]=await Promise.all([U(e),X(e,{timeoutMs:B})]);if(t.length===0)return;let r=new Map(n.map(e=>[e.bundleId,e])),i=new Map;for(let e of t){let t=r.get(e);t&&i.set(t.bundleId,t)}return i.size===1?[...i.values()][0]:void 0}catch{return}}async function U(e){let{runSimctlForDevice:t}=await import(`./simctl-execution.js`).then(e=>e.n),n=await t(e,[`spawn`,e.id,`launchctl`,`list`],{allowFailure:!0,timeoutMs:B});if(n.exitCode!==0)return[];let r=[];for(let e of n.stdout.split(`
`)){let t=V.exec(e);t?.[1]&&r.push(t[1])}return r}async function W(e,t,n={}){if(!a(e)||e.kind!==`simulator`)return;let r=J(t);if(!r)return;let{remainingLookupBudget:i}=await import(`./simulator-app-budget.js`),o=i(n),s=await $(e,o()),c=[];for(let t of s)t.bundleId.startsWith(`com.callstack.agentdevice.runner`)||(await K(e,t.bundleId,o(),t.path)).has(r)&&c.push(t);let l=c.filter(e=>e.applicationType===`User`);if(l.length===1)return l[0]?.bundleId;if(!(l.length>1))return c.length===1?c[0]?.bundleId:void 0}async function G(e,t,n={}){if(!a(e)||e.kind!==`simulator`)return;let{remainingLookupBudget:r}=await import(`./simulator-app-budget.js`),i=r(n),o=[...await K(e,t,i())].filter(e=>e.startsWith(`exp+`)&&e.length>4);if(o.length!==1)return;let s=o[0];return await W(e,`${s}://`,i())===t?s:void 0}async function K(e,t,n,r){return q(await c().simulatorApps.readInfoPlist({device:e,bundleId:t,appPath:r,...n}))}function q(e){let t=new Set,n=e?.CFBundleURLTypes;if(!Array.isArray(n))return t;for(let e of n)if(Array.isArray(e?.CFBundleURLSchemes))for(let n of e.CFBundleURLSchemes)typeof n==`string`&&n.trim()&&t.add(n.trim().toLowerCase());return t}function J(e){return/^([A-Za-z][A-Za-z0-9+.-]*):/.exec(e.trim())?.[1]?.toLowerCase()}async function Y(e,t){if(i(e))return await D(t);if(e.kind===`simulator`){let n=await X(e);return l(n,t)}return await u(e).listApps(e,t)}async function X(e,t){return(await $(e,t)).map(e=>({bundleId:e.bundleId,name:e.name}))}const Z=`app-list-unreadable`;async function Q(e){let n=await c().simulatorApps.list(e);if(n.type===`unreadable`)throw new t(`COMMAND_FAILED`,`Could not list the apps installed on ${e.id}`,{reason:Z,deviceId:e.id,exitCode:n.exitCode,stderr:n.stderr,hint:"Check that the simulator is booted and that `xcrun simctl listapps <udid>` answers, then retry."});return n.apps.map(e=>({bundleId:e.bundleId,name:e.name}))}async function $(e,t){let n=await c().simulatorApps.list(e,t);return n.type===`listed`?n.apps:[]}export{F as a,k as c,C as d,v as f,P as i,b as l,w as m,H as n,W as o,E as p,L as r,G as s,A as t,y as u};