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.71 kB
import{B as e,ht as t,nt as n,v as r}from"./sdk-contracts.js";import{a as i}from"./snapshot-capture-annotations.js";import{i as a}from"./snapshot-diagnostics.js";import{o}from"./snapshot-private-evidence.js";import"./capture.js";import{St as s}from"./sdk-io.js";import"./diagnostics.js";import{R as c}from"./sdk-device.js";import{n as l}from"./success-text.js";import{a as u}from"./session-store.js";import{n as d}from"./android-snapshot-quality.js";import{a as f}from"./android-system-chrome.js";import{r as p}from"./snapshot-runtime-capture-input.js";import{d as m}from"./request.js";import{t as h}from"./context.js";import{a as g}from"./session-routing.js";import{t as _}from"./snapshot-quality-verdict.js";import{t as v}from"./quality-warnings.js";import{d as y,r as b,t as x}from"./runtime-session.js";import{i as S,r as C}from"./capture-disclosure.js";import{i as w,r as T,s as E}from"./snapshot-runtime-binding.js";import{a as D,i as O,n as k,t as A}from"./screenshot-runtime.js";import{promises as j}from"node:fs";import M from"node:os";import N from"node:path";function P(e){return{captureFailed:!0,error:e}}function F(e){return{path:e,overlayRefsRequested:!0,overlayRefsAnnotated:!1,overlayRefSource:`unavailable`}}function I(e,t){let n={path:e,overlayRefsRequested:!0,overlayRefSource:`session-snapshot`};return L(t)?{...n,overlayRefsAnnotated:!0,overlayRefs:t}:{...n,overlayRefsAnnotated:!1,overlayRefs:[]}}function L(e){return e.length>0}function R(e,t){return{path:e,overlayRefsRequested:!0,overlayRefsAnnotated:!1,overlayRefSource:`session-snapshot`,overlayAnnotationError:t}}function z(e){return`captureFailed`in e?{overlayRefCount:void 0,overlayRefsAnnotated:void 0}:{overlayRefCount:`overlayRefs`in e?e.overlayRefs.length:0,overlayRefsAnnotated:e.overlayRefsAnnotated}}function ee(e){return d(e)===`accessibility-timeout`}async function B(t){if(t.command===`diff`||t.device.platform!==`android`)return;let n=e(t.error);if(ee(n))return{ok:!1,error:{...n,details:{...n.details??{},androidSnapshotTimeoutScreenshot:await V(t)}}}}async function V(t){try{let e=await O({device:t.device,overlayRefs:!1,inspectFacts:t.inspectFacts,platformServices:t.platformServices,bindDevice:t.bindDevice});if(!e.ok)throw new r(`UNSUPPORTED_OPERATION`,`The target does not support screenshot capture, so no timeout evidence was taken.`);let n=await j.mkdtemp(N.join(M.tmpdir(),`agent-device-android-snapshot-timeout-`)),i=N.join(n,`snapshot-timeout-overlay-refs.png`);await e.runtime.captureScreenshot({outPath:i,options:{appBundleId:t.session?.appBundleId,stabilize:!1,surface:t.session?.surface},execution:k(h(t.logPath,{screenshotNoStabilize:!0},t.session?.appBundleId,t.session?.trace?.outPath))}),await j.access(i);let a=await H(i,t.session);return s({level:`warn`,phase:`android_snapshot_timeout_screenshot_captured`,data:{path:i,...z(a)}}),a}catch(t){let n=e(t);return s({level:`warn`,phase:`android_snapshot_timeout_screenshot_failed`,data:{error:n.message}}),P(n.message)}}async function H(t,n){if(!n?.snapshot)return F(t);try{return I(t,await D({screenshotPath:t,snapshot:n.snapshot}))}catch(n){let r=e(n);return s({level:`warn`,phase:`android_snapshot_timeout_screenshot_overlay_failed`,data:{path:t,error:r.message}}),R(t,r.message)}}function U(e){let{verdict:t,appBundleId:n,latch:r}=e;return!t||t.backend===`android-helper`?{latch:r}:t.state===`healthy`?{latch:void 0}:t.state!==`recovered`||t.reasonCode===`requested-backend`?{latch:r}:t.reasonCode===`deferred`?r!==void 0&&r.appBundleId===n?{latch:r}:{warning:v(t.backend),latch:{appBundleId:n}}:{latch:{appBundleId:n}}}function W(e){let{session:t,data:n,verdict:r,internalObservation:i}=e;if(i||!t)return n;let a=U({verdict:r,appBundleId:t.appBundleId,latch:t.recoveredSnapshotWarningLatch});return t.recoveredSnapshotWarningLatch=a.latch,a.warning?{...n,warnings:[a.warning,...l(n)]}:n}async function G(e){let t=await T(e,e.command);if(!t.ok)return t.response;let{ref:n,session:r,device:i,snapshotScope:a}=t;return await E(r,i,async()=>{let{req:s,logPath:c,sessionStore:l}=e,u=n?.address??e.sessionName,d={},{runtime:f,sessions:p}=await K({createCommandSurface:e.createCommandSurface,req:s,sessionName:u,logPath:c,sessionStore:l,ref:n,session:r,device:i,snapshotScope:a,capturedQuality:d,captureSnapshotData:t.captureSnapshot}),m=()=>{let e=p.getRef();return e?l.requireCurrent(e):void 0},h;try{h=await e.execute({runtime:f,sessionName:u,req:s,device:i,snapshotScope:a,getSession:m})}catch(t){let n=await B({error:t,command:e.command,logPath:c,session:r,device:i,inspectFacts:e.inspectFacts,platformServices:e.platformServices,bindDevice:e.bindDevice});if(!n)throw t;return n}let g=m();h.record&&$({req:s,sessionName:u,sessionStore:l,ref:p.getRef(),result:h.record});let _=W({session:g,data:h.data,verdict:d.value,internalObservation:s.internal?.observationOnly===!0});return{ok:!0,data:o(h.data,_)}},e.platformResourceCleanup)}async function K(e){let{req:t,sessionName:n,logPath:r,sessionStore:i,session:a,device:o,snapshotScope:s}=e,c=x({sessionName:n,sessionStore:i,ref:e.ref,recordOptions:{includeSnapshot:!0},setRecord:(e,r,a)=>{let s=te(e),c=J(r,s,q(t)),l=c?r.snapshot:s.snapshot,u=a?i.update(a,{}):w({sessionName:n,sessionScope:g(t),device:o,snapshot:l,appBundleId:e.appBundleId});return u.appName=e.appName??r?.appName,y(u,{snapshot:l,scopeSource:Z({current:r,keepCurrentSnapshot:c,refScopedSnapshot:q(t)}),keptCurrentSnapshot:c,previousGeneration:r?.snapshotGeneration}),X(u,c,Y(t)),a??i.publish(n,u)}});return{runtime:await e.createCommandSurface({backend:Q({req:t,logPath:r,session:a,device:o,snapshotScope:s,capturedQuality:e.capturedQuality,captureSnapshotData:e.captureSnapshotData}),...S(`snapshot`),signal:m(t.meta?.requestId),sessions:c}),sessions:c}}function q(e){return e.flags?.snapshotScope?.trim().startsWith(`@`)===!0}function J(e,t,n){return n&&t.snapshot?.nodes.length===0&&e?.snapshot!==void 0}function Y(e){return e.command===`snapshot`&&e.internal?.observationOnly!==!0}function X(e,t,n){!t&&n&&p(e)}function Z(e){let{current:t,keepCurrentSnapshot:n,refScopedSnapshot:r}=e;if(r)return n?t?.snapshotScopeSource:t?.snapshotScopeSource??t?.snapshot}function Q(e){let{req:t,logPath:n,session:r,device:o,snapshotScope:s}=e;return{platform:c(o),captureSnapshot:async c=>{let l=await b({device:o,session:r,flags:t.flags,logPath:n,snapshotScope:s,signal:c.signal,captureData:e.captureSnapshotData}),u=i(l);e.capturedQuality.value=u.quality;let d=a(r);return{snapshot:l.snapshot,...u,...d?{snapshotDiagnostics:d}:{},appName:r?.appBundleId?r.appName??r.appBundleId:void 0,appBundleId:r?.appBundleId}}}}function $(e){e.ref&&e.sessionStore.recordAction(e.ref,{command:e.req.command,positionals:e.req.positionals??[],flags:e.req.flags??{},result:ne(e.result),interactiveObservation:u(e.req)})}function te(e){if(!e.snapshot)throw new r(`UNKNOWN`,`snapshot runtime did not produce session state`);return e}function ne(e){return e.kind===`snapshot`?{nodes:e.nodes,truncated:e.truncated}:{mode:e.mode,baselineInitialized:e.baselineInitialized,summary:e.summary}}async function re(e){let t=e.session;if(!t||!_(e.verdict)||e.req.internal?.observationOnly===!0)return;let n=await ie({...e,session:t});if(n===void 0)return;let{path:r,displayRotation:i}=n;return{path:r,artifact:{field:`fallbackScreenshotPath`,artifactType:`screenshot`,path:r,fileName:`snapshot-fallback.png`,...i?{displayRotation:i}:{}}}}async function ie(e){let{req:t,session:n}=e;try{let r=await O({device:n.device,overlayRefs:!1,inspectFacts:e.inspectFacts,platformServices:e.platformServices,bindDevice:e.bindDevice});return r.ok?await A({createCommandSurface:e.createCommandSurface,session:n,sessionName:e.sessionName,captureScreenshot:r.runtime.captureScreenshot,dispatchContext:h(e.logPath,t.flags,n.appBundleId,n.trace?.outPath,t.meta?.requestId,t.meta)}):void 0}catch{return}}async function ae(e){let r={},i=await G({...e,command:`snapshot`,execute:async({runtime:i,sessionName:a,req:s,snapshotScope:c,getSession:l})=>{let u=await i.capture.snapshot({session:a,...t(s.flags,n),scope:c});u.targetActivation&&(r.targetActivation??=u.targetActivation);let d=oe(s,l()),p=f(u.nodes),m=o(u,p===u.nodes?u:{...u,nodes:p}),h=l(),g=await re({createCommandSurface:e.createCommandSurface,req:s,session:h,sessionName:a,logPath:e.logPath,verdict:u.snapshotQuality,inspectFacts:e.inspectFacts,platformServices:e.platformServices,bindDevice:e.bindDevice}),_=o(m,g?{...m,fallbackScreenshotPath:g.path,artifacts:[g.artifact]}:m);return{data:d===void 0?_:o(_,{..._,refsGeneration:d}),record:{kind:`snapshot`,nodes:u.nodes.length,truncated:u.truncated}}}});return C(i,r)}function oe(e,t){return e.internal?.observationOnly===!0?void 0:t?.snapshotGeneration}export{G as n,ae as t};