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 • 3.35 kB
import{r as e}from"./rolldown-runtime.js";import{v as t}from"./sdk-contracts.js";import{Ot as n}from"./sdk-io.js";import"./diagnostics.js";import{H as r,m as i,q as a,y as o}from"./sdk-device.js";import{n as s}from"./device-isolation.js";import{a as c,i as l,o as u,r as d}from"./device-inventory-context.js";import{AsyncLocalStorage as f}from"node:async_hooks";var p=e({buildDeviceInventoryRequestFromFlags:()=>C,resolveTargetDevice:()=>v,resolveTargetDeviceSelection:()=>y,withResolveTargetDeviceCacheScope:()=>w});async function m(e){let{resolveInventoryDeviceSelection:t}=await import(`./device-selection-resolver.js`).then(e=>e.t);return await t(e)}const h=new f;function g(e,n){return!i(n)&&e instanceof t&&e.code===`DEVICE_NOT_FOUND`}function _(e,t){return!i(e)&&(!e.platform||e.platform===`apple`||e.platform===`ios`)&&e.target!==`desktop`&&(!t||t.kind===`device`)}async function v(e,t){return(await y(e,t)).device}async function y(e,t){let r={...C(e,t.env),androidAvdSelection:t.androidAvdSelection??`running-only`},{iosSimulatorSetPath:i,...a}=r,s=D(r,t),l={platform:r.platform,target:e.target,...r.udid?{udid:r.udid}:{},...i?{iosSimulatorSetPath:i}:{},cacheHit:!1};return await n(`resolve_target_device`,async()=>{let e=T(s);if(e)return l.cacheHit=!0,await b(e,t);let n=await c(r),u=[...n.devices];if(o(a.platform)&&n.source===`local`){let e=await x(u,a,{simulatorSetPath:i,appleSimulatorAppTarget:t.appleSimulatorAppTarget,boot:t.boot});return await b(E(s,e),t)}let d=await m({devices:u,selector:a,source:n.source,boot:t.boot});return await b(E(s,d),t)},l)}async function b(e,t){if(t.webSession!==void 0&&e.source===`local`&&e.device.platform===`web`)return{...e,device:a(e.device,t.webSession)};let n=t.appDeviceTarget,r=n?l():void 0;if(!n||!r||e.source!==`local`)return e;let i=await r(e.device,n);return i?{...e,device:i}:e}async function x(e,n,r){let i;try{i=await m({devices:e,selector:n,source:`local`,appleSimulatorAppTarget:r.appleSimulatorAppTarget,boot:r.boot})}catch(e){if(!g(e,n))throw e}let a=await S(n,r,i?.device);if(a)return a;if(i)return i;throw new t(`DEVICE_NOT_FOUND`,`No devices found`,{selector:n})}async function S(e,t,n){if(_(e,n))try{return await m({devices:await d({platform:e.platform??`ios`,target:e.target,iosSimulatorSetPath:t.simulatorSetPath,kind:`simulator`}),selector:e,source:`local`,boot:t.boot})}catch(e){if(u(e))throw e;return}}function C(e,n){let i=e.platform;if(e.target&&!i)throw new t(`INVALID_ARGS`,`Device target selector requires --platform. Use --platform ios|macos|android|vega|linux|apple with --target mobile|tv|desktop.`);let a=r({requestedSetPath:e.iosSimulatorDeviceSet,platform:i,target:e.target,env:n}),o=s(e.androidDeviceAllowlist,n);return{platform:i,target:e.target,deviceName:e.device,udid:e.udid,serial:e.serial,leaseId:e.leaseId,leaseProvider:e.leaseProvider,deviceKey:e.deviceKey,clientId:e.clientId,iosSimulatorSetPath:a,androidSerialAllowlist:o?Array.from(o).sort():void 0}}async function w(e){return h.getStore()?await e():await h.run(new Map,e)}function T(e){let t=h.getStore()?.get(e);if(t)return{...t,device:{...t.device}}}function E(e,t){return h.getStore()?.set(e,{...t,device:{...t.device}}),t}function D(e,t){let{env:n,appleSimulatorAppTarget:r,appDeviceTarget:i,webSession:a,...o}=t;return JSON.stringify({request:e,options:o})}export{w as a,y as i,p as n,v as r,C as t};