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 • 6.84 kB
import{un as e}from"./sdk-batch.js";import{s as t,v as n}from"./sdk-contracts.js";import{w as r,x as i}from"./sdk-device.js";import"./session-store.js";import{c as a}from"./device-shell.js";import{n as o}from"./provider-device-admission.js";import{A as s,g as c,s as l,t as u}from"./snapshot-runtime-capture-input.js";import{n as d}from"./request-runtime-binding.js";import{t as f}from"./platform-request-scope.js";import{n as p}from"./request-device-resolution.js";import{a as m}from"./runtime-session.js";import{a as h,i as g,n as _,t as v}from"./session-runtime-admission.js";async function y(e,t){if(!r(e)||e.kind!==`simulator`)return;let n=await t.appleSessionObservation.resolveSoleForegroundApp({simulatorSetPath:e.simulatorSetPath});if(!n)return;let i=b(n.device,n.app.bundleId),a=`One booted device found ("${n.device.name}", udid ${n.device.id}) with ${n.app.bundleId} running. Run: ${i}`;return a.length<=350?a:void 0}function b(e,t){let n=e.simulatorSetPath?` --ios-simulator-device-set ${a(e.simulatorSetPath)}`:``;return`agent-device open ${t} --platform ios --udid ${e.id}${n}`}async function x(e,t,n,r){let i=r??e.lookup(t),a=i?e.requireCurrent(i):void 0;if(!a){let{requireNamedSessionHoldsDevice:e}=await import(`./explicit-session-resolution.js`);e({req:n,sessionAddress:t,session:a})}return{ref:i,session:a}}async function S(e,t,n,r){let{ref:i,session:a}=await x(e,t,n,r);return{ref:i,session:a,device:a?.device??await p(n,{env:e.host.env})}}async function C(e,t,n,r){if(!e&&!r)throw Error(`Platform resource cleanup was not injected`);try{return await n()}finally{!e&&!o(t)&&await r.cleanupSessionlessExecutionHost(t)}}function w(e){let{sessionName:t,sessionScope:n,device:r,snapshot:i,appBundleId:a}=e;return{name:t,sessionScope:n,device:r,createdAt:Date.now(),appBundleId:a,snapshot:i,...i?.comparisonSafe===!0?{lastComparisonSafeSnapshot:i}:{},actions:[]}}function T(e){let{readTextAtPoint:t}=e.operations,n=t?{operations:{readTextAtPoint:t}}:void 0;return Object.freeze(n?{readTextAtPoint:async e=>await n.operations.readTextAtPoint(e)}:{})}function E(e){let{findText:t}=e.operations;return Object.freeze(t?{findText:async e=>{e.signal?.throwIfAborted();try{let n=await t(e);return e.signal?.throwIfAborted(),n}catch{return e.signal?.throwIfAborted(),{found:!1}}}}:{})}function D(e){let{focusPoint:t,typeText:n}=e.operations;return Object.freeze({...t?{focusPoint:t}:{},...n?{typeText:n}:{}})}async function O(e){let{command:t,device:n,session:r,plan:i}=e,a=await v({device:n,plan:i,inspectFacts:e.inspectFacts});if(!a.admitted)return{ok:!1,response:await R({operation:a.operation,fact:a.fact,session:r,device:n,command:t,platformServices:e.platformServices})};let o=await M(e.bindDevice,a,e.readinessScope,e.platformServices);return Object.freeze({ok:!0,capture:async e=>await o.captureSnapshot(e),...o.readTextAtPoint?{readTextAtPoint:o.readTextAtPoint}:{},...o.findText?{findText:o.findText}:{},...o.focusPoint?{focusPoint:o.focusPoint}:{},...o.typeText?{typeText:o.typeText}:{}})}function k(e){return e?.device.platform===`android`&&e.snapshot!==void 0&&!l(e.snapshot)&&Date.now()-e.snapshot.createdAt>=500&&!s(e)&&!c(e)}async function A(t,r){let{req:a,sessionName:o,sessionStore:s}=t,{ref:c,session:l,device:d}=await S(s,o,a,t.sessionRef);if(a.flags?.snapshotObserveOnly===!0&&(r!==`snapshot`||!i(d)||!l?.appBundleId))throw new n(`UNSUPPORTED_OPERATION`,`observe-only snapshot requires an iOS or iPadOS app session.`);let p=m(a.flags?.snapshotScope,l);if(!p.ok)return{ok:!1,response:p};let h=await O({command:r,device:d,session:l,plan:e({customActions:a.flags?.snapshotCustomActions===!0,hasActiveApp:l?.appBundleId!==void 0}),inspectFacts:t.inspectFacts,platformServices:t.platformServices,bindDevice:t.bindDevice,readinessScope:l?void 0:f(a)});if(!h.ok)return h;let g=u({flags:a.flags,logPath:t.logPath,meta:a.meta,session:l,snapshotScope:p.scope});return Object.freeze({ok:!0,ref:c,session:l,device:d,snapshotScope:p.scope,captureSnapshot:async()=>await h.capture(k(l)?{...g,options:{...g.options,captureBasis:`unchanged-since-observation`}}:g)})}async function j(e){let t=await M(e.bindDevice,e.admission,void 0,e.platformServices);return async e=>await t.captureSnapshot(e)}async function M(e,t,n,r){let i=_(e),a=async(e,t)=>{let a=await i(e,t);return n&&await d(a,r,n),a},{device:o,plan:s}=h(t);switch(s.kind){case`active-app`:return F(await a(o,s.use));case`selector-active-app`:return await N(a,o,s);case`custom-actions-active-app`:return I(await a(o,s.use));case`without-active-app`:return L(await a(o,s.use));case`selector-without-active-app`:return await P(a,o,s);case`custom-actions-without-active-app`:return I(await a(o,s.use))}}async function N(e,t,n){switch(n.intent){case`capture-only`:return F(await e(t,n.use));case`element-text`:{let r=await e(t,n.use);return{...F(r),...T(r)}}case`wait-observation`:{let r=await e(t,n.use);return{...F(r),...E(r)}}case`find-focus`:{let r=await e(t,n.use);return{...F(r),...D(r)}}case`find-type`:{let r=await e(t,n.use);return{...F(r),...D(r)}}}}async function P(e,t,n){switch(n.intent){case`capture-only`:return L(await e(t,n.use));case`element-text`:{let r=await e(t,n.use);return{...L(r),...T(r)}}case`wait-observation`:{let r=await e(t,n.use);return{...L(r),...E(r)}}case`find-focus`:{let r=await e(t,n.use);return{...L(r),...D(r)}}case`find-type`:{let r=await e(t,n.use);return{...L(r),...D(r)}}}}function F(e){return Object.freeze({captureSnapshot:async t=>await e.operations.captureSnapshot(t)})}function I(e){return Object.freeze({captureSnapshot:async t=>await e.operations.captureSnapshotWithCustomActions(t)})}function L(e){return Object.freeze({captureSnapshot:async t=>await e.operations.captureSnapshotWithoutActiveApp(t)})}async function R(e){if(e.operation===`captureSnapshot`||e.operation===`focusPoint`||e.operation===`typeText`)return g(e.command,e.fact);if(e.operation===`captureSnapshotWithCustomActions`)return z(e);let n=await y(e.device,e.platformServices);return t(`SESSION_NOT_FOUND`,`iOS ${e.command} requires an active app session on the target device. Run open first (for example: open --session ${e.session?.name??`sim`} --platform ios --device "<name>" <app>).`,{reason:`ios_app_session_required`,...n?{hint:n}:{}})}function z(e){let n=e.command===`diff`?`--actions requires an iOS simulator: custom actions are read through the private accessibility snapshot backend, which ${e.device.platform}/${e.device.kind} targets do not have.`:`--actions requires an iOS simulator: custom actions are unavailable on this ${e.device.platform}/${e.device.kind} target.`;return t(`UNSUPPORTED_OPERATION`,n,{reason:e.fact.available?void 0:e.fact.reason},{hint:e.command===`diff`||e.fact.available||!e.fact.hint?`Re-run without --actions, or target an iOS simulator.`:e.fact.hint})}export{x as a,w as i,j as n,S as o,A as r,C as s,O as t};