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 • 8.64 kB
import{Nt as e,ar as t,cn as n,cr as r,ir as i,rr as a}from"./sdk-batch.js";import{B as o,ot as s,p as c,s as l}from"./sdk-contracts.js";import{t as u}from"./snapshot-private-evidence.js";import"./capture.js";import{r as d,t as f}from"./success-text.js";import{o as p}from"./session-store.js";import{n as m,t as h}from"./interaction-error.js";import{r as g}from"./snapshot-occlusion.js";import{o as _,x as v}from"./snapshot-runtime-capture-input.js";import{r as y}from"./snapshot-lines.js";import{t as b}from"./context.js";import{t as x}from"./focus-runtime.js";import{a as ee,l as S,n as C,s as w,t as T}from"./selector-pipeline-policy.js";import{r as te}from"./snapshot-baseline.js";import{t as ne}from"./snapshot-quality-verdict.js";import{a as E,n as D,r as O,t as k}from"./node-touch-point.js";import{n as A}from"./capture-disclosure.js";import{t as j}from"./snapshot-runtime-binding.js";import{n as M,t as N}from"./type-text-runtime.js";import{n as re,u as P}from"./selector-runtime.js";function F(e,t,n){return l(`AMBIGUOUS_MATCH`,`find matched ${e.length} elements for ${t} "${n}". Use a more specific locator or selector.`,{locator:t,query:n,...y(e),dispatched:`no`})}function I(e,t){return l(`AMBIGUOUS_MATCH`,`find matched ${e}, which contains ${t.length} text fields. Use a more specific locator or selector.`,{ref:e,...y(t),dispatched:`no`})}function L(e,t){let n=t[0]?.rect;if(!n)return e;let r=e.filter(e=>{if(!e.rect)return!1;let t=s(e.rect);return t.x>=n.x&&t.x<=n.x+n.width&&t.y>=n.y&&t.y<=n.y+n.height}),i=r.length>0?r:e;return i.length<2?i:R(i,t,w(t))}function R(e,t,n){return e.map((e,r)=>({node:e,index:r,score:z(e,t,n)})).sort((e,t)=>t.score===e.score?V(e.node)-V(t.node)||e.index-t.index:t.score-e.score).map(e=>e.node)}function z(e,t,n){let r=n(e);if(r.reason===`covered`)return 0;let i=B(r,t[0]);return i>0?i:e.hittable&&e.rect&&!S(e,t[0])?3:+!!e.rect}function B(e,t){return e.node.rect?e.reason===`semantic-target`||e.reason===`same-rect-descendant`?4:e.reason===`hittable-ancestor`&&!S(e.node,t)?2:0:0}function V(e){return e.rect?e.rect.width*e.rect.height:1/0}function ie(e){if(e.ambiguity!==`reject-candidates`)throw Error(`find's resolution policy must reject candidates, got "${e.ambiguity}"`)}function H(e){let{nodes:t,locator:n,query:r,selectorExpression:a,flags:o,platform:s}=e,c=T.findAct,u=t.filter(e=>!S(e,t[0])),d=c.resolution,f;if(f=a?C(c,u,a,{platform:s}).list?.matchedNodes??[]:i(u,n,r,{requireRect:d.requireRect}).matches,f=L(f,t),f.length>1){ie(d);let e=U(f,o);if(!e)return{ok:!1,response:F(f,n,r)};f=e}let p=f[0]??null;return p?{ok:!0,node:p}:{ok:!1,response:l(`COMMAND_FAILED`,`find did not match any element`)}}function U(e,t){return t?.findFirst?[e[0]]:t?.findLast?[e.at(-1)]:null}function W(e){let{device:t,req:n,logPath:r,locator:i,query:a,sessionStore:o,sessionName:s}=e,c=P({ref:e.ref,device:t,sessionStore:o,sessionName:s,req:n,logPath:r,capture:e.capture,captureProof:e.captureProof});return async()=>{let{snapshot:e}=await c.capture({flags:{...n.flags,snapshotInteractiveOnly:!0},recovery:{legacyIosSparse:{query:a,shouldScope:K(i)},sparseVerdictQueryScope:{query:a,shouldScope:K(i)}}});return e}}function G(e){return l(`COMMAND_FAILED`,`find could not read the current accessibility tree`,{reason:e.reason,hint:`The snapshot quality verdict is sparse. Use screenshot as visual truth, navigate with coordinates if needed, then retry find after reaching a readable screen.`})}function K(e){return e!==`role`}async function q(i){let{req:o,sessionName:s,logPath:u,sessionStore:d,invoke:f}=i,p=d.lookup(s),m=o.command;if(m!==`find`)return null;let h=o.positionals??[],g=a(h,o.flags);if(!g.ok)return l(g.code,g.message);let{locator:_,query:v,action:y,value:b}=g.parsed;if(o.flags?.record&&!t(y))return l(`INVALID_ARGS`,`find ${y} is a mutating action and is always recorded; --record only applies to a read-only find (exists, wait, list, get text, get attrs).`);let x=await re({createCommandSurface:i.createCommandSurface,req:o,sessionName:s,logPath:u,sessionStore:d,inspectFacts:i.inspectFacts,platformServices:i.platformServices,bindDevice:i.bindDevice});if(x)return x;let S=p?d.requireCurrent(p):void 0;if(!S||!p)return c();let C=S.device,w=await j({command:`find`,device:C,session:S,plan:n({hasActiveApp:S.appBundleId!==void 0,intent:e(y)}),inspectFacts:i.inspectFacts,platformServices:i.platformServices,bindDevice:i.bindDevice});if(!w.ok)return w.response;let E=r(_,v),D={},O=W({ref:p,device:C,req:o,logPath:u,locator:_,query:v,sessionStore:d,sessionName:s,capture:w.capture,captureProof:D}),k={sessionRef:p,req:o,sessionName:s,logPath:u,sessionStore:d,invoke:f,device:C,command:m,locator:_,query:v,publicFlags:le(o.flags),boundFocusPoint:w.focusPoint,boundTypeText:w.typeText},M=await O();if(ne(M.snapshotQuality))return A({response:G(M.snapshotQuality),consumedTree:M,captureProof:D});let{nodes:N}=M,P=H({nodes:N,locator:_,query:v,selectorExpression:E,flags:o.flags,platform:C.platform});if(!P.ok)return A({response:P.response,consumedTree:M,captureProof:D});let F=P.node,I=await ee(T.findAct,N,F),L=I.node,R=`@${L.ref}`,z={...o.flags??{},noRecord:!0},B=await J(k,{node:F,resolvedNode:L,ref:R,tree:te(M),...M.keyboard?{keyboard:M.keyboard}:{},actionFlags:z,...I.kind===`occluded`?{occludedNode:I.node}:{}},y,b);return B&&A({response:B,consumedTree:M,captureProof:D})}async function J(e,t,n,r){let i={click:()=>ae(e,t),fill:()=>X(e,t,r),focus:()=>oe(e,t),type:()=>se(e,t,r)}[n];return i?await i():null}function Y(e){return{ref:e.ref,node:e.resolvedNode,tree:e.tree,...e.keyboard?{keyboard:e.keyboard}:{}}}async function ae(e,t){let{req:n,sessionName:r,sessionStore:i,sessionRef:a,invoke:o,command:c,locator:l,query:u,publicFlags:m}=e,h=await o({token:n.token,session:r,command:`click`,positionals:[t.ref],flags:t.actionFlags,internal:{findResolvedTarget:Y(t)}});if(!h.ok)return h;let g=t.resolvedNode.rect?s(t.resolvedNode.rect):t.node.rect?s(t.node.rect):null,_={ref:t.ref,locator:l,query:u};g&&(_.x=g.x,_.y=g.y);let v=f(h.data)??`Tapped ${t.ref}${g?` (${g.x}, ${g.y})`:``}`;return Object.assign(_,d(v)),p(i,a,n,c,{ref:t.ref,action:`click`,locator:l,query:u},{flags:m}),{ok:!0,data:_}}async function X(e,t,n){let{req:r,sessionName:i,sessionStore:a,sessionRef:o,invoke:s,command:c,publicFlags:u}=e;if(n===void 0)return l(`INVALID_ARGS`,`find fill requires text (use "" to clear the field)`);let d=await s({token:r.token,session:i,command:`fill`,positionals:[t.ref,n],flags:t.actionFlags,internal:{findResolvedTarget:Y(t)}});return d.ok&&p(a,o,r,c,{ref:t.ref,action:`fill`},{flags:u}),d}async function oe(e,t){let n=await Z(e,t);return n.ok&&$(e,t,`focus`),n}async function se(e,t,n){let{req:r,logPath:i}=e;if(!n)return l(`INVALID_ARGS`,`find type requires text`);let a=()=>{let t=e.sessionStore.requireCurrent(e.sessionRef);return b(i,r.flags,t.appBundleId,t.trace?.outPath)},o=M([n],a(),{rejectRefTarget:!1}),s=await Z(e,t);if(!s.ok)return s;let c=e.sessionStore.requireCurrent(e.sessionRef);_(c);let u=e.boundTypeText;if(!u)return l(`COMMAND_FAILED`,`find type was admitted without a text-entry operation`);let d=await N({operations:{typeText:u}},o,a());return $(e,t,`type`),{ok:!0,data:d}}async function Z(e,t){let{req:n,logPath:r}=e,i=ce(t,`focus`);if(i)return i;let a=k({located:t.node,resolved:t.resolvedNode,nodes:t.tree.nodes,platform:e.device.platform});if(a.kind===`several-fields`)return I(t.ref,a.fields);if(a.kind===`disabled-field`)return l(`COMMAND_FAILED`,`Matched element @${a.node.ref} is a disabled text field and cannot be focused`,{reason:h.targetDisabled,ref:`@${a.node.ref}`,hint:`Enable the field first, or target a different one.`});let s=a.node;if(g(s))return Q(s,`focus`);let c=`@${s.ref}`,d;try{await u(t.tree.nodes,`Ref ${c}`),d=D(s,t.tree.nodes,{invalidMessage:`Ref ${c} has no usable bounds`,blockedTargetLabel:`Ref ${c}`,blockedTargetDetails:{ref:c}}),O({nodes:t.tree.nodes,node:s,action:`focus`,label:`Ref ${c}`,...t.keyboard?{keyboard:t.keyboard}:{},tapPoint:d})}catch(e){return{ok:!1,error:o(e)}}let f=e.sessionStore.requireCurrent(e.sessionRef);_(f);let p=e.boundFocusPoint;return p?{ok:!0,data:await x({operations:{focusPoint:p}},d,b(r,n.flags,f.appBundleId,f.trace?.outPath))}:l(`COMMAND_FAILED`,`find focus was admitted without a focus operation`)}function ce(e,t){return e.occludedNode?Q(e.occludedNode,t):null}function Q(e,t){return l(`COMMAND_FAILED`,`Matched element @${e.ref} is covered by another visible element and cannot ${E(t)} safely`,m(e))}function $(e,t,n){let{req:r,sessionStore:i,sessionRef:a,command:o,publicFlags:s}=e;p(i,a,r,o,{ref:t.ref,action:n},{flags:s})}function le(e){return{...v(e)??{}}}export{q as handleFindCommands};