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.16 kB
JavaScript
import{S as e,v as t}from"./sdk-contracts.js";import{I as n,S as r,b as i,rt as a,x as o,y as s}from"./sdk-io.js";import{n as c,o as l}from"./file.js";import"./command.js";import{A as u,a as d,c as f,d as p,l as m,s as h,w as g,x as _,y as v}from"./host-file.js";import{d as y,i as b,u as x}from"./agent-browser-process-record.js";import{resolveAgentBrowserIdleTimeoutMs as S}from"./agent-browser-lifecycle.js";import C from"node:path";async function w(e){h(e.packageRoot);let t=E(e.env,[`install`,`--prefix`,e.packageRoot,`--no-global`,`--no-audit`,`--no-fund`,`--no-save`,e.packageSpec]);await a(t.command,t.args,{env:e.env,timeoutMs:e.timeoutMs})}function T(e){u(C.join(e.installDir,`manifest.json`),JSON.stringify({package:e.packageName,version:e.version,node:i(),installedAt:new Date().toISOString()},null,2))}function E(e,n){if(o()!==`win32`)return{command:`npm`,args:n};let r=D(e);if(!r)throw new t(`TOOL_MISSING`,`npm not found in PATH`,{nodeExecPath:s(),hint:"Install Node.js with npm, or add npm to PATH, and run `agent-device web setup` again."});return{command:s(),args:[r,...n]}}function D(e){let t=e.npm_execpath?.trim();if(t){let e=C.resolve(t);if(C.basename(e)===`npm-cli.js`&&O(e))return e}let n=C.join(C.dirname(s()),`node_modules`,`npm`,`bin`,`npm-cli.js`);return O(n)?n:void 0}function O(e){try{return p(e).isFile()}catch{return!1}}const k=`0.39.0`,A=`agent-browser`,j=3e5,M=6e4;async function N(e,t){let n=I({host:t.host,stateDir:t.stateDir});if(!n.installed)throw G(n);return await L(n,e,t)}async function P(e){let t=I(e);q(t.nodeMajor);let i=r();return await l({acquire:()=>c({lockDirPath:C.join(t.installDir,`..`,`.agent-browser-install.lock`),owner:{pid:i,startTime:n(i),acquiredAtMs:Date.now()},timeoutMs:j,description:`managed agent-browser setup`}),task:async()=>{let t=I(e);if(t.installed)return t;h(t.installDir),await w({packageRoot:C.join(t.installDir,`package`),packageSpec:`${A}@${k}`,timeoutMs:j,env:e.host.env});let n=I(e);if(!n.entryScript)throw K(n);return await L(n,[`install`],{host:e.host,timeoutMs:j}),await L(n,[`doctor`,`--offline`,`--quick`],{host:e.host,timeoutMs:M}),T({installDir:n.installDir,packageName:A,version:k}),await I(e)}})}async function F(e){let t=I(e);if(!t.installed)throw G(t);return{status:t,...await L(t,[`doctor`,`--offline`,`--quick`],{host:e.host,timeoutMs:M,allowFailure:!0})}}function I(e){let t=e.stateDir??e.host.env.AGENT_DEVICE_STATE_DIR??J(e.host),{installDir:n,homeDir:r,runtimeHomeDir:a,socketDir:o}=y(t,k),s=U(n),c=W(n),l=V(s),u=l!==void 0&&B(n),d=Number.parseInt(i().replace(/^v/,``).split(`.`)[0]??`0`,10);return{version:k,stateDir:t,installDir:n,packageDir:s,binaryPath:c,entryScript:l,homeDir:r,runtimeHomeDir:a,socketDir:o,installed:u,nodeMajor:d,nodeSupported:d>=24}}async function L(e,t,n){if(!e.entryScript)throw G(e);return await a(s(),[e.entryScript,...t],{allowFailure:n.allowFailure,env:R(e,n.host.env),timeoutMs:n.timeoutMs,signal:n.signal,stdin:n.stdin})}function R(e,t){return h(e.homeDir),z(e),h(e.socketDir),{...t,HOME:e.runtimeHomeDir,AGENT_BROWSER_SOCKET_DIR:e.socketDir,AGENT_BROWSER_IDLE_TIMEOUT_MS:String(S(t)),AGENT_BROWSER_ARGS:b(t.AGENT_BROWSER_ARGS,e)}}function z(e){if(e.runtimeHomeDir!==e.homeDir){h(C.dirname(e.runtimeHomeDir));try{let t=m(e.runtimeHomeDir);if(t.isSymbolicLink()&&v(e.runtimeHomeDir)===e.homeDir||t.isDirectory())return;t.isSymbolicLink()&&g(e.runtimeHomeDir)}catch(e){if(!x(e))throw e}try{d(e.homeDir,e.runtimeHomeDir)}catch{h(e.runtimeHomeDir)}}}function B(e){return f(C.join(e,`manifest.json`))}function V(e){let t;try{let n=JSON.parse(_(C.join(e,`package.json`)));t=typeof n==`object`&&n?n.bin:void 0}catch{return}let n=typeof t==`string`?t:typeof t==`object`&&t?t[A]:void 0;if(typeof n!=`string`)return;let r=C.resolve(e,n);return H(r)?r:void 0}function H(e){try{return p(e).isFile()}catch{return!1}}function U(e){return C.join(e,`package`,`node_modules`,A)}function W(e){let t=o()===`win32`?`${A}.cmd`:A;return C.join(e,`package`,`node_modules`,`.bin`,t)}function G(e){return new t(`TOOL_MISSING`,`Managed web browser backend is not installed.`,{version:k,installDir:e.installDir,hint:e.nodeSupported===!1?`Web automation requires Node 24+; current Node is ${i()}.`:"Run `agent-device web setup` to install the managed web backend."})}function K(e){return new t(`TOOL_MISSING`,`Managed web backend install produced no runnable entry.`,{version:k,installDir:e.installDir,packageDir:e.packageDir,hint:`Remove ${e.installDir} and run \`agent-device web setup\` again.`})}function q(e){if(!(e>=24))throw new t(`UNSUPPORTED_OPERATION`,`Web automation requires Node 24 or newer.`,{currentNode:i(),requiredNodeMajor:24,hint:`Run agent-device with Node 24+ for web setup and web automation.`})}function J(e){return C.join(e.env.HOME??e.cwd,`.agent-device`)}function Y(n){let r=e(n);return r.code===`TOOL_MISSING`?new t(r.code,r.message,{...r.details??{},hint:typeof r.details?.hint==`string`?r.details.hint:"Run `agent-device web setup` to install the managed web backend."}):r}export{F as doctorManagedAgentBrowser,I as getManagedAgentBrowserStatus,Y as mapManagedAgentBrowserError,N as runManagedAgentBrowser,P as setupManagedAgentBrowser};