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.7 kB
import{Yi as e,ai as t,pi as n,xi as r}from"./sdk-batch.js";import{O as i,gt as a,pt as o,v as s}from"./sdk-contracts.js";import{t as c}from"./interaction-error.js";import{a as l,n as u,o as d,s as f}from"./rect-center.js";import{d as p}from"./selector-pipeline-policy.js";import{i as m,t as h}from"./tap-keyboard-occlusion.js";function g(e){switch(e){case`click`:case`press`:case`tap`:return`be tapped`;case`fill`:return`be filled`;case`focus`:return`be focused`;case`longPress`:return`be long-pressed`;case`hover`:return`be hovered`;case`scroll`:return`be scrolled`;case`swipe`:return`be swiped`;case`pinch`:return`be pinched`;case`pan`:return`be panned`;case`drag`:return`be dragged`;case`fling`:return`be flung`;case`rotate`:return`be rotated`;case`transform`:return`be transformed`}}function _(e){let n=e.node.rect;if(!n||!e.tapPoint)return;let r=m({nodes:e.nodes,viewport:t(e.nodes).resolveViewport(n),point:e.tapPoint,node:e.node,...e.keyboard?{keyboard:e.keyboard}:{}});if(r.kind===`occluded`)throw b({label:e.label,action:e.action,ref:e.node.ref,targetRect:n,surface:r.surface})}function v(e){let t=m({nodes:e.nodes,viewport:e.viewport,point:e.point,...e.keyboard?{keyboard:e.keyboard}:{}});if(t.kind===`occluded`)return y(e.point,t.surface)}function y(e,t){return`(${e.x}, ${e.y}) is behind the visible keyboard, so this tap lands on the keyboard rather than anything under it (${h.reason}; keyboard frame ${Math.round(t.frame.y)}..${Math.round(t.frame.y+t.frame.height)}). `+h.hint}function b(e){return i(new s(`COMMAND_FAILED`,`${e.label} is behind the visible keyboard and cannot ${g(e.action)} safely`,{...h,ref:`@${e.ref}`,rect:e.targetRect,keyboardFrame:e.surface.frame}),`no`)}function x(e,t,n={}){let i=l(t.rect);if(!i||!S(i))return{kind:`invalid`};let o=f(t),s=o?r(i,o):i;if(!s||!S(s))return{kind:`invalid`};let c=d(s);if(!Number.isFinite(c.x)||!Number.isFinite(c.y))return{kind:`invalid`};let u=C(e,t,i);if(u.length===0)return{kind:`resolved`,point:c,strategy:`center`};let p=(n.parentOwnedBounds??[]).reduce((e,t)=>e?r(e,t):void 0,s),m=p?T(i,p,c,u):null;return m?{kind:`resolved`,point:m,strategy:`parent-owned`}:{kind:`blocked`,competitorRefs:a(u),competitorCount:u.length}}function S(e){return e.width>0&&e.height>0}function C(e,t,n){let i=new Map(e.map(e=>[e.index,e]));return e.filter(e=>e.index!==t.index).filter(e=>w(e,t,i)).filter(p).flatMap(e=>{let t=l(e.rect);if(!t||u(t,n))return[];let i=r(t,n);return i?[{ref:e.ref,index:e.index,rect:i}]:[]}).sort((e,t)=>e.index-t.index)}function w(e,t,n){let r=new Set,i=e;for(;typeof i.parentIndex==`number`&&!r.has(i.index);){if(r.add(i.index),i.parentIndex===t.index)return!0;let e=n.get(i.parentIndex);if(!e)return!1;i=e}return!1}function T(e,t,n,r){let i=A(e),a=O(t.x,t.width,n.x,r.flatMap(({rect:e})=>[e.x,e.x+e.width]),i),o=O(t.y,t.height,n.y,r.flatMap(({rect:e})=>[e.y,e.y+e.height]),i);return a.flatMap(a=>o.flatMap(o=>{let s={x:Math.round(a),y:Math.round(o)};if(s.x<t.x||s.x>=t.x+t.width||s.y<t.y||s.y>=t.y+t.height)return[];let c=E(s,e,n,r,i);return c?[c]:[]})).sort(D)[0]?.point??null}function E(e,t,n,r,i){let a=k(e,t,r);return a.parentEdge<i||a.competitor<12?null:{point:e,distanceSquared:(e.x-n.x)**2+(e.y-n.y)**2,clearance:Math.min(a.parentEdge,a.competitor)}}function D(e,t){return e.distanceSquared-t.distanceSquared||t.clearance-e.clearance||e.point.y-t.point.y||e.point.x-t.point.x}function O(e,t,n,r,i){let a=e+i,o=Math.min(e+t-i,Math.ceil(e+t)-1);if(a>o)return[];let s=[...new Set([e,e+t,...r])].sort((e,t)=>e-t),c=new Set([n,a,o]);for(let e of r)c.add(e-12),c.add(e+12);for(let e=1;e<s.length;e+=1)c.add((s[e-1]+s[e])/2);return[...c].filter(e=>e>=a&&e<=o).sort((e,t)=>e-t)}function k(e,t,n){let r=Math.min(e.x-t.x,t.x+t.width-e.x,e.y-t.y,t.y+t.height-e.y),i=1/0;for(let{rect:t}of n){let n=e.x<t.x?t.x-e.x:Math.max(0,e.x-(t.x+t.width)),r=e.y<t.y?t.y-e.y:Math.max(0,e.y-(t.y+t.height));i=Math.min(i,Math.max(n,r))}return{parentEdge:r,competitor:i}}function A(e){let t=Math.min(e.width,e.height);return Math.max(0,Math.min(12,Math.floor(t/2)-1))}function j(e){let{located:t,resolved:n,nodes:r,platform:i}=e,a=M(t,n,r).find(e=>I(e)&&L(e,i));if(a)return a.enabled===!1?{kind:`disabled-field`,node:a}:{kind:`target`,node:a};let o=P(n,r,i),s=o.filter(e=>e.enabled!==!1);return s.length>1?{kind:`several-fields`,fields:s}:s[0]?{kind:`target`,node:s[0]}:o[0]?{kind:`disabled-field`,node:o[0]}:{kind:`target`,node:n}}function M(e,t,n){let r=new Map(n.map(e=>[e.index,e]));if(!w(e,t,r))return[e,t];let i=[e],a=N(e,r);for(;a&&a.index!==t.index;)i.push(a),a=N(a,r);return[...i,t]}function N(e,t){return typeof e.parentIndex==`number`?t.get(e.parentIndex):void 0}function P(e,n,r){let i=new Map(n.map(e=>[e.index,e])),a=t([...n]),o=n.filter(t=>I(t)&&L(t,r)&&w(t,e,i)&&F(t)&&a.isVisibleOnScreen(t));return o.filter(e=>!o.some(t=>w(e,t,i)))}function F(e){let t=l(e.rect);return t!==null&&t.width>0&&t.height>0}function I(e){return e.ref!==void 0}function L(e,t){return e.editable??(e.password===!0||!!e.type?.trim()&&n(e.type??``,t))}function R(t,n){return n.filter(n=>n.parentIndex===t.parentIndex&&n.index<t.index&&F(n)&&e(n)&&!z(n)).at(-1)??null}function z(e){let t=`${e.type??``} ${e.role??``} ${e.subrole??``}`.toLowerCase();return/scroll\s?(bar|indicator)/.test(t)}function B(e,t,n){if(F(e))return{};let r=R(e,t);if(!r)return{};let i=n?`run scroll <direction> --until ${n} to bring it on screen, then retry with the same selector.`:`scroll toward it, then retry with a selector (e.g. text=/id=) rather than an @ref — the scroll expires the ref frame, so re-run snapshot -i before reusing any @ref.`;return{...r.ref?{scrollContainerRef:r.ref}:{},hint:`The target reports no frame, so nothing confirms it is on screen, and the frame names no direction. It may be scrolled out of a scrollable container: ${i}`}}function V(e,n,r){let a=t(n),o=x(n,e,{parentOwnedBounds:[a.resolveEffectiveViewport(e),e.rect?a.resolveViewport(e.rect):null].filter(e=>e!==null)});if(o.kind===`resolved`)return o.point;throw o.kind===`invalid`?i(new s(`COMMAND_FAILED`,r.invalidMessage,{reason:c.targetBoundsInvalid,...H(r.blockedTargetDetails),...B(e,n,r.shellQuotedSelector)}),`no`):i(new s(`COMMAND_FAILED`,`${r.blockedTargetLabel} has no parent-owned touch point outside its interactive descendants`,{reason:`covered_by_interactive_descendants`,...r.blockedTargetDetails,competitorRefs:o.competitorRefs.slice(0,5).map(e=>`@${e}`),competitorCount:o.competitorCount,hint:`Tap the specific interactive child you intend, or use a more specific selector. Every safely tappable region of the parent belongs to one of its child controls.`}),`no`)}function H(e){return`ref`in e?{ref:o(e.ref)??e.ref}:e}export{g as a,v as i,V as n,_ as r,j as t};