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.
2 lines • 8.85 kB
JavaScript
import{C as e,Nr as t,a as n,n as r,r as i}from"./sdk-batch.js";import{S as a,c as o,f as s,o as c,v as l}from"./sdk-contracts.js";function u(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function d(e,t,n,r){let i=n(e[t]);if(i===void 0)throw new l(`COMMAND_FAILED`,r,{response:e});return i}function f(e,t,n){return n(e[t])}function p(e,t,n){let r=e[t];return r===null?null:n(r)}function ee(e,t){return d(e,t,O,`Daemon response is missing "${t}".`)}function m(e,t){return f(e,t,O)}function h(e,t){return p(e,t,O)}function g(e,t){return d(e,t,k,`Daemon response is missing numeric "${t}".`)}function _(e,t){return d(e,t,A,`Daemon response has invalid "${t}".`)}function te(e,t){return d(e,t,j,`Daemon response has invalid "${t}".`)}function ne(e,t){return f(e,t,j)}function v(e,t){return f(e,t,M)??`mobile`}function y(e){if(!P(e))return;let t=D(e,`x`),n=D(e,`y`),r=D(e,`width`),i=D(e,`height`);if(t!==void 0&&n!==void 0&&r!==void 0&&i!==void 0)return{x:t,y:n,width:r,height:i}}function re(e){if(e!==void 0)return P(e)?e.kind===`absent`?{kind:`absent`,...b(e)}:e.kind===`unmeasurable`?typeof e.reason==`string`&&e.reason.trim().length>0?{kind:`unmeasurable`,reason:e.reason}:{kind:`unmeasurable`,reason:`unreported-reason`}:e.kind===`tree-rejected`?x(e):e.kind===`visibility-only`?typeof e.visible==`boolean`?{kind:`visibility-only`,visible:e.visible}:{kind:`unmeasurable`,reason:`invalid-visibility-probe`}:e.kind===`derived`&&e.derivation!==`docked-tree`?{kind:`unmeasurable`,reason:`unrecognized-derivation`}:e.kind===`visible`||e.kind===`derived`?S(e.frame,e.kind,b(e)):{kind:`unmeasurable`,reason:`unrecognized-kind`}:{kind:`unmeasurable`,reason:`malformed-fact`}}function b(e){return e.treeBound===!0?{treeBound:!0}:{}}function x(e){return e.derivation===`docked-tree`?e.visible!==void 0&&typeof e.visible!=`boolean`?{kind:`unmeasurable`,reason:`invalid-visibility-probe`}:{kind:`tree-rejected`,derivation:`docked-tree`,...typeof e.visible==`boolean`?{visible:e.visible}:{}}:{kind:`unmeasurable`,reason:`unrecognized-derivation`}}function S(e,t,n){let r=y(e);return C(r)?t===`derived`?{kind:`derived`,derivation:`docked-tree`,frame:r,...n}:{kind:`visible`,frame:r,...n}:{kind:`unmeasurable`,reason:`invalid-${t}-frame`}}function C(e){if(!e)return!1;let{x:t,y:n,width:r,height:i}=e;return![t,n,r,i].every(Number.isFinite)||!Number.isFinite(t+r)||!Number.isFinite(n+i)||r<=0||i<=0?!1:t!==-Number.MAX_VALUE/2||n!==-Number.MAX_VALUE/2||r!==Number.MAX_VALUE||i!==Number.MAX_VALUE}const w={presented:`presented`,absent:`absent`,unknown:`unknown`},T=`presentedSurface`;function E(e){if(!Object.hasOwn(e,T))return;let t=e[T];return typeof t==`string`&&Object.hasOwn(w,t)?w[t]:`unknown`}function ie(e){if(!P(e))return;let t=D(e,`x`),n=D(e,`y`);if(t!==void 0&&n!==void 0)return{x:t,y:n}}function D(e,t){let n=e[t];return typeof n==`number`?n:void 0}function O(e){return typeof e==`string`&&e.length>0?e:void 0}function k(e){return typeof e==`number`&&Number.isFinite(e)?e:void 0}function A(e){return e===`apple`?`ios`:e===`ios`||e===`macos`||e===`android`||e===`harmonyos`||e===`vega`||e===`linux`||e===`web`?e:void 0}function j(e){return e===`simulator`||e===`emulator`||e===`device`?e:void 0}function M(e){return e===`tv`||e===`mobile`||e===`desktop`?e:void 0}function N(e){if(!P(e))throw new l(`COMMAND_FAILED`,`Daemon returned an unexpected response shape.`,{value:e});return e}function P(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function F(e){return P(e)?e:void 0}function I(e){let t={};for(let[n,r]of Object.entries(e))r!==void 0&&(t[n]=r);return t}function L(e){return e.split(`
`).map(e=>e.trim()).filter(e=>e.length>0)}function R(e){return Number.isInteger(e)&&e>=1&&e<=1e3}function z(e,t){if(e>t)throw new l(`INVALID_ARGS`,`batch has ${e} steps; max allowed is ${t}.`)}const B=`Each batch step is {"command":"<name>","input":{...}}, where input is that command's own structured input object, keyed by field name. There is no positional step form: args, argv, positionals, and flags are not step fields.`;function V(e,t,n=B){if(!u(e))throw new l(`INVALID_ARGS`,`Invalid batch step ${t}.`,{hint:n});return e}function H(e,t,n=B){let r=e.input;if(!u(r))throw new l(`INVALID_ARGS`,`Batch step ${t} input must be an object.`,{hint:n});return r}function U(e,t){if(e!==void 0)try{return c.parse(e)}catch(e){throw new l(`INVALID_ARGS`,`Batch step ${t} runtime is invalid: ${e instanceof Error?e.message:String(e)}`)}}function W(e){let t=e.trim();if(!t||/\s/.test(t))return!1;let n=/^([A-Za-z][A-Za-z0-9+.-]*):(.+)$/.exec(t);if(!n)return!1;let r=n[1]?.toLowerCase(),i=n[2]??``;return r===`http`||r===`https`||r===`ws`||r===`wss`||r===`ftp`||r===`ftps`?i.startsWith(`//`):!0}function G(e){let t=e.trim().split(`:`)[0]?.toLowerCase();return t===`http`||t===`https`}const K=`com.apple.mobilesafari`;function q(e,t){let n=e?.trim();if(n)return n;if(G(t))return K}const J=new Set(r);async function Y(e,t,n){let r=Q(e.flags),i=r?.batchOnError??`stop`;if(i!==`stop`)return $(`INVALID_ARGS`,`Unsupported batch on-error mode: ${i}.`);let o=r?.batchMaxSteps??100;if(!R(o))return $(`INVALID_ARGS`,`Invalid batch max-steps: ${String(r?.batchMaxSteps)}`);try{let i=X(r?.batchSteps,o),a=Date.now(),s=[];for(let[r,a]of i.entries()){let o=await de(e,t,a,n,{stepNumber:r+1,totalSteps:i.length,remainingSteps:i.slice(r+1).map(e=>({command:e.command,positionals:e.positionals,flags:e.flags,...e.input===void 0?{}:{input:e.input}}))});if(!o.ok)return ae(o,a,{executedSteps:i.slice(0,r),total:i.length,partialResults:s});s.push(o.result)}return{ok:!0,data:{total:i.length,executed:i.length,totalDurationMs:Date.now()-a,results:s}}}catch(e){let t=a(e);return $(t.code,t.message,t.details)}}function ae(e,t,n){let{error:r}=e;return{ok:!1,error:{code:r.code,message:`Batch failed at step ${e.step} (${t.command}): ${r.message}`,hint:r.hint,diagnosticId:r.diagnosticId,logPath:r.logPath,...r.retriable===void 0?{}:{retriable:r.retriable},...r.supportedOn===void 0?{}:{supportedOn:r.supportedOn},details:{...r.details??{},...n.executedSteps.some(oe)&&(r.details?.reason!==`bounded_composite_resource_unsupported`||r.details.dispatched!==`no`)?{dispatched:`unknown`}:{},step:e.step,command:t.command,positionals:t.positionals,executed:n.executedSteps.length,total:n.total,partialResults:n.partialResults}}}}function oe(t){return e(t)!==`observes-app`}function X(e,r){if(!Array.isArray(e)||e.length===0)throw new l(`INVALID_ARGS`,`batch requires a non-empty batchSteps array.`);z(e.length,r);let i=[];for(let r=0;r<e.length;r+=1){let a=e[r];if(!P(a))throw new l(`INVALID_ARGS`,`Invalid batch step at index ${r}.`);let o=Object.keys(a).filter(e=>!J.has(e));if(o.length>0){let e=o.map(e=>`"${e}"`).join(`, `);throw new l(`INVALID_ARGS`,`Batch step ${r+1} has unknown field(s): ${e}. Allowed fields: command, positionals, input, flags, runtime.`)}let s=t(a.command);if(!s)throw new l(`INVALID_ARGS`,`Batch step ${r+1} requires command.`);if(n(s,r+1),a.positionals!==void 0&&!Array.isArray(a.positionals))throw new l(`INVALID_ARGS`,`Batch step ${r+1} positionals must be an array.`);let c=a.positionals??[];if(c.some(e=>typeof e!=`string`))throw new l(`INVALID_ARGS`,`Batch step ${r+1} positionals must contain only strings.`);if(a.flags!==void 0&&!P(a.flags))throw new l(`INVALID_ARGS`,`Batch step ${r+1} flags must be an object.`);if(se(a.flags,r+1),a.input!==void 0&&!P(a.input))throw new l(`INVALID_ARGS`,`Batch step ${r+1} input must be an object.`);i.push({command:s,positionals:c,input:a.input,flags:a.flags??{},runtime:U(a.runtime,r+1)})}return i}function se(e,t){if(e?.textStdin===!0)throw new l(`INVALID_ARGS`,`Batch step ${t}: fill textStdin is not supported because batch steps have no stdin of their own.`,{reason:`fill_text_stdin_in_batch`})}function ce(e,t){let{batchSteps:n,batchOnError:r,batchMaxSteps:i,...a}=t??{};return Z(Q(e),a)}function Z(e,t){let n=Q(e)??{},r=t;for(let e of i)r[e]===void 0&&n[e]!==void 0&&(r[e]=n[e]);return t}function le(e,t){return o(e)?t?e:`digest`:e}function ue(e,t){let n=e?.responseLevel,r=le(n,t);return r===n?e:{...e,responseLevel:r}}async function de(e,t,n,r,i){let{stepNumber:a,totalSteps:o}=i,c=a===o,l=Date.now(),u=ce(e.flags,n.flags);u.session===void 0&&s(e)&&(u.session=t);let d=await r({token:e.token,session:t,command:n.command,positionals:n.positionals,input:n.input,flags:u,runtime:n.runtime===void 0?e.runtime:n.runtime,meta:ue(e.meta,c)},i),f=Date.now()-l;return d.ok?{ok:!0,step:a,result:{step:a,command:n.command,ok:!0,data:d.data??{},durationMs:f}}:{ok:!1,step:a,error:d.error}}function Q(e){return e}function $(e,t,n){return{ok:!1,error:{code:e,message:t,...n?{details:n}:{}}}}export{I as A,E as C,ee as D,_ as E,re as O,m as S,g as T,ie as _,W as a,h as b,B as c,U as d,H as f,P as g,N as h,K as i,u as j,L as k,z as l,F as m,Y as n,G as o,V as p,X as r,q as s,Z as t,R as u,y as v,te as w,ne as x,v as y};