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 • 6.33 kB
JavaScript
import{r as e}from"./rolldown-runtime.js";import{v as t}from"./sdk-contracts.js";import{Q as n,St as r}from"./sdk-io.js";import"./diagnostics.js";import"./command.js";import{l as i}from"./config.js";import{c as a}from"./devicectl.js";import{d as o}from"./simctl.js";var s=e({appleLitPanel:()=>N,appleSimulatorDisplayArgvFragment:()=>z,buildInventory:()=>k,establishedLitPanel:()=>M,knownAppleMultiScreen:()=>d,panelContentRotation:()=>L,parseCoreDeviceDisplays:()=>b,queryAppleDisplayInventory:()=>f,readPanelPower:()=>D,resolveAppleCaptureDisplay:()=>P,resolveAppleCaptureTarget:()=>F});const c=new Set([`activeOn`,`inactiveOn`,`activeDimmed`]),l=new Set([`off`]),u=new Map;function d(e){return u.get(e.id)}async function f(e,t){let n=t.source===`guest`||o(e).simulatorSetPath?await m(e,t):await p(e,t);if(!n.ok)return v(e,n.reason,n.detail),y();let r=g(e,n.payload);return r.unresolved||u.set(e.id,r.multiScreen),r}async function p(e,t){try{let n=await a({jsonPrefix:`agent-device-apple-displays`,args:[`devicectl`,`device`,`info`,`displays`,`--device`,e.id],timeoutMs:t.timeoutMs??5e3,signal:t.signal});return n.ok?{ok:!0,payload:n.payload}:{ok:!1,reason:n.reason,detail:n.result.stderr.trim()}}catch(e){return t.signal?.aborted?h(e,`probe-canceled`):h(e,n(e)?`probe-timed-out`:`probe-failed`)}}async function m(e,t){let r=AbortSignal.timeout(i),a=t.signal?AbortSignal.any([t.signal,r]):r;try{let{createFoldHelper:n}=await import(`./fold-helper.js`).then(e=>e.r);return{ok:!0,payload:await(t.foldHelper??n(e)).readPanelPayload({signal:a,timeoutMs:t.timeoutMs??5e3})}}catch(e){return t.signal?.throwIfAborted(),h(e,n(e)||r.aborted?`probe-timed-out`:`guest-panels-failed`)}}function h(e,t){return{ok:!1,reason:t,detail:e instanceof Error?e.message:String(e)}}function g(e,t){let n=b(t),r=_(t);return n.filter(e=>e.integrated).length===r&&n.length>0?k(n):r>=2?{displays:n.filter(e=>e.integrated),multiScreen:!0,ambiguous:!0,unresolved:!1}:(v(e,n.length===0?`empty-display-list`:`incomplete-panel-table`,``),y())}function _(e){let t=e?.result?.displays;return Array.isArray(t)?t.filter(e=>typeof e==`object`&&!!e&&O(e.type)).length:0}function v(e,t,n){r({level:t===`probe-canceled`?`info`:`warn`,phase:`apple_display_inventory_unresolved`,data:{platform:e.platform,deviceId:e.id,reason:t,...n?{detail:n}:{},...t===`command-failed`||t===`unreadable-json`?{hint:`This Xcode/CoreDevice toolchain does not report 'devicectl device info displays'. Update Xcode to a version that ships the display-information feature before targeting multi-display Apple devices.`}:{}}})}function y(){return{displays:[],multiScreen:!1,ambiguous:!1,unresolved:!0}}function b(e){let t=e?.result?.displays;if(!Array.isArray(t))return[];let n=[];for(let e of t){if(!e||typeof e!=`object`)continue;let t=x(e);t&&n.push(t)}return n}function x(e){let t=S(e);if(t)return{...t,power:D(e.backlightState,e.active),primary:e.primary===!0,currentOrientation:typeof e.currentOrientation==`string`?e.currentOrientation:`unknown`,integrated:O(e.type)}}function S(e){let t=C(e.name),n=T(e.displayId),r=E(e.pointScale),i=w(e.nativeSize);if(!(!t||n===void 0||r===void 0||!i))return{name:t,displayId:n,pointScale:r,widthPx:i.widthPx,heightPx:i.heightPx}}function C(e){if(typeof e!=`string`)return;let t=e.trim();return t.length>0?t:void 0}function w(e){if(!Array.isArray(e))return;let t=E(e[0]),n=E(e[1]);if(t!==void 0&&n!==void 0)return{widthPx:t,heightPx:n}}function T(e){return typeof e==`number`&&Number.isInteger(e)?e:void 0}function E(e){return typeof e==`number`&&Number.isFinite(e)&&e>0?e:void 0}function D(e,t){return typeof e==`string`?c.has(e)?`lit`:l.has(e)?`dark`:`unknown`:t===!0?`lit`:t===!1?`dark`:`unknown`}function O(e){return typeof e==`object`&&!!e&&Object.hasOwn(e,`integrated`)}function k(e){let t=e.filter(e=>e.integrated);if(t.length<2)return{displays:e,multiScreen:!1,ambiguous:!1,unresolved:!1};j(t);let n=t.filter(e=>e.power===`lit`),i=t.find(e=>e.primary),a=n.length===1,o=a?n[0]:i,s=!a||i===void 0;return s&&r({level:`warn`,phase:`apple_display_capture_ambiguous`,data:{reason:i===void 0?`no-primary-panel`:`lit-panel-count`,litPanelCount:n.length,...o?{capturedDisplay:o.name,hint:`Panel power did not identify exactly one lit panel, so the capture names the primary panel without claiming to know which panel the device is showing. Read the device pose from the app under test with UIHinge.status.`}:{hint:A}}}),{displays:t,multiScreen:!0,...o?{activeDisplay:o}:{},ambiguous:s,unresolved:!1}}const A=`The panel table named no primary panel and panel power did not identify exactly one lit panel. Retry after one panel reports a lit backlightState; agent-device help foldable names the panel table source for each device set.`;function j(e){let t=e.find(e=>e.primary);if(!t)return;let n=e=>e.widthPx*e.heightPx,i=e.filter(e=>e!==t).reduce((e,t)=>n(t)>n(e)?t:e);n(t)<n(i)||r({level:`warn`,phase:`apple_display_primary_geometry_conflict`,data:{primaryDisplay:t.name,largestDisplay:i.name,hint:"The panel table reported the primary panel as at least as large as the other panel. Its `primary` flag still selects the capture fallback; verify the device type capabilities."}})}function M(e){if(!e.multiScreen||e.unresolved||!e.activeDisplay||e.displays.some(e=>e.power===`unknown`))return;let t=e.displays.filter(e=>e.power===`lit`);if(t.length===1&&t[0]===e.activeDisplay)return t[0]}function N(e){let t=M(e);if(!(!t||t.displayId<=0))return{displayId:t.displayId,widthPt:t.widthPx/t.pointScale,heightPt:t.heightPx/t.pointScale}}async function P(e,t={}){return(await F(e,t)).display}async function F(e,n={}){let r=await f(e,n);if(r.unresolved)return{};if(r.multiScreen){if(r.activeDisplay)return{display:r.activeDisplay};throw new t(`COMMAND_FAILED`,`The panel table did not identify which panel of ${e.name} to capture`,{deviceId:e.id,reason:`capture-panel-unidentified`,litPanelCount:r.displays.filter(e=>e.power===`lit`).length,hint:A})}let i=r.displays.find(e=>e.integrated),a=i?R(i):void 0;return a?{displayRotation:a}:{}}const I=new Map([[`rot0`,`portrait`],[`rot90`,`landscape-right`],[`rot180`,`portrait-upside-down`],[`rot270`,`landscape-left`]]);function L(e){return I.get(e)}function R(e){return L(e.currentOrientation)}function z(e){return e?[`--display=${e.name}`]:[]}export{d as a,F as c,M as i,z as n,L as o,s as r,f as s,N as t};