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 • 13.9 kB
import{N as e,Wr as t,_i as n,_t as r,fn as i,gt as a,ht as o,it as s,ln as c,lt as l,pn as u,vt as d}from"./sdk-batch.js";import{K as f,s as p,v as m}from"./sdk-contracts.js";import{St as h,q as g}from"./sdk-io.js";import"./diagnostics.js";import{T as _,y as v}from"./sdk-device.js";import{i as y,r as b}from"./success-text.js";import{o as x}from"./session-store.js";import{_ as ee,f as S,t as te}from"./alert-contract.js";import{i as ne,l as re,m as C,p as ie,r as ae,x as w}from"./settings.js";import{t as T}from"./location-coordinates.js";import{r as E}from"./absence-observation.js";import{t as oe}from"./absence-observation-errors.js";import"./retry.js";import{n as D,o as O,t as se}from"./snapshot-runtime-capture-input.js";import{d as ce}from"./request.js";import{h as le}from"./daemon-command-registry.js";import{t as k}from"./context.js";import{t as A}from"./platform-request-scope.js";import{n as j}from"./runtime-admission.js";import{t as M}from"./wait.js";import{c as ue,p as N,r as P,u as F}from"./runtime-session.js";import{n as I,t as L}from"./capture-disclosure.js";import{a as R,o as z,s as B}from"./snapshot-runtime-binding.js";import{a as V,c as H,d as U,f as de,h as fe,l as pe,o as me,p as W,s as G,t as he}from"./selector-runtime.js";import{n as K,t as ge}from"./snapshot-runtime.js";async function _e(e){let{device:t,action:n,inspectFacts:i,bindDevice:s,platformServices:c}=e,l={command:`alert`,device:t,inspectFacts:i,bindDevice:s,platformServices:c,readinessScope:e.session?void 0:e.requestScope};if(n===`wait`){let e=await j({...l,use:d});return e.type===`response`?{ok:!1,response:e.response}:{ok:!0,execute:t=>e.runtime.operations.awaitAlert(t)}}if(n===`accept`){let e=await j({...l,use:o});return e.type===`response`?{ok:!1,response:e.response}:{ok:!0,execute:t=>e.runtime.operations.acceptAlert(t)}}if(n===`dismiss`){let e=await j({...l,use:a});return e.type===`response`?{ok:!1,response:e.response}:{ok:!0,execute:t=>e.runtime.operations.dismissAlert(t)}}let u=await j({...l,use:r});return u.type===`response`?{ok:!1,response:u.response}:{ok:!0,execute:e=>u.runtime.operations.readAlert(e)}}async function ve(e){let{req:t,logPath:n,sessionStore:r,ref:i,device:a,inspectFacts:o,bindDevice:s,platformServices:c}=e,l=i?r.requireCurrent(i):void 0,u=be(t.positionals?.[0]),d=xe(u,t.positionals?.[1]),f=await _e({requestScope:A(t),device:a,action:u,session:l,inspectFacts:o,bindDevice:s,platformServices:c});if(!f.ok)return f.response;l=i?r.requireCurrent(i):void 0,l&&le(t)===`may-invalidate`&&O(l);let p=k(n,t.flags,l?.appBundleId,l?.trace?.outPath),m=await f.execute({timeoutMs:d,...ye(l),execution:D(p)});return x(r,i,t,t.command,m),{ok:!0,data:m}}function ye(e){return{...e?.appBundleId===void 0?{}:{appBundleId:e.appBundleId},...e?.surface===void 0?{}:{surface:e.surface}}}function be(e){let t=ee(e??`get`);if(!t)throw new m(`INVALID_ARGS`,`alert requires a subcommand: ${te.join(`, `)} (got "${e}")`);return t}function xe(e,t){let n=s(t)??1e4;if(e===`wait`&&n>2147453647)throw new m(`INVALID_ARGS`,`alert wait timeoutMs exceeds the maximum of ${l} ms`,{hint:`Pass a shorter timeout, or run repeated alert waits.`,reason:S,maxWaitWindowMs:l});return n}function Se(e){let t=e.positionals??[],n=c(t);if(n.kind===`read`)return{ok:!0,parsed:{leg:`read`,setting:n.setting}};try{return{ok:!0,parsed:{leg:`write`,args:re(t)}}}catch(e){if(!(e instanceof m))throw e;return p(e.code,e.message)}}function Ce(e){if(e.permission!==void 0)return{permissionTarget:e.permission,permissionMode:e.mode};if(e.latitude!==void 0)return{latitude:T(e.latitude,`latitude`),longitude:T(e.longitude,`longitude`)}}function we(e,t,n){return{setting:e.setting,state:e.state,...e.permission===void 0?{}:{permissionTarget:e.permission,permissionMode:e.mode},...e.latitude===void 0?{appBundleId:t}:{latitude:e.latitude,longitude:e.longitude},platform:n}}async function Te(e,t,n){let{setting:r,state:i}=t,a=await e.operations.setSetting(t);return a&&typeof a==`object`?y({setting:r,state:i,...a},typeof a.message==`string`&&a.message.length>0?a.message:n):{setting:r,state:i,...b(n)}}async function Ee(e){return e.parsed.leg===`read`?await De(e,e.parsed.setting):await Oe(e,e.parsed.args)}async function De(e,t){let{req:n,logPath:r,sessionStore:a,ref:o,device:s,inspectFacts:c,bindDevice:l,platformServices:u}=e,d=o?a.requireCurrent(o):void 0,f=await j({command:`settings ${t}`,subject:t,device:s,use:i,inspectFacts:c,bindDevice:l,platformServices:u,readinessScope:d?void 0:A(n)});if(f.type===`response`)return f.response;d=o?a.requireCurrent(o):void 0,h({level:`debug`,phase:`settings_read`,data:{setting:t,platform:s.platform}});let p=k(r,n.flags,d?.appBundleId,d?.trace?.outPath),m=await f.runtime.operations.readSetting({setting:t,execution:D(p)}),g={...m,...b(ae(m))};return x(a,o,n,n.command,g),{ok:!0,data:g}}async function Oe(e,t){let{req:n,logPath:r,sessionStore:i,ref:a,device:o,inspectFacts:s,bindDevice:c,platformServices:l}=e,d=a?i.requireCurrent(a):void 0,{setting:f,state:p}=t,m=ke(o,n,f,p);if(m!==void 0)return m;let g=await j({command:`settings ${f}`,subject:f,device:o,use:u,inspectFacts:s,bindDevice:c,platformServices:l,readinessScope:d?void 0:A(n)});if(g.type===`response`)return g.response;d=a?i.requireCurrent(a):void 0;let _=Ae(n,t,d,o),v=je(t,_);if(v!==void 0)return v;d&&O(d),h({level:`debug`,phase:`settings_apply`,data:we(t,_,o.platform)});let y=await Te(g.runtime,Me(t,_,k(r,n.flags,_,d?.trace?.outPath)),ne(f,p,_));return x(i,a,n,n.command,y),{ok:!0,data:y}}function q(e){let t=e.input?.app;if(typeof t!=`string`)return;let n=t.trim();return n.length>0?n:void 0}function J(e){return _(e)?`macos-host`:v(e.platform)?`apple`:`mobile`}function ke(e,t,n,r){let i=q(t);if(i===void 0||C(J(e),n,r)!==`device-level`)return;let a=ie(n,r,i);return p(a.code,a.message,a.details,{hint:a.hint})}function Ae(e,t,n,r){let i=C(J(r),t.setting,t.state)===`app-scoped`?q(e):void 0;return t.app??i??e.internal?.settingsAppBundleId??n?.appBundleId}function je(e,t){if(w(e.setting)?.appRequired&&!t)return p(`INVALID_ARGS`,`settings ${e.setting} requires an app id when no app is bound to the session`,f())}function Me(e,t,n){let r=Ce(e);return{setting:e.setting,state:e.state,...t===void 0?{}:{appBundleId:t},...r===void 0?{}:{options:r},execution:D(n)}}async function Ne(e){return await K({...e,command:`audit`,execute:async({runtime:e,sessionName:t,device:n,getSession:r})=>{let i=await e.capture.auditSnapshot({session:t,target:n.target}),a=r();if(!a||i.findings.length===0)return{data:i};ue(a,i.findings.map(e=>e.ref));let o=a.snapshotGeneration;return{data:o===void 0?i:{...i,refsGeneration:o}}}})}async function Pe(e){return await K({...e,command:`diff`,execute:async({runtime:e,sessionName:t,req:n,snapshotScope:r})=>{let i=await e.capture.diffSnapshot({session:t,interactiveOnly:n.flags?.snapshotInteractiveOnly,depth:n.flags?.snapshotDepth,scope:r,raw:n.flags?.snapshotRaw});return{data:i,record:{kind:`diff`,mode:`snapshot`,baselineInitialized:i.baselineInitialized,summary:i.summary}}}})}const Fe=[`application`,`window`,`tabbar`,`scrollbar`,`image`],Ie=new Set([`tab bar`]);async function Le(e,t){if(t.ok||!Re(t.error.details?.reason)||t.error.details?.captureStalled===!0||t.error.details?.captureTruncated===!0)return t;let n=await ze(e).catch(()=>null);return n?p(t.error.code,`${t.error.message}. Current surface: ${n.summary}.`,{...t.error.details??{},currentSurface:n.details}):t}function Re(e){return e===M.targetAbsent||e===M.stableTimeout}async function ze(e){let t={...e.req.flags,snapshotInteractiveOnly:!0},n=await P({device:e.device,session:e.session,flags:t,logPath:e.logPath??``,captureData:async()=>await e.capture(se({flags:t,logPath:e.logPath??``,meta:e.req.meta,session:e.session,snapshotScope:void 0}))});L(e.captureProof,n.snapshot);let r=[...n.snapshot.nodes].sort(Be),i=Y(r,6,{includeIdentifiers:!0});if(i.length===0)return null;let a=Y(r.filter(e=>!Q(e)),4,{includeIdentifiers:!1}),o=Y(r.filter(He),4,{includeIdentifiers:!0});return{summary:(a.length>0?a:i.slice(0,4)).join(`, `),details:{labels:i,...o.length>0?{buttons:o}:{}}}}function Y(e,t,n){let r=new Set,i=[];for(let a of e){let e=Z(a,n);if(!(!e||r.has(e))&&(r.add(e),i.push(e),i.length>=t))break}return i}function Be(e,t){return X(e)-X(t)||Ve(e,t)}function X(e){let t=!!Z(e,{includeIdentifiers:!1});return(Q(e)?2:0)+ +!t}function Ve(e,t){return e.rect&&t.rect?e.rect.y-t.rect.y||e.rect.x-t.rect.x:e.rect?-1:t.rect?1:(e.depth??0)-(t.depth??0)||e.index-t.index}function Z(e,t){let n=(t.includeIdentifiers?[e.label,e.value,e.identifier]:[e.label,e.value]).map(e=>typeof e==`string`?e.trim():``).find(e=>e.length>0);return n?n.replaceAll(/\s+/g,` `).slice(0,80):``}function Q(e){let t=n(`${e.type??``} ${e.role??``} ${e.subrole??``}`),r=`${e.label??``} ${e.value??``}`.trim().toLowerCase();return Fe.some(e=>t.includes(e))||Ie.has(r)||r.endsWith(`.fill`)}function He(e){let t=`${e.type??``} ${e.role??``} ${e.subrole??``}`;return n(t).includes(`button`)}async function Ue(e){let{req:t,sessionName:n,sessionStore:r}=e,i=We(t);if(`ok`in i)return i;let a=i;if(a.kind===`sleep`)return await $(e,a.durationMs);let{ref:o,session:s,device:c}=await z(r,n,t),l=await pe({requestScope:A(t),command:`wait`,device:c,session:s,inspectFacts:e.inspectFacts,platformServices:e.platformServices,bindDevice:e.bindDevice});if(!l.ok)return l.response;let u=l.operations,d=t.internal?.replayLandmarkGuard,f=Ge(a,s);if(`ok`in f)return f;let{waitParsed:p,staleRefsWarning:m}=f;return e.consumedSnapshot??={},e.captureProof??={},I({response:await B(s,c,()=>Ke({...e,ref:o},p,s,c,u,m,d),e.platformResourceCleanup),consumedTree:he(e),captureProof:e.captureProof})}function We(t){let n=e(t.positionals??[]);if(!n)return p(`INVALID_ARGS`,`wait requires a duration or text`);if(n.kind===`invalid`)return p(`INVALID_ARGS`,n.message);if(n.kind!==`absent`)return n;let r=E({depth:t.flags?.snapshotDepth,scope:t.flags?.snapshotScope});if(!r)return n;let i=oe(r,`wait`);return p(i.code,i.message,i.details)}async function $(e,t){let{req:n,sessionName:r,sessionStore:i}=e,{ref:a}=await R(i,r,n);return await G(async()=>{let r=e.signal??ce(n.meta?.requestId);r?.throwIfAborted(),t>0&&await g(t,r),r?.throwIfAborted();let o={kind:`sleep`,waitedMs:t};return U(i,a,n,o,void 0,`landmark`),W(o)})}function Ge(e,t){if(e.kind!==`ref`)return{waitParsed:e};let n=fe(e.rawRef);if(!n.ok)return n.response;let r=N(t,n.ref);return r?p(r.code,r.message,r.details):{waitParsed:{...e,rawRef:n.ref},staleRefsWarning:F({session:t,ref:n.ref,mintedGeneration:n.generation})}}async function Ke(e,t,n,r,i,a,o){let{req:s,sessionName:c,sessionStore:l}=e,u=await H({...e,ref:e.ref,session:n,device:r,bound:i}),d=await G(async()=>{let r=await u.selectors.wait({session:c,requestId:s.meta?.requestId,target:qe(t,n,o)}),i=me(r);U(l,e.ref,s,de(r),i,`landmark`);let d=W(r);return a?{...d,warning:a}:d}),f=await Le({req:s,logPath:e.logPath,session:n,device:r,capture:i.capture,captureProof:e.captureProof},d);return await V(e,f,`wait`)}function qe(e,t,n){switch(e.kind){case`selector`:return{kind:`selector`,selector:e.selectorExpression,timeoutMs:e.timeoutMs,...n?{recordedLandmark:n}:{}};case`absent`:return{kind:`absent`,selector:e.selectorExpression,timeoutMs:e.timeoutMs};case`ref`:return Ye(e,t);case`stable`:return{kind:`stable`,quietMs:e.quietMs,timeoutMs:e.timeoutMs};case`text`:return Xe(e);default:return Je(e)}}function Je(e){throw Error(`Unsupported wait positional target: ${String(e)}`)}function Ye(e,t){if(!t?.snapshot)throw new m(`INVALID_ARGS`,`Ref wait requires an existing snapshot in session.`);return{kind:`ref`,ref:e.rawRef,timeoutMs:e.timeoutMs}}function Xe(e){let n=t(e.text);if(!n.ok)throw new m(n.code,n.message);return{kind:`text`,text:n.text,timeoutMs:e.timeoutMs}}const Ze={snapshot:async({req:e,createCommandSurface:t,sessionName:n,logPath:r,sessionStore:i,inspectFacts:a,bindDevice:o,platformServices:s,platformResourceCleanup:c})=>await ge({createCommandSurface:t,req:e,sessionName:n,logPath:r,sessionStore:i,inspectFacts:a,bindDevice:o,platformServices:s,platformResourceCleanup:c}),diff:async({req:e,createCommandSurface:t,sessionName:n,logPath:r,sessionStore:i,inspectFacts:a,bindDevice:o,platformServices:s,platformResourceCleanup:c})=>e.positionals?.[0]===`snapshot`?await Pe({createCommandSurface:t,req:e,sessionName:n,logPath:r,sessionStore:i,inspectFacts:a,bindDevice:o,platformServices:s,platformResourceCleanup:c}):p(`INVALID_ARGS`,`diff currently supports only: diff snapshot`),audit:async({req:e,createCommandSurface:t,sessionName:n,logPath:r,sessionStore:i,inspectFacts:a,bindDevice:o,platformServices:s,platformResourceCleanup:c})=>await Ne({createCommandSurface:t,req:e,sessionName:n,logPath:r,sessionStore:i,inspectFacts:a,bindDevice:o,platformServices:s,platformResourceCleanup:c}),wait:async({req:e,createCommandSurface:t,sessionName:n,logPath:r,sessionStore:i,inspectFacts:a,bindDevice:o,platformServices:s,platformResourceCleanup:c})=>await Ue({createCommandSurface:t,req:e,sessionName:n,logPath:r,sessionStore:i,inspectFacts:a,bindDevice:o,platformServices:s,platformResourceCleanup:c}),alert:async({req:e,sessionName:t,logPath:n,sessionStore:r,inspectFacts:i,bindDevice:a,platformServices:o,platformResourceCleanup:s})=>{let{ref:c,session:l,device:u}=await z(r,t,e);return await B(l,u,async()=>await ve({req:e,logPath:n,sessionStore:r,ref:c,device:u,inspectFacts:i,bindDevice:a,platformServices:o}),s)},settings:async({req:e,sessionName:t,logPath:n,sessionStore:r,inspectFacts:i,bindDevice:a,platformServices:o,platformResourceCleanup:s})=>{let c=Se(e);if(!c.ok)return c;let{ref:l,session:u,device:d}=await z(r,t,e);return await B(u,d,async()=>await Ee({req:e,logPath:n,sessionStore:r,ref:l,device:d,parsed:c.parsed,inspectFacts:i,bindDevice:a,platformServices:o}),s)}};async function Qe(e){let t=e.req.command,n=Ze[t];return n?await n(e):null}export{Qe as handleSnapshotCommands};