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 • 5.1 kB
JavaScript
import{li as e}from"./sdk-batch.js";import{v as t}from"./sdk-contracts.js";import{t as n}from"./string-enum.js";const r=[`up`,`down`,`left`,`right`,`top`,`bottom`],i=[`up`,`down`,`left`,`right`],a=[`left`,`right`,`left-edge`,`right-edge`],o=new Set([`left-edge`,`right-edge`]);function s(e){return o.has(e)}const c=[`one-way`,`ping-pong`],l=1e4,u=6e4,d=n(i,{message:e=>`Unknown direction: ${e}`}),f=.6;function p(e){let t=e.direction,n=t===`up`||t===`down`,r=n?e.referenceHeight:e.referenceWidth,i=n?e.referenceWidth:e.referenceHeight,a=O(e.amount),o=e.pixels===void 0?Math.round(r*a):k(e.pixels),s=h(r),c=Math.max(1,r-s*2),l=Math.max(1,Math.min(o,c)),u=Math.round(l/2),d=Math.round(r/2),f={axisStart:d-u,axisEnd:d+u,cross:Math.round(i/2)},p=h(i),_=e.container?g(f,e.container,n,{axis:{min:s,max:r-s},cross:{min:p,max:i-p},crossFrame:{min:0,max:i}}):f,v=_.axisEnd-_.axisStart,y=v<l;return{direction:t,...m(t,_),referenceWidth:e.referenceWidth,referenceHeight:e.referenceHeight,amount:e.amount,pixels:y?v:l,...y?{requestedPixels:l}:{}}}function m(e,t){let n=e===`up`||e===`down`,r=n?{x:t.cross,y:t.axisStart}:{x:t.axisStart,y:t.cross},i=n?{x:t.cross,y:t.axisEnd}:{x:t.axisEnd,y:t.cross},[a,o]=e===`up`||e===`left`?[r,i]:[i,r];return{x1:a.x,y1:a.y,x2:o.x,y2:o.y}}function h(e){return Math.max(1,Math.round(e*.1))}function g(e,t,n,r){let i=_(n?t.y:t.x,n?t.height:t.width,r.axis),a=n?t.x:t.y,o=n?t.width:t.height,s=_(a,o,r.cross)??_(a,o,r.crossFrame);if(!i||!s)return e;let c=e.cross>=s.min&&e.cross<=s.max?e.cross:Math.round((s.min+s.max)/2);if(e.axisEnd-e.axisStart>=i.max-i.min)return{axisStart:i.min,axisEnd:i.max,cross:c};let l=e.axisStart<i.min?i.min-e.axisStart:Math.min(0,i.max-e.axisEnd);return{axisStart:e.axisStart+l,axisEnd:e.axisEnd+l,cross:c}}function _(e,t,n){let r=Math.max(n.min,Math.ceil(e)+8),i=Math.min(n.max,Math.floor(e+t)-1-8);return i>r?{min:r,max:i}:void 0}function v(e){O(e.amount),e.pixels!==void 0&&k(e.pixels)}function y(e,t){if(e===`left`||e===`right`){let n=x(e,t);return{preset:e,x1:n.x1,y1:n.y1,x2:n.x2,y2:n.y2,referenceWidth:n.referenceWidth,referenceHeight:n.referenceHeight}}let[n,r]=e===`left-edge`?[99,15]:[1,85],i=w(C(t,n,50),t,8),a=w(C(t,r,50),t,8);return{preset:e,x1:i.x,y1:i.y,x2:a.x,y2:a.y,referenceWidth:t.referenceWidth,referenceHeight:t.referenceHeight}}function b(e,t){switch(e){case`up`:return{x:0,y:-t};case`down`:return{x:0,y:t};case`left`:return{x:-t,y:0};case`right`:return{x:t,y:0}}}function x(e,t){let n=e===`left`||e===`up`,r=n?85:15,i=n?15:85,a=e===`up`||e===`down`,o=w(C(t,a?50:r,a?r:50),t,8),s=w(C(t,a?50:i,a?i:50),t,8);return{direction:e,x1:o.x,y1:o.y,x2:s.x,y2:s.y,referenceWidth:t.referenceWidth,referenceHeight:t.referenceHeight}}function S(e){let t=E(e);if(t)return{referenceWidth:t.width,referenceHeight:t.height}}function C(e,t,n){let r=Math.trunc(e.referenceWidth*t/100),i=Math.trunc(e.referenceHeight*n/100);return{x:Number.isFinite(r)?r:0,y:Number.isFinite(i)?i:0}}function w(e,t,n){return{x:A(e.x,n,t.referenceWidth),y:A(e.y,n,t.referenceHeight)}}function T(e){return d.parse(e)}function E(t){let n=t.filter(t=>e(t)&&D(t.rect)).map(e=>e.rect).sort((e,t)=>(t?.width??0)*(t?.height??0)-(e?.width??0)*(e?.height??0))[0];if(n)return n;let r=t.map(e=>e.rect).filter(D);if(r.length===0)return;let i=Math.max(...r.map(e=>e.x+e.width)),a=Math.max(...r.map(e=>e.y+e.height));if(!(i<=0||a<=0))return{x:0,y:0,width:i,height:a}}function D(e){return!!e&&e.width>0&&e.height>0}function O(e){if(e===void 0)return f;if(!Number.isFinite(e)||e<=0)throw new t(`INVALID_ARGS`,`scroll amount must be a positive number`);return e}function k(e){if(!Number.isFinite(e)||e<=0)throw new t(`INVALID_ARGS`,`scroll pixels must be a positive integer`);return Math.max(1,Math.round(e))}function A(e,t,n){let r=Math.round(t);if(!Number.isFinite(r))return 0;let i=Math.max(r,Math.round(n-t));return!Number.isFinite(i)||!Number.isFinite(e)?r:Math.min(i,Math.max(r,Math.round(e)))}const j=Object.freeze({reason:`scroll_keyboard_occludes_surface`,hint:"The on-screen keyboard covers the surface this scroll would swipe, so it cannot reach it. End editing with a visible dismissal control, or use `keyboard dismiss` on Android. You can also scroll before focusing the field."});function M(e,t){if(!P(e)||!P(t))return{kind:`unobstructed`};let n=e.x+e.width/2;if(n<t.x||n>=t.x+t.width)return{kind:`unobstructed`};let r=t.y;if(r>=e.y+e.height||t.y+t.height<=e.y)return{kind:`unobstructed`};let i=Math.max(0,r-12-e.y);return i<.15*e.height?{kind:`occluded`,keyboardMinY:r,visibleHeight:i}:{kind:`avoided`,viewport:{...e,height:i},keyboardMinY:r}}function N(e,n){return new t(`COMMAND_FAILED`,`scroll ${e} refused: the keyboard leaves ${n.visibleHeight}px of ${n.viewportHeight}px visible, below the 15% needed for a swipe`,{keyboardMinY:n.keyboardMinY,visibleHeight:n.visibleHeight,viewportHeight:n.viewportHeight,minVisibleFraction:.15,...j})}function P(e){return[e.x,e.y,e.width,e.height].every(e=>Number.isFinite(e))&&e.width>0&&e.height>0}export{T as _,c as a,u as c,p as d,y as f,s as g,S as h,j as i,v as l,b as m,i as n,l as o,M as p,r,a as s,f as t,x as u,N as v};