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 • 8.79 kB
import{In as e}from"./sdk-batch.js";import{F as t,S as n,r,v as i}from"./sdk-contracts.js";import{u as a}from"./snapshot-private-evidence.js";import"./capture.js";import{R as o,w as s}from"./sdk-device.js";import{r as c}from"./target-evidence.js";import{H as l,K as u,U as d,m as f,s as p,t as m}from"./parameterized-recorded-fill.js";import{n as h}from"./rect-center.js";import{i as g}from"./mobile-snapshot-semantics.js";import{n as _}from"./provider-device-admission.js";import{_ as v,g as y,n as b,o as x,s as S,x as C}from"./snapshot-runtime-capture-input.js";import{d as w}from"./request.js";import{n as T}from"./recording-gestures.js";import{f as E,t as D}from"./runtime-session.js";import{i as ee,t as O}from"./capture-disclosure.js";function k(e){return{...e,sessionRef:`sessionRef`in e?e.sessionRef:e.sessionStore.lookup(e.sessionName)}}function A(e,t,n){let r=e.surfaceBundleId??n,i=a(e.nodes);if(!(!r||!i))return{targetBundleId:r,point:t,textInputRects:i.rects}}function j(e,t){return!(!e||!s(e.device)||_(e.device)||t.skipPendingPostGestureStabilization&&y(e))}function M(t){let{session:n,selectorExpression:r}=t;return j(n,{skipPendingPostGestureStabilization:!0})?e(r):null}function N(e){let t=e.identifier?.trim();if(t)return{key:`id`,value:t};let n=e.label?.trim();return n?{key:`label`,value:n}:null}const P=new Set([`ELEMENT_NOT_FOUND`,`ELEMENT_OFFSCREEN`,`AMBIGUOUS_MATCH`]);function F(e,r){let i=n(e);return i.details?.dispatched!==`no`||t(i)?!1:P.has(i.code)?r.delegateSemanticFailures:i.code===`COMMAND_FAILED`}function I(e,t){return e?.matchCount!==1||e.hittable===void 0||!h(e.rect,t)?`unknown`:e.hittable?`hittable`:`notHittable`}async function L(e){let{session:t,node:n,querySelector:r,execution:i,signal:a}=e,o=N(n);if(!o)return`unknown`;try{let{hittability:e}=await r({selector:o,probe:`hittability`,...a?{signal:a}:{},...t.appBundleId===void 0?{}:{options:{appBundleId:t.appBundleId}},execution:i});return I(e,n.rect)}catch{return`unknown`}}async function R(e){let{session:t,node:n,rootViewport:r,querySelector:i,execution:a}=e,o=N(n);if(!o)return null;let s,c;try{({found:s,node:c}=await i({selector:o,...t.appBundleId===void 0?{}:{options:{appBundleId:t.appBundleId}},execution:a}))}catch{return null}if(!s||!c?.rect)return null;let l={rect:c.rect,hittable:c.hittable===!0};return g(l,r)?l.rect:null}function z(e){let{operations:t,command:n,positionals:r,actionCommand:i=n,flags:a,result:o,responseData:s,recordedTarget:l,recordedTargets:u,actionStartedAt:d,actionFinishedAt:f,visualization:p,androidFreshnessBaseline:m,preActionTree:h,settle:g}=e,_=C(a),[v,y]=V({command:n,positionals:r,flags:_,result:o,responseData:s}),b=t.isSessionRecording()&&l?c(l):void 0,x=t.isSessionRecording()&&u?B(u):void 0;return t.recordAction({command:n,positionals:r,flags:_??{},result:v,...b?{targetEvidence:b}:{},...x?{targetEvidences:x}:{}}),t.markDeferredOutcome({command:n,action:i,positionals:r,flags:a,androidFreshnessBaseline:m,preActionTree:h,settle:g}),p!==`recorded-during-execution`&&t.recordGestureVisualization(i,r,v,_??{},d,f),{ok:!0,data:y}}function B(e){let t=c(e.source),n=c(e.destination);return t&&n?{source:t,destination:n}:void 0}function V(e){if(e.command!==`fill`||!d(e.flags))return[e.result,e.responseData];let t=l({ts:0,command:`fill`,positionals:e.positionals,flags:e.flags,result:e.result}),n=u(e.flags),r=e=>m(p(e,n),t,n);return[r(e.result),r(e.responseData)]}async function H(e){let t=k(e).sessionRef;if(!t)throw new i(`SESSION_NOT_FOUND`,r);let n=e.sessionStore.requireCurrent(t);return await q({createCommandSurface:e.createCommandSurface,requestId:e.req.meta?.requestId,flags:e.req.flags,session:n,contextFromFlags:e.contextFromFlags,captureSnapshot:async(n,r)=>{let i=await e.captureSnapshotForSession(t,n,e.sessionStore,e.contextFromFlags,r);return e.consumedCapture&&(e.consumedCapture.state=i),O(e.captureProof,i)},runtimeSessions:D({sessionName:e.sessionName,sessionStore:e.sessionStore,ref:t,recordOptions:{includeSnapshot:!0,omitRefFrameSnapshot:e.req.internal?.findResolvedTarget!==void 0},setRecord:(e,t)=>{e.snapshot&&E(t,e.snapshot)}}),coordinateTapTextInputEvidence:(n,r)=>{let i=e.sessionStore.requireCurrent(t);if(o(i.device)!==`ios`||!r)return;let a=i.snapshot;if(!(!a||a.nodes!==r.nodes||S(a)))return A(r,n,i.appBundleId)},expireRefFrame:()=>x(e.sessionStore.requireCurrent(t)),confirmOffscreenTargetVisible:U(e,n),probeTargetHittability:W(e,n),pairedGestureViewport:e.pairedGestureViewport,touchExecutor:e.touchExecutor,gestures:e.gestures,boundCapture:K(e)})}function U(e,t){let n=e.touchExecutor?.querySelector??e.gestures?.querySelector;if(n)return async(r,i)=>await R({session:t,node:r,rootViewport:i,querySelector:n,execution:b(e.contextFromFlags(e.req.flags,t.appBundleId,t.trace?.outPath))})}function W(e,t){let n=e.touchExecutor?.querySelector??e.gestures?.querySelector;if(n)return async(r,i)=>await L({session:t,node:r,signal:i,querySelector:n,execution:b(e.contextFromFlags(e.req.flags,t.appBundleId,t.trace?.outPath))})}function G(e){let{ref:t,sessionStore:n,...r}=e,i=n.requireCurrent(t);return z({...r,operations:{recordAction:n.recordAction.bind(n,t),markDeferredOutcome:e=>v({session:i,...e}),isSessionRecording:f.bind(null,i),recordGestureVisualization:T.bind(null,i)}})}function K(e){return e.touchExecutor?.captureSnapshot??e.gestures?.captureSnapshot??e.settleObservationCapture}function q(e){return e.createCommandSurface({backend:J(e),...ee(`interaction commands`,{plural:!0}),sessions:e.runtimeSessions,signal:w(e.requestId)})}function J(e){let{flags:t,session:n}=e;return{platform:o(n.device),captureSnapshot:async(n,r)=>({snapshot:await e.captureSnapshot(t,{interactiveOnly:r?.interactiveOnly===!0,preferredBackend:r?.preferredBackend,includeRects:r?.includeRects===!0,signal:n.signal,boundCapture:e.boundCapture})}),...Y(e,()=>e.contextFromFlags(t,n.appBundleId,n.trace?.outPath)),...e.confirmOffscreenTargetVisible?{confirmOffscreenTargetVisible:async(t,n,r)=>await e.confirmOffscreenTargetVisible(n,r)}:{},...e.probeTargetHittability?{probeTargetHittability:async(t,n)=>await e.probeTargetHittability(n,t.signal)}:{},...X(e.touchExecutor,e.expireRefFrame,t,e.coordinateTapTextInputEvidence)}}function Y(e,t){let n=e.gestures,r=e.pairedGestureViewport;return n?{resolveGestureViewport:async()=>r??await n.gestureViewport?.(t()),performGesture:async(r,i)=>(e.expireRefFrame(),Z(await n.performPlan(i,t())))}:r?{resolveGestureViewport:async()=>r}:{}}function X(e,t,n,r){if(!e)return{};let{tapPoint:i,tapRef:a,fillPoint:o,fillRef:s,longPressPoint:c,hoverPoint:l,hoverRef:u}=e,d=async e=>(t(),Z(await e()));return{tap:i?(e,t,a)=>{let o=r?.(t,a?.pointObservation);return d(()=>i(t,{...n,...o?{coordinateTapTextInputEvidence:o}:{}}))}:void 0,tapTarget:a?(e,t)=>d(()=>a(t.ref)):void 0,fill:o?(e,t,n,r)=>d(()=>o(t,n,r)):void 0,fillTarget:s?(e,t,n,r)=>d(()=>s(t.ref,n,r)):void 0,longPress:c?(e,t,n)=>d(()=>c(t,n?.durationMs)):void 0,hover:l?(e,t)=>d(()=>l(t)):void 0,hoverTarget:u?(e,t)=>d(()=>u(t.ref)):void 0}}function Z(e){return e&&typeof e==`object`?e:void 0}function Q(e,t,n){return{ok:!1,error:{code:e,message:t,...n?{details:n}:{}}}}function te(e,t){if(!t||t.settle===!0)return null;let n=[];return t.settleQuietMs!==void 0&&n.push(`--settle-quiet`),n.length===0?null:Q(`INVALID_ARGS`,`${e}: ${n.join(`, `)} require${n.length===1?`s`:``} --settle.`)}function ne(e){if(e?.settle===!0)return{...e.settleQuietMs===void 0?{}:{quietMs:e.settleQuietMs},...e.timeoutMs===void 0?{}:{timeoutMs:e.timeoutMs}}}function re(e){try{return $(e),null}catch(e){if(e instanceof i)return Q(e.code,e.message,e.details);throw e}}function $(e){let t=e.ref.startsWith(`@`)?e.ref.slice(1):e.ref,{admission:n,scope:r,currentGeneration:a}=e.frame;if(n.admitted)return;let o=n.reason===`plain_ref_requires_complete_frame`&&r!==`all`&&r.has(t)&&a!==void 0?`@${t}~s${a}`:void 0;throw new i(`COMMAND_FAILED`,ae(n.reason,e.ref),{reason:n.reason,ref:e.ref,currentGeneration:a,scope:r===`all`?`all`:Array.from(r),...e.mintedGeneration===void 0?{}:{mintedGeneration:e.mintedGeneration},...o?{suggestedRef:o}:{},hint:o?`Retry with the exact emitted ref ${o}.`:e.staleRefsWarning??ie})}const ie=`Capture a fresh interactive snapshot (snapshot -i) or use a stable selector, then retry.`;function ae(e,t){switch(e){case`ref_frame_expired`:return`Ref ${t} belongs to an expired ref frame — a device action since the snapshot invalidated it`;case`ref_generation_mismatch`:return`Ref ${t} was minted from a superseded snapshot generation`;case`plain_ref_requires_complete_frame`:return`Ref ${t} needs a complete snapshot — the current frame only authorizes its emitted refs`;case`ref_not_issued`:return`Ref ${t} was not issued by the current ref frame`}}export{H as a,j as c,te as i,M as l,re as n,G as o,ne as r,F as s,$ as t,k as u};