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 • 5.95 kB
JavaScript
import{v as e}from"./sdk-contracts.js";import{W as t}from"./sdk-device.js";import{r as n}from"./physical-device-constants.js";import{l as r}from"./simctl.js";const i=/^(iphone|ipad|ipod|appletv|realitydevice)/i,a=/ipad/i,o=/\b(apple vision|vision pro|xros|visionos|realitydevice)\b/i,s=/\bSimRuntime\.watchOS-|\bSimDeviceType\.Apple-Watch-/i,c=/\b(iphone|ipad|ipod)\b/i,l=/^appletv/i,u=[`apple tv`,`appletv`,`tvos`];function d(e){return(e??``).trim().toLowerCase()}function f(e){return e.includes(`tvos`)}function p(e){return e.includes(`xros`)||e.includes(`visionos`)}function m(e){return f(d(e))?`tv`:`mobile`}function h(e){let t=d(e);return t.includes(`ios`)||t.includes(`tvos`)||t.includes(`xros`)||t.includes(`visionos`)||t.includes(`watchos`)}function g(e){let t=d(e);return u.some(e=>t.includes(e))}function _(e){return c.test(e.trim())}function v(e){return g(e)?`tv`:_(e)||o.test(e)?`mobile`:null}function y(e,t){return e===`tv`?`tvos`:t.some(e=>o.test(e))?`visionos`:t.some(e=>s.test(e))?`watchos`:t.some(e=>a.test(e))?`ipados`:`ios`}function b(e){return i.test(e.trim())}function x(e){return l.test(e.trim())}function S(e){return[e.name??``,e.deviceProperties?.name??``,e.deviceProperties?.deviceType??``]}function C(e){return e.hardwareProperties?.productType??e.deviceProperties?.productType??``}function w(e){return f(d(e.hardwareProperties?.platform))||x(C(e))||S(e).some(g)?`tv`:`mobile`}function T(e){let t=d(e.hardwareProperties?.platform);return t.includes(`ios`)||f(t)||p(t)||b(C(e))?!0:S(e).some(g)}function E(e){if(!T(e))return null;let t=e.hardwareProperties?.udid??e.identifier??``,r=e.name??e.deviceProperties?.name??t;if(!t)return null;let i=w(e),a=e.hardwareProperties?.marketingName?.trim(),o=e.deviceProperties?.osVersionNumber?.trim();return{platform:`apple`,id:t,name:r,kind:`device`,target:i,appleOs:y(i,[C(e),...S(e)]),...a?{model:a}:{},...o?{osVersion:o}:{},iosPhysicalDeviceBackend:`coredevice`,booted:n(e.connectionProperties?.tunnelState)}}const D=/^==\s*(.+?)\s*==$/,O=/^(?<name>.+?)\s+(?:\[(?<idBracket>[^[\]]+)\]|\((?<osVersion>[^)]+)\)\s+\((?<idParen>[^)]+)\))\s*$/;function k(e){let t=[],n=null;for(let r of e.split(/\r?\n/)){let e=r.trim();if(!e)continue;let i=D.exec(e);if(i){n=i[1]?.trim()??null;continue}if(n!==`Devices`)continue;let a=A(e);a&&t.push(a)}return t}function A(e){let t=O.exec(e)?.groups;if(!t)return;let n=t.name?.trim(),r=t.idBracket?.trim()||t.idParen?.trim();if(!n||!r)return;let i=v(n);if(!i)return;let a=t.osVersion?.trim();return{platform:`apple`,id:r,name:n,kind:`device`,target:i,appleOs:y(i,a?[n,a]:[n]),...a?{osVersion:a}:{},iosPhysicalDeviceBackend:`xctest`,booted:!0}}async function j(e,t){let[n,r]=await Promise.all([M(e,t),N(e,t)]);return P(n,r)}async function M(e,t){let n;try{return n=await e.files.createTemporaryTextFile({prefix:`agent-device-devicectl-`,suffix:`.json`}),(await e.appleTools.run({tool:`devicectl`,args:[`list`,`devices`,`--json-output`,n.path],allowFailure:!0,timeoutMs:8e3},t.signal)).exitCode===0?(JSON.parse(await n.readText()).result?.devices??[]).flatMap(e=>{let t=E(e);return t?[t]:[]}):[]}catch{return t.signal.throwIfAborted(),[]}finally{try{await n?.[Symbol.asyncDispose]()}catch{}}}async function N(e,t){try{let n=await e.appleTools.run({tool:`xctrace`,args:[`list`,`devices`],allowFailure:!0},t.signal);return n.exitCode===0?k(n.stdout):[]}catch{return t.signal.throwIfAborted(),[]}}function P(e,t){let n=new Set(e.map(e=>e.id)),r=[...e];for(let e of t)n.has(e.id)||(n.add(e.id),r.push(e));return r}function F(e,t){let n=I(e),r=[];for(let[i,a]of Object.entries(e.devices))if(h(i))for(let e of a){if(!e.isAvailable)continue;let a=m(i);r.push({platform:`apple`,id:e.udid,name:e.name,kind:`simulator`,target:a,appleOs:y(a,[i,e.deviceTypeIdentifier??``,e.name]),...n(i,e),booted:e.state===`Booted`,...t?{simulatorSetPath:t}:{}})}return r}function I(e){let t=new Map(e.devicetypes?.map(e=>[e.identifier,e.name])),n=new Map(e.runtimes?.map(e=>[e.identifier,e.version]));return(e,r)=>{let i=t.get(r.deviceTypeIdentifier??``),a=n.get(e);return{...i?{model:i}:{},...a?{osVersion:a}:{}}}}async function L(n,i,a){let o=i.iosSimulatorSetPath?.trim()||void 0,s=await n.appleTools.run({tool:`simctl`,args:r(o),...i.booted===!0?{timeoutMs:3e3}:{}},a.signal),c;try{c=F(JSON.parse(s.stdout),o)}catch(t){throw new e(`COMMAND_FAILED`,`Failed to parse simctl devices JSON`,void 0,t)}for(let e of c)e.booted===!0&&await n.observations.deviceBooted(e);return t(c)}function R(e){return Object.freeze({discover:async(t,n)=>await z(e,t,n)})}async function z(n,r,i){if(B(r))return[U(n)];if(n.hostOs!==`darwin`)throw H(n.hostOs);if(!await n.appleTools.isXcrunAvailable(i.signal))throw new e(`TOOL_MISSING`,`xcrun not found in PATH`);let a=L(n,r,i);if(r.kind===`simulator`)return await a;if(r.iosSimulatorSetPath||r.udid){let e=await a,o=[...e,U(n)];if(r.iosSimulatorSetPath||e.some(e=>e.id===r.udid))return t(o);let s=await j(n,i);return t(W(o,s))}let[o,s]=await Promise.all([a,j(n,i)]),c=[...o,U(n)];return t(W(c,s))}function B(e){return e.platform===`macos`||e.platform===`apple`&&e.target===`desktop`}const V={darwin:`macOS`,linux:`Linux`,win32:`Windows`,other:`a non-macOS host`};function H(t){return new e(`UNSUPPORTED_PLATFORM`,`Apple tools are only available on macOS`,{supportedOn:`macOS`,hostOs:t,retriable:!1,hint:`This host runs ${V[t]}, so it cannot boot iOS Simulators or run macOS sessions. Drive Android here instead with the same command shape: agent-device devices --platform android to list targets, then agent-device open <app.apk> --platform android. To use an iOS Simulator from this machine, run agent-device on a macOS host and pass --daemon-base-url <that daemon URL>.`})}function U(e){return{platform:`apple`,id:`host-macos-local`,name:e.hostName,kind:`device`,target:`desktop`,appleOs:`macos`,booted:!0}}function W(e,t){let n=new Set(e.map(e=>e.id));return[...e,...t.filter(e=>!n.has(e.id)&&(n.add(e.id),!0))]}export{R as createAppleInventorySource};