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 • 4.91 kB
JavaScript
import{r as e}from"./rolldown-runtime.js";import{v as t}from"./sdk-contracts.js";import{A as n,B as r,F as i,I as a,L as o,M as s,N as c,m as l,w as u,y as d}from"./sdk-device.js";var f=e({assertDeviceHonorsPin:()=>O,assertRequestHonorsDevicePin:()=>T,effectiveDevicePinSelector:()=>w,markSelectionBootOccurred:()=>h,resolveExistingSessionDeviceSelection:()=>m,resolveInventoryDeviceSelection:()=>p});async function p(e){let{devices:t,selector:n,source:a}=e,o=l(n);if(a===`provider`&&!o)return v(t,n);if(a===`local`&&!o){let r=await y(t,n,e.appleSimulatorAppTarget);if(r)return r}let s=t.filter(e=>c(e,n,{includeExplicitSelectors:o})),u=e.boot&&a===`local`&&!o?i(s):void 0;if(u&&e.boot?.pick===`newest`)return{device:u,reason:`newest-bootable-local`,source:a,candidateCount:s.length,bootOccurred:!1};try{return g(await r([...t],n),s,{explicitSelector:o,source:a})}catch(e){throw b(e,s,u?`To boot one of these shut-down devices, pass --pick newest (pick: "newest" from Node or MCP).`:void 0)}}function m(e){return{device:e,reason:`existing-session`,source:`session`,candidateCount:1,bootOccurred:!1}}function h(e){if(!e||e.source!==`local`)return e;let{device:t}=e;return t.booted!==!0&&(t.kind===`simulator`||t.kind===`emulator`)?{...e,bootOccurred:!0}:e}function g(e,t,n){return{device:e,reason:n.explicitSelector?`explicit-selector`:n.source===`provider`?`single-provider-device`:_(e,t),source:n.source,candidateCount:t.length,bootOccurred:!1}}function _(e,t){return e.booted===!0&&t.filter(e=>e.booted===!0).length===1?`single-booted-local`:!e.booted&&t.length===1?`single-bootable-local`:`preferred-local`}async function v(e,n){let i=e.filter(e=>c(e,n)),a=i[0];if(a!==void 0&&i.length===1)return g(a,i,{explicitSelector:!1,source:`provider`});try{await r([...e],n)}catch(e){throw b(e,i)}throw new t(`AMBIGUOUS_MATCH`,`Multiple provider devices match this request.`,{...x(i),matches:i.length,dispatched:`no`,hint:`Select the leased provider device explicitly with --device, --udid, or --serial.`,retrySelectors:S(i)})}async function y(e,n,r){let i=r?.trim();if(!i)return;let{readInstalledAppProbe:a}=await import(`./device-inventory-context.js`).then(e=>e.t),o=a();if(!o)return;let s=e.filter(e=>c(e,n)&&u(e)&&e.kind===`simulator`&&e.booted===!0);if(s.length<2)return;let l=(await Promise.all(s.map(async e=>await o(e,i)?e:void 0))).filter(e=>e!==void 0),d=l[0];if(d!==void 0&&l.length===1)return{device:d,reason:`single-app-installed-local`,source:`local`,candidateCount:1,bootOccurred:!1};throw l.length===0?new t(`APP_NOT_INSTALLED`,`No booted iOS simulator has ${i} installed`,{appTarget:i,...x(s),hint:`Install the app on a booted simulator, or pass --udid to select the intended device explicitly.`,retrySelectors:S(s)}):new t(`AMBIGUOUS_MATCH`,`Multiple booted iOS simulators have ${i} installed`,{appTarget:i,...x(l),dispatched:`no`,hint:`Pass --udid to select the intended simulator explicitly.`,retrySelectors:S(l)})}function b(e,n,r){if(!(e instanceof t)||e.code!==`AMBIGUOUS_MATCH`)return e;let i=o([...n]);return new t(e.code,e.message,{...e.details??{},...x(i),retrySelectors:S(i),...r?{hint:[e.details?.hint,r].filter(Boolean).join(` `)}:{}},e)}function x(e){return{devices:e.slice(0,10).map(e=>({id:e.id,name:e.name}))}}function S(e){return e.slice(0,10).flatMap(e=>[{flag:n(e.platform)?`--serial`:`--udid`,value:e.id},{flag:`--device`,value:e.name}])}function C(e){return`${e.kind===`project`?`project `:``}config file ${e.path}`}function w(e,t){let n=t.platform;return{platform:n&&n!==`apple`?n:e.selector.platform??n,device:t.device??e.selector.device,udid:t.udid??e.selector.udid,serial:t.serial??e.selector.serial}}function T(e,t){let n=[],r=(t,r)=>{let i=r===void 0?{}:{step:r};for(let r of Object.keys(e.selector)){let a=e.selector[r];if(a!==void 0)for(let e of[`platform`,`device`,`udid`,`serial`]){let o=t?.[e];typeof o==`string`&&E(r,a,e,o)&&n.push({key:e,pinned:a,requested:o,...e===r?{}:{pinnedKey:r},...i})}}};r(t.flags),t.flags?.batchSteps?.forEach((e,t)=>r(e.flags,t+1)),n.length&&k(e,n)}function E(e,t,n,r){return e===n?n===`device`?!s({name:t},r):n===`platform`?a(r,t):r!==t:e===`platform`?!D(n,t):n===`platform`?!D(e,r):e===`udid`&&n===`serial`||e===`serial`&&n===`udid`}function D(e,t){return e===`udid`?d(t):e!==`serial`||n(t)}function O(e,t,n={}){let r=e=>c(t,{...e,deviceName:e.device},{includeExplicitSelectors:!0});r(e.selector)&&r(n)||k(e,[],{id:t.id,name:t.name})}function k(e,n,r){let i=n[0],a=i?.step===void 0?``:`Batch step ${i.step}: `,o=i?`${i.key} ${JSON.stringify(i.requested)}`:`Device selection ${JSON.stringify(r?.name)} (${r?.id})`,s=C(e.source);throw new t(`INVALID_ARGS`,`${a}${o} conflicts with the device pin from ${s}.`,{reason:`device_pin_conflict`,pin:{...e.selector},pinSource:s,conflicts:n,...r?{device:r}:{},hint:`Use a device matching ${JSON.stringify(e.selector)}, or edit or remove devicePin in ${e.source.path}.`})}export{h as n,m as r,f as t};