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 • 3.74 kB
JavaScript
import{C as e,v as t}from"./sdk-contracts.js";import{G as n,H as r}from"./sdk-io.js";import{r as i}from"./success-text.js";import{a,d as o,i as s,m as c,n as l,p as u,s as d}from"./alert-contract.js";import"./retry.js";async function f(e,n){if(e===`get`)return x(n.platform,await p(n));if(e===`wait`){let e=Date.now(),a=(await m(n,r.fromTimeoutMs(C(n.timeoutMs)))).value;if(a.kind!==`alert`)throw new t(`COMMAND_FAILED`,`alert wait timed out`,{reason:o});return{kind:`alertWait`,platform:n.platform,action:`wait`,alert:a.candidate.alert,waitedMs:Date.now()-e,...i(`Alert visible`)}}return await h(e,n)}async function p(t){if(t.signal?.aborted)throw e();let n;try{n=await t.capture({signal:t.signal??S})}catch(n){throw t.signal?.aborted?e({dispatched:`no`},n):n}return t.findCandidate(n)}async function m(e,t,r=e=>e.kind===`alert`){let i=!1,{settled:a,value:o}=await n({probe:()=>p(e),settled:e=>(e.kind===`undecided`&&(i=!0),r(e)),deadline:t,intervalMs:300,signal:e.signal});return{settled:a,value:o,sawUndecided:i}}async function h(n,r){let a=await g(n,r);if(r.signal?.aborted)throw e({dispatched:`no`});let o=a.candidate,c=r.choose(o,n);if(!c)throw new t(`COMMAND_FAILED`,`alert ${n} found an alert but no button that clearly matches the action`,{reason:s,dispatched:`no`,alert:o.alert,hint:`Inspect alert get --json for the visible buttons, then press the choice you want by label/ref.`});try{await r.press(c,{signal:r.signal??S}),await _(r,o.alert,n)}catch(t){throw r.signal?.aborted?e({dispatched:`unknown`},t):t}return{kind:`alertHandled`,platform:r.platform,action:n,handled:!0,alert:o.alert,button:c.kind===`press`?c.button.label:c.label,...c.kind===`press`?{coordinates:{x:c.button.x,y:c.button.y}}:{},...i(`Alert ${n}ed`)}}async function g(e,t){let n=await m(t,r.fromTimeoutMs(l)),i=n.settled?n.value:n.sawUndecided?{kind:`undecided`}:{kind:`none`};if(i.kind!==`alert`)throw v(e,i);return i}async function _(e,n,i){let a=await m(e,r.fromTimeoutMs(l),e=>e.kind===`none`||e.kind===`alert`&&!b(e.candidate.alert,n));if(!a.settled)throw a.value.kind===`alert`&&b(a.value.candidate.alert,n)?y(i,n):new t(`COMMAND_FAILED`,`alert ${i} pressed the button but the dismissal could not be confirmed`,{alert:n,reason:u,hint:`The alert button was pressed; the post-press checks never settled the dialog as gone, so it may already be gone. Run alert get --json before retrying.`})}function v(e,n){return n.kind===`undecided`?new t(`COMMAND_FAILED`,`alert ${e} refused: the capture is alert-shaped but cannot be resolved to one alert`,{reason:a,dispatched:`no`,hint:`Inspect alert get --json and snapshot -i, then press the visible choice by label/ref.`}):new t(`COMMAND_FAILED`,`alert not found`,{reason:d,dispatched:`no`,hint:`If a sheet is visible in snapshot but alert reports no alert, alert did not recognize it. Use snapshot -i and press @ref on the visible button.`})}function y(e,n){return new t(`COMMAND_FAILED`,`alert ${e} did not dismiss the visible alert`,{alert:n,reason:u,hint:`The alert button was pressed but the dialog was visible on the last check that answered. Inspect alert get --json, then press the visible button by label/ref or retry.`})}function b(e,t){return e.title===t.title&&e.buttons.join(`\0`)===t.buttons.join(`\0`)}function x(e,t){return{kind:`alertStatus`,platform:e,action:`get`,alert:t.kind===`alert`?t.candidate.alert:null,status:t.kind===`alert`?`visible`:t.kind===`undecided`?`undecided`:`none`,...t.kind===`alert`?i(`Alert visible`):t.kind===`undecided`?i(`Cannot decide the alert: the capture is alert-shaped but unresolvable`):i(`No alert visible`)}}const S=new AbortController().signal;function C(e){return typeof e==`number`&&Number.isFinite(e)&&e>=0&&e<=2147483647?e:c}export{f as runTreeAlertAction};