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.03 kB
import{l as e}from"./sdk-batch.js";import{v as t}from"./sdk-contracts.js";import{t as n}from"./screenshot.js";import"./capture.js";import{i as r,r as i}from"./catalog.js";import"./src3.js";import{n as a,t as o}from"./flag-registry.js";import{r as s,t as c}from"./command-schema.js";import{i as l,t as u}from"./merge-flags.js";import{n as d,r as f,t as p}from"./cli-command-aliases.js";const m=`open <app> --relaunch`,h={start:{command:`open`,example:m},restart:{command:`open`,example:m},touch:{command:`press`,example:`press`},input:{command:`fill`,example:`fill`},settext:{command:`fill`,example:`fill`},entertext:{command:`fill`,example:`fill`},screencap:{command:`screenshot`,example:`screenshot`},capture:{command:`screenshot`,example:`screenshot`},dismiss:{command:`keyboard`,example:`keyboard dismiss`},"get-text":{command:`get`,example:`get text`},gettext:{command:`get`,example:`get text`},get_text:{command:`get`,example:`get text`},"open-url":{command:`open`,example:`open <url>`},"close-session":{command:`close`,example:`close`}};function g(e){if(e.length<=2)return[];let t=r(),n=t.filter(t=>t.startsWith(e));return n.length>0?n.sort(_).slice(0,3):v(e,t)}function _(e,t){return e.length-t.length||e.localeCompare(t)}function v(e,t){let n=t.map(t=>({name:t,distance:b(e,t)})).filter(t=>t.distance<=y(e));if(n.length===0)return[];let r=Math.min(...n.map(e=>e.distance));return n.filter(e=>e.distance===r).sort((e,t)=>e.name.localeCompare(t.name)).slice(0,3).map(e=>e.name)}function y(e){return e.length<4?1:e.length<=6?2:3}function b(e,t){return e===t?0:e.startsWith(t)||t.startsWith(e)?Math.abs(e.length-t.length):x(e,t)}function x(e,t){let n=e.length+1,r=t.length+1,i=Array.from({length:n},()=>Array(r).fill(0));for(let e=0;e<n;e+=1)i[e][0]=e;for(let e=0;e<r;e+=1)i[0][e]=e;for(let a=1;a<n;a+=1)for(let n=1;n<r;n+=1){let r=e[a-1]===t[n-1]?0:1;i[a][n]=Math.min(i[a-1][n]+1,i[a][n-1]+1,i[a-1][n-1]+r)}return i[n-1][r-1]}function S(e){let t=e.toLowerCase(),n=h[t]?.example;if(n)return n;let r=g(t);if(r.length!==0)return r.length===1?r[0]:`one of: ${r.join(`, `)}`}const C=new Set([`--bundle-id`,`--bundleid`,`--bundle`,`--package`,`--package-name`,`--packagename`,`--app-id`,`--appid`,`--pkg`]),w=new Set([`--session-locked`,`--session-lock-conflicts`]),ee={"--output":`--out`,"--path":`--out`};function T(e,t){let n=e.toLowerCase(),r=ee[n];if(r&&E(r,t))return r;let i=D(n,t);if(i.length!==0)return i.length===1?i[0]:`one of: ${i.join(`, `)}`}function E(e,t){let n=a().find(t=>t.names.includes(e));return n?l(n.key,t):!1}function D(e,t){let n=k(e);if(n.length<=2)return[];let r=O(t),i=r.filter(e=>k(e).startsWith(n));return i.length>0?i.sort(_).slice(0,3):v(n,r.map(e=>k(e))).map(e=>`--${e}`)}function O(e){let t=new Set;for(let n of a()){if(!l(n.key,e))continue;let r=n.names[0];r&&t.add(r)}return[...t]}function k(e){return e.replace(/^--?/,``)}function A(e,t){let r=e.toLowerCase();if(C.has(r))return`Unknown flag: ${e}. The app or bundle id is a positional argument, e.g. ${m}.`;if(w.has(r))return`Unknown flag: ${e}. Use --session-lock reject|strip instead.`;if(r===n.cliToken)return`Unknown flag: ${e}. ${n.migration.screenshot}. ${n.migration.record}.`;let i=T(e,t);return i?`Unknown flag: ${e}. Did you mean ${i}?`:`Unknown flag: ${e}`}function j(e){let n={json:!1,help:!1,version:!1},r=null,i=null,a=[],o=[],s=[],c=!0;for(let o=0;o<e.length;o+=1){let l=e[o];if(c&&l===`--`){c=!1;continue}if(!c){r?a.push(l):(i=l,r=$(l));continue}if(F(r)){a.push(l);continue}let u=l.startsWith(`--`),d=l.startsWith(`-`)&&l.length>1;if(!u&&!d){r?a.push(l):(i=l,r=$(l));continue}let[f,p]=u?z(l):[l,void 0];if(N(r,f))continue;let m=I(f,r);if(P(r,m)){a.push(l);continue}if(!m){if(W(r,a,l)){r?a.push(l):r=l;continue}throw new t(`INVALID_ARGS`,A(f,r))}let h=B(m,f,p,e[o+1]);h.consumeNext&&(o+=1);let g=n[m.key];if(m.multiple){let e=Array.isArray(g)?[...g,h.value]:g===void 0?[h.value]:[g,h.value];n[m.key]=e}else n[m.key]=h.value;s.push({key:m.key,token:f})}return M(i,n),{command:r,positionals:a,flags:n,warnings:o,providedFlags:s}}function M(e,t){if(e)for(let n of p(e)?.impliedFlags??[])t[n]=!0}function N(e,t){return t===`-c`&&(e===`snapshot`||e===`diff`)}function P(e,t){return e===`react-devtools`?!t||!l(t.key,e):!1}function F(e){return e===`cdp`}function I(e,t){let n=a().filter(t=>t.names.includes(e));if(n.length<=1)return n[0]??o(e);if(t){let e=n.find(e=>l(e.key,t));if(e)return e}return o(e)}function L(n,r){let a=r?.strictFlags??!0,o=[...n.warnings],s=u({json:!1,help:!1,version:!1},r?.defaultFlags??{});if(u(s,n.flags),n.command&&!i(n.command)&&!s.help){let e=S(n.command),r=e?`Unknown command: ${n.command}. Did you mean ${e}?`:`Unknown command: ${n.command}`;throw new t(`INVALID_ARGS`,r)}let d=n.providedFlags.filter(e=>!l(e.key,n.command));if(d.length>0){let e=d.map(e=>e.token),r=X(n.command,e);if(a)throw new t(`INVALID_ARGS`,r);o.push(r);for(let e of d)delete s[e.key]}let f=J(n);if(f.length>0){let e=Y(n.command,n.positionals[0],f);if(a)throw new t(`INVALID_ARGS`,e);o.push(e);for(let e of f)delete s[e.key]}for(let e of Object.keys(s))s[e]!==void 0&&(l(e,n.command)||delete s[e]);q(n,s),R(n),e(n.command,s);let p=K({command:n.command,positionals:n.positionals,flags:s,warnings:o});if(c(p.command,p.positionals),p.command===`batch`&&+!!s.steps+ +!!s.stepsFile!=1)throw new t(`INVALID_ARGS`,`batch requires exactly one step source: --steps or --steps-file.`);return p}function R(e){if(e.command!==`back`)return;let n=e.providedFlags.filter(e=>e.key===`backMode`);if(!(new Set(n.map(e=>e.token)).size<=1))throw new t(`INVALID_ARGS`,`back accepts only one explicit mode flag: use either --in-app or --system.`)}function z(e){let t=e.indexOf(`=`);return t===-1?[e,void 0]:[e.slice(0,t),e.slice(t+1)]}function B(e,n,r,i){if(e.setValue!==void 0){if(r!==void 0)throw new t(`INVALID_ARGS`,`Flag ${n} does not take a value.`);return{value:e.setValue,consumeNext:!1}}if(e.type===`boolean`){if(r!==void 0)throw new t(`INVALID_ARGS`,`Flag ${n} does not take a value.`);return{value:!0,consumeNext:!1}}if(e.type===`booleanOrString`){if(r!==void 0){if(r.trim().length===0)throw new t(`INVALID_ARGS`,`Flag ${n} requires a non-empty value when provided.`);return{value:r,consumeNext:!1}}return i===void 0||H(i)?{value:!0,consumeNext:!1}:U(i)?{value:i,consumeNext:!0}:{value:!0,consumeNext:!1}}let a=r??i;if(a===void 0||r===void 0&&H(a))throw new t(`INVALID_ARGS`,`Flag ${n} requires a value.`);if(e.type===`string`)return{value:a,consumeNext:r===void 0};if(e.type===`enum`){if(!e.enumValues?.includes(a))throw new t(`INVALID_ARGS`,`Invalid ${V(n)}: ${a}`);return{value:a,consumeNext:r===void 0}}if(a.trim().length===0)throw new t(`INVALID_ARGS`,`Flag ${n} requires a value.`);let o=Number(a);if(!Number.isFinite(o)||typeof e.min==`number`&&o<e.min||typeof e.max==`number`&&o>e.max)throw new t(`INVALID_ARGS`,`Invalid ${V(n)}: ${a}`);return{value:e.type===`int`?Math.floor(o):o,consumeNext:r===void 0}}function V(e){return e.replace(/^-+/,``)}function H(e){if(!e.startsWith(`-`)||e===`-`)return!1;let[t]=e.startsWith(`--`)?z(e):[e,void 0];return o(t)!==void 0}function U(e){let t=e.trim();return!t||/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//.test(t)?!1:!!(t.startsWith(`./`)||t.startsWith(`../`)||t.startsWith(`~/`)||t.startsWith(`/`)||t.includes(`/`)||t.includes(`\\`))}function W(e,t,n){if(!G(n)||!e)return!1;let r=s(e);if(!r||r.allowsExtraPositionals)return!0;let i=r.positionalArgs??[];return i.length===0?!1:t.length<i.length||i.some(e=>e.includes(`?`))}function G(e){return/^-\d+(\.\d+)?$/.test(e)}function K(e){if(e.flags.help)return e;if(e.command===`snapshot`&&e.flags.snapshotDiff){let{snapshotDiff:t,...n}=e.flags;return{command:`diff`,positionals:[`snapshot`,...e.positionals],flags:n,warnings:e.warnings}}return e}function q(e,t){let n=s(e.command)?.explicitOnlyFlags;if(n===void 0)return;let r=new Set(e.providedFlags.map(e=>e.key));for(let e of n)r.has(e)||delete t[e]}function J(e){let t=s(e.command)?.flagsByAction,n=e.positionals[0];if(t===void 0||n===void 0||!Object.hasOwn(t,n))return[];let r=t[n];if(r===void 0)return[];let i=new Set(Object.values(t).flat());return e.providedFlags.filter(e=>i.has(e.key)&&!r.includes(e.key))}function Y(e,t,n){return`${e} ${t} does not read ${n.map(e=>e.token).join(`, `)}. Run \`${e} ${t} --help\` for the options it reads.`}function X(e,t){return e?t.length===1?`Flag ${t[0]} is not supported for command ${e}.`:`Flags ${t.join(`, `)} are not supported for command ${e}.`:t.length===1?`Flag ${t[0]} requires a command that supports it.`:`Flags ${t.join(`, `)} require a command that supports them.`}async function Z(){let{buildUsageText:e}=await import(`./cli-help.js`).then(e=>e.n);return e()}async function Q(e){let{buildCommandUsageText:t}=await import(`./cli-help.js`).then(e=>e.n);return t($(e))}async function te(e){let{buildCommandSynopsis:n}=await import(`./cli-help.js`).then(e=>e.n);try{return n($(e))}catch(e){if(e instanceof t)return null;throw e}}function $(e){let n=f(e);if(n)throw new t(`INVALID_ARGS`,n);return d(e)}export{Q as a,Z as i,L as n,S as o,j as r,te as t};