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.92 kB
import{a as e}from"./snapshot-capture-annotations.js";import"./capture.js";import{St as t,q as n}from"./sdk-io.js";import"./diagnostics.js";import"./retry.js";const r=2500,i=5e3,a=1500,o=[250,400,600];function s(e){return e?.cacheInvalidated===!0&&e.rootPresent===!0&&e.helperTruncated===!1&&e.missingRootWindowTypes?.length===0}function c(e){return e===`press`||e===`click`||e===`back`||e===`open`}function l(e){return e.map(e=>[e.depth??0,e.type??``,e.label??``,e.value??``,e.identifier??``,e.enabled===!1?`disabled`:`enabled`,e.hittable===!0?`hittable`:`not-hittable`].join(`|`))}function u(e,t){return e<12?!1:t<=Math.floor(e*.2)}function d(e,t,n){return n.interactiveOnly&&e.snapshot.nodes.length===0&&e.rawNodeCount!==void 0&&e.rawNodeCount>=12?`empty-interactive`:n.mode===`ref-refresh`?null:u(t.baselineCount,e.snapshot.nodes.length)?f(e.snapshot)?null:`sharp-drop`:e.cacheInvalidated===!0?null:t.routeComparable&&c(t.action)&&p(t.baselineSignatures,e.snapshot.nodes)?`stuck-route`:null}function f(e){return e.nodes.some(e=>e.hittable===!0||!!e.label?.trim()||!!e.value?.trim()||!!e.identifier?.trim())}function p(e,t){if(!e||e.length===0)return!1;let n=Math.max(e.length,t.length);if(n<12)return!1;let r=l(t),i=Math.min(e.length,r.length),a=0;for(let t=0;t<i;t+=1)e[t]===r[t]&&(a+=1);let o=Math.max(0,r.length-e.length),s=Math.max(0,e.length-r.length),c=Math.max(3,Math.floor(n*.15));return a>=Math.floor(n*.9)&&o<=c&&s<=c}async function m(t){let r=t.window.markedAt+t.retry.retryBudgetMs,i=await t.capture(),a=t.classify(i),o=0;for(let e of t.retry.delaysMs){if(!a)break;let s=r-Date.now();if(s<=0)break;await n(Math.min(e,s)),i=await t.capture(),o+=1,a=t.classify(i)}a||t.onTrustworthyCapture?.();let s=o>0||a?{action:t.window.action,retryCount:o,staleAfterRetries:!!a,reason:a??void 0}:void 0;return{...i,annotations:{...i.annotations,...e({freshness:s})}}}const h={now:()=>Date.now(),sleep:e=>new Promise(t=>setTimeout(t,e))};async function g(e){let t=e.clock??h,n={params:e,clock:t,state:{startedMs:t.now(),budgetMs:e.schedule.budgetMs,observations:0,polls:[],previous:void 0,last:void 0}};if(e.initial!==void 0){if(_(n,`hard`)<=0)return S(n,{kind:`deadline`,last:void 0});n.state.observations+=1;let t=x(n,e.initial);if(t)return t}for(;;){let e=await y(n);if(e)return e;let t=await b(n);if(t)return t}}function _({state:e,clock:t,params:n},r){return r?(n.deadlineMs??1/0)-t.now():e.startedMs+e.budgetMs-t.now()}function v({params:e,state:t}){return t.observations<(e.schedule.minPolls??1)}async function y(e){let{state:t,clock:n,params:r}=e,i=()=>S(e,{kind:`deadline`,last:t.last});if(_(e,`hard`)<=0)return i();if(t.observations===0)return;let a=v(e);if(!a&&_(e)<=0)return S(e,{kind:`expired`,last:t.last});let{intervalMs:o}=r.schedule,s=a?o:Math.max(0,Math.min(o,_(e)));return s>=_(e,`hard`)?i():(await n.sleep(s),_(e,`hard`)>0?void 0:i())}async function b(e){let{params:t,clock:n,state:r}=e,i=n.now(),a=e=>{r.observations+=1,r.polls.push({startedMs:i-r.startedMs,durationMs:n.now()-i,outcome:e})},o;try{o=await t.capture()}catch(t){return a(`failed`),S(e,{kind:`failed`,last:r.last,error:t})}return a(`observed`),_(e,`hard`)<=0?S(e,{kind:`deadline`,last:r.last}):x(e,o)}function x(e,t){let{params:n,state:r}=e,i=n.verdict(t,r.previous,r.polls.length);if(r.previous=t,r.last=t,i.kind===`done`)return S(e,{kind:`done`,result:i.result,value:t});i.budgetMs!==void 0&&(r.budgetMs=Math.max(r.budgetMs,i.budgetMs))}function S(e,t){let{state:n,clock:r}=e;return{...t,polls:n.polls,waitedMs:r.now()-n.startedMs}}const C={intervalMs:200,budgetMs:1500,minPolls:2},w=C.budgetMs+2e3,T={intervalMs:C.intervalMs,budgetMs:1e4,minPolls:2};function E({kind:e,gesture:t}){let n=[t.action,...t.positionals].join(` `).trim();return e===`loading`?`The app was still loading after ${n} when this tree was read: it may show a launch or loading screen. Run snapshot -i again, or wait for expected content (wait text ...) before acting.`:e===`unsettled`?`The surface was still changing after ${n} when this tree was read, so it may not match where the surface comes to rest: an element missing from it is not proof of absence.`:`${n} produced no visible change: the tree still matches its pre-gesture state. Either the container is already at its edge, or it ignores synthesized scrolls — a raw drag moves such lists: swipe x1 y1 x2 y2 (start inside the list).`}function D(e,t){return{kind:e,gesture:{action:t.action,positionals:t.positionals}}}function O(e){let{baseline:t,quiet:n,elapsedMs:r,distrustCapMs:i}=e;return t?.provenance!==`pre-action`||t.signature.length===0||e.classifyBaselineEvidence(t,n)!==`unchanged`?{verdict:`trust`}:r<i?{verdict:`distrust`}:{verdict:`accept-stale`,preActionBaseline:t}}async function k(e){let{pending:n,hooks:r,clock:i}=e,a=()=>i?.now()??Date.now(),o=a(),s=0,c=n.baseline,l=!1,u=n.readiness===`first-screen`,d,f=e=>(d?.value===e||(d={value:e,...r.readSurface(e)}),d),p=await g({...e.initial===void 0?{}:{initial:e.initial},capture:()=>r.capture(),schedule:u?T:C,...i?{clock:i}:{},verdict:(e,i)=>{s+=1;let d=f(e);if(i===void 0)return{kind:`continue`};let p=f(i);if(l=r.signaturesStable(p.signature,d.signature),!l)return{kind:`continue`};let m=a()-o;if(u&&!r.showsFirstScreen(e))return{kind:`continue`};if(c&&c.comparisonKey!==d.comparisonKey)return t({level:`debug`,phase:`post_gesture_snapshot_baseline_rebased`,data:{action:n.action,from:c.comparisonKey,to:d.comparisonKey,attempts:s}}),c={provenance:`post-action`,signature:d.signature,comparisonKey:d.comparisonKey,keyboard:d.keyboard},{kind:`continue`};let h=O({baseline:c,quiet:d,elapsedMs:m,distrustCapMs:w,classifyBaselineEvidence:r.classifyBaselineEvidence});return h.verdict===`distrust`?{kind:`continue`,budgetMs:w}:(A(h.verdict,n.action,s,m),{kind:`done`,result:j(h,n,d,r)})}});if(p.kind===`done`)return p.result;if(p.kind===`failed`)throw p.error;t({level:`warn`,phase:`post_gesture_snapshot_stabilization_timeout`,data:{action:n.action,attempts:s,durationMs:p.waitedMs,lastPairAgreed:l}});let m=p.last;return u&&!r.showsFirstScreen(m)?{value:m,postGestureOutcome:D(`loading`,n)}:l?{value:m}:{value:m,postGestureOutcome:D(`unsettled`,n)}}function A(e,n,r,i){if(e===`accept-stale`){t({level:`warn`,phase:`post_gesture_snapshot_stale_accept`,data:{action:n,attempts:r,durationMs:i,matchedPreGestureBaseline:!0}});return}t({level:r>2?`info`:`debug`,phase:`post_gesture_snapshot_stabilized`,data:{action:n,attempts:r,durationMs:i}})}function j(e,n,r,i){if(e.verdict!==`accept-stale`)return{value:r.value};let a=e.preActionBaseline;return i.surfacesIdentical(a.signature,r.signature)?{value:r.value,postGestureOutcome:D(`no-effect`,n)}:(t({level:`info`,phase:`post_gesture_no_effect_vetoed`,data:{action:n.action,backend:r.comparisonKey,reason:`surface_divergence`,...i.summarizeDivergence(a.signature,r.signature)}}),{value:r.value})}export{i as a,r as c,u as d,c as f,m as i,d as l,k as n,a as o,s as p,g as r,o as s,E as t,l as u};