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.45 kB
import{v as e}from"./sdk-contracts.js";import{a as t,i as n,o as r,s as i}from"./sdk-batch-runner.js";import{T as a,y as o}from"./sdk-device.js";import{a as s}from"./session.js";import{t as c}from"./platform-runtime-android-mechanics.js";const l=new Set([`app`,`desktop`,`frontmost-app`]);function u(t){let{device:n,surfaceFlag:r,openTarget:i,existingSurface:o}=t;if(n.platform===`linux`)return d(r,i,o);if(a(n)){let t=f(r,i,o);if(n.hostApp&&t!==`app`)throw new e(`INVALID_ARGS`,`This session holds ${n.hostApp.bundleId} as its own device, so it opens only the app surface. Use a new --session for --surface ${t}.`,{reason:`app-device-scope`,bundleId:n.hostApp.bundleId});return t}return p(r)}function d(t,n,r){if(!t)return r??`app`;let i=s(t);if(!l.has(i))throw new e(`INVALID_ARGS`,`Linux supports --surface app, desktop, and frontmost-app (got "${t}")`);return m(i,n),i}function f(e,t,n){if(!e)return n??`app`;let r=s(e);return r!==`app`&&r!==`menubar`&&m(r,t),r}function p(t){if(t)throw new e(`INVALID_ARGS`,`surface is only supported on macOS and Linux`);return`app`}function m(t,n){if(t!==`app`&&t!==`menubar`&&n)throw new e(`INVALID_ARGS`,`open --surface ${t} does not accept an app target`)}async function h(e){let{target:n,platform:r,surface:i}=e;if(n&&t(n))return`open --relaunch does not support URL targets.`;if(i!==void 0&&i!==`app`)return`open --relaunch is supported only for app surfaces.`;if(r===`android`&&n){let{classifyAndroidAppTarget:e,formatAndroidInstalledPackageRequiredMessage:t}=await c();if(e(n)===`binary`)return t(n)}}async function g(e,s,c){if(!(!o(e.platform)||!s))return t(s)?a(e)?void 0:e.kind===`device`?i(c,s):r(s)?n:c??await _(e,s):await v(e,s)}async function _(e,t){try{let{resolveIosSimulatorDeepLinkBundleId:n}=await import(`./app-resolution-facade.js`);return await n(e,t)}catch{return}}async function v(e,t){try{let{resolveIosApp:n}=await import(`./app-resolution-facade.js`);return await n(e,t)}catch{return}}function y(e,n){return e.platform===`android`&&!!(n&&t(n))}function b(e){let n=e?.trim();if(!(!n||t(n)||!n.includes(`.`)))return n}async function x(e,t,n){return e.platform===`harmonyos`?b(t)??n:await g(e,t,n)??await S(e,t)??(y(e,t)?n:void 0)}async function S(e,t){if(!(e.platform!==`android`||!t))try{let{resolveAndroidPackageForOpen:n}=await c();return await n(e,t)}catch{return}}export{u as resolveRequestedOpenSurface,x as resolveSessionAppBundleIdForTarget,h as validateOpenRelaunchTarget};