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 • 1.35 kB
import{v as e}from"./sdk-contracts.js";import{c as t}from"./device-shell.js";import{a as n,c as r,o as i}from"./daemon-lifecycle-errors.js";import{ensureManagedDaemon as a,n as o,t as s}from"./daemon-client-lifecycle.js";function c(r,i,a){return new e(`COMMAND_FAILED`,`The registered daemon does not match this client instance.`,{reason:`daemon-binding-mismatch`,stateDir:a,bound:{pid:r.pid,processStartTime:r.processStartTime},...i?{registered:n(i)}:{},hint:`Check ${t(`${a}/daemon.json`)} or obtain clientConfig from a running host. A pinned client never starts or replaces a daemon.`})}async function l(t,n){if(t.remoteBaseUrl||t.daemonLifecycle!==`attach`)return await a(t,n);let l=t.paths.baseDir,u=await o(t,{refineUnreachable:!0}).catch(n=>{throw t.daemonBinding&&n instanceof e&&n.details?.reason===`daemon-record-unreadable`?c(t.daemonBinding,null,l):n});if(!u)throw t.daemonBinding?c(t.daemonBinding,null,l):r(l);let{info:d,decision:f}=u;switch(f.kind){case`reuse`:break;case`refuseNewer`:throw await i({info:d,mismatch:`newer`,stateDir:l});case`replace`:throw f.cause===`unreachable`?r(l,d):await i({info:d,mismatch:f.cause,stateDir:l})}let p=t.daemonBinding;if(p&&(d.token!==p.token||d.pid!==p.pid||d.processStartTime!==p.processStartTime))throw c(p,d,l);return await s(d,l,t.host),{info:d,startedByClient:!1}}export{l as connectToDaemon};