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.01 kB
import{s as e,v as t}from"./sdk-contracts.js";import{A as n,M as r,P as i,w as a}from"./sdk-device.js";import{c as o}from"./device-shell.js";import{t as s}from"./device-isolation.js";function c(e){return`${e.device.platform} device "${e.device.name.trim()}" (${e.device.id})`}function l(e,t,n={}){return e.session.screenRecording?f(e.address,t):m(e,t,n)}function u(t,n,r={}){return e(`DEVICE_IN_USE`,`Device is already in use by session "${t.address}".`,{session:t.address,deviceId:n.id,deviceName:n.name,...r.waitedMs===void 0?{}:{waitedMs:r.waitedMs},hint:l(t,`device-in-use`,r),dispatched:`no`})}function d(t,n,r={}){return e(`DEVICE_IN_USE`,`Device is already in use by workspace session "${t.address}".`,{reason:`WORKSPACE_SESSION_SCOPE_CONFLICT`,session:t.address,deviceId:n.id,deviceName:n.name,...r.waitedMs===void 0?{}:{waitedMs:r.waitedMs},hint:p(t.address,r),dispatched:`no`})}function f(e,t){let n=o(e),r=`agent-device close --session ${n}`,i=`agent-device record stop --session ${n}`,a=t===`selector-conflict`?`use --session ${n} without device selectors`:`reuse --session ${n}`;return`A recording session, maybe another agent's or user's, holds this device. ${g}Only if it is yours: run ${i}, then ${r} if still open; or ${a}.`}function p(e,t){let n=o(e);return`${_(t)}The holding session belongs to another workspace, possibly another agent's or user's. ${g}${v(t)}Run agent-device close --session ${n} only if that workspace is yours.`}function m(e,t,n){let r=e.address,i=o(r),a=`agent-device close --session ${i}`;return t===`selector-conflict`?`To use the selected device, open it with a different --session name.`+y(e,n)+` Only if session "${r}" is yours: rerun with --session ${i} without device selectors, or run ${a}.`:`${_(n)}${h}${g}${v(n)}Only if that session is yours: reuse --session ${i}, or run ${a} first.`}const h=`The holding session may belong to another agent or user. `,g=`Run agent-device devices and open another device with a device selector. `;function _(e){return e.waitedMs===void 0?``:`Waited ${Math.round(e.waitedMs)}ms for this device and it stayed busy. `}function v(e){return e.offersDeviceWait&&e.waitedMs===void 0?`To wait for the device to free up, run the same open with --wait <ms>. `:``}function y(e,t){return t.offersPlatformSession===!0&&e.session.sessionScope?.kind===`cwd`?` Or name the other platform with --platform to open its own session for this workspace.`:``}function b(e,n){let{address:r,session:i}=e,a=x(i,n);if(a.length===0)return;let o=S(i,a);throw new t(`INVALID_ARGS`,`Session "${r}" is already bound to ${c(i)}, but this request selected ${a.map(C).join(`, `)}.`+(o?` ${o}`:``),{session:r,conflicts:a.map(C),hint:l(e,`selector-conflict`,{offersPlatformSession:a.some(e=>e.key===`platform`)})})}function x(e,t){if(!t)return[];let o=[],c=e.device,l=t.platform;if(l&&!i(c,l)&&o.push({key:`platform`,value:t.platform}),t.target&&t.target!==(c.target??`mobile`)&&o.push({key:`target`,value:t.target}),t.udid&&(!a(c)||t.udid!==c.id)&&o.push({key:`udid`,value:t.udid}),t.serial&&(!n(c.platform)||t.serial!==c.id)&&o.push({key:`serial`,value:t.serial}),t.device&&!r(c,t.device)&&o.push({key:`device`,value:t.device}),t.iosSimulatorDeviceSet){let e=t.iosSimulatorDeviceSet.trim(),n=c.simulatorSetPath?.trim();(!a(c)||c.kind!==`simulator`||e!==n)&&o.push({key:`iosSimulatorDeviceSet`,value:t.iosSimulatorDeviceSet})}if(t.androidDeviceAllowlist){let e=s(t.androidDeviceAllowlist);(c.platform!==`android`||!e.has(c.id))&&o.push({key:`androidDeviceAllowlist`,value:t.androidDeviceAllowlist})}return o}function S(e,t){let n=e.device;if(a(n)&&t.some(e=>e.key===`device`&&e.value===n.id))return`--device selects a device by name; select this device by UDID with --udid ${n.id}.`}function C(e){return`${w(e.key)}=${e.value}`}function w(e){switch(e){case`iosSimulatorDeviceSet`:return`--ios-simulator-device-set`;case`androidDeviceAllowlist`:return`--android-device-allowlist`;default:return`--${e}`}}export{u as a,c,x as i,S as n,d as o,C as r,l as s,b as t};