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 • 1.96 kB
import{v as e}from"./sdk-contracts.js";import{q as t}from"./sdk-io.js";import{T as n,w as r}from"./sdk-device.js";import{d as i,l as a,m as o,n as s,o as c}from"./alert-contract.js";import"./retry.js";import{t as l}from"./runner-client.js";import{a as u}from"./helper.js";function d(e,t,r,i){if(n(e)){let e=r?.surface===`frontmost-app`?{surface:r.surface}:{bundleId:r?.appBundleId,surface:r?.surface};return async t=>await u(t,{...e,env:i})}return async(n,i)=>await l(e,{command:`alert`,action:n,appBundleId:r?.appBundleId,timeoutMs:g(i)},t)}async function f(e){try{return await e}catch(e){if(y(e))return;throw e}}async function p(e,t,n,r){let i=g(n?.timeoutMs,c);return await d(e,t,n,r)(`get`,i)}async function m(n,r,a,o){let s=d(n,r,a,o),c=a?.timeoutMs??1e4,l=Date.now(),u;for(;Date.now()-l<c;){let e=_(l,c);if(u&&e<=300){await t(e);break}try{return await s(`get`,u?e:c)}catch(e){if(!x(e))throw e;u=e}let n=c-(Date.now()-l);n>0&&await t(Math.min(300,n))}throw b(u)?u:new e(`COMMAND_FAILED`,`alert wait timed out`,{...u?.details,reason:i})}async function h(e,n,i,a,l){let u=d(e,n,a,l),f=r(e)?o:s,p=Date.now(),m,h=!0;for(;Date.now()-p<s;){try{let e=h?f:Math.max(_(p,s),c);return h=!1,await u(i,e)}catch(e){if(m=e,!x(e))break}await t(300)}throw v(m)}function g(e,t=0){return Math.min(e===void 0||!Number.isFinite(e)?o:Math.max(e,t),a)}function _(e,t){return Math.max(1,t-(Date.now()-e))}function v(t){return!(t instanceof e)||!y(t)?t:new e(t.code,t.message,{...t.details??{},hint:`If the permission sheet is visible in snapshot or screenshot but alert reports no alert, take a scoped snapshot around the visible button label and use press @ref.`})}function y(t){if(!(t instanceof e))return!1;let n=t.details??{};return n.runnerErrorCode===`ALERT_NOT_FOUND`||n.reason===`alert-not-found`}function b(t){return t instanceof e&&t.details?.runnerErrorCode===`ALERT_UNRESOLVED`}function x(t){return t instanceof e&&(y(t)||b(t))}export{p as i,f as n,m as r,h as t};