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 • 2.61 kB
import{$r as e,Jr as t,Xr as n,Yr as r,Zr as i}from"./sdk-batch.js";import{v as a}from"./sdk-contracts.js";function o(e){return e?.startsWith(`@`)?e.slice(1):e}const s=/^e\d+$/;function c(t){if(t[0]?.startsWith(`@`)){let e=_(t.slice(1));return{ref:t[0],...e===void 0?{}:{label:e}}}if(s.test(t[0]??``))throw new a(`INVALID_ARGS`,`Did you mean "@${t[0]}"? Snapshot refs need the @ prefix.`);let n=e(t);if(n){if(n.rest.length>0)throw new a(`INVALID_ARGS`,m(t,n));return{selector:n.selectorExpression}}return u(t)}function l(t){let n=t[0];if(n?.startsWith(`@`)){let e=t.slice(1),r=e.length===0?void 0:e.join(` `);return{kind:`ref`,target:{ref:n},text:r}}if(s.test(n??``))throw new a(`INVALID_ARGS`,`Did you mean "@${n}"? Snapshot refs need the @ prefix.`);let r=e(t,{preferTrailingValue:!0});if(r)return{kind:`selector`,target:{selector:r.selectorExpression},text:r.rest.length===0?void 0:r.rest.join(` `)};let i=t.slice(2);return{kind:`point`,target:u(t.slice(0,2)),text:i.length===0?void 0:i.join(` `)}}function u(e){let t=e[0]??``,n=r(t);if(n){let r=d(t)?n.value:[n.value,...e.slice(1)].join(` `).trim();throw new a(`INVALID_ARGS`,f({key:n.key,value:r}))}let i=Number(e[0]),o=Number(e[1]);if(Number.isFinite(i)&&Number.isFinite(o))return{x:i,y:o};throw new a(`INVALID_ARGS`,p(e))}function d(e){let t=e.slice(e.indexOf(`=`)+1).trim();if(t.length<2)return!1;let n=t[0];return(n===`"`||n===`'`)&&t.endsWith(n)}function f(e){let{key:r,value:i}=e;return`Unknown selector key "${r}". Supported: ${t.join(`, `)}. ${n(r)?`Did you mean role=${r} label=${g(i)}?`:`Did you mean label=${g(i)}?`}`}function p(e){let t=e.filter(e=>e!==void 0).join(` `);if(!t.trim())return`Missing target. Pass an @ref from snapshot, a selector like text="Sign in", or "x y" coordinates.`;let n=`Target "${t}" is not a @ref, selector, or "x y" point.`;return e.some(e=>i(e??``))?`${n} Selector values with spaces need quotes, e.g. text="Sign in".`:`${n} To match by visible text, use text=${g(t)} (or label=/id=/role=), or pass an @ref from snapshot.`}function m(e,t){return`${`Selector ${t.selectorExpression} is followed by unexpected extra arguments: "${t.rest.join(` `)}".`} Selector values with spaces need quotes, e.g. ${h(e,t)??`text="Sign in"`}.`}function h(e,t){let n=e.length-t.rest.length,r=e[n-1];if(r===void 0)return;let i=r.indexOf(`=`);if(i<=0)return;let a=[r.slice(i+1),...t.rest].join(` `),o=`${r.slice(0,i+1)}${g(a)}`;return[...e.slice(0,n-1),o].join(` `)}function g(e){return`"${e.replaceAll(`\\`,String.raw`\\`).replaceAll(`"`,String.raw`\"`)}"`}function _(e){return e.join(` `).trim()||void 0}export{c as n,o as r,l as t};