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.
10 lines • 12.1 kB
JavaScript
import{Vi as e,fi as t,zi as n}from"./sdk-batch.js";import"./sdk-contracts.js";import{n as r}from"./success-text.js";import{t as i}from"./mobile-snapshot-semantics.js";import{i as a}from"./output-common.js";import{i as o,t as s}from"./snapshot-lines.js";import{i as c,n as l,r as u,t as d}from"./snapshot-tree.js";function f(e){return e.width>0&&e.height>0}function p(e,t){return!e||!t?!1:e.x>=t.x-2&&e.y>=t.y-2&&e.x+e.width<=t.x+t.width+2&&e.y+e.height<=t.y+t.height+2}function m(e,t){if(!e||!t)return!0;let n=e.y+e.height/2,r=t.y+t.height/2;return Math.abs(n-r)<=Math.max(e.height,t.height,1)}function h(e){return typeof e.parentIndex!=`number`}function g(e){let t=(e.type??``).toLowerCase(),n=(e.identifier??``).trim().toLowerCase();return e.editable===!0||t.includes(`edittext`)||t.includes(`textfield`)||n===`composer`}function _(e){let t=(e.type??``).toLowerCase();return t.includes(`scroll`)||t.includes(`list`)||t.includes(`recyclerview`)}function v(e){let n=t(e);if(!n||n!==e.identifier?.trim()||!b(n))return n;let r=(e.type??``).toLowerCase();return r.includes(`view`)||r.includes(`layout`)||r.includes(`image`)||r.includes(`list`)||r.includes(`recyclerview`)||r.includes(`collection`)?``:n}function y(e){let t=e.trim().replaceAll(/\s+/g,` `).toLowerCase();return!t||/^(true|false|\d+)$/.test(t)?null:t}function b(e){return/^[\w.]+:id\/[\w.-]+$/i.test(e)}const ee=[`button`,`switch`,`checkbox`,`radio`],x=[`button`,`image`,`textview`,`view`];function S(e,t,n,r){for(let i of e){if(n.has(i.index)||!te(i))continue;let e=ne(l(t,i.index),t,i,n);if(e.label){r.set(i.index,{...i,...r.get(i.index),label:e.label});for(let r of e.removableIndexes)c(t,r,n)}}}function C(e,t,n,r){let i=new Map;for(let a of e){if(n.has(a.index)||!k(a))continue;let e=y(v(a));if(!e)continue;let o=i.get(e);if(o&&E(o,a,n)){let s=D(t,o,a,n,r);i.set(e,s);continue}i.set(e,a)}}function w(e,t,n){for(let r of e){if(n.has(r.index)||!ie(r))continue;let e=y(v(r));if(!e)continue;let i=l(t,r.index).filter(t=>!n.has(t.index)&&ae(r,t,e));for(let e of i.filter(T))c(t,e.index,n);let a=i.filter(e=>!T(e)),o=new Set(a.map(e=>y(v(e))).filter(e=>!!e));if(!(a.length<2||o.size<2))for(let e of a)c(t,e.index,n)}}function te(e){return e.hittable===!0&&!g(e)&&!!(e.rect&&f(e.rect))&&v(e).trim().length===0}function ne(e,t,n,r){let i=[],a=[],o=new Set,s=new Set;for(let c of e){if(c.hittable===!0||g(c)||c.parentIndex!==void 0&&s.has(c.parentIndex)){s.add(c.index);continue}if(r.has(c.index)||!re(c,t)||!p(c.rect,n.rect))continue;let e=v(c).trim().replaceAll(/\s+/g,` `),l=y(e);a.push(c.index),!(!e||!l||o.has(l))&&(o.add(l),i.push(e))}return{label:i.join(`, `),removableIndexes:a}}function re(e,t){if(T(e))return!0;let n=(e.type??``).toLowerCase();return e.hittable!==!0&&!g(e)&&n.includes(`view`)&&v(e).trim().length>0&&!t.get(e.index)?.length}function T(e){if(e.hittable===!0||g(e))return!1;let t=(e.type??``).toLowerCase();return t.includes(`text`)||t.includes(`image`)||t.includes(`icon`)}function ie(e){return e.hittable===!0&&!g(e)&&!!(e.rect&&f(e.rect))&&!!y(v(e))}function ae(e,t,n){if(!k(t)||!p(t.rect,e.rect))return!1;let r=y(v(t));return!!(r&&n!==r&&n.includes(r))}function E(e,t,n){return!n.has(e.index)&&m(e.rect,t.rect)&&(P(e,t)||N(e,t))}function D(e,t,n,r,i){let a=A(t,n),o=a.index===t.index?n:t,s=(o.type??``).toLowerCase().includes(`image`)?`has image`:null,l=i.get(a.index)??a,u=i.get(o.index)?.presentationHints??o.presentationHints;return i.set(a.index,{...l,presentationHints:O(l.presentationHints,u,s)}),c(e,o.index,r),i.get(a.index)??a}function O(e,t,n){return[...new Set([...Array.isArray(e)?e:[],...Array.isArray(t)?t:[],...n?[n]:[]])]}function k(e){if(!e.rect||!f(e.rect)||h(e)||g(e))return!1;let t=(e.type??``).toLowerCase();return t===`text`||M(t,x)}function A(e,t){let n=j(e);return j(t)>n?t:e}function j(e){let t=(e.type??``).toLowerCase(),n=0;return M(t,ee)?n+=100:t.includes(`image`)?n+=30:t.includes(`textview`)||t===`text`?n+=20:t.includes(`view`)&&(n+=10),e.hittable===!0&&(n+=20),e.enabled!==!1&&(n+=5),n}function M(e,t){return t.some(t=>e.includes(t))}function N(e,t){let n=e.parentIndex===t.index?t:t.parentIndex===e.index?e:null,r=n?.index===e.index?t:n?.index===t.index?e:null;return!n||!r?!1:A(n,r).index===n.index}function P(e,t){return e.parentIndex===t.parentIndex?Math.abs(e.index-t.index)<=3:e.parentIndex===t.index||t.parentIndex===e.index?!1:Math.abs((e.depth??0)-(t.depth??0))<=1&&Math.abs(e.index-t.index)<=2}function F(e,t,n){if(n.raw||!I(e))return{nodes:t,filteredCount:0};let r=L(t);return{nodes:r,filteredCount:t.length-r.length}}function I(e){let t=e.androidSnapshot;return!t||typeof t!=`object`?!1:t.backend===`android-helper`}function L(e){if(e.length===0)return e;let t=new Set,n=new Map,r=new Map(e.map(e=>[e.index,e])),i=d(e);return R(e,i,t),z(e,r,i,t,n),S(e,i,t,n),w(e,i,t),C(e,i,t,n),e.filter(e=>!t.has(e.index)).map(e=>n.get(e.index)??e)}function R(e,t,n){for(let r of e)!r.rect||f(r.rect)||h(r)||c(t,r.index,n)}function z(e,t,n,r,i){for(let a of e){if(r.has(a.index)||a.rect||h(a))continue;let e=u(a,t,_);e&&V(i,e,B(a,n)),c(n,a.index,r)}}function B(e,t){let n=(t.get(e.parentIndex)??[]).filter(e=>e.rect&&f(e.rect)).map(e=>e.index);return n.length===0?null:e.index<Math.min(...n)?`above`:e.index>Math.max(...n)?`below`:null}function V(e,t,n){if(!n)return;let r=e.get(t.index)??t;e.set(t.index,{...r,hiddenContentAbove:r.hiddenContentAbove===!0||n===`above`||void 0,hiddenContentBelow:r.hiddenContentBelow===!0||n===`below`||void 0})}function H(e){if(e.length<20)return!1;let n=new Map;for(let r of e){let e=(r.type??``).toLowerCase(),i=U(t(r));if(!i)continue;let a=`${e}|${i}`,o=n.get(a)??[];o.push(r),n.set(a,o)}let r=0;for(let e of n.values())e.length<=1||!K(e)||(r+=e.length);return r>=8}function U(e){let t=e.trim().replaceAll(/\s+/g,` `).toLowerCase();return!t||W(t)?null:t}function W(e){return e.split(` `).some(G)}function G(e){let t=e.indexOf(`@`,1);return t!==-1&&e.lastIndexOf(`.`,e.length-2)>t+1}function K(e){for(let t=0;t<e.length;t+=1)for(let n=t+1;n<e.length;n+=1)if(q(e[t]?.rect,e[n]?.rect))return!0;return!1}function q(e,t){if(!e||!t)return!0;let n=.5;return!(e.x+e.width<=t.x+n||t.x+t.width<=e.x+n||e.y+e.height<=t.y+n||t.y+t.height<=e.y+n)}function J(t,r={}){let a=t.nodes,o=Array.isArray(a)?a:[],s=e(t.backend)?t.backend:void 0,c=n(s),l=F(t,o,r),u=le(t),d=ye(t,o,r,l),f=d.length>0?`${d.join(`
`)}\n`:``,p=r.raw?null:oe(t);if(p)return`${u}${f}${se(p)}\n`;let m=fe(t,r),h=pe(m),g=r.raw||!c?null:i(l.nodes),_=!!t.truncated,v=g?.nodes??o,y=r.raw||!c?null:ge(t,g,v.length,o.length,l.filteredCount),b=ue(o.length,y,_);return o.length===0?`${u}${b}\n${f}`:r.raw?`${u}${b}\n${f}${de(o)}\n`:r.flatten?`${u}${b}\n${h}${f}${me(v,m)}${X(g)}\n`:`${u}${b}\n${h}${f}${he(v,m)}${X(g)}\n`}function oe(e){let t=e.unchanged;if(!t||typeof t!=`object`)return null;let n=t;return typeof n.ageMs!=`number`||typeof n.nodeCount!=`number`?null:{ageMs:n.ageMs,nodeCount:n.nodeCount,interactiveOnly:n.interactiveOnly===!0||void 0,scope:typeof n.scope==`string`?n.scope:void 0}}function se(e){let t=ce(e.ageMs);return e.scope?[`Scoped snapshot unchanged for scope "${e.scope}" since previous read ${t} ago.`,`Previous refs in this scope remain valid. Use find/get/is for a targeted query, or --force-full to re-emit.`].join(`
`):e.interactiveOnly?[`Interactive snapshot unchanged since previous read ${t} ago.`,`${e.nodeCount} visible nodes are unchanged. Previous @e refs are still valid. Use find/get/is for a targeted query, or --force-full to re-emit.`].join(`
`):[`Snapshot unchanged since previous read ${t} ago.`,`Refs from the previous snapshot are still valid. Use --force-full to re-emit the tree, or use find/get/is for a targeted query.`].join(`
`)}function ce(e){if(e<1e3)return`${Math.round(e)}ms`;if(e<6e4)return`${(Math.round(e/100)/10).toFixed(1)}s`;let t=e/6e4;if(t<60)return`${(Math.round(t*10)/10).toFixed(1)}m`;let n=t/60;return`${(Math.round(n*10)/10).toFixed(1)}h`}function le(e){let t=typeof e.appName==`string`?e.appName:void 0,n=typeof e.appBundleId==`string`?e.appBundleId:void 0,r=[];return t&&r.push(`Page: ${t}`),n&&r.push(`App: ${n}`),r.length>0?`${r.join(`
`)}\n`:``}function ue(e,t,n){let r=n?` (truncated)`:``;return t?.partial?t.totalNodeCount>t.visibleNodeCount?`Snapshot: ${t.visibleNodeCount} visible nodes (${t.totalNodeCount} total)${r}`:`Snapshot: ${t.visibleNodeCount} visible nodes${r}`:`Snapshot: ${e} nodes${r}`}function de(e){return e.map(e=>JSON.stringify(e)).join(`
`)}function fe(e,t){if(!(t.scoped!==!0||t.raw))return typeof e.refsGeneration==`number`?e.refsGeneration:void 0}function pe(e){return e===void 0?``:`Refs generation: ${e}\n`}function me(e,t){return Ee(e,t).join(`
`)}function he(e,t){return Ce(s(e,{summarizeTextSurfaces:!0}),t).join(`
`)}function Y(e,t,n){let r=a(t.ref,n);if(!r)return e;let i=`@${t.ref}`,o=e.indexOf(i);return o===-1?e:`${e.slice(0,o)}${r}${e.slice(o+i.length)}`}function X(e){return e&&e.summaryLines.length>0?`\n${e.summaryLines.join(`
`)}`:``}function ge(e,t,n,r,i=0){let a=_e(e);if(i===0&&a)return a;let o=(t?.hiddenCount??0)+i,s=t?t.nodes.some(e=>e.hiddenContentAbove||e.hiddenContentBelow):!1;return o>0?{partial:!0,visibleNodeCount:n,totalNodeCount:Math.max(r,a?.totalNodeCount??r),reasons:ve([...a?.reasons??[],`offscreen-nodes`])}:a||(s?{partial:!0,visibleNodeCount:n,totalNodeCount:n,reasons:[]}:null)}function _e(e){let t=e.visibility;if(!t||typeof t!=`object`)return null;let n=t;return typeof n.partial!=`boolean`||typeof n.visibleNodeCount!=`number`||typeof n.totalNodeCount!=`number`||!Array.isArray(n.reasons)?null:{partial:n.partial,visibleNodeCount:n.visibleNodeCount,totalNodeCount:n.totalNodeCount,reasons:n.reasons.filter(e=>typeof e==`string`)}}function ve(e){return[...new Set(e)]}function ye(e,t,n,r={nodes:t,filteredCount:0}){let i=[...Q(e),...be(e)];if(xe(e)){let e=Z(t,n);e&&i.push(e)}!n.raw&&r.filteredCount>0&&i.push(`Collapsed ${r.filteredCount} Android helper node${r.filteredCount===1?``:`s`} from the agent-facing text snapshot; use --raw or --json for the full hierarchy.`);let a=r.filteredCount>0?r.nodes:t;return!n.raw&&H(a)&&i.push(`Warning: possible repeated nav subtree detected.`),i}function be(e){let t=e.fallbackScreenshotPath;return typeof t==`string`&&t.length>0?[`Captured a screenshot of this screen automatically as visual truth: ${t}`]:[]}function xe(e){return!e.snapshotQuality&&!Se(e)}function Se(e){let t=e.snapshotDiagnostics;if(!t||typeof t!=`object`)return!1;let n=t.stats;return!!(n&&typeof n==`object`&&n.platform===`web`)}function Z(e,t){if(t.scoped===!0||t.depthLimited===!0||e.length>3)return null;let n=e.length===1?`node`:`nodes`;return`Hint: sparse accessibility snapshot returned ${e.length} ${n}; snapshot state is invalid or unavailable for this screen. Use plain screenshot, not screenshot --overlay-refs, as visual truth. If screenshot shows the Home Screen or another app, run open for this app again first. Then navigate away with coordinates if needed and retry snapshot -i on the next screen.`}function Q(e){return r(e).filter(e=>e.length>0)}function Ce(e,t){let n=[],r=[],i=new Map(e.map(e=>[e.node.index,e.node])),a=e=>{for(;r.length>0&&(!e||we(e,r.at(-1),i));)n.push(...$(r.pop(),`below`))};for(let i of e)a(i),n.push(Y(i.text,i.node,t)),n.push(...$(i,`above`)),i.node.hiddenContentBelow&&r.push(i);return a(),n}function we(e,t,n){return!Te(e.node,t.node,n)&&e.depth<=t.depth}function Te(e,t,n){let r=e;for(;typeof r.parentIndex==`number`;){if(r.parentIndex===t.index)return!0;let e=n.get(r.parentIndex);if(!e)return!1;r=e}return!1}function Ee(e,t){return s(e,{summarizeTextSurfaces:!0}).flatMap(e=>[Y(o(e.node,0,!1,e.type,{summarizeTextSurfaces:!0}),e.node,t),...$({...e,depth:0})])}function $(e,t){let n=De(e.type);if(!n)return[];let r=[];if(e.node.hiddenContentAbove&&t!==`below`&&r.push(`[content above ${n} hidden]`),e.node.hiddenContentBelow&&t!==`above`&&r.push(`[content below ${n} hidden]`),r.length===0)return[];let i=` `.repeat(e.depth+1);return r.map(e=>`${i}${e}`)}function De(e){return e===`scroll-area`||e===`list`||e===`collection`||e===`table`?e:null}export{Q as n,J as t};