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 • 9.25 kB
import{Gn as e,Kn as t,Mi as n,Ni as r,Qn as i,Qr as a,Xn as o,_i as s,li as c,ui as l}from"./sdk-batch.js";import{lt as u}from"./sdk-contracts.js";import{a as d,c as f,n as p}from"./rect-center.js";import{r as m}from"./snapshot-occlusion.js";function h(e,n,r,a){let s=r.ambiguity;if(s===`first-match`||s===`reject-candidates`){let t=o(e,n,{...a,requireRect:r.requireRect});return!t||t.matchedNodes.length===0?{kind:`none`}:s===`first-match`?_(t):t.matchedNodes.length>1?g(t):_(t)}let c=i(e,n,{...a,...t({ambiguity:s,requireRect:r.requireRect})});return c.firstMatch?c.resolution?{kind:`resolved`,resolution:c.resolution,matchedNodes:c.firstMatch.matchedNodes}:g(c.firstMatch):{kind:`none`}}function g(e){return{kind:`ambiguous`,selector:e.selector.raw,selectorIndex:e.selectorIndex,matchedNodes:e.matchedNodes}}function _(e){let t=e.matchedNodes[0];return t?{kind:`resolved`,matchedNodes:e.matchedNodes,resolution:{node:t,selector:e.selector,selectorIndex:e.selectorIndex,matches:e.matchedNodes.length,diagnostics:[{selector:e.selector.raw,matches:e.matchedNodes.length}]}}:{kind:`none`}}function v(e,t,n){let r=o(e,a(t),n);return r?{...r,selector:r.selector.raw}:null}function y(e,t,n,r){let i=h(e,a(t),n,r);return i.kind===`resolved`?{...i,resolution:{...i.resolution,selector:i.resolution.selector.raw}}:i}function b(e){return e}function x(e,t){let n=Math.max(e.x,t.x),r=Math.max(e.y,t.y),i=Math.min(e.x+e.width,t.x+t.width),a=Math.min(e.y+e.height,t.y+t.height);return i<=n||a<=r?0:(i-n)*(a-r)}const S=[`button`,`link`,`menuitem`,`tabitem`,`textfield`,`searchfield`,`securetextfield`,`checkbox`,`radio`,`switch`,`cell`];function C(e){return[e.type,e.role,e.subrole].map(e=>s(e??``)).some(e=>e===`tab`||S.some(t=>e.includes(t)))}function ee(e,t){let n=t[0];if(!n)return{kind:`ambiguous`,candidates:t};let r=V(e);if(!w(t,r.nodesByIndex))return{kind:`ambiguous`,candidates:t};let i=N(e,n,r).node;for(let n of t.slice(1))if(N(e,n,r).node.index!==i.index){let e=E(t,r);return e?{kind:`equivalent`,node:e}:{kind:`ambiguous`,candidates:t}}return{kind:`equivalent`,node:i}}function w(e,t){for(let n=0;n<e.length;n+=1)for(let r=n+1;r<e.length;r+=1){let i=e[n],a=e[r];if(!T(i,a,t)&&!T(a,i,t))return!1}return!0}function T(e,t,n){let r=t,i=new Set;for(;r.parentIndex!==void 0&&!i.has(r.index);){if(i.add(r.index),r.parentIndex===e.index)return!0;let t=n.get(r.parentIndex);if(!t)return!1;r=t}return!1}function E(e,t){if(e.length<2||!w(e,t.nodesByIndex)||e.some(e=>e.hittable!==void 0))return null;let n=e.reduce((e,t)=>(t.depth??0)>(e.depth??0)?t:e);if(!C(n)||!e.every(e=>e===n||!C(e)))return null;let r=d(n.rect);return r&&e.every(e=>A(d(e.rect),r))?n:null}function D(e){return[e.type,e.role,e.subrole].map(e=>s(e??``)).some(e=>e.includes(`accessibilityelement`))}function O(e,t){if(e.length<2||!w(e,t.nodesByIndex)||e.some(e=>C(e)))return null;let n=e.reduce((e,t)=>(t.depth??0)<(e.depth??0)?t:e),r=n.label?.trim();if(!r)return null;let i=d(n.rect);return i&&e.every(e=>(e===n||D(e))&&e.label?.trim()===r&&A(d(e.rect),i))?n:null}function k(e,t){let n=V(e);return E(t,n)??O(t,n)}function A(e,t){return e?Math.abs(e.x-t.x)<=1&&Math.abs(e.y-t.y)<=1&&Math.abs(e.width-t.width)<=1&&Math.abs(e.height-t.height)<=1:!1}function j(e,t){if(!t?.rect||!e.rect||!c(e))return!1;let n=e.rect,r=t.rect;return n.x===r.x&&n.y===r.y&&n.width===r.width&&n.height===r.height}function te(e,t){return N(e,t)}function M(e){let t=V(e);return n=>N(e,n,t)}function N(e,t,n){return m(t)?{node:t,reason:`covered`}:P(e,t,n)??F(t)??I(e,t,n)??{node:t,reason:`original`}}function P(e,t,n){let r=R(e,t,n);return r?.rect&&f(r.rect)?{node:r,reason:`same-rect-descendant`}:null}function F(e){return C(e)&&e.rect&&f(e.rect)?{node:e,reason:`semantic-target`}:null}function I(e,t,n){let r=L(e,t,n);return!r?.rect||m(r)||!f(r.rect)?null:z(t,r,e,n)?{node:t,reason:`overly-broad-ancestor`}:{node:r,reason:`hittable-ancestor`}}function L(e,t,i){if(t.hittable)return t;let a=e=>e.hittable===!0;return i?r(e,t,i.nodesByIndex,e=>a(e)?e:null):n(e,t,a)}function R(e,t,n){let r=d(t.rect);if(!r)return null;let i=t,a=new Set;for(;!a.has(i.index);){a.add(i.index);let t=(n?n.childrenByParentIndex.get(i.index)??[]:e.filter(e=>e.parentIndex===i.index)).filter(e=>{if(!e.hittable||m(e))return!1;let t=d(e.rect);return t?p(t,r):!1});if(t.length!==1)break;i=t[0]}return i===t?null:i}function z(e,t,n,r){let i=d(e.rect),a=d(t.rect);if(!i||!a)return!1;if(B(t)&&!p(i,a))return!0;let o=l(n,i,r?.viewportRootRects);return!o||!H(a,o)?!1:!p(i,a)}function B(e){let t=s(e.type??``);return t.includes(`scrollview`)||t.includes(`scrollarea`)||t.includes(`listview`)||t.includes(`recyclerview`)||t.includes(`collectionview`)||t===`list`||t===`table`||t===`collection`}function V(e){let t=new Map,n=new Map,r=[];for(let i of e){if(t.set(i.index,i),typeof i.parentIndex==`number`){let e=n.get(i.parentIndex);e?e.push(i):n.set(i.parentIndex,[i])}if(c(i)){let e=d(i.rect);e&&r.push(e)}}return{nodesByIndex:t,childrenByParentIndex:n,viewportRootRects:r}}function H(e,t){let n=x(e,t),r=e.width*e.height,i=t.width*t.height;if(n<=0||r<=0||i<=0)return!1;let a=n/i,o=n/r;return a>=.9&&o>=.8}function U(e,t){let n=d(t.rect);return!n||!C(t)?!1:e.some(e=>{if(e.index===t.index||!C(e))return!1;let r=d(e.rect);if(!r)return!1;let i=r.x<=n.x+1&&r.y<=n.y+1&&r.x+r.width>=n.x+n.width-1&&r.y+r.height>=n.y+n.height-1,a=r.width>n.width+1||r.height>n.height+1;return i&&a})}async function W(e,t,n,r,i={}){let a=y(Q(e,t),n,e.resolution,r);if(a.kind===`none`)return{kind:`none`};if(a.kind===`resolved`)return await K(e,t,{node:a.resolution.node,selector:a.resolution.selector,selectorIndex:a.resolution.selectorIndex,matches:a.resolution.matches,matchedNodes:a.matchedNodes},i);let o=G(t,a);return o?await K(e,t,o,i):a}function G(e,t){let n=k(e,t.matchedNodes);return n?{node:n,selector:t.selector,selectorIndex:t.selectorIndex,matches:t.matchedNodes.length,matchedNodes:t.matchedNodes}:null}async function K(e,t,n,r){let i=await Y(e,t,n.node,r);return i.kind===`occluded`?{kind:`occluded`,node:i.node,selector:n.selector}:{...n,kind:`target`,node:i.node}}function q(e,t,n,r){let i=Q(e,t);return{candidates:i,list:Z(e,v(i,n,{...r,requireRect:e.resolution.requireRect}))}}function J(e,t,n,r){let{list:i}=q(e,t,n,r),a=i?.matchedNodes??[];if(!i||a.length===0)return{kind:`none`};let o=[{selector:i.selector,matches:a.length}];if(a.length===1)return{kind:`resolved`,resolution:{node:a[0],selector:i.selector,selectorIndex:i.selectorIndex,matches:1,diagnostics:o}};let s=ee(t,a);return s.kind===`ambiguous`?{kind:`ambiguous`,selector:i.selector,candidates:s.candidates}:{kind:`resolved`,resolution:{node:X(e,s.node),selector:i.selector,selectorIndex:i.selectorIndex,matches:a.length,diagnostics:o,disambiguation:{matchCount:a.length,tiebreak:`structural-equivalence`,alternatives:a.filter(e=>e.index!==s.node.index)}}}}async function Y(e,t,n,r={}){r.onResolved?.(n,t);let i=ne(e.promotion,t,n);return e.occlusion!==`ignore`&&(i.covered||m(i.node))?{kind:`occluded`,node:X(e,i.node)}:{kind:`target`,node:X(e,await re(e,t,i.node,r))}}function X(e,t){if(e.resolution.requireRect&&!u(t))throw Error(`selector pipeline row requires an addressable target, but resolved a ref-less node`);return t}function Z(e,t){return t?{...t,matchedNodes:t.matchedNodes.map(t=>X(e,t))}:null}function Q(e,t){let n=e.resolution.requireRect?t.filter(u):t;return e.occlusion===`exclude-and-refuse`?n.filter(e=>!m(e)):n}function ne(e,t,n){if(e===`none`)return{node:n,covered:!1};let r=te(t,n);return r.reason===`covered`?{node:r.node,covered:!0}:e===`hittable-ancestor-below-root`&&j(r.node,t[0])&&n.rect?{node:n,covered:!1}:{node:r.node,covered:!1}}async function re(e,t,n,r){if(e.offscreen!==`refuse`)return n;if(!r.offscreen)throw Error(`selector pipeline row declares an off-screen refusal but this route supplies no refusal shape`);return await r.offscreen(n,t)}function ie(e){if(e.poll===`none`)throw Error(`selector pipeline row resolves against one capture and declares no poll budget`);return e.poll}const $={defaultTimeoutMs:1e4,intervalMs:300},ae={promotedTarget:{resolution:e.act,occlusion:`exclude-and-refuse`,offscreen:`refuse`,promotion:`hittable-ancestor`,poll:`none`},resolvedTarget:{resolution:e.act,occlusion:`exclude-and-refuse`,offscreen:`refuse`,promotion:`none`,poll:`none`},coveredDiagnosis:{resolution:e.actCoveredDiagnosis,occlusion:`refuse`,offscreen:`ignore`,promotion:`none`,poll:`none`},readText:{resolution:e.readText,occlusion:`ignore`,offscreen:`ignore`,promotion:`none`,poll:`none`},readUnique:{resolution:e.readUnique,occlusion:`ignore`,offscreen:`ignore`,promotion:`none`,poll:`none`},readAny:{resolution:e.readAny,occlusion:`ignore`,offscreen:`ignore`,promotion:`none`,poll:`none`},findWait:{resolution:e.readAny,occlusion:`ignore`,offscreen:`ignore`,promotion:`none`,poll:$},wait:{resolution:e.wait,occlusion:`ignore`,offscreen:`ignore`,promotion:`none`,poll:$},readList:{resolution:e.readList,occlusion:`ignore`},findAct:{resolution:e.findAct,occlusion:`refuse`,offscreen:`ignore`,promotion:`hittable-ancestor-below-root`,poll:`none`},cropTarget:{resolution:e.cropTarget,occlusion:`ignore`,offscreen:`ignore`,promotion:`none`,poll:`none`}};export{Y as a,U as c,C as d,x as f,W as i,j as l,q as n,ie as o,b as p,J as r,M as s,ae as t,k as u};