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 • 4.97 kB
import{v as e}from"./sdk-contracts.js";import{St as t}from"./sdk-io.js";import"./diagnostics.js";import{n}from"./runtime-slots.js";import{s as r}from"./durable-capture.js";import{n as i,o as a}from"./perf-capture-session-resource.js";import{n as o}from"./screen-recording-session-resource.js";import{c as s}from"./artifact-tracking.js";import{p as c}from"./session-runtime.js";import{a as l,i as u,n as d}from"./session-capture-binding.js";import f from"node:crypto";import{rmSync as p}from"node:fs";import m from"node:os";import h from"node:path";import g from"node:fs/promises";const _=2147483647,v={label:`Materialized paths`,plural:!0,expiredHint:`Materialized paths are released automatically when their TTL expires; re-run the command that materialized them if the paths are still needed.`},y=Symbol(`materializedPaths`),b=()=>n(y,()=>new Map,e=>{for(let t of e.values()){clearTimeout(t.timer);try{p(t.rootPath,{recursive:!0,force:!0})}catch{}}});async function x(e){let t=S(e.ttlMs),n=await g.mkdtemp(h.join(m.tmpdir(),`agent-device-materialized-`));try{let r=await D(e.installablePath,h.join(n,`installable`)),i=e.archivePath?await D(e.archivePath,h.join(n,`archive`)):void 0,a=f.randomUUID(),o=Date.now()+t,s=setTimeout(()=>{T(a)},t);return b().set(a,{rootPath:n,installablePath:r,archivePath:i,tenantId:e.tenantId,sessionName:e.sessionName,expiresAt:o,timer:s}),{materializationId:a,installablePath:r,...i?{archivePath:i}:{},expiresAt:new Date(o).toISOString()}}catch(e){throw await g.rm(n,{recursive:!0,force:!0}),e}}function S(t=9e5){if(typeof t!=`number`||!Number.isInteger(t)||t<=0||t>_)throw new e(`INVALID_ARGS`,`retentionMs must be an integer from 1 to ${_}`);return t}async function C(e,t){await E(e,s(b(),e,t,v))}async function w(e){let t=Array.from(b().entries()).filter(([,t])=>t.sessionName===e);await Promise.all(t.map(async([e,t])=>{await E(e,t)}))}async function T(e){let t=b().get(e);if(t)try{await E(e,t)}catch{}}async function E(e,t){clearTimeout(t.timer),b().delete(e),await g.rm(t.rootPath,{recursive:!0,force:!0})}async function D(e,t){let n=await g.stat(e);await g.mkdir(t,{recursive:!0});let r=h.join(t,h.basename(e));return n.isDirectory()?(await g.cp(e,r,{recursive:!0}),r):(await g.copyFile(e,r),r)}async function O(e){let t=e.sessionStore.refresh(e.ref);if(!t.session.appLog)return;let{forceCleanupSessionAppLog:n}=await import(`./app-log-session-resource.js`).then(e=>e.n);await n({...e,ref:t})}async function k(e){let t=u(e.sessionStore,e.ref);t.read()&&await i({binding:t,intent:`disposal`})}async function A(e,t){await t.stopSnapshotHelper(e.device)}async function j(e){let{ref:t,sessionStore:n,platformCleanup:r}=e,i=n.resolveCurrent(t)??t.session;await r.closeManagedBrowser({device:i.device,stateDir:n.resolveDaemonStateDir(),openSessionNames:()=>c(n)})}async function M(e){let t=[];for(let{step:n,run:r}of e)try{await r()}catch(e){t.push({step:n,error:e})}return t}function N(n){if(n.failures.length===0)return;let r=n.failures.map(({step:e})=>e),i=n.failures.map(({step:e,error:t})=>`${e}: ${t instanceof Error?t.message:String(t)}`);return t({level:`error`,phase:n.phase,data:{session:n.sessionName,failedSteps:r,errors:i}}),new e(`COMMAND_FAILED`,`Session cleanup left ${n.failures.length} resource(s) unreleased: ${i.join(`; `)}`,{reason:`session_cleanup_incomplete`,session:n.sessionName,failedSteps:r,hint:n.hint??`Some session resources failed to release. The session was retired; inspect the session log, device state, and retained claims before recovery. Do not retry close on the retired session.`})}function P(e){let{ref:t,sessionStore:n,platformCleanup:r}=e,i=n.resolveCurrent(t)??t.session;return[...[{step:`recording`,run:()=>L({ref:t,sessionStore:n})},...e.appLog===`run`?[{step:`app_log`,run:()=>O({ref:t,sessionStore:n})}]:[],{step:`audio_probe`,run:()=>R({ref:t,sessionStore:n})},{step:`perf_capture`,run:()=>k({ref:t,sessionStore:n})}].map(F),{step:`platform_snapshot_helper`,run:()=>A(i,r)}]}function F(e){return{...e,run:async()=>{try{await e.run()}catch(e){if(!r(e))throw e}}}}async function I(t){let{ref:n,sessionStore:r}=t,i=n.address;if(!t.platformCleanup)throw new e(`INTERNAL_ERROR`,`Platform resource cleanup was not supplied by root runtime composition`);let a=t.platformCleanup,o=[...P({...t,platformCleanup:a}),{step:`web_browser`,run:()=>j({ref:n,sessionStore:r,platformCleanup:a})}];o.push({step:`materialized_paths`,run:()=>w(i)});let s=N({sessionName:i,phase:`session_teardown_cleanup_failed`,failures:await M(o)});if(s)throw s}async function L(e){let t=l(e.sessionStore,e.ref),n=t.read();if(!n)return;let r=n.envelope.metadata?.remoteCaptureOutput===!0,i=await o({binding:t,intent:`disposal`});if(!e.ref.session.lease)return;let{retainLeasedSessionRecording:a}=await import(`./leased-recording-retention.js`);a(e.ref,i,r)}async function R(e){let t=d(e.sessionStore,e.ref);t.read()&&await a({binding:t,intent:`disposal`})}export{C as a,x as c,I as i,P as n,w as o,O as r,S as s,N as t};