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 • 2.32 kB
JavaScript
import{O as e,v as t}from"./sdk-contracts.js";import{St as n}from"./sdk-io.js";import"./diagnostics.js";import{n as r}from"./android-snapshot-quality.js";import{l as i}from"./adb-failure.js";import{E as a,_ as o,b as s,f as c,h as l,m as u,u as d,v as f,y as p}from"./snapshot-helper-install.js";async function m(e){let t=await d(e);return t?await h(t,e):void 0}async function h(e,t){let{session:i,deviceKey:s,resolved:c}=e,l=Date.now(),d=i.capturedCount>0;try{let e=await p({port:i.port,commandTimeoutMs:c.commandTimeoutMs,signal:t.signal,idleWait:t.idleWait,commands:i.commands});return i.capturedCount+=1,{xml:e.xml,metadata:{...e.metadata,transport:`persistent-session`,sessionReused:d}}}catch(e){let c=i.processRecord.current();await u({deviceKey:s,identity:i.identity,failedAfterMs:Date.now()-l,fallbackBudgetMs:a(t).commandTimeoutMs,reusedSessionTimedOut:d&&o(e),signal:t.signal,cause:e}),t.signal?.throwIfAborted(),n({level:`warn`,phase:`android_snapshot_helper_session_fallback`,data:{deviceKey:s,reason:e instanceof Error?e.message:String(e),instrumentation:c,uiAutomationConnectionTimeout:r(e)===`accessibility-timeout`}});return}}function g(n,r){return n!==`gesture`&&n!==`input`||!(r instanceof t)?r:e(r,i(r)?`no`:`unknown`)}async function _(r){r.signal?.throwIfAborted();let i=await c(r.deviceKey);if(!i)return;if(!v(i.helper,r.helper)){n({level:`warn`,phase:`android_snapshot_helper_session_touch_identity_mismatch`,data:{deviceKey:r.deviceKey,sessionHelper:i.helper,requestedHelper:r.helper}}),await l(r.deviceKey);return}let a=`${r.action}-${Date.now()}-${Math.random().toString(16).slice(2)}`,o=r.payloadBase64?`${r.action} ${a} ${r.payloadBase64}`:`${r.action} ${a}`,u;try{let e=await s(i.port,o,r.timeoutMs,r.signal);u=f(e,a)}catch(e){throw await l(r.deviceKey,{signal:r.signal,cause:e}),g(r.action,e)}if(u.ok!==`true`){let n=new t(`COMMAND_FAILED`,u.message||u.errorType||`Android automation helper ${r.action} failed`,{errorType:u.errorType,helper:u});throw r.action===`input`&&u.injected===`false`?e(n,`no`):g(r.action,n)}return u}function v(e,t){return e.packageName===t.packageName&&e.runner===t.runner&&y(e.helperVersion,t.helperVersion)&&y(e.helperVersionCode,t.helperVersionCode)&&y(e.sha256,t.sha256)}function y(e,t){return e===void 0||t===void 0||e===t}export{_ as i,h as n,g as r,m as t};