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 • 10.8 kB
import{T as e,v as t}from"./sdk-contracts.js";import{i as n,n as r,r as i}from"./interactor-operation-binding.js";function a(e){return typeof e!=`string`||e.trim().length===0?Object.freeze({status:`unreadable`,reason:`no-text-at-point`}):Object.freeze({status:`read`,text:e})}function o(e){return Object.freeze({readTextAtPoint:e.readTextAtPoint})}function s(t){return Object.freeze({readTextAtPoint:async n=>{let r=t.signal;r.throwIfAborted();let i=await t.resolveInteractor(t.device,{...n.execution,appBundleId:n.options?.appBundleId,signal:r});if(typeof i.readTextAtPoint!=`function`)throw e(`Runtime owner advertised readTextAtPoint without an interactor implementation`);return a(await i.readTextAtPoint(n.point,{appBundleId:n.options?.appBundleId,surface:n.options?.surface,signal:r,...n.options?.target?{target:n.options.target}:{}}))}})}function ee(e){return s({...e,resolveInteractor:async(t,n)=>{let{providerInteractorSource:r}=await import(`./interactor-operation-binding.js`).then(e=>e.t);return await r({...e,operation:`element-text`})(n)}})}function te(e){return Object.freeze({focusPoint:e.focus})}function c(e,t){return Object.freeze({focusPoint:async n=>{e.throwIfAborted(),await(await t({...n.execution,appBundleId:n.options?.appBundleId,signal:e})).focus(n.point.x,n.point.y)}})}function l(e){return c(e.signal,r(e))}function ne(e){return c(e.signal,i({...e,operation:`focus`}))}function re(e){let t=t=>t??e.unsupported;return Object.freeze({performGesturePlan:t(e.plan),performDirectionalFlingPlan:t(e.directionalFling),performMultiTouchGesturePlan:t(e.multiTouch),performTargetAuthoredDrag:t(e.targetAuthoredDrag),gestureViewport:t(e.viewport)})}function u(t,n,r){let i=async t=>{let i=await d(n,r,t);if(typeof i.performGesture!=`function`)throw e(`Runtime owner advertised gesture execution without an interactor implementation`);return await i.performGesture(t.plan)};return Object.freeze({...t.performGesturePlan.available?{performGesturePlan:i}:{},...t.performDirectionalFlingPlan.available?{performDirectionalFlingPlan:i}:{},...t.performMultiTouchGesturePlan.available?{performMultiTouchGesturePlan:i}:{},...t.performTargetAuthoredDrag.available?{performTargetAuthoredDrag:i}:{},...t.gestureViewport.available?{gestureViewport:async t=>{let i=await d(n,r,t);if(typeof i.gestureViewport!=`function`)throw e(`Runtime owner advertised gestureViewport without an interactor implementation`);return t.includesKeyboard?await i.gestureViewport(!0):await i.gestureViewport()}}:{}})}async function d(e,t,n){return e.throwIfAborted(),await t({...n.execution,appBundleId:n.options?.appBundleId,signal:e})}function f(e){return u(e.facts,e.signal,r(e))}function p(e){return u(e.facts,e.signal,i({...e,operation:`gesture`}))}function m(e){let t=e??{},n=h(t.pixels),r=h(t.x1),i=h(t.y1),a=h(t.x2),o=h(t.y2),s=r===void 0||i===void 0||a===void 0||o===void 0?void 0:{x:r/2+a/2,y:i/2+o/2};return{report:t,...n===void 0?{}:{pixels:n},...s===void 0?{}:{midpoint:s}}}function h(e){return typeof e==`number`&&Number.isFinite(e)?e:void 0}function g(e){return Object.freeze({scrollDirection:e.scroll})}function _(e,t){return Object.freeze({scrollDirection:async n=>(e.throwIfAborted(),m(await(await t({...n.execution,appBundleId:n.target?.appBundleId,signal:e})).scroll(n.direction,n.options)))})}function ie(e){return _(e.signal,r(e))}function ae(e){return _(e.signal,i({...e,operation:`scroll`}))}function oe(e){return Object.freeze({findText:e.findText,querySelector:e.querySelector})}function v(e,t){return t.signal===void 0?e:AbortSignal.any([e,t.signal])}function y(e){return Object.freeze({captureSnapshot:e.capture,captureSnapshotWithCustomActions:e.customActions,captureSnapshotWithoutActiveApp:e.withoutActiveApp})}function b(e){let n=async n=>{await x(e,n);let r=v(e.signal,n),i={...n.execution,appBundleId:n.options?.appBundleId,signal:r},a=e.ownership===`local`?await e.resolveInteractor(e.device,i):e.resolveInteractor(i);if(!a)throw new t(`UNSUPPORTED_OPERATION`,`Provider-owned snapshot operation has no bound provider interactor.`,{reason:`provider-runtime-interactor-missing`,deviceId:e.device.id});let o=await a.snapshot({...n.options,signal:r});if(!w(o))return o;if(!e.presentIosAcquisition)throw new t(`COMMAND_FAILED`,`Acquired iOS snapshot has no host presentation owner.`,{reason:`ios-snapshot-presenter-missing`,deviceId:e.device.id});return await e.presentIosAcquisition(o,n.options)};return Object.freeze({captureSnapshot:n,captureSnapshotWithCustomActions:n,captureSnapshotWithoutActiveApp:n})}async function x(e,n){if(n.options?.observeOnly!==!0)return;let{isHandheldAppleDevice:r}=await import(`./sdk-device.js`).then(e=>e.f);if(!(e.ownership===`local`&&r(e.device)))throw new t(`UNSUPPORTED_OPERATION`,`observe-only snapshot requires a local iOS or iPadOS runner.`)}function S(e){return b({...e,ownership:`local`})}function C(e){return b({...e,ownership:`provider`})}function w(e){return`stage`in e&&e.stage===`acquired`}function T(e){return Object.freeze({typeText:e.type})}function E(e,t){return Object.freeze({typeText:async n=>(e.throwIfAborted(),await(await t({...n.execution,appBundleId:n.options?.appBundleId,signal:e})).type(n.text,n.delayMs))})}function D(e){return E(e.signal,r(e))}function O(e){return E(e.signal,i({...e,operation:`type`}))}function k(e){return Object.freeze({back:e.back})}function A(e,t){return Object.freeze({back:async n=>{e.throwIfAborted(),await(await t({...n.execution,appBundleId:n.options?.appBundleId,signal:e})).back(n.mode)}})}function j(e){return Object.freeze({readAlert:e.read,awaitAlert:e.wait,acceptAlert:e.accept,dismissAlert:e.dismiss})}async function M(e,t,n){return e.throwIfAborted(),await t({...n.execution,appBundleId:n.appBundleId,signal:e})}function N(e,t){return{...e.timeoutMs===void 0?{}:{timeoutMs:e.timeoutMs},...e.appBundleId===void 0?{}:{appBundleId:e.appBundleId},...e.surface===void 0?{}:{surface:e.surface},...t?{signal:t}:{}}}const P={readAlert:`alert get`,awaitAlert:`alert wait`,acceptAlert:`alert accept`,dismissAlert:`alert dismiss`};function F(e,t,n){return Object.freeze({[e]:async r=>{let i=await M(t,n,r),{requireInteractorMethod:a}=await import(`./interactor-operation-binding.js`).then(e=>e.t);return await a(i[e],P[e]).call(i,N(r,t))}})}function I(e){return Object.freeze({triggerAppEvent:e.triggerAppEvent})}function L(e,t){return Object.freeze({triggerAppEvent:async n=>{e.throwIfAborted(),await(await t({...n.execution,appBundleId:n.options?.appBundleId,signal:e})).open(n.eventUrl,{appBundleId:n.options?.appBundleId})}})}const R=[`home`,`appSwitcher`,`actionButton`,`volumeUp`,`volumeDown`,`power`,`mute`,`sleep`,`wake`,`mediaPlayPause`,`mediaNext`,`mediaPrevious`,`mediaRewind`,`mediaFastForward`],z={home:`home`,appSwitcher:`app-switcher`,actionButton:`action-button`,volumeUp:`volume-up`,volumeDown:`volume-down`,power:`power`,mute:`mute`,sleep:`sleep`,wake:`wake`,mediaPlayPause:`media-play-pause`,mediaNext:`media-next`,mediaPrevious:`media-previous`,mediaRewind:`media-rewind`,mediaFastForward:`media-fast-forward`};function B(e){let t={};for(let n of R)t[n]=e[n]??e.unsupported;return Object.freeze(t)}const V=Object.freeze({available:!1,reason:`unsupported-device-kind`,hint:`hardware-button is not supported on Android TV targets; use tv-remote.`}),H=Object.freeze({available:!1,reason:`unsupported-device-kind`,hint:`hardware-button is supported on Android emulators and physical devices.`}),U=Object.freeze({available:!0}),W=R.filter(e=>e!==`home`&&e!==`appSwitcher`&&e!==`actionButton`);function G(e){let t=K(e),n={};for(let e of W)n[e]=t;return n}function K(e){return e.kind===`simulator`?H:e.target===`tv`?V:U}function q(e,t,r){let i=async i=>{t.throwIfAborted();let a=await r({...i.execution,appBundleId:i.options?.appBundleId,signal:t});await n(a[e],z[e]).call(a)};return Object.freeze({[e]:i})}function se(e){let t=t=>t??e.unsupported;return Object.freeze({readClipboard:t(e.read),writeClipboard:t(e.write)})}const J={readClipboard:`clipboard read`,writeClipboard:`clipboard write`};async function Y(e,t,n){return e.throwIfAborted(),await t({...n.execution,appBundleId:n.options?.appBundleId,signal:e})}async function X(e,t){let{requireInteractorMethod:n}=await import(`./interactor-operation-binding.js`).then(e=>e.t);return n(e[t],J[t])}function Z(e,t){return Object.freeze({readClipboard:async n=>{let r=await Y(e,t,n);return await(await X(r,`readClipboard`)).call(r)}})}function ce(e,t){return Object.freeze({writeClipboard:async n=>{let r=await Y(e,t,n);await(await X(r,`writeClipboard`)).call(r,n.text)}})}function le(e){let t=t=>t??e.unsupported;return Object.freeze({keyboardStatus:t(e.status),keyboardDismiss:t(e.dismiss),keyboardEnter:t(e.enter)})}async function ue(e,t,n){return e.throwIfAborted(),await t({...n.execution,appBundleId:n.options?.appBundleId,signal:e})}const Q={keyboardStatus:`keyboard status`,keyboardDismiss:`keyboard dismiss`,keyboardEnter:`keyboard enter`};function de(e,t,n){let r=async r=>{let i=await ue(t,n,r),{requireInteractorMethod:a}=await import(`./interactor-operation-binding.js`).then(e=>e.t);return await a(i[e],Q[e]).call(i)};return Object.freeze({[e]:r})}const fe=Object.freeze({available:!1,reason:`unsupported-platform-leaf`,hint:`orientation auto is only supported on ADB-backed Android.`});function pe(e){return Object.freeze({setOrientation:e.orientation,unlockOrientation:e.unlock??fe})}function me(e,t){return Object.freeze({setOrientation:async n=>(e.throwIfAborted(),await(await t({...n.execution,appBundleId:n.options?.appBundleId,signal:e})).setOrientation(n.rotation))})}function he(e,t){return Object.freeze({unlockOrientation:async n=>{e.throwIfAborted();let r=await t({...n.execution,appBundleId:n.options?.appBundleId,signal:e}),{requireInteractorMethod:i}=await import(`./interactor-operation-binding.js`).then(e=>e.t);await i(r.unlockOrientation,`orientation auto`).call(r)}})}function ge(e){let{tvRemote:t,buttonSupport:n}=e;return Object.freeze({tvRemote:t.available&&n?Object.freeze({available:!0,subjects:Object.freeze(Object.fromEntries(n.supported.map(e=>[e,{available:!0}]))),unlisted:n.unsupported}):t})}const $=`tv-remote`;function _e(e,t){return Object.freeze({tvRemote:async n=>{e.throwIfAborted();let r=await t({...n.execution,appBundleId:n.options?.appBundleId,signal:e}),{requireInteractorMethod:i}=await import(`./interactor-operation-binding.js`).then(e=>e.t);await i(r.tvRemote,$).call(r,n.button,n.durationMs)}})}export{v as A,ne as B,k as C,T as D,O as E,g as F,s as H,f as I,p as L,oe as M,ie as N,S as O,ae as P,re as R,F as S,D as T,ee as U,te as V,o as W,B as _,he as a,P as b,de as c,Z as d,ce as f,q as g,G as h,me as i,y as j,C as k,le as l,z as m,_e as n,pe as o,se as p,ge as r,Q as s,$ as t,J as u,I as v,A as w,j as x,L as y,l as z};