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.63 kB
import{di as e}from"./sdk-batch.js";import{v as t}from"./sdk-contracts.js";import{t as n}from"./bounds.js";import{t as r}from"./parser.js";function i(e){try{return r(e)}catch(e){throw new t(`COMMAND_FAILED`,`Failed to parse WebDriver page source XML: ${e instanceof Error?e.message:String(e)}`,void 0,e)}}function a(e){let t=n(e.bounds??null);if(t)return t;let r=u(e.x),i=u(e.y),a=u(e.width),o=u(e.height);if(r!==void 0&&i!==void 0&&a!==void 0&&o!==void 0)return{x:r,y:i,width:a,height:o}}function o(e,t){for(let n of t){let t=s(e[n]);if(t!==void 0)return t}}function s(e){return e||void 0}function c(e,t){return e===void 0?t:e===`true`||e===`1`?!0:e===`false`||e===`0`?!1:t===void 0&&void 0}function l(e){return!!(e&&e.width>0&&e.height>0)}function u(e){if(e===void 0||e===``)return;let t=Number(e);return Number.isFinite(t)?t:void 0}function d(e,t){return s(t.class)??s(e.replace(/^XCUIElementType/,``).toLowerCase())}function f(e,t=`ios`){let n=i(e),r=[],a=[];for(let e of n)p(r,e,void 0,0,a,t);return{nodes:r,roots:a}}function p(e,t,n,r,i,a){let o=m(t,a)?n:g(e,t,n,r,i,a),s=o===n?r:r+1;for(let n of t.children)p(e,n,o,s,i,a)}function m(e,t){if(Object.keys(e.attributes).length===0)return!0;let n=e.name.toLowerCase();return t===`ios`&&(n===`hierarchy`||n===`appiumaut`)}function h(e,t){return t===`android`?s(e.attributes.class)??e.name:e.name}function g(e,t,n,r,i,o){let s=e.length,c=a(t.attributes),l=h(t,o);return e.push(_(s,l,t.attributes,n,r,c,o)),n===void 0&&i.push({type:l,...c?{rect:c}:{},rectStatus:O(t.attributes,c)}),s}function _(e,t,n,r,i,a,l){return{index:e,type:t,role:d(t,n),...v(n,l),...l===`android`?y(t,n):{value:s(n.value)},identifier:o(n,[`resource-id`,`id`,`accessibility-id`,`name`]),...l===`android`&&n.package?{bundleId:n.package}:{},rect:a,...T(n,a,l),...C(t,n,l),...x(`heading`,c(n.heading)),...x(`roleDescription`,o(n,[`roleDescription`,`role-description`])??(l===`android`?w(n.extras):void 0)),depth:i,parentIndex:r}}function v(e,t){if(t===`ios`)return{label:o(e,[`label`,`text`,`name`])};let n=s(e[`content-desc`]),r=s(e.text),i=r??n;return{label:i,...n!==void 0&&n!==i?{contentDescription:n}:{},...i!==void 0&&r===void 0?{text:``}:{}}}function y(t,n){let r=c(n[`showing-hint`]??n[`hint-showing`]??n.hintShowing);return{value:e({type:t,text:n.text,editable:c(n.editable)===!0||b(n[`input-type`]),password:c(n.password),hintShowing:r}),...x(`placeholder`,s(n.hint)),...x(`hintShowing`,r)}}function b(e){let t=u(e);if(t===void 0||!Number.isInteger(t)||t<0||t>2147483647)return!1;let n=t&15;return n>=1&&n<=4}function x(e,t){return t===void 0?{}:{[e]:t}}const S=/(CheckBox|RadioButton|Switch|ToggleButton|CheckedTextView)$/;function C(e,t,n){let r={...x(`password`,c(t.password)),...x(`editable`,c(t.editable))},i=c(t.checked);if(n===`ios`)return{...r,...x(`checked`,i)};let a=c(t.checkable)??S.test(e);return{...r,...a&&i!==void 0?{checked:i}:{}}}function w(e){let t=e?.split(`;`).find(e=>e.startsWith(`AccessibilityNodeInfo.roleDescription=`));return s(t?.slice(38))}function T(e,t,n){let r=n===`android`||void 0,i=c(e.enabled,r),a=c(e.displayed??e.visible,r);return n===`android`?{enabled:i,selected:c(e.selected,!1),focused:c(e.focused,!1),visibleToUser:a,hittable:a===!0&&i===!0&&l(t)}:{...E(`enabled`,i),...E(`selected`,c(e.selected)),...E(`focused`,c(e.focused)),...E(`visibleToUser`,a),...D(e.hittable)}}function E(e,t){return t===void 0?{}:{[e]:t}}function D(e){let t=c(e);return t===void 0?{}:{hittable:t}}function O(e,t){let n=e.bounds!==void 0,r=[`x`,`y`,`width`,`height`].every(t=>e[t]!==void 0);return!n&&!r?`not-provided`:l(t)?`reported`:`invalid`}export{f as parseWebDriverSourceFacts};