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 • 5.01 kB
import{At as e,I as t,Tn as n,en as r}from"./sdk-batch.js";import{St as i}from"./sdk-io.js";import"./diagnostics.js";import{Z as a}from"./sdk-device.js";import"./perf-runtime-plan.js";import{h as o}from"./daemon-resolution.js";import{a as s}from"./session-artifact-paths.js";import"./durable-capture-resource.js";import{t as c}from"./app-log-durable-resource.js";import{n as l,r as u}from"./screen-recording-resource-recovery.js";import{a as d,r as f}from"./perf-capture-session-resource.js";import{i as p}from"./screen-recording-session-resource.js";const m=e({required:[`appLogReattach`,`appLogCleanup`]});function h(e){return c.recoverAll({sessionsDir:e.sessionsDir,..._(e)})}function g(e){return c.recoverOne({sessionsDir:e.sessionsDir,..._(e)},e.resourcePath)}function _(e){return{scope:e.scope,acquisitionDeadlineMs:e.acquisitionDeadlineMs,onDiagnostic:e.onDiagnostic,acquireControl:async(t,n)=>await v(e.gateway,t,n)}}async function v(e,t,r){return await u({gateway:e,envelope:t,scope:r,create:e=>{let t=n(e,m);return Object.freeze({reattach:async e=>await t.operations.appLogReattach({envelope:e}),cleanup:async e=>await t.operations.appLogCleanup({envelope:e}),[Symbol.asyncDispose]:async()=>await e[Symbol.asyncDispose]()})}})}function y(e){return d.recoverOne({sessionsDir:e.sessionsDir,scope:e.scope,acquisitionDeadlineMs:e.acquisitionDeadlineMs,onDiagnostic:e.onDiagnostic,acquireControl:async(t,n)=>await b(e.gateway,t,n)},e.resourcePath)}async function b(e,r,i){return await u({gateway:e,envelope:r,scope:i,create:e=>x({runtime:n(e,t),dispose:async()=>await e[Symbol.asyncDispose]()})})}function x(e){return Object.freeze({reattach:async t=>await e.runtime.operations.audioProbeReattach({envelope:t}),cleanup:async t=>await e.runtime.operations.audioProbeCleanup({envelope:t}),[Symbol.asyncDispose]:e.dispose})}function S(e){return f.recoverOne({sessionsDir:e.sessionsDir,scope:e.scope,acquisitionDeadlineMs:e.acquisitionDeadlineMs,onDiagnostic:e.onDiagnostic,acquireControl:async(t,n)=>await C(e.gateway,t,n)},e.resourcePath)}async function C(e,t,i){return await u({gateway:e,envelope:t,scope:i,create:e=>w({runtime:n(e,r),dispose:async()=>await e[Symbol.asyncDispose]()})})}function w(e){return Object.freeze({reattach:async t=>await e.runtime.operations.perfNativeCaptureReattach({envelope:t}),cleanup:async t=>await e.runtime.operations.perfNativeCaptureCleanup({envelope:t}),[Symbol.asyncDispose]:e.dispose})}function T(e){return async(t,n)=>{let r=n??E(t);if(r.status===`retained`)return r;try{let n=o(t.stateDir).sessionsDir,i=s(n,t.session);for(let{resourceKind:a,resourcePath:o}of r.ownedResources){let r=D[a],s=O(r,t,i);if(s.status===`retained`)return s;if(s.status!==`missing`&&await r.recover(e,n,o)===`retained`)return{status:`retained`,reason:`${r.resourceKind}-cleanup-pending`}}let a=E(t);if(a.status===`retained`)return a;let c=new Set(r.ownedResources.map(({resourcePath:e})=>e)),l=a.ownedResources.find(({resourceKind:e,resourcePath:t})=>!c.has(t)||a.recoveryKinds.includes(e));return l?{status:`retained`,reason:`${l.resourceKind}-cleanup-pending`}:{status:`reconciled`}}catch(e){return i({level:`warn`,phase:`device_claim_orphan_reconciliation_failed`,data:{deviceKey:t.deviceKey,ownerSession:t.session,error:e instanceof Error?e.message:String(e)}}),{status:`retained`,reason:`durable-resource-reconciliation-failed`}}}}function E(e){try{let t=o(e.stateDir).sessionsDir,n=s(t,e.session),r=[],i=[];for(let t of Object.values(D)){let a=O(t,e,n);if(a.status===`retained`)return a;a.status===`owned`&&(i.push({resourceKind:t.resourceKind,resourcePath:a.resourcePath}),a.requiresRecovery&&r.push(t.resourceKind))}return{status:`inspectable`,recoveryKinds:r,ownedResources:i}}catch(t){return i({level:`warn`,phase:`device_claim_orphan_reconciliation_failed`,data:{deviceKey:e.deviceKey,ownerSession:e.session,error:t instanceof Error?t.message:String(t)}}),{status:`retained`,reason:`durable-resource-reconciliation-failed`}}}const D={"app-log":{resourceKind:`app-log`,store:c.store,recover:async(e,t,n)=>await g({sessionsDir:t,resourcePath:n,gateway:e.gateway,scope:e.scope})},"screen-recording":{resourceKind:`screen-recording`,store:p.store,recover:async(e,t,n)=>await l({sessionsDir:t,resourcePath:n,gateway:e.gateway,scope:e.scope})},"audio-probe":{resourceKind:`audio-probe`,store:d.store,recover:async(e,t,n)=>await y({sessionsDir:t,resourcePath:n,gateway:e.gateway,scope:e.scope})},"perf-capture":{resourceKind:`perf-capture`,store:f.store,recover:async(e,t,n)=>await S({sessionsDir:t,resourcePath:n,gateway:e.gateway,scope:e.scope})}};function O(e,t,n){let r=e.store.resolvePath(n),i=e.store.read(r);return i.status===`missing`?{status:`missing`}:i.status===`unreattachable`?{status:`retained`,reason:`${e.resourceKind}-${i.reason}`}:i.envelope.sessionId!==t.session||!a(i.envelope.device,t.device)?{status:`retained`,reason:`${e.resourceKind}-owner-mismatch`}:{status:`owned`,resourcePath:r,requiresRecovery:i.envelope.lifecycle===`open`}}export{E as n,h as r,T as t};