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 • 7.8 kB
JavaScript
import{C as e,O as t,X as n,v as r}from"./sdk-contracts.js";import{c as i,r as a,u as o}from"./alert-contract.js";import{C as s,m as c}from"./host2.js";import l from"node:crypto";const u=`RUNNER_BUSY`,d=`MAIN_THREAD_TIMEOUT`,ee=`COMMAND_ALREADY_FINISHED`,f=1e3,p=`RUNNER_WEDGED`,m=`APP_NOT_RUNNING`;function te(e){let t=c(e.requestId);return e.signal?!t||t===e.signal?e.signal:AbortSignal.any([t,e.signal]):t}function h(e){return e instanceof DOMException&&e.name===`TimeoutError`}function g(e){return s(e.requestId)||c(e.requestId)?.aborted?!1:e.signal?.aborted===!0&&h(e.signal.reason)}function _(e){let t=c(e.requestId),n=e.signal;if(!n||n===t)return t;let r=new AbortController,i=()=>{h(n.reason)||r.abort(n.reason)};return n.aborted?i():n.addEventListener(`abort`,i,{once:!0}),t?AbortSignal.any([t,r.signal]):r.signal}const v=`SCROLL_KEYBOARD_OCCLUDES_SURFACE`,y=`KEYBOARD_DISMISS_UNAVAILABLE`,b=`KEYBOARD_RETURN_UNAVAILABLE`,x=`HARDWARE_CONTROL_UNAVAILABLE`,S=`IN_APP_BACK_CONTROL_UNAVAILABLE`,C=new Set([`APP_SCREEN_WINDOW_UNRESOLVED`,`APP_SCREEN_UNRESOLVED`,`APP_SCREEN_CAPTURE_UNRENDERABLE`,`APP_SCREEN_OFF_LIT_PANEL`]),w=`NO_TEXT_INPUT_AT_POINT`,T=`TEXT_INPUT_PROBE_UNAVAILABLE`,E=`SYNTHESIZED_GESTURE_NOT_DISPATCHED`,D=`SYNTHESIZED_GESTURE_OUTCOME_UNKNOWN`,O=`BACK_SWIPE_NO_CHANGE`,k=`BACK_SWIPE_UNVERIFIED`,A=`BACK_SWIPE_NOT_DISPATCHED`,ne=new Map([[u,{retriable:!0}],[d,{}],[m,{retriable:!0}],[i,{}],[v,{}],[y,{}],[b,{}],[x,{}],[S,{}],[E,{}],[D,{}],[O,{}],[k,{}],[A,{}],...[...C].map(e=>[e,{}])]),re=new Map([[y,`UNSUPPORTED_OPERATION`],[b,`UNSUPPORTED_OPERATION`],[x,`UNSUPPORTED_OPERATION`]]),ie=new Map([[u,`runner_busy`],[d,`runner_main_thread_timeout`],[w,`no_text_input_at_point`],[T,`text_input_probe_unavailable`],[E,`synthesized_gesture_not_dispatched`],[D,`synthesized_gesture_outcome_unknown`],[O,`back_swipe_no_change`],[k,`back_swipe_unverified`],[A,`back_swipe_not_dispatched`],[y,`keyboard_dismiss_unavailable`],[b,`keyboard_return_unavailable`],[x,`hardware_control_unavailable`],[S,`in_app_back_control_unavailable`]]),j=new Map([[`INVALID_ARGS`,`no`],[`ELEMENT_NOT_FOUND`,`no`],[`ELEMENT_OFFSCREEN`,`no`],[`AMBIGUOUS_MATCH`,`no`],[u,`no`],[p,`no`],[m,`no`],[`OBSERVATION_UNAVAILABLE`,`no`],[v,`no`],[i,`no`],[w,`no`],[T,`no`],[a,`no`],[o,`no`],[E,`no`],[A,`no`],[y,`no`],[x,`no`],[S,`no`],...[...C].map(e=>[e,`no`])]);function M(e){let t=e===void 0?void 0:ne.get(e),r=e===void 0?void 0:re.get(e),i=e===void 0?void 0:ie.get(e);return Object.freeze({code:t?r??`COMMAND_FAILED`:n(e),details:Object.freeze({runnerErrorCode:e,...t?.retriable===!0?{retriable:!0}:{},...i===void 0?{}:{reason:i},dispatched:(e===void 0?void 0:j.get(e))??`unknown`})})}function N(e,t){if(e!==O)return[];let n=t?.dismissControls;return Array.isArray(n)?n.flatMap(e=>{if(typeof e!=`object`||!e)return[];let{label:t,identifier:n,pressBy:r}=e;if(typeof t!=`string`)return[];let i=typeof n==`string`&&n.length>0?n:void 0,a=r===`identifier`&&i!==void 0?`role=button id=${JSON.stringify(i)}`:r===`label`&&t.length>0?`role=button label=${JSON.stringify(t)}`:void 0;return a===void 0?[]:[{selector:a,label:t,...i===void 0?{}:{identifier:i}}]}):[]}function P(e){let[t]=e;if(t!==void 0)return e.length===1?`Press '${t.selector}' to dismiss this screen; back only pops the navigation stack.`:`Press one of details.dismissControls by its selector to dismiss this screen; back only pops the navigation stack.`}function F(e){let t;try{t=JSON.parse(e)}catch{throw new r(`COMMAND_FAILED`,`Invalid runner response`,{text:e})}if(!I(t))throw new r(`COMMAND_FAILED`,`Invalid runner response`,{text:e});return t}function I(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function L(e){return e.ok===!0}function R(e){return!e.data||typeof e.data!=`object`||Array.isArray(e.data)?{}:e.data}function z(e,t){let n=B(e.error?.code),i=typeof e.error?.message==`string`?e.error.message:void 0,a=typeof e.error?.hint==`string`?e.error.hint:void 0,o=M(n),s=N(n,e.error);return new r(o.code,i??`Runner error`,{runner:e,...o.details,...s.length>0?{dismissControls:s}:{},xcodebuild:{exitCode:1,stdout:``,stderr:``},hint:P(s)??a,logPath:t})}function B(e){return typeof e==`string`&&e.trim().length>0?e.trim():void 0}function V(e){return e.command===`status`||e.commandId?.trim()?e:{...e,commandId:H()}}function H(){return`runner-${l.randomUUID()}`}function U(t){if(s(t))throw e()}const W=`runner_token_unsupported`;function G(){return l.randomBytes(24).toString(`base64url`)}function K(e,t){return{AGENT_DEVICE_RUNNER_PORT:String(e.port),AGENT_DEVICE_RUNNER_TOKEN:e.token,...t?{AGENT_DEVICE_RUNNER_BIND_IP:t}:{}}}function q(e){return{"Content-Type":`application/json`,"X-Agent-Device-Runner-Token":e.token}}function J(e){if(e.headers.get(`X-Agent-Device-Runner-Auth`)===`required`)return e;throw new r(`COMMAND_FAILED`,`XCTest runner does not require a listener token`,{reason:W,hint:`Rebuild the runner with this agent-device version. A configured iosXctestrunFile must come from the same version.`})}const Y=[`tap`,`doubleTap`,`longPress`];function ae(e){return typeof e==`string`&&Y.includes(e)}function X(e,n){return t(new r(`INVALID_ARGS`,e,{...n,reason:`runner_sequence_invalid`}),`no`)}function Z(e,t,n){return X(n,{stepIndex:e,kind:typeof t==`string`?t:void 0})}function oe(e){if(!Array.isArray(e)||e.length===0)throw X(`sequence requires at least one step`,{stepCount:Array.isArray(e)?e.length:0});if(e.length>20)throw X(`sequence accepts at most 20 steps, received ${e.length}`,{stepCount:e.length,maxSteps:20});e.forEach((e,t)=>se(e,t))}function se(e,t){if(!ae(e.kind))throw Z(t,e.kind,`sequence step ${t} has unsupported kind "${String(e.kind)}"; allowed: ${Y.join(`, `)}`);if(!Number.isFinite(e.x)||!Number.isFinite(e.y))throw Z(t,e.kind,`sequence step ${t} (${e.kind}) requires finite x and y`);e.durationMs!==void 0&&Q(e.durationMs,0,1e4,t,e.kind,`durationMs`),e.pauseMs!==void 0&&Q(e.pauseMs,0,1e4,t,e.kind,`pauseMs`)}function Q(e,t,n,r,i,a){if(!Number.isFinite(e)||e<t||e>n)throw Z(r,i,`sequence step ${r} (${i}) ${a} must be between ${t} and ${n}`)}function $(e,t){return oe(e),{command:`sequence`,steps:e,appBundleId:t}}function ce(e){let t=de(e.sequenceResults),n=typeof e.completedSteps==`number`&&Number.isFinite(e.completedSteps)?e.completedSteps:t.filter(e=>e.ok).length,r=t.findIndex(e=>!e.ok),i=typeof e.failedStepIndex==`number`&&Number.isFinite(e.failedStepIndex)?e.failedStepIndex:r>=0?r:void 0;if(i!==void 0)throw le(t,n,i);let a=e.sequenceOrientation;return{results:t,completedSteps:n,failedStepIndex:i,...a===1||a===2||a===3||a===4?{sequenceOrientation:a}:{}}}function le(e,t,i){let a=e[i],o=a?.kind??`step`,s=a?.errorMessage??`sequence step ${i} (${o}) failed`,c=n(a?.errorCode),l=M(a?.errorCode),u=l.code!==c;return new r(u?l.code:c,s,{...u?{runnerErrorCode:a?.errorCode,...l.details.reason===void 0?{}:{reason:l.details.reason}}:{},...ue(a?.errorCode)?{dispatched:l.details.dispatched}:{},failedStepIndex:i,failedStepKind:o,completedSteps:t,sequenceResults:e,hint:a?.errorHint??`Run snapshot -i to inspect the current UI, then continue from the observed state.`})}function ue(e){return e!==void 0&&(j.has(e)||e===D)}function de(e){return Array.isArray(e)?e.map(e=>{let t=e??{};return{ok:t.ok===!0,kind:typeof t.kind==`string`?t.kind:`unknown`,errorCode:typeof t.errorCode==`string`?t.errorCode:void 0,errorMessage:typeof t.errorMessage==`string`?t.errorMessage:void 0,errorHint:typeof t.errorHint==`string`?t.errorHint:void 0,gestureStartUptimeMs:typeof t.gestureStartUptimeMs==`number`?t.gestureStartUptimeMs:void 0,gestureEndUptimeMs:typeof t.gestureEndUptimeMs==`number`?t.gestureEndUptimeMs:void 0}}):[]}export{q as C,K as S,ce as _,W as a,te as b,U as c,g as d,M as f,G as g,L as h,f as i,z as l,h as m,d as n,p as o,F as p,u as r,v as s,ee as t,$ as u,R as v,V as w,_ as x,J as y};