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.41 kB
import{x as e}from"./sdk-batch.js";import{B as t,D as n,O as r,S as i,ot as a,v as o}from"./sdk-contracts.js";import{St as s,q as c}from"./sdk-io.js";import"./diagnostics.js";import"./retry.js";import{r as l}from"./snapshot-occlusion.js";import{n as u}from"./provider-device-admission.js";import{o as d}from"./snapshot-runtime-capture-input.js";const f=/\bis(?:n(?:'|&apos;|&#39;)?t| not)\s+responding\b/i,p=/^close app$/i,m=`Wait for Android to recover, close the dialog, restart the app, or reboot the emulator, then retry.`;function h(e){if(!e)throw new o(`INTERNAL_ERROR`,`Android observation was not supplied by root runtime composition`);return e}async function g(e){let{session:n}=e;if(n.device.platform!==`android`||!n.screenRecording||M(n))return{status:`absent`};let r=h(e.observation),i;try{i=await T(n,r)}catch(e){let r=t(e);return s({level:`warn`,phase:`android_blocking_dialog_inspection_failed`,data:{session:n.name,deviceId:n.device.id,error:e instanceof Error?e.message:String(e)}}),{status:`unknown`,reason:`inspection-failed`,warning:C(r)}}let a=D(i);if(!a?.rect)return{status:`absent`};try{let e=await E(n,a,r);return e.ok?await k(n,r)?n.appBundleId&&(await r.openApp(n.device,n.appBundleId),!await A(n,n.appBundleId,r))?(s({level:`warn`,phase:`android_blocking_dialog_relaunch_unfocused`,data:{session:n.name,deviceId:n.device.id,appBundleId:n.appBundleId}}),{status:`failed`,reason:`relaunch-failed`}):(s({level:`warn`,phase:`android_blocking_dialog_recovered`,data:{session:n.name,deviceId:n.device.id,appBundleId:n.appBundleId,x:e.x,y:e.y}}),{status:`recovered`}):(s({level:`warn`,phase:`android_blocking_dialog_still_present`,data:{session:n.name,deviceId:n.device.id}}),{status:`failed`,reason:`dismiss-failed`}):(s({level:`warn`,phase:`android_blocking_dialog_tap_failed`,data:{session:n.name,deviceId:n.device.id,exitCode:e.exitCode,stdout:e.stdout.trim(),stderr:e.stderr.trim()}}),{status:`failed`,reason:`tap-failed`})}catch(e){return s({level:`warn`,phase:`android_blocking_dialog_recovery_failed`,data:{session:n.name,deviceId:n.device.id,error:e instanceof Error?e.message:String(e)}}),{status:`failed`,reason:`error`}}}async function _(e){let{session:t,command:n}=e;if(t.device.platform!==`android`||M(t))return{status:`clear`};let r=h(e.observation),i=await r.readBlockingDialog(t.device);if(i.status!==`dialog`)return i.status===`unknown`&&s({level:`warn`,phase:`android_blocking_dialog_unobserved`,data:{session:t.name,deviceId:t.device.id,command:n,commandPhase:e.phase}}),{status:`clear`};let a=i.focus;if(y(t,a)){if(await v(t,r)){let r=`Recovered Android app ANR before ${n}: closed and relaunched ${t.appBundleId}.`;if(e.phase===`before-command`)return{status:`recovered`,warning:r};throw x({session:t,command:n,focus:a,message:`Android app ANR appeared after ${n}; ${t.appBundleId} was closed and relaunched. Retry the command against the fresh app session.`,hint:`Retry the command. If the ANR returns, inspect app logs or restart the emulator.`})}throw x({session:t,command:n,focus:a,message:`Android app ANR blocked ${n}: ${S(a)}. Automatic recovery failed.`,hint:m})}throw x({session:t,command:n,focus:a,message:`Android system dialog is blocking ${n}: ${S(a)}.`,hint:m})}async function v(e,t){try{return await b(e,t)}catch(t){return s({level:`warn`,phase:`android_app_anr_recovery_failed`,data:{session:e.name,deviceId:e.device.id,appBundleId:e.appBundleId,error:t instanceof Error?t.message:String(t)}}),!1}}function y(e,t){return!!(e.appBundleId&&t.package===e.appBundleId)}async function b(e,t){if(!e.appBundleId)return!1;let n=D(await T(e,t),{requireDialogSignal:!1});if(!n?.rect)return!1;let r=await E(e,n,t);if(!r.ok)return!1;await t.openApp(e.device,e.appBundleId);let i=await A(e,e.appBundleId,t,{requireNoBlockingDialog:!0});return i&&s({level:`warn`,phase:`android_app_anr_recovered`,data:{session:e.name,deviceId:e.device.id,appBundleId:e.appBundleId,x:r.x,y:r.y}}),i}function x(e){let{session:t,command:n,focus:r,message:i,hint:a}=e;return new o(`COMMAND_FAILED`,i,{command:n,expectedPackage:t.appBundleId,focusedPackage:r.package,focusedWindow:r.focusedWindow,rawFocus:r.raw,hint:a})}function S(e){return e.package?`${e.focusedWindow} (package ${e.package})`:e.focusedWindow}function C(e){let t=[`Inspection error: ${w(e.message)}`];return e.hint&&t.push(`Hint: ${w(e.hint)}`),[`Android blocking-dialog readiness could not be inspected; the command continued.`,...t].join(` `)}function w(e){let t=e.replaceAll(/\s+/g,` `).trim();return t.length<=240?t:`${t.slice(0,239)}…`}async function T(e,t){return await t.readSnapshotNodes(e.device)}async function E(e,t,n){if(!t.rect)return{ok:!1,exitCode:1,stdout:``,stderr:`button has no rect`};let{x:r,y:i}=a(t.rect);d(e);let o=await n.tap(e.device,r,i);return o.exitCode===0?{ok:!0,x:r,y:i}:{ok:!1,exitCode:o.exitCode,stdout:o.stdout.trim(),stderr:o.stderr.trim()}}function D(e,t={}){if(!(t.requireDialogSignal!==!1&&!F(e)))return e.filter(O).find(e=>P(e).some(e=>p.test(e))&&e.rect)}function O(e){return!l(e)}async function k(e,t){for(let n=0;n<12;n+=1){if(!F(await T(e,t)))return!0;await c(500)}return!F(await T(e,t))}async function A(e,t,n,r={}){for(let i=0;i<12;i+=1){if(await j(e,t,n,r))return!0;await c(500)}return await j(e,t,n,r)}async function j(e,t,n,r){return await n.readAppFocus(e.device,t,r)}function M(e){return!!e.lease?.leaseProvider||u(e.device)}function N(e){return P(e).join(` `).trim()}function P(e){let t=[e.label,e.identifier];return typeof e.value==`string`&&e.value.trim().length>0&&t.push(e.value),t.filter(e=>typeof e==`string`&&e.trim().length>0).map(e=>e.trim())}function F(e){return e.filter(O).some(e=>{let t=N(e);return t.length>0&&f.test(t)})}async function I(t,n,r){let a=e(t);try{let e=await r();if(!e||e.ok)return e;let t=L(a,e.error.details,n);return t===e.error.details?e:{...e,error:{...e.error,details:t}}}catch(e){let t=i(e),r=L(a,t.details,n);throw r===t.details?e:(t.details=r,t)}}function L(e,t,r){if(r.dispatchedSteps>0){let e=n(t,r.dispatchedSteps);return t?.reason===`bounded_composite_resource_unsupported`&&t.dispatched===`no`?{...e,dispatched:`no`}:e}return e===void 0?t:e===`no`?t?.dispatched===`no`?t:{...t,dispatched:`no`}:t?.dispatched===void 0?{...t,dispatched:`unknown`}:t}function R(e){return e.ok?e:{...e,error:{...e.error,details:{...e.error.details,dispatched:`no`}}}}function z(e){return r(i(e),`no`)}export{g as a,_ as i,R as n,z as r,I as t};