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 • 2.55 kB
import{r as e}from"./rolldown-runtime.js";import{h as t}from"./daemon-resolution.js";import{r as n}from"./session-paths.js";import{i as r}from"./metro-reload-endpoints.js";import i from"node:fs";import a from"node:path";var o=e({clearMetroSessionHints:()=>d,clearMetroSessionHintsQuietly:()=>v,readMetroSessionHints:()=>u,readMetroSessionRuntime:()=>m,recordOpenedMetroSession:()=>_,recordPreparedMetroSession:()=>g,resolveSessionMetroServerUrl:()=>f,writeMetroSessionHints:()=>l});function s(e){let{host:n}=e;return{stateDir:t(e.stateDir??n.env.AGENT_DEVICE_STATE_DIR,{env:n.env,cwd:n.cwd}).baseDir,session:e.session}}function c(e,t){return a.join(e,`metro-sessions`,`${n(t)}.json`)}function l(e){let t=c(e.stateDir,e.session);i.mkdirSync(a.dirname(t),{recursive:!0}),i.writeFileSync(t,`${JSON.stringify(e.hints,null,2)}\n`,`utf8`)}function u(e){let t=c(e.stateDir,e.session),n;try{n=i.readFileSync(t,`utf8`)}catch{return}let a;try{a=JSON.parse(n)}catch{return}if(!a||typeof a!=`object`||Array.isArray(a))return;let o=a;try{let e={metroHost:typeof o.metroHost==`string`?o.metroHost:void 0,metroPort:typeof o.metroPort==`number`?o.metroPort:void 0,bundleUrl:typeof o.bundleUrl==`string`?o.bundleUrl:void 0};if(typeof o.controlBaseUrl!=`string`&&Object.values(e).every(e=>e===void 0))return;let t=typeof o.controlBaseUrl==`string`?new URL(o.controlBaseUrl):r({runtime:e});return t.protocol===`http:`||t.protocol===`https:`?{controlBaseUrl:t.toString(),...Array.isArray(o.deviceBaseUrls)?{deviceBaseUrls:o.deviceBaseUrls.filter(e=>typeof e==`string`)}:{}}:void 0}catch{return}}function d(e){i.rmSync(c(e.stateDir,e.session),{force:!0})}function f(e,t={}){let n=t.bundleUrl!==void 0||t.metroHost!==void 0&&t.metroPort!==void 0;return r({...t,runtime:(n?void 0:p(e))??t.runtime})}function p(e){try{let t=u(e);return t?{bundleUrl:t.controlBaseUrl}:void 0}catch{return}}function m(e){let t;try{t=s(e)}catch{return}return p(t)}function h(e){let{metroHost:t,metroPort:n,bundleUrl:i}=e??{};return t===void 0&&n===void 0&&i===void 0?void 0:r({runtime:e}).toString()}function g(e,t){try{let n=s(e),r=new Set;for(let e of[t.iosRuntime,t.androidRuntime])try{let t=h(e);t&&r.add(t)}catch{}l({...n,hints:{controlBaseUrl:new URL(`.`,t.statusUrl).toString(),deviceBaseUrls:[...r]}})}catch{}}function _(e,t){try{let n=s(e),r=h(t.runtime);if(r){let e=t.explicitRuntime?void 0:u(n);if(e&&(t.sessionReused||e.deviceBaseUrls?.includes(r)))return;l({...n,hints:{controlBaseUrl:r}})}else t.sessionReused||d(n)}catch{}}function v(e){try{d(s(e))}catch{}}export{f as n,o as t};