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 • 10.2 kB
JavaScript
import{Nr as e}from"./sdk-batch.js";import{S as t,l as n,s as r,v as i}from"./sdk-contracts.js";import{C as a,rt as o}from"./sdk-io.js";import{E as s,G as c,R as l,T as u,w as ee}from"./sdk-device.js";import"./session-store.js";import{o as d}from"./session.js";import{v as f}from"./daemon-http.js";import{r as p}from"./runtime-transport-hints.js";import"./command.js";import{a as m}from"./daemon-command-registry.js";import{r as te}from"./application-lifecycle-runtime.js";import h from"node:os";const ne=/^[1-9][0-9]{0,9}$/;function g(e){let t=e?.trim()??``,n=t.lastIndexOf(`@`),r=n<0?t:t.slice(0,n),a=n<0?void 0:t.slice(n+1);if(!s(r)||a!==void 0&&!ne.test(a))throw new i(`INVALID_ARGS`,`A macos-app lease device key must be <bundleId> or <bundleId>@<pid>.`,{deviceKey:t});return a===void 0?{bundleId:r}:{bundleId:r,pid:Number(a)}}const _=new Set([`open`,`batch`]),v=[`out`,`saveScript`,`sessionSaveScript`,`baseline`,`launchConsole`,`launchArgs`,`launchUrl`,`bundleUrl`,`artifactsDir`,`stepsFile`,`searchPath`,`retainPaths`,`installSource`,`metroProjectRoot`,`metroRuntimeFile`,`iosXctestrunFile`,`iosXctestDerivedDataPath`,`iosXctestEnvDir`],re=[`device`,`udid`,`serial`,`target`,`iosSimulatorDeviceSet`,`androidDeviceAllowlist`];function y(e,t,n={}){return new i(`UNAUTHORIZED`,t,{...n,reason:`MACOS_APP_LEASE_DENIED`,rule:e,retriable:!1,hint:`This lease is limited to one macOS app; retrying will not help. Use an allowed command on the leased app.`})}function b(t,n,r,i){if(t.backend!==`macos-app`)return;let a=g(t.deviceKey);if(S(i,a,n.command,n.positionals??[],x(n.runtime,{...n.input,...n.flags})),(n.command===`open`||n.command===`batch`)&&n.flags?.platform!==`macos`)throw y(`device`,`A macos-app lease needs platform macos on ${n.command}.`);if(n.command===`batch`)for(let t of n.flags?.batchSteps??[]){let n=Array.isArray(t.positionals)?t.positionals.map(String):[];S(i,a,e(t.command),n,x(t.runtime,{...t.input,...t.flags}))}if(r)E(a,r);else if(!_.has(n.command))throw y(`session`,`A macos-app lease runs ${n.command} only in a session of ${a.bundleId}; open it first.`,{command:n.command,bundleId:a.bundleId})}function x(e,t){return{...t,...e?.launchUrl?{launchUrl:e.launchUrl}:{},...e?.bundleUrl?{bundleUrl:e.bundleUrl}:{}}}function S(e,t,n,r,i){if(!m(n))throw y(`command`,`A macos-app lease does not allow ${n}.`,{command:n});if(ae(i),ie(i),n===`screenshot`?C(r,i):w(i),n===`open`&&oe(e,t,r),n===`close`&&r.length>0&&r[0]!==t.bundleId)throw y(`app`,`A macos-app lease only closes ${t.bundleId}.`,{bundleId:t.bundleId})}function C(e,t){if(t.screenshotFullscreen===!0)throw y(`capture`,`A macos-app lease captures only the app window.`);let{out:n,...r}=t;w(r);for(let t of[e[0],n])if(!(t===void 0||typeof t==`string`&&f(t,`screenshot`,`.png`)))throw y(`host-path`,`A macos-app lease does not write files on the host.`)}function w(e){let t=v.find(t=>e[t]!==void 0&&e[t]!==!1);if(t)throw y(`host-path`,`A macos-app lease does not accept ${t}, which names a host path or launch.`,{field:t})}function ie(e){let t=re.find(t=>e[t]!==void 0&&e[t]!==``);if(t)throw y(`device`,`A macos-app lease does not accept the ${t} device selector.`,{field:t})}function ae(e){if(e.surface!==void 0&&e.surface!==`app`)throw y(`surface`,`A macos-app lease is limited to the app surface, not ${String(e.surface)}.`,{surface:e.surface});if(e.platform!==void 0&&e.platform!==`macos`)throw y(`app`,`A macos-app lease only drives a macOS app.`,{platform:e.platform})}function oe(e,t,n){if(n.length!==1||n[0]!==t.bundleId)throw y(`app`,`A macos-app lease only opens ${t.bundleId}.`,{bundleId:t.bundleId});T(e)}function T(e){if(d(t=>e[t])!==`native`)throw y(`backend`,`A macos-app lease needs the native macOS app backend (AGENT_DEVICE_MACOS_APP_BACKEND=native).`)}function E(e,t){if(!u(t.device)||(t.surface??`app`)!==`app`||t.appBundleId!==e.bundleId)throw y(`app`,`This session is not the leased app ${e.bundleId}.`,{bundleId:e.bundleId})}const D=/bundleID="([^"]+)"/,O=async e=>{if(!a(e))return;let t=await o(`/usr/bin/lsappinfo`,[`info`,`-only`,`bundleid`,String(e)],{allowFailure:!0,timeoutMs:5e3});return D.exec(t.stdout)?.[1]};async function k(e,t=O){if(e.backend!==`macos-app`)return;let n=g(e.deviceKey);if(n.pid!==void 0&&await t(n.pid)!==n.bundleId)throw y(`process`,`The leased app process ${n.bundleId}@${n.pid} is no longer running.`,{bundleId:n.bundleId,pid:n.pid})}const A=[`sessionStateDir`,`runnerLogPath`,`requestLogPath`,`eventLogPath`,`device`,`id`,`kind`,`model`,`osVersion`,`serial`,`device_udid`,`ios_simulator_device_set`],j=/(?<![\p{L}\p{N}_./])\/(?:Users|home|private|var|tmp|Volumes|Library|Applications|opt|System|usr|etc|Network|cores)(?:\/[\p{L}\p{N}_.@%+~-]+)*\/?/gu;function M(){return{hostName:h.hostname(),homeDirectory:h.homedir()}}function N(e,t,n=M()){return t.ok?{ok:!0,data:P(e,t.data,n)}:{ok:!1,error:F(t.error,n)}}function P(e,t,n){if(!t)return t;let{fallbackScreenshotPath:r,...i}=t;if(e!==`open`)return i;for(let e of A)delete i[e];let{snapshot:a,initialSnapshotError:o,warnings:s,...c}=i;return{...c,...a===void 0?{}:{snapshot:P(`snapshot`,a,n)},...o===void 0?{}:{initialSnapshotError:F(o,n)},...s===void 0?{}:{warnings:L(s,n)}}}function F(e,t){if(e.code===`DEVICE_IN_USE`)return I(e);let{logPath:n,diagnosticsRecord:r,...i}=e;return L(i,t)}function I(e){let t=e.details?.reason;return{code:`DEVICE_IN_USE`,message:`The leased app is in use by another session.`,hint:`Retry once the other session ends.`,...typeof t==`string`?{details:{reason:t}}:{},...e.retriable===void 0?{}:{retriable:e.retriable}}}function L(e,t){return typeof e==`string`?R(e,t):Array.isArray(e)?e.map(e=>L(e,t)):typeof e!=`object`||!e?e:Object.fromEntries(Object.entries(e).map(([e,n])=>[e,L(n,t)]))}function R(e,t){let n=e;if(t.homeDirectory.length>1){let e=t.homeDirectory.replaceAll(/[.*+?^${}()|[\]\\]/g,String.raw`\$&`);n=n.replaceAll(RegExp(`${e}[\\w.@%+~/-]*`,`g`),`<host-path>`)}for(let e of new Set([t.hostName,t.hostName.split(`.`)[0]]))e.length>=4&&(n=n.replaceAll(e,`<host>`));return n.replace(j,`<host-path>`)}function z(e,t){if(!(t===void 0||!e.hasHeldMacOsAppLease(t)))throw y(`diagnostics`,`A macos-app lease does not serve request diagnostics.`)}function B(e){return e.device.platform===`web`}function V(e){return e.toArray().flatMap(e=>c(e.device.id)??[])}function H(e){return ee(e)&&e.kind===`simulator`}function U(e){return e.platform===`android`&&e.kind===`emulator`}const W=[`platform`,`metroHost`,`metroPort`,`bundleUrl`,`launchUrl`];function G(e){return e?[e.metroHost,e.metroPort,e.bundleUrl,e.launchUrl].filter(e=>e!==void 0&&e!==``).length:0}function K(e){if(!e)return{};let t={};return e.metroHost!==void 0&&(t.metroHost=e.metroHost),e.metroPort!==void 0&&(t.metroPort=String(e.metroPort)),e.bundleUrl!==void 0&&(t.bundleUrl=e.bundleUrl),e.launchUrl!==void 0&&(t.launchUrl=e.launchUrl),t}function q(e){return te(K(e))}function J(e,t){if(e!==void 0){if(typeof e!=`string`)throw new i(`INVALID_ARGS`,`Invalid open runtime ${t}: expected string.`);return p(e)}}function Y(e){if(e!==void 0){if(!Number.isInteger(e)||e<1||e>65535)throw new i(`INVALID_ARGS`,`Invalid runtime metroPort: ${String(e)}. Use an integer between 1 and 65535.`);return e}}function se(e){if(e!==void 0){if(typeof e!=`number`)throw new i(`INVALID_ARGS`,`Invalid open runtime metroPort: expected integer.`);return Y(e)}}function ce(e,t,r){if(e===void 0)return r;if(!n(e))throw new i(`INVALID_ARGS`,`Invalid open runtime platform: ${String(e)}. Use "ios", "android", or "harmonyos".`);if(r&&e!==r)throw new i(`INVALID_ARGS`,`open runtime targets ${e}, but session "${t}" is bound to ${r}.`);return e}function X(e){if(n(e))return e}function le(e,t){return{platform:t,metroHost:p(e?.metroHost),metroPort:Y(e?.metroPort),bundleUrl:p(e?.bundleUrl),launchUrl:p(e?.launchUrl)}}function ue(e,t){return{platform:t.platform??e?.platform,metroHost:t.metroHost??e?.metroHost,metroPort:t.metroPort??e?.metroPort,bundleUrl:t.bundleUrl??e?.bundleUrl,launchUrl:t.launchUrl??e?.launchUrl}}function de(e){if(U(e))return`10.0.2.2`;if(H(e))return`127.0.0.1`}function Z(e,t){if(!e||p(e.metroHost)||p(e.bundleUrl)||e.metroPort===void 0)return e;let n=de(t);return n?{...e,metroHost:n}:e}function fe(e){let{runtime:t,sessionName:n,platform:r}=e;if(t===void 0)return;if(!t||typeof t!=`object`||Array.isArray(t))throw new i(`INVALID_ARGS`,`open runtime must be an object.`);let a=t,o=Object.keys(a).find(e=>!W.includes(e));if(o)throw new i(`INVALID_ARGS`,`Invalid open runtime field: ${o}. Supported fields are ${W.join(`, `)}.`);return{platform:ce(a.platform,n,r),metroHost:J(a.metroHost,`metroHost`),metroPort:se(a.metroPort),bundleUrl:J(a.bundleUrl,`bundleUrl`),launchUrl:J(a.launchUrl,`launchUrl`)}}function pe(e,t,n){if(n){if(G(n)===0){e.setRuntimeHints(t,void 0);return}return e.setRuntimeHints(t,n),n}}function me(e,t,n,r){let a=e.getRuntimeHints(t);if(!a)return;let o=n?l(n):void 0,s=X(o)??r;if(a.platform&&n&&!s)throw new i(`INVALID_ARGS`,`Session runtime hints are only supported on iOS, Android, and HarmonyOS sessions, but session "${t}" is bound to ${o}.`);if(a.platform&&s&&a.platform!==s)throw new i(`INVALID_ARGS`,`Session runtime hints target ${a.platform}, but session "${t}" is bound to ${o}. Clear the runtime hints or use a different session.`);return s&&a.platform!==s?{...a,platform:s}:a}function Q(e){let{req:t,sessionStore:n,sessionName:r,device:i}=e,a=i?X(l(i)):e.platform,o=n.getRuntimeHints(r),s=fe({runtime:t.runtime,sessionName:r,platform:a});if(t.runtime===void 0){let e=me(n,r,i,a);return{runtime:i?Z(e,i):e,previousRuntime:o,replacedStoredRuntime:!1}}let c=s&&G(s)>0?s:void 0;return{runtime:i?Z(c,i):c,previousRuntime:o,replacedStoredRuntime:!0}}function he(e){return Q(e).runtime}function ge(e){try{return{ok:!0,data:Q(e)}}catch(e){let n=t(e);return r(n.code,n.message,n.details)}}async function _e(e){let{previousRuntime:t,clearRuntimeHints:n}=e;if(!$(e))return;let r=e.session;r?.appBundleId&&await n({appId:r.appBundleId,values:K(t)})}function $(e){return e.replacedStoredRuntime&&!!e.session?.appBundleId&&q(e.previousRuntime)&&!q(e.runtime)}export{g as _,ue as a,pe as c,ge as d,B as f,z as g,k as h,_e as i,$ as l,b as m,G as n,he as o,V as p,q as r,K as s,le as t,X as u,N as v};