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 • 4.47 kB
import{v as e}from"./sdk-contracts.js";import{t}from"./screenshot.js";import{g as n}from"./sdk-batch-runner.js";import"./capture.js";import{a as r}from"./sdk-device.js";import{g as i}from"./src3.js";import{i as a}from"./flag-registry.js";import{N as o}from"./device.js";import{h as s,m as c}from"./file.js";import{a as l,n as u,r as d,t as f}from"./merge-flags.js";import p from"node:fs";import m from"node:path";function h(e){let t={flags:{}};for(let n of _(e.cwd,e.cliFlags.config,e.env)){let e=y(n);f(t.flags,e.flags),e.devicePin&&(t.devicePin=e.devicePin)}return f(t.flags,w(e.env,e.command)),t.devicePin&&f(t.flags,t.devicePin.selector),t}const g=a();function _(e,t,n){let r=t??n.AGENT_DEVICE_CONFIG;return r?[{path:v(r,e,n),required:!0,source:`explicit`}]:[{path:c(n),required:!1,source:`user`},{path:m.resolve(e,`agent-device.json`),required:!1,source:`project`}]}function v(e,t,n){return s(e,{cwd:t,env:n})}function y(t){let{path:r,required:i}=t;if(!p.existsSync(r)){if(i)throw new e(`INVALID_ARGS`,`Config file not found: ${r}`);return{flags:{}}}let a;try{a=p.readFileSync(r,`utf8`)}catch(t){throw new e(`INVALID_ARGS`,`Failed to read config file: ${r}`,{cause:t instanceof Error?t.message:String(t)})}let s;try{s=JSON.parse(a)}catch(t){throw new e(`INVALID_ARGS`,`Invalid JSON in config file: ${r}`,{cause:t instanceof Error?t.message:String(t)})}if(!n(s))throw new e(`INVALID_ARGS`,`Config file must contain a JSON object: ${r}`);let{devicePin:c,...l}=s,u={flags:S(l,{source:t.source,label:b(t)})};return c!==void 0&&(u.devicePin={selector:o(c,b(t)),source:{kind:t.source,path:t.path}}),u}function b(e){return`${e.source===`project`?`project `:``}config file ${e.path}`}const x={flagKey:`record`,envVar:`AGENT_DEVICE_RECORD`,migration:`--record is per-invocation only: pass --record on the command, or record: true in the SDK/MCP call, to keep a corrective read in a healed script`};function S(n,r){let a={},o=Object.entries(n).filter(([e])=>e!==`plugins`||r.source!==`user`);for(let[n,s]of o){if(n===x.flagKey)throw new e(`INVALID_ARGS`,`Config key "${n}" in ${r.label} was removed; ${x.migration}.`);let o=n,c=d(o);if(!c)throw n===t.flagKey?new e(`INVALID_ARGS`,`Config key "${n}" in ${r.label} was removed; use "screenshotScale" (0.01-1) to downscale screenshots. Recordings capture at native resolution.`):new e(`INVALID_ARGS`,`Unknown config key "${n}" in ${r.label}.`);if(!c.configurable)throw new e(`INVALID_ARGS`,`Config key "${n}" is not allowed in ${r.label}. This key is not supported in config files.`);if(r.source===`project`&&!g.has(o))throw new e(`INVALID_ARGS`,`Config key "${n}" is not allowed in ${r.label}. Move it to ~/.agent-device/config.json, pass it with --config or AGENT_DEVICE_CONFIG, or provide it through CLI flags/environment variables.`);if(o===`installSource`){a.installSource=i(s,r.label);continue}a[o]=l(c,s,r.label,n)}return a}const C=new Set([`screenshot`,`record`]);function w(n,r){if(r!==null&&d(x.flagKey)?.supportsCommand(r)===!0&&typeof n[x.envVar]==`string`&&n[x.envVar].trim().length>0)throw new e(`INVALID_ARGS`,`${x.envVar} was removed; ${x.migration}.`);let i=n[t.envVar];if(r!==null&&C.has(r)&&typeof i==`string`&&i.trim().length>0)throw new e(`INVALID_ARGS`,`${t.envVar} was removed. ${t.migration[r===`record`?`record`:`screenshot`]}`);let a={};for(let e of u(r)){if(e.key===`installSource`)continue;let t=e.env.names.map(e=>({name:e,value:n[e]})).find(e=>typeof e.value==`string`&&e.value.trim().length>0);t&&(a[e.key]=l(e,t.value,`environment variable ${t.name}`,t.name))}return a}function T(e,t){let n=E(t),r={...e};return n.defaultPlatform&&r.platform===void 0&&(r.platform=n.defaultPlatform),r}function E(e){let{env:t}=e,n=e.inheritedPlatform??e.configuredPlatform??O(t.AGENT_DEVICE_PLATFORM),r=j(e.configuredSession??t.AGENT_DEVICE_SESSION);return{defaultPlatform:n,lockPolicy:D(e.policyOverrides,t,r)}}function D(e,t,n){let r=e?.sessionLock??A(t.AGENT_DEVICE_SESSION_LOCK);if(r)return r;if(n)return`reject`}function O(t){if(t===void 0)return;let n=t.trim().toLowerCase();if(n){if(k(n))return n;throw new e(`INVALID_ARGS`,`Invalid AGENT_DEVICE_PLATFORM: ${t}. Use one of: ${r.join(`, `)}.`)}}function k(e){return r.includes(e)}function A(t){if(t===void 0)return;let n=t.trim().toLowerCase();if(n){if(n===`reject`||n===`strip`)return n;throw new e(`INVALID_ARGS`,`Invalid session lock mode: ${t}. Use reject or strip.`)}}function j(e){return typeof e==`string`&&e.trim().length>0}export{E as n,h as r,T as t};