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 • 11.3 kB
import{r as e}from"./rolldown-runtime.js";import{v as t}from"./sdk-contracts.js";import n from"node:crypto";function r(e){return Object.freeze({family:e.platform,...e.appleOs===void 0?{}:{appleOs:e.appleOs},kind:e.kind,...e.target===void 0?{}:{target:e.target},...e.iosPhysicalDeviceBackend===void 0?{}:{iosPhysicalDeviceBackend:e.iosPhysicalDeviceBackend}})}function i(e){return Object.freeze({id:e.id,...r(e)})}function a(e){return JSON.stringify([e.id,...s(e)])}function o(e,t){return a(e)===a(t)}function ee(e,t){return JSON.stringify(s(e))===JSON.stringify(s(t))}function s(e){return[e.family,e.appleOs??null,e.kind,e.target??null,e.iosPhysicalDeviceBackend??null]}var te=e({BOOT_PICK_POLICIES:()=>Fe,DEVICE_KINDS:()=>d,DEVICE_TARGETS:()=>f,PLATFORMS:()=>c,PLATFORM_SELECTORS:()=>u,PUBLIC_PLATFORMS:()=>l,canHostScopedDevices:()=>fe,canonicalDeviceId:()=>_,decodeDeviceIdentity:()=>z,deviceFieldsFromPublicPlatform:()=>j,deviceIdentityFlag:()=>J,devicesOverlap:()=>S,hasExplicitDeviceIdentitySelector:()=>C,hostAndDeviceIds:()=>de,hostAppDevice:()=>y,hostAppDeviceId:()=>h,hostDeviceIdOf:()=>g,hostDeviceIdPrefix:()=>v,isAppleOs:()=>R,isApplePlatform:()=>w,isDeviceTarget:()=>L,isHandheldAppleDevice:()=>O,isHandheldAppleSimulator:()=>D,isHostScopedDeviceOf:()=>x,isIosFamily:()=>E,isMacOs:()=>T,isMacOsBundleId:()=>ae,isMobilePlatform:()=>k,isPlatform:()=>F,isPublicPlatform:()=>I,isSerialAddressablePlatform:()=>Z,isTvOsDevice:()=>M,matchesDeviceNameSelector:()=>Q,matchesDeviceSelector:()=>X,matchesPlatformSelector:()=>B,pickNewestBootableDevice:()=>Ie,platformSelectorsConflict:()=>V,preferredDeviceCandidates:()=>Y,publicPlatformString:()=>A,resolveApplePlatformName:()=>H,resolveDevice:()=>q,resolveDeviceAppleOs:()=>P,resolveRequestAppleSimulatorSetPath:()=>W,runnerSynthesizesTap:()=>N,sortAppleDevicesForSelection:()=>G,webBackendKeyOf:()=>ue,webSessionBackendKey:()=>b,webSessionDevice:()=>le});const ne=[`ios`,`ipados`,`tvos`,`watchos`,`visionos`,`macos`],c=[`apple`,`android`,`harmonyos`,`vega`,`linux`,`web`],l=[`ios`,`macos`,`android`,`harmonyos`,`vega`,`linux`,`web`],u=[...c,`ios`,`macos`],d=[`simulator`,`emulator`,`device`],f=[`mobile`,`tv`,`desktop`],re=`:app:`,p=`:session:`,m=[re,p],ie=/^[A-Za-z0-9_-]+(?:\.[A-Za-z0-9_-]+)+$/;function ae(e){return ie.test(e)}function h(e,t){return`${v(e)}${t.toLowerCase()}`}function g(e){let t=m.map(t=>e.indexOf(t)).filter(e=>e>=0);return t.length===0?e:e.slice(0,Math.min(...t))}function _(e){let t=g(e),n=v(t);return e.startsWith(n)?h(t,e.slice(n.length)):e}function v(e){return`${e}${re}`}function y(e,t){return{...e,id:h(e.id,t),hostApp:{hostDeviceId:e.id,bundleId:t}}}const oe=/^[A-Za-z0-9_-]+$/;function se(e){return e===void 0?`p_`:`t_${n.createHash(`sha256`).update(e).digest(`hex`).slice(0,12)}_`}function ce(e,n){let r=e.toLowerCase(),i=32-n;if(!oe.test(e)||r.length>i)throw new t(`INVALID_ARGS`,`A web session name must be 1 to ${i} characters of letters, digits, "_" and "-".`,{reason:`web_session_name_invalid`,sessionName:e,maxLength:i,hint:`Pass --session with a name such as "checkout-1".`});return r}function b(e){if(`kind`in e)return`w_${e.id}`;let t=se(e.namespace?.id);return`${t}${ce(e.name,t.length)}`}function le(e,t){let n=`${e.id}${p}${b(t)}`;return{...e,id:n}}function ue(e){let t=e.slice(g(e).length);return t.startsWith(p)?t.slice(9):void 0}function de(e){return e.hostApp?[e.hostApp.hostDeviceId,e.id]:[e.id]}function fe(e){return T(e)||e.platform===`web`}function x(e,t){return m.some(n=>t.startsWith(`${e}${n}`))}function S(e,t){return e===t||x(t,e)||x(e,t)}function C(e){return[e.deviceName,e.udid,e.serial].some(e=>typeof e==`string`&&e.trim().length>0)}function w(e){return e===`apple`||e===`ios`||e===`macos`}function T(e){return e.appleOs===`macos`||e.platform===`macos`}function E(e){return w(e.platform)&&!T(e)}function D(e){if(e.kind!==`simulator`)return!1;let t=P(e);return t===`ios`||t===`ipados`}function O(e){if(!E(e))return!1;let t=P(e);return t===`ios`||t===`ipados`}function k(e){return e.platform===`android`||e.platform===`harmonyos`||w(e.platform)&&!T(e)}function A(e){return w(e.platform)?T(e)?`macos`:`ios`:e.platform}function j(e){return e===`macos`?{platform:`apple`,appleOs:`macos`}:e===`ios`?{platform:`apple`}:{platform:e}}function M(e){return w(e.platform)&&e.target===`tv`}function N(e){return E(e)&&!M(e)&&e.appleOs!==`visionos`}function P(e){return e.appleOs?e.appleOs:e.platform===`macos`?`macos`:M(e)?`tvos`:`ios`}function F(e){return c.includes(e)}function I(e){return l.includes(e)}function L(e){return f.includes(e)}function R(e){return ne.includes(e)}function z(e){if(!Se(e))return null;let t=me(e.id),n=he(e.family),r=ge(e.kind),i=_e(e.target);return!t||!n||!r||i===null||!ve(e,n,r)?null:pe(e,t,n,r,i)}function pe(e,t,n,r,i){return Object.freeze({id:t,family:n,...e.appleOs===void 0?{}:{appleOs:e.appleOs},kind:r,...i===void 0?{}:{target:i},...e.iosPhysicalDeviceBackend===void 0?{}:{iosPhysicalDeviceBackend:e.iosPhysicalDeviceBackend}})}function me(e){return xe(e)?e:null}function he(e){return F(e)?e:null}function ge(e){return be(e)?e:null}function _e(e){if(e!==void 0)return L(e)?e:null}function ve(e,t,n){if(t===`apple`){if(!R(e.appleOs))return!1}else if(e.appleOs!==void 0||e.iosPhysicalDeviceBackend!==void 0)return!1;return ye(e.iosPhysicalDeviceBackend,n)}function ye(e,t){return e===void 0?!0:t===`device`?e===`coredevice`||e===`xctest`:!1}function be(e){return d.includes(e)}function xe(e){return typeof e==`string`&&e.trim().length>0}function Se(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function B(e,t){return t?t===`apple`?w(e.platform):t===`ios`?w(e.platform)&&!T(e):t===`macos`?w(e.platform)&&T(e):e.platform===t:!0}function V(e,t){return!e||!t||e===t?!1:e===`apple`?!w(t):t!==`apple`||!w(e)}function H(e,t){return t?Ce(t):e===`macos`||e===`desktop`?`macOS`:e===`tv`?`tvOS`:`iOS`}function Ce(e){switch(e){case`tvos`:return`tvOS`;case`macos`:return`macOS`;case`visionos`:return`visionOS`;case`watchos`:return`watchOS`;default:return`iOS`}}const U=`AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET`;function W(e){let{platform:n,target:r,env:i}=e;if(n===`macos`||r===`desktop`)return;let a=e.requestedSetPath?.trim()||void 0;if(!K(n))return a;let o=i[U]?.trim()||void 0;if(!o)return a;if(a&&a!==o)throw new t(`INVALID_ARGS`,`This daemon only reaches iOS simulators in the set "${o}" (${U}), but the request named --ios-simulator-device-set "${a}".`,{reason:`ios_simulator_device_set_outside_daemon_set`,hint:`Omit --ios-simulator-device-set, or pass exactly "${o}".`});return o}function G(e){return e.map((e,t)=>({device:e,index:t})).sort((e,t)=>We(e,t)).map(({device:e})=>e)}function K(e){return!e||e===`apple`||e===`ios`}async function q(e,t,n={}){let r=e.filter(e=>X(e,t));return we(r,t)||Oe(r,t.deviceName)||(Ke(r)&&(r=G(r)),Ae(r,t,n))}function we(e,t){if(Te(t),t.udid)return Ee(e,t.udid);if(t.serial)return De(e,t)}function Te(e){let n=e.platform;if(n){if(e.udid&&Z(n))throw new t(`INVALID_ARGS`,`--udid selects Apple devices, but this request selected --platform ${n}.`,{hint:`Use --serial ${e.udid} for ${n} devices.`});if(e.serial&&w(n))throw new t(`INVALID_ARGS`,`--serial selects Android and HarmonyOS devices, but this request selected --platform ${n}.`,{hint:`Use --udid ${e.serial} for Apple devices.`})}}function Ee(e,n){let r=e.find(e=>e.id===n&&w(e.platform));if(!r)throw new t(`DEVICE_NOT_FOUND`,`No Apple device with UDID ${n}`,{dispatched:`no`});return r}function De(e,n){let r=e.find(e=>e.id===n.serial&&Z(e.platform));if(!r)throw new t(`DEVICE_NOT_FOUND`,Ve(n),{dispatched:`no`});return r}function Oe(e,n){if(!n)return;let r=e.find(e=>Q(e,n));if(!r){let r=ke(e,n);throw new t(`DEVICE_NOT_FOUND`,`No device named ${n}`,r===void 0?{dispatched:`no`}:{hint:r,dispatched:`no`})}return r}function ke(e,t){let n=e.find(e=>e.id===t);if(!n)return;let r=J(n.platform);if(r)return`${t} is the id of ${JSON.stringify(n.name)}, not its name. Did you mean --${r} ${t}?`}function J(e){if(w(e))return`udid`;if(Z(e))return`serial`}function Ae(e,t,n){let r=e[0];if(r!==void 0&&e.length===1)return r;e.length===0&&He(t,n);let i=Y(e);i.length>1&&je(i);let a=i[0];return a||He(t,n),a}function je(e){let n=e.slice(0,10);throw new t(`AMBIGUOUS_MATCH`,`${e.length} devices match this request equally; select one explicitly.`,{devices:n.map(e=>({id:e.id,name:e.name})),matches:e.length,dispatched:`no`,hint:Me(n)})}function Me(e){return`Retry with the id of the intended device: ${e.map(e=>`--${J(e.platform)??`udid`} ${e.id}`).join(` or `)}.`}function Y(e){let t=Ke(e)?Pe(e):Ne(e),n=t.filter(e=>e.booted);return n.length>0?n:t}function Ne(e){let t=e.filter(e=>e.kind!==`device`);return t.length>0?t:e}function Pe(e){let t=Math.min(...e.map(e=>$(e)));return e.filter(e=>$(e)===t)}const Fe=[`newest`];function Ie(e){let t=Y(e);if(t.length<2||t.some(e=>e.booted))return;let n=new Intl.Collator(`en`,{numeric:!0});return[...t].sort((e,t)=>Le(n,e,t))[0]}function Le(e,t,n){return Number(Re(n))-Number(Re(t))||ze(e,t.osVersion,n.osVersion)||e.compare(t.name,n.name)||e.compare(t.id,n.id)}function Re(e){return(e.target??`mobile`)===`mobile`}function ze(e,t,n){return t===n?0:t===void 0?1:n===void 0?-1:e.compare(n,t)}function X(e,t,n={}){return B(e,t.platform)&&(!t.target||(e.target??`mobile`)===t.target)&&(!n.includeExplicitSelectors||Be(e,t))}function Be(e,t){return!(t.udid&&!(e.id===t.udid&&w(e.platform))||t.serial&&!(e.id===t.serial&&Z(e.platform))||t.deviceName&&!Q(e,t.deviceName))}function Z(e){return e===`android`||e===`harmonyos`||e===`vega`}function Ve(e){return e.platform===`android`?`No Android device with serial ${e.serial}`:e.platform===`vega`?`No Vega VVD with serial ${e.serial}`:e.platform===`harmonyos`?`No HarmonyOS device with serial ${e.serial}`:`No Android, HarmonyOS device, or Vega VVD with serial ${e.serial}`}function He(e,n){let r=n.simulatorSetPath;throw r&&K(e.platform)?new t(`DEVICE_NOT_FOUND`,`No devices found in the scoped simulator set`,{simulatorSetPath:r,hint:`The simulator set at "${r}" appears to be empty. Create a compatible simulator first with xcrun simctl --set "${r}" create, or remove the scoped simulator set.`,selector:e,dispatched:`no`}):new t(`DEVICE_NOT_FOUND`,`No devices found`,{selector:e,dispatched:`no`})}function Q(e,t){return Ue(e.name)===Ue(t)}function Ue(e){return e.toLowerCase().replaceAll(`_`,` `).replaceAll(/\s+/g,` `).trim()}function We(e,t){return $(e.device)-$(t.device)||Number(t.device.booted===!0)-Number(e.device.booted===!0)||e.device.name.localeCompare(t.device.name)||e.index-t.index}function $(e){return e.appleOs===`watchos`?15:e.kind===`simulator`?Ge(e,0,1,2,3):e.kind===`device`&&w(e.platform)&&!T(e)?Ge(e,10,11,12,13):14}function Ge(e,t,n,r,i){let a={mobile:qe(e)?n:t,tv:r,desktop:i};return e.appleOs===`visionos`?i:a[e.target??`mobile`]}function Ke(e){return e.length>0&&e.every(e=>w(e.platform))}function qe(e){return e.appleOs?e.appleOs===`ipados`:/\bipad\b/i.test(e.name)}export{Z as A,q as B,x as C,k as D,ae as E,Ie as F,ue as G,W as H,V as I,i as J,b as K,Y as L,Q as M,X as N,F as O,B as P,ee as Q,A as R,D as S,T,N as U,P as V,G as W,r as X,a as Y,o as Z,g as _,u as a,L as b,_ as c,J as d,te as f,y as g,de as h,c as i,M as j,I as k,z as l,C as m,d as n,l as o,S as p,le as q,f as r,fe as s,Fe as t,j as u,R as v,E as w,O as x,w as y,H as z};