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 • 15.5 kB
JavaScript
import{f as e}from"./sdk-batch.js";import{g as t}from"./sdk-batch-runner.js";import{a as n,o as r,r as i,t as a}from"./sdk-device.js";import{t as o}from"./recording-scope.js";import{a as s}from"./scroll-gesture.js";import{n as c,t as l}from"./catalog.js";import{n as ee}from"./session.js";import{t as te}from"./click-button.js";import{_ as u,n as d,p as f,u as p}from"./device.js";import{t as m}from"./back-mode.js";import{i as h}from"./tv-remote.js";const ne=[`status`,`get`,`dismiss`,`enter`,`return`];function g(e){return ne.includes(e)}const re=[`id`,`deviceId`,`device_id`,`serial`,`udid`,`device_udid`,`identifier`],_={ios:`iOS`,ipados:`iPadOS`,tvos:`tvOS`,watchos:`watchOS`,visionos:`visionOS`,macos:`macOS`,apple:`Apple`,android:`Android`,linux:`Linux`,web:`Web`};function ie(e,t){let n=t??{};switch(e.command){case c.devices:return ae(n.devices);case c.apps:return oe(n.apps,e.flags?.appsFilter);case c.boot:return v(`Booted`,n,!0);case c.shutdown:return v(`Shut down`,n,!1);case c.install:return y(`Installed`,n);case c.reinstall:return y(`Reinstalled`,n);case c.screenshot:return se(e,n);default:return{}}}function ae(e){if(!Array.isArray(e))return{};let t=e.flatMap(e=>{let t=ce(e);return t?[t]:[]}),n=t.slice(0,5),r=Math.max(0,t.length-n.length),i=t.slice(0,3);return{summary:D(`Found ${t.length} ${O(`device`,t.length)}`,i.map(e=>w(e,!0)),Math.max(0,t.length-i.length)),details:{deviceCount:t.length,devices:n,...r>0?{omittedDeviceCount:r}:{}}}}function oe(e,t){if(!Array.isArray(e))return{};let n=e.flatMap(e=>{let t=k(e);return t?[t]:[]}),r=n.slice(0,5),i=Math.max(0,n.length-r.length),a=n.slice(0,3);return{summary:D(`Found ${n.length} ${O(`app`,n.length)}`,a,Math.max(0,n.length-a.length)),details:{appCount:n.length,apps:r,...i>0?{omittedAppCount:i}:{},...t===`all`||t===`user-installed`?{filter:t}:{}}}}function v(e,t,n){let r=x(t);if(!r)return{};let i={device:r,...C(t),booted:n};return{summary:`${e} ${w(i,!1)}`,details:i}}function y(e,t){let n=k(t.appName),r=k(t.bundleId),i=T(t,[`packageName`,`package`]),a=k(t.platform),o=k(t.launchTarget),s=T(t,[`bundleId`,`packageName`,`package`,`appId`,`launchTarget`]),c=fe(n,s);if(!c)return{};let l={};return E(l,`appName`,n),E(l,`bundleId`,r),E(l,`packageName`,i),E(l,`platform`,a),o!==s&&E(l,`launchTarget`,o),{summary:`${e} ${c}`,details:l}}function se(e,t){let n=b(e,t);return n?{details:{requestedFileName:n}}:{}}function b(e,t){let n=F(e.meta?.clientArtifactPaths?.path)??F(t.path);if(n)return P(n)}function ce(e){if(!t(e))return;let n=x(e);if(n)return{name:n,...C(e)}}function x(e){let t=T(e,[`name`,`deviceName`,`device`]);return!t||!le(e,t)?t:ue(e)}function le(e,t){let n=S(t);return re.some(t=>{let r=k(e[t]);return r!==void 0&&S(r)===n})}function S(e){return e.toLowerCase()}function ue(e){let t=k(e.appleOs),n=k(e.platform),r=k(e.kind),i=de(t,n);return i&&(r===`simulator`||r===`emulator`)?`${i} ${r}`:i?`${i} device`:r===`simulator`?`Simulator`:r===`emulator`?`Emulator`:`Unnamed device`}function de(e,t){let n=e??t;if(!(!n||!Object.hasOwn(_,n)))return _[n]}function C(e){let t=k(e.platform),n=k(e.appleOs),r=k(e.kind),i=k(e.target);return{...t?{platform:t}:{},...n?{appleOs:n}:{},...r?{kind:r}:{},...i?{target:i}:{},...typeof e.booted==`boolean`?{booted:e.booted}:{}}}function w(e,t){let n=k(e.name)??k(e.device)??`device`,r=k(e.platform),i=k(e.appleOs),a=[r,i===r?void 0:i,k(e.kind),k(e.target),t&&typeof e.booted==`boolean`?e.booted?`booted`:`stopped`:void 0].filter(e=>!!e);return a.length>0?`${n} (${a.join(`, `)})`:n}function fe(e,t){return e&&t&&e!==t?`${e} (${t})`:e??t}function T(e,t){for(let n of t){let t=k(e[n]);if(t)return t}}function E(e,t,n){n&&(e[t]=n)}function D(e,t,n){let r=[...t,...n>0?[`+${n} more`]:[]];return r.length>0?`${e}: ${r.join(`, `)}`:e}function O(e,t){return t===1?e:`${e}s`}function k(e){let t=F(e);if(!t)return;let n=t.trim(),r=Array.from(n);return r.length<=120?n:`${r.slice(0,119).join(``)}…`}function A(e){return k(e)}function j(e){return typeof e==`boolean`?e:void 0}function M(e,t){let n=A(e);return n&&t.includes(n)?n:void 0}function N(e){return Object.fromEntries(Object.entries(e).filter(([,e])=>e!==void 0))}function P(e){let t=F(e);return t?k(t.split(/[\\/]/).at(-1)):void 0}function F(e){if(typeof e==`string`)return e.trim().length>0?e:void 0}function I(e){return typeof e==`number`&&Number.isFinite(e)?e:void 0}const L=[`up`,`down`,`left`,`right`],R=[`top`,`bottom`];function pe(e){return`${e.command===c.reinstall?`Reinstalled`:`Installed`} ${_e(e.result??{})??`app`}`}function me(e){let t=e.result??{};switch(e.command){case c.scroll:return ye(t);case c.screenshot:return Se(t);case c.home:return`Went home`;case c.back:return Ce(t);case c.appSwitcher:return`Opened app switcher`;case c.actionButton:return`Pressed Action Button`;case c.hardwareButton:return Me(t);case c.orientation:return Ee(t);case c.fold:return we(t);case c.telephony:return Te(t);case c.viewport:return De(t);case c.clipboard:return Oe(t);case c.keyboard:return ke(t);case c.tvRemote:return Ne(t);case c.gesture:return Pe(t);case c.swipe:return Fe(t);case c.trace:return B(`trace`,e);case c.record:return B(`recording`,e);default:return}}function he(e){let t=e.result??{};switch(e.command){case c.scroll:return N({direction:M(t.direction,L),edge:M(t.edge,R),passes:I(t.passes),amount:I(t.amount),pixels:I(t.pixels),durationMs:I(t.durationMs),x1:I(t.x1),y1:I(t.y1),x2:I(t.x2),y2:I(t.y2)});case c.screenshot:return N({requestedFileName:z(t)});case c.back:return N({mode:M(t.mode,m)});case c.orientation:return N({orientation:M(t.orientation,u)});case c.fold:return N({pose:M(t.pose,p)});case c.telephony:return N({action:M(t.action,d)});case c.viewport:return N({width:I(t.width),height:I(t.height)});case c.clipboard:return N({action:M(t.action,[`read`,`write`])});case c.keyboard:return je(t);case c.tvRemote:return N({button:h(t.button)?t.button:void 0,durationMs:I(t.durationMs)});case c.hardwareButton:return N({button:M(t.button,f)});case c.gesture:case c.swipe:return Ie(t);case c.trace:case c.record:return N({action:M(t.action,[`start`,`stop`]),fileName:V(e),showTouches:j(t.showTouches)});default:return{}}}function ge(e,t){if(e.command!==c.screenshot)return t;let n=b(e,t);return n?{...t,requestedFileName:n}:t}function _e(e){let t=A(e.appName),n=Re(e,[`bundleId`,`packageName`,`appBundleId`,`launchTarget`]);return t&&n&&t!==n?`${t} (${n})`:t??n??ve(e)}function ve(e){let t=A(e.app);return t&&!t.includes(`/`)&&!t.includes(`\\`)?t:void 0}function ye(e){let t=M(e.direction,L),n=M(e.edge,R),r=I(e.passes);return n?be(n,r):t?xe(t,e):`Scrolled`}function be(e,t){return t===0?`Already at ${e}`:t===void 0?`Scrolled to ${e}`:`Scrolled to ${e} in ${t} ${t===1?`pass`:`passes`}`}function xe(e,t){let n=I(t.pixels);if(n!==void 0)return`Scrolled ${e} by ${n}px`;let r=I(t.amount);return r===void 0?`Scrolled ${e}`:`Scrolled ${e} by ${r}`}function Se(e){let t=z(e);return t?`Captured screenshot ${t}`:`Captured screenshot`}function z(e){return P(e.requestedFileName)??P(e.path)}function Ce(e){return M(e.mode,m)===`system`?`Went back using system navigation`:`Went back`}function we(e){let t=M(e.pose,p);return t?`Folded to ${t}`:`Changed fold pose`}function Te(e){switch(M(e.action,d)){case`call`:return`Simulated an incoming call`;case`accept`:return`Accepted the simulated call`;case`cancel`:return`Ended the simulated call`;case`sms`:return`Simulated an incoming SMS`;case void 0:return`Simulated a telephony event`}}function Ee(e){let t=M(e.orientation,u);return t?t===`auto`?`Rotation follows the device sensor`:j(e.confirmed)===!1?`Requested ${t} (unconfirmed)`:`Rotated to ${t}`:`Changed orientation`}function De(e){let t=I(e.width),n=I(e.height);return t!==void 0&&n!==void 0?`Set viewport to ${t}×${n}`:`Changed viewport`}function Oe(e){let t=M(e.action,[`read`,`write`]);return t===`read`?`Read clipboard`:t===`write`?`Updated clipboard`:`Used clipboard`}function ke(e){let t=F(e.action);if(!t||!g(t))return`Used keyboard`;if(t===`dismiss`)return Ae(e);if(t===`enter`||t===`return`)return`Pressed keyboard return`;let n=j(e.visible);return n===void 0?`Inspected keyboard`:n?`Keyboard is visible`:`Keyboard is hidden`}function Ae(e){return j(e.dismissed)===!1?`Keyboard was already hidden`:`Dismissed keyboard`}function je(e){let t=F(e.action);return N({action:t&&g(t)?t:void 0,platform:M(e.platform,[`ios`,`android`]),visible:j(e.visible),wasVisible:j(e.wasVisible),dismissed:j(e.dismissed),attempts:I(e.attempts),mechanism:M(e.mechanism,[`dismissKey`])})}function Me(e){let t=M(e.button,f);return t===void 0?`Pressed hardware button`:`Pressed hardware button ${t}`}function Ne(e){let t=h(e.button)?e.button:void 0;return t?`Pressed TV remote ${t}`:`Used TV remote`}function Pe(e){let t=M(e.kind,[`fling`,`pan`,`pinch`,`rotate`,`transform`,`tap`]);return t?`Ran ${t} gesture`:`Ran gesture`}function Fe(e){let t=I(e.count);return t!==void 0&&t>1?`Swiped ${t} times`:`Swiped`}function Ie(e){return N({kind:M(e.kind,[`fling`,`pan`,`pinch`,`rotate`,`transform`,`tap`]),durationMs:I(e.durationMs),pointerCount:I(e.pointerCount),x1:I(e.x1),y1:I(e.y1),x2:I(e.x2),y2:I(e.y2),count:I(e.count),completed:I(e.completed),pauseMs:I(e.pauseMs),pattern:M(e.pattern,[`one-way`,`ping-pong`]),scale:I(e.scale)})}function B(e,t){let n=Le(e,M(t.result?.action,[`start`,`stop`])),r=V(t);return r?`${n} ${r}`:n}function Le(e,t){return t===`start`?e===`trace`?`Started trace`:`Started recording`:t===`stop`?e===`trace`?`Stopped trace`:`Stopped recording`:e===`trace`?`Updated trace`:`Updated recording`}function V(e){let t=e.result??{};return P(t.requestedFileName)??P(t.outPath)??P(t.path)??P(e.positionals[1])}function Re(e,t){for(let n of t){let t=A(e[n]);if(t)return t}}function ze(e){switch(e.command){case c.open:return`Opened ${H(e)??`session`}`;case c.close:return`Closed ${A(e.result?.session)??`session`}`;case c.click:case c.press:return`Tapped ${H(e)??`target`}`;case c.longPress:return`Long pressed ${H(e)??`target`}`;case c.hover:return`Hovered ${H(e)??`target`}`;case c.fill:return`Filled ${H(e)??`target`}`;case c.type:return`Typed text`;case c.install:case c.reinstall:case l.installSource:return pe(e);default:return me(e)??`Ran ${e.command}`}}function Be(e){return N({command:e.command,positionals:U(e),flags:rt(e),...Ve(e),...He(e),...Ue(e),...he(e)})}function Ve(e){if(!We(e.command))return{};let t=e.result??{};return N({ref:$(t),targetLabel:H(e),selectorChainLength:et(t.selectorChain)?.length,x:I(t.x),y:I(t.y),x2:I(t.x2),y2:I(t.y2),durationMs:I(t.durationMs)})}function He(e){if(!Ge(e.command))return{};let t=e.result??{};return N({platform:M(t.platform,r),target:M(t.target,i),appName:A(t.appName),appBundleId:A(t.appBundleId),bundleId:A(t.bundleId),packageName:A(t.packageName),launchTarget:A(t.launchTarget)})}function Ue(e){let t=e.result??{};return e.command===c.snapshot?Array.isArray(t.nodes)?{nodeCount:t.nodes.length}:{}:e.command!==c.wait&&e.command!==c.find&&e.command!==c.get&&e.command!==c.is?{}:N({waitedMs:I(t.waitedMs),found:j(t.found)})}function We(e){return e===c.click||e===c.press||e===c.longPress||e===c.hover||e===c.focus||e===c.fill}function Ge(e){return e===c.open||e===c.install||e===c.reinstall||e===l.installSource}function H(e){let t=e.result??{};return Ke(t)??qe(t)??Je(t)??K(e.positionals[0])}function Ke(e){let t=$(e);if(t)return t.startsWith(`@`)?t:`@${t}`}function qe(e){let t=I(e.x),n=I(e.y);if(t!==void 0&&n!==void 0)return`(${t}, ${n})`}function Je(e){return A(e.appName)??A(e.appBundleId)??A(e.bundleId)??A(e.packageName)}function U(e){if(e.positionals.length!==0)return e.command===c.type?[q(`text`)]:e.command===c.fill?Ye(e):e.command===c.find?Xe(e):e.command===c.clipboard?Ze(e):e.command===c.push?W(e,`app`,`payload`):e.command===c.triggerAppEvent?W(e,`event`,`payload`):e.positionals.map(G)}function Ye(e){let t=q(`text`),n=e.result??{},r=$(n);if(r)return[`@${r}`,t];if(A(n.selector))return[q(`target`),t];let i=I(n.x),a=I(n.y);return i!==void 0&&a!==void 0?[String(i),String(a),t]:[t]}function Xe(e){let t=+!!$e(e.positionals[0]);if(e.positionals[t]===void 0)return;let n=t+1,r=[...t===1?[String(e.positionals[0])]:[],q(`query`)],i=e.positionals[n];return i?i===`fill`||i===`type`?[...r,i,q(`text`)]:[...r,...e.positionals.slice(n).map(Qe)]:r}function Ze(e){let t=e.positionals[0]?.toLowerCase();return t===`read`?[`read`]:t===`write`?[`write`,q(`text`)]:e.positionals.map(G)}function W(e,t,n){let[r,i,...a]=e.positionals;return[...r?[q(t)]:[],...i?[q(n)]:[],...a.map(G)]}function Qe(e){return e===`click`||e===`focus`||e===`exists`||e===`get`||e===`text`||e===`attrs`||e===`wait`?e:G(e)}function G(e){return K(e)??q(`arg`)}function K(e){if(!e)return;let t=e.trim();if(/^@[a-zA-Z0-9:_-]+$/.test(t))return t}function q(e){return`<${e}>`}function $e(e){return e===`text`||e===`label`||e===`value`||e===`role`||e===`id`}function et(e){return Array.isArray(e)&&e.every(e=>typeof e==`string`)?e:void 0}const tt={enums:[{source:`platform`,values:n},{source:`target`,values:i}]},nt={booleans:[{source:`settle`}],numbers:[{source:`settleQuietMs`}]},J={[c.open]:{booleans:[{source:`relaunch`},{source:`testIme`}],enums:[{source:`surface`,values:ee}]},[c.click]:Y(),[c.press]:Y(),[c.longPress]:Y(),[c.hover]:Y(),[c.fill]:X(),[c.type]:X(),[c.scroll]:{numbers:[{source:`pixels`},{source:`durationMs`}]},[c.gesture]:Z(),[c.swipe]:Z(),[c.screenshot]:{booleans:[{source:`overlayRefs`},{source:`screenshotFullscreen`,output:`fullscreen`},{source:`screenshotNoStabilize`,output:`noStabilize`},{source:`screenshotNormalizeStatusBar`,output:`normalizeStatusBar`}],numbers:[{source:`screenshotPixelDensity`,output:`pixelDensity`},{source:`screenshotScale`,output:`scale`}]},[c.snapshot]:{booleans:[{source:`snapshotInteractiveOnly`,output:`interactiveOnly`},{source:`snapshotDiff`,output:`diff`},{source:`snapshotRaw`,output:`raw`},{source:`snapshotCustomActions`,output:`actions`},{source:`snapshotForceFull`,output:`forceFull`}],numbers:[{source:`snapshotDepth`,output:`depth`}]},[c.back]:{enums:[{source:`backMode`,output:`mode`,values:m}]},[c.record]:{booleans:[{source:`hideTouches`}],numbers:[{source:`fps`}],enums:[{source:`recordingScope`,output:`scope`,values:o}]},[c.apps]:{enums:[{source:`appsFilter`,output:`filter`,values:[`user-installed`,`all`]}]},[c.boot]:{booleans:[{source:`headless`}],enums:[{source:`pick`,values:a}]},[c.close]:{booleans:[{source:`shutdown`}]}};function rt(e){let t=e.flags??{},n={},r=Object.hasOwn(J,e.command)?J[e.command]:void 0;return Q(n,t,tt),Q(n,t,r),Q(n,t,it(e.command)),Object.keys(n).length>0?n:void 0}function it(t){return e(t)?nt:void 0}function Y(){return{booleans:[{source:`doubleTap`}],numbers:[{source:`count`},{source:`intervalMs`},{source:`holdMs`},{source:`jitterPx`}],enums:[{source:`clickButton`,values:te}]}}function X(){return{numbers:[{source:`delayMs`}]}}function Z(){return{numbers:[{source:`pointerCount`},{source:`count`},{source:`pauseMs`}],enums:[{source:`pattern`,values:s}]}}function Q(e,t,n){n&&(at(e,t,n.booleans??[]),ot(e,t,n.numbers??[]),st(e,t,n.enums??[]))}function at(e,t,n){for(let r of n)ct(e,r.output??r.source,t[r.source])}function ot(e,t,n){for(let r of n)lt(e,r.output??r.source,t[r.source])}function st(e,t,n){for(let r of n)ut(e,r.output??r.source,t[r.source],r.values)}function $(e){let t=A(e.ref);if(!t)return;let n=t.startsWith(`@`)?t.slice(1):t;return/^[a-zA-Z0-9:_-]+$/.test(n)?n:void 0}function ct(e,t,n){typeof n==`boolean`&&(e[t]=n)}function lt(e,t,n){let r=I(n);r!==void 0&&(e[t]=r)}function ut(e,t,n,r){let i=M(n,r);i!==void 0&&(e[t]=i)}export{ie as a,ge as i,ze as n,g as o,U as r,Be as t};