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 • 3.36 kB
import{Yi as e,ai as t,li as n}from"./sdk-batch.js";import{ct as r}from"./sdk-contracts.js";import{r as i}from"./mobile-snapshot-semantics.js";function a(e,t,n={}){let r=o(e);if(r.length===0)return{canScroll:!1,emptySnapshot:!0};let a=i(r),c=s(r,a,t,n);return c?{canScroll:f(c,a.get(c.index),t),emptySnapshot:!1,...c.rect?{containerRect:c.rect}:{},scope:g(c,r),fingerprint:C(c,r)}:{canScroll:!1,emptySnapshot:!1,fingerprint:w(r)}}function o(e){return e.map((e,t)=>({...e,ref:`ref`in e&&e.ref?e.ref:`e${t+1}`,kind:`kind`in e&&e.kind?e.kind:r(e.type??`Element`)}))}function s(n,r,i,a){let o=t(n),s=e=>f(e,r.get(e.index),`top`)||f(e,r.get(e.index),`bottom`),m=e=>p(e,o.nodeByIndex,s)??e,h=[...new Set(n.filter(t=>e(t)&&S(t.rect)).map(m))];if(h.length===0)return null;let g=d(a.nodeIndex,o.nodeByIndex);if(g)return m(g);let _=a.point;if(_)return l(h,r,i,_)||c(h,r,i,o);let v=u(n);if(v){let e=l(h,r,i,v);if(e)return e}return c(h,r,i,o)}function c(e,t,n,r){let i=e.filter(e=>f(e,t.get(e.index),n)).sort((e,t)=>y(t,e));return i.length>0?i[0]??null:e.filter(r.isVisibleInEffectiveViewport).sort((e,t)=>y(t,e))[0]??e.sort((e,t)=>y(t,e))[0]??null}function l(e,t,n,r){let i=e.filter(e=>e.rect&&x(e.rect,r)).sort(y);return i.find(e=>f(e,t.get(e.index),n))??i[0]??null}function u(e){let t=e.filter(e=>n(e)&&S(e.rect)).sort((e,t)=>y(t,e))[0]?.rect;if(t)return{x:t.x+t.width/2,y:t.y+t.height/2}}function d(t,n){if(t===void 0)return null;let r=new Set,i=n.get(t);for(;i&&!r.has(i.index);){if(r.add(i.index),e(i)&&S(i.rect))return i;i=m(i,n)}return null}function f(e,t,n){return n===`bottom`?e.hiddenContentBelow===!0||t?.hiddenContentBelow===!0:e.hiddenContentAbove===!0||t?.hiddenContentAbove===!0}function p(t,n,r){let i=new Set([t.index]),a,o=t,s=m(o,n);for(;s&&!i.has(s.index);){if(i.add(s.index),e(s)&&S(s.rect)){if(!h(s.rect,o.rect)||!r(s))return a;a=s,o=s}s=m(s,n)}return a}function m(e,t){return e.parentIndex===void 0?void 0:t.get(e.parentIndex)}function h(e,t){return!!(e&&t&&e.x===t.x&&e.y===t.y&&e.width===t.width&&e.height===t.height)}function g(e,t){return[e.identifier,e.label].filter(e=>typeof e==`string`).map(e=>e.trim()).find(n=>v(n)&&_(n,e,t))}function _(e,t,n){let r=e.toLowerCase(),i=n.filter(e=>[e.identifier,e.label,e.value].some(e=>typeof e==`string`&&e.trim().toLowerCase().includes(r)));return i.length===1&&i[0]?.index===t.index}function v(e){return e.length>0&&e.length<=80&&!/^(true|false)$/i.test(e)&&!/^\d+$/.test(e)&&!/^\d+%$/.test(e)}function y(e,t){return b(e.rect)-b(t.rect)}function b(e){return e?e.width*e.height:0}function x(e,t){return t.x>=e.x&&t.x<=e.x+e.width&&t.y>=e.y&&t.y<=e.y+e.height}function S(e){return!!(e&&e.width>0&&e.height>0)}function C(e,t){let n=new Map;for(let e of t)n.set(e.index,e);let r=[];for(let i of t){let t=i.rect;S(t)&&(i.index===e.index||!E(i,e.index,n)||r.push(`${i.type}#${T(i)}@${Math.round(t.x)},${Math.round(t.y)}`))}return r.length===0?``:(r.sort(),r.join(`;`))}function w(e){let n=t(e);return e.filter(e=>S(e.rect)&&n.isVisibleInEffectiveViewport(e)).map(e=>`${e.type}#${T(e)}@${Math.round(e.rect.x)},${Math.round(e.rect.y)}`).sort().join(`;`)}function T(e){return`${(e.identifier??``).trim()}${(e.label??``).trim()}${(e.value??``).trim()}`}function E(e,t,n){let r=e.parentIndex,i=n.size+1;for(;r!==void 0&&i-->0;){if(r===t)return!0;r=n.get(r)?.parentIndex}return!1}export{a as analyzeScrollEdgeState};