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 • 4.94 kB
import{Mn as e}from"./sdk-batch.js";import{v as t}from"./sdk-contracts.js";import{St as n}from"./sdk-io.js";import"./diagnostics.js";import{V as r}from"./sdk-device.js";import{f as i,x as a}from"./device.js";import{t as o}from"./fold-runtime.js";import{c as s,s as c}from"./config.js";import{i as l}from"./simulator.js";import{s as u}from"./display-inventory.js";import{n as d}from"./fold-helper.js";async function f(e,t,r={}){r.signal?.throwIfAborted(),l(e,`fold`);let i=t,o=i.keyframes?.at(-1)?.angle,s=i.pose??a(o),c=d(e);h(e,await u(e,{signal:r.signal,foldHelper:c})),await c.setPose(i.keyframes??s,r.signal),n({level:`info`,phase:`apple_fold_pose_dispatched`,data:{deviceId:e.id,pose:s}});let f=await g(e,c,s,r.signal,o),p=await T(e,c,r.signal);return{pose:a(f),hingeAngleDegrees:f,...p?{screen:E(p)}:{}}}function p(e){return e instanceof t&&e.details?.reason===`fold-hinge-angle-no-sample`}async function m(e,n,r,i,a,o){let s=await T(e,n,i);return new t(`COMMAND_FAILED`,`${e.name} received the ${r} pose request, but the first hinge-angle read returned no sample to verify it`,{deviceId:e.id,requestedPose:r,...a===void 0?{}:{targetAngleDegrees:a},reason:`fold-pose-dispatched-unverified`,...s?{screen:E(s)}:{},...o instanceof t?{hingeReadFailure:o.details}:{},hint:`The pose request was sent and the device may have followed it, but the first hinge-angle read returned no sample, so fold cannot confirm it. Take a screenshot or snapshot to see which panel is lit; hingeReadFailure names why the read failed.`},o)}function h(e,n){if(n.unresolved)throw new t(`COMMAND_FAILED`,`The simulator reported no panel table`,{deviceId:e.id,hint:`fold needs the simulator's integrated displays to identify a foldable: the default set uses CoreDevice and a scoped set uses the fold helper. The apple_display_inventory_unresolved diagnostic names why the read failed.`});if(!n.multiScreen)throw new t(`UNSUPPORTED_OPERATION`,`${e.name} is not a foldable simulator: fold requires more than one integrated panel`,{deviceId:e.id,reason:`single-panel-device`,hint:`fold sets the hinge angle of a foldable simulator such as iPhone Duo; this simulator reports one integrated panel, so it has no hinge to pose.`})}async function g(e,t,n,r,i){let a,o;for(let s=1;s<=8;s+=1){r?.throwIfAborted(),o=a;try{a=await t.readHingeAngle({signal:r,windowMs:s>1?c:0})}catch(o){throw a!==void 0||!p(o)?o:await m(e,t,n,r,i,o)}if(_(a,o,n,i))return a}throw b(e,n,a,o,i)}function _(e,t,n,r){return r===void 0?a(e)===n?n!==`half-open`||t!==void 0&&x(e,t):!1:v(e,t,r)}function v(e,t,n){return y(e,n)?n===0||n===180||t!==void 0&&y(t,n)&&Math.abs(e-t)<=.5:!1}function y(e,t){return t===void 0||e!==void 0&&Math.abs(e-t)<=.5}function b(e,n,r,i,o){return y(r,o)?o!==void 0&&r!==void 0?w(e,a(r),r,i):r!==void 0&&n===`half-open`&&a(r)===n?w(e,n,r,i):C(e,n,r):new t(`COMMAND_FAILED`,`The hinge did not reach the final keyframe angle`,{reason:`fold-angle-unverified`,targetAngleDegrees:o,hingeAngleDegrees:r,deviceId:e.id})}function x(e,t){return a(e)===`half-open`&&a(t)===`half-open`&&Math.abs(e-t)<=.5}function S(e,t,n){return{deviceId:e.id,requestedPose:t,observedPose:n===void 0?void 0:a(n),hingeAngleDegrees:n}}function C(e,n,r){return new t(`COMMAND_FAILED`,`${e.name} did not reach the ${n} pose: the simulator still reports a hinge angle of ${r}°`,{...S(e,n,r),reason:`fold-pose-unverified`,hint:`The simulator HID command completed, but the hinge did not follow. Verify that the selected Xcode and simulator runtime support foldable HID control, then retry.`})}function w(e,n,r,i){return new t(`COMMAND_FAILED`,`${e.name} was observed ${n} at ${r}° but did not settle: 8 hinge reads never held two consecutive angles within ${s}° of each other`,{...S(e,n,r),reason:`fold-pose-unsettled`,...i===void 0?{}:{previousHingeAngleDegrees:i},hint:`The hinge reached the requested pose and was still moving when the read budget ended. Retry the fold to see whether the simulator holds the pose.`})}async function T(e,t,n){let r=await u(e,{signal:n,source:`guest`,foldHelper:t});if(!(r.unresolved||r.ambiguous))return r.activeDisplay}function E(e){return{display:e.name,coordinateSpace:i,widthPt:Math.round(e.widthPx/e.pointScale),heightPt:Math.round(e.heightPx/e.pointScale)}}const D=Object.freeze({available:!0}),O=Object.freeze({available:!1,reason:`unsupported-device-kind`,hint:`fold is supported on foldable iPhone simulators controlled through simulator HID; a physical device is folded by hand.`}),k=Object.freeze({available:!1,reason:`unsupported-platform-leaf`,hint:`fold poses the hinge of a foldable iPhone; tvOS, macOS, watchOS and visionOS simulators have no hinge.`});function A(e){if(e.kind!==`simulator`)return O;let t=r(e);return t!==`ios`&&t!==`ipados`?k:D}function j(e){return o({fold:A(e)})}function M(t){let{device:n,signal:r}=t;return e(j(n).setFoldPose,()=>({setFoldPose:async e=>(r.throwIfAborted(),await f(n,e,{signal:r}))}))}export{M as n,j as t};