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.88 kB
JavaScript
import{v as e}from"./sdk-contracts.js";import{J as t,Z as n}from"./sdk-device.js";import{r}from"./session-paths.js";import{a as i,c as a,i as o,l as s,n as c,o as l,r as u}from"./durable-capture.js";import d from"node:crypto";import f from"node:path";function p(e,t){t.admissionLedger.assertStartAllowed(t.device),h(e,t.resourcePath,t.device);let n=e.store.read(t.resourcePath);return Object.freeze({token:d.randomUUID(),generation:n.status===`decoded`?n.envelope.fence.generation+1:1})}function m(r,i,a){let o=t(a),c=f.dirname(f.dirname(i)),l=[];for(let t of r.store.list(c)){let a=r.store.read(t);if(a.status===`unreattachable`)throw new e(`COMMAND_FAILED`,`${s(g(r.displayName))} ${r.displayName} recovery record is unreattachable: ${a.message}`,{reason:a.reason,retriable:!1,hint:`Retain the corrupt or future-version ${r.displayName} manifest for manual recovery; no replacement capture is safe.`});if(a.status!==`decoded`||a.envelope.lifecycle===`completed`)continue;let c=n(a.envelope.device,o);(c||t===i)&&l.push({resourcePath:t,envelope:a.envelope,onStartDevice:c})}return{sessionsDir:c,records:l}}function h(t,n,r){let[i]=m(t,n,r).records;if(!i)return;let a=i.envelope.sessionId,o=i.onStartDevice?void 0:i.envelope.device.id;throw new e(`COMMAND_FAILED`,`${s(g(t.displayName))} ${t.displayName} resource for this ${o?`session`:`device`} has not reached a confirmed terminal state`,{reason:`cleanup-unconfirmed`,session:a,hint:o?`The open ${t.displayName} record of session "${a}" is on device "${o}". Stop it in that session, or start this one under another session name.`:`The open ${t.displayName} record belongs to session "${a}". Stop or close that session, or retry once its device is reachable; exact-owner cleanup must confirm it before a replacement starts.`})}function g(e){return/^[aeiou]/i.test(e)?`an`:`a`}function _(e){let t=(t,n)=>e.store.resolvePath(t.resolveSessionDir(n)),n=t=>({definition:e,resolveSessionDir:e=>f.join(t.sessionsDir,r(e)),...t});return Object.freeze({store:e.store,resourcePath:t,createNextFence(t){return p(e,t)},adoptStarted(t){return a(e,{...t,reportUndurableCleanup:(e,n)=>{n.confirmed?t.admissionLedger.clearUndurableCleanup(e):t.admissionLedger.blockUndurableCleanup(e,n.reason)}},e.store.resolvePath(t.binding.sessionDir))},finishLive(t){return i(e,t,e.store.resolvePath(t.binding.sessionDir))},finishRecovered(t){return c(e,t)},forceCleanupLive(t){return l(e,{...t,resourcePath:e.store.resolvePath(t.binding.sessionDir)})},recoverAll(e){return o(n(e))},recoverOne(e,t){return u(n(e),t)},async settleStartConflicts(t){let{resourcePath:r,device:i,holdsSession:a,...o}=t,s=m(e,r,i);for(let t of s.records)if(!(!t.onStartDevice||a(t.envelope.sessionId)))try{await c(e,{resourcePath:t.resourcePath,scope:o.scope,acquireControl:o.acquireControl})}catch{await u(n({...o,sessionsDir:s.sessionsDir}),t.resourcePath)}}})}export{_ as t};