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 • 7.62 kB
import{v as e}from"./sdk-contracts.js";import{f as t,g as n,m as r}from"./scroll-gesture.js";import{t as i}from"./gesture-plan-types.js";const a={pinchAxisDegrees:-90,samplingProfile:`default`},o={ios:a,macos:a,android:{pinchAxisDegrees:0,samplingProfile:`android`},harmonyos:a,vega:a,linux:a,web:a};function s(e,t,n){let r=w(t),i=L(n);switch(e.intent){case`fling`:return d(e,r);case`pan`:return f(e,r,i);case`pinch`:return m({intent:`pinch`,origin:e.origin??N(r),delta:{x:0,y:0},scale:j(e.scale,`gesture pinch scale`),rotationDegrees:0,durationMs:300},r,i);case`rotate`:{let t=A(e.degrees,`gesture rotate degrees`);return m({intent:`rotate`,origin:e.origin??N(r),delta:{x:0,y:0},scale:1,rotationDegrees:t,durationMs:O(t)},r,i)}case`transform`:{let t=A(e.degrees,`gesture transform degrees`);return m({intent:`transform`,origin:e.origin,delta:e.delta,scale:j(e.scale,`gesture transform scale`),rotationDegrees:t,durationMs:T(e.durationMs,O(t),`gesture transform durationMs`)},r,i)}case`tap`:return v(e,r)}}function c(e){let[{samples:t}]=e.pointers,n=t[0],r=t.at(-1);return{start:n.point,end:r.point}}function l(t,n){let r=w(n),a=E(t.sourceHoldMs,800,`gesture drag sourceHoldMs`),o=T(t.moveMs,500,`gesture drag moveMs`),s=D(t.destinationHoldMs,0,`gesture drag destinationHoldMs`);if(a+o+s>1e4)throw new e(`INVALID_ARGS`,`gesture drag total duration must be at most ${i}`);return u(p(`pan`,t.from,t.to,o,r,`timed-pan`),a,s)}function u(e,t,n){let r=e.pointers[0],[i,a]=r.samples,o=t+e.durationMs+n,s=[{offsetMs:0,point:i.point},{...i,offsetMs:i.offsetMs+t},{...a,offsetMs:a.offsetMs+t},...n>0?[{offsetMs:o,point:a.point}]:[]];return{...e,durationMs:o,pointers:[{...r,samples:s}]}}function d(e,t){if(`preset`in e){let{from:r,to:i}=I(e.preset,t),a=p(`fling`,r,i,100,t,`endpoint-hold`);return n(e.preset)?{...a,contactOrigin:`screen-edge`}:a}if(`from`in e)return p(`fling`,e.from,e.to,100,t,`endpoint-hold`);let i=k(e.origin,`gesture fling origin`),a=M(e.distance??180,`gesture fling distance`);return p(`fling`,i,P(i,r(e.direction,a)),100,t,`endpoint-hold`)}function f(t,n,r){let i=T(t.durationMs,500,`gesture pan durationMs`);if((t.pointerCount??1)===1){let e=k(t.origin,`gesture pan origin`);return p(`pan`,e,P(e,k(t.delta,`gesture pan delta`)),i,n,t.executionProfile??`timed-pan`)}if(t.pointerCount!==2)throw new e(`INVALID_ARGS`,`gesture pan pointerCount must be 1 or 2`);return m({intent:`pan`,origin:t.origin,delta:t.delta,scale:1,rotationDegrees:0,durationMs:i},n,r)}function p(e,t,n,r,i,a){let o=k(t,`gesture ${e} start`),s=k(n,`gesture ${e} end`),c=[{offsetMs:0,point:o},{offsetMs:r,point:s}];return S(c,i,{intent:e,pointerId:0}),{topology:`single`,intent:e,executionProfile:a,durationMs:r,viewport:i,pointers:[{pointerId:0,samples:c}]}}function m(e,t,n){let r=k(e.origin,`gesture ${e.intent} origin`),i=P(r,k(e.delta,`gesture ${e.intent} delta`)),a=Math.min(t.width,t.height)*_(e.intent),o=e.scale>1?a/e.scale:a,s=o*e.scale;if(Math.min(o,s)<48)throw C({intent:e.intent,viewport:t,initialSpan:o,finalSpan:s,minimumReliableSpan:48});let c=o/2,l=b(e.durationMs,n.samplingProfile),u=(a,o)=>{let s=l.map(t=>({offsetMs:t,point:h({...e,start:r,end:i,initialRadius:c,offsetMs:t,side:o,profile:n})}));return S(s,t,{intent:e.intent,pointerId:a}),{pointerId:a,samples:s}};return{topology:`multi`,intent:e.intent,durationMs:e.durationMs,viewport:t,pointers:[u(0,1),u(1,-1)]}}function h(e){let t=e.offsetMs/e.durationMs,n=F(e.start,e.end,t),r=e.initialRadius*(1+(e.scale-1)*t),i=R(g(e.intent,e.profile)+e.rotationDegrees*t);return{x:n.x+Math.cos(i)*r*e.side,y:n.y+Math.sin(i)*r*e.side}}function g(e,t){return e===`pinch`?t.pinchAxisDegrees:-90}function _(e){return e===`pinch`?.4:.25}function v(e,t){let n=T(e.holdMs,50,`gesture tap holdMs`);return{topology:`multi`,intent:`tap`,durationMs:n,viewport:t,pointers:y(e.points).map((e,r)=>{let i=[{offsetMs:0,point:e},{offsetMs:n,point:e}];return S(i,t,{intent:`tap`,pointerId:r}),{pointerId:r,samples:i}})}}function y(t){if(t.length<2||t.length>5)throw new e(`INVALID_ARGS`,`gesture tap requires 2 to 5 points`,{pointCount:t.length,hint:`Use press for a single contact.`});let n=t.map((e,t)=>k(e,`gesture tap point ${t+1}`));return n.forEach((t,r)=>{if(n.findIndex(e=>e.x===t.x&&e.y===t.y)!==r)throw new e(`INVALID_ARGS`,`gesture tap points must be distinct`,{reason:`GESTURE_TAP_DUPLICATE_POINT`,point:t,hint:`Give each contact its own coordinates.`})}),n}function b(t,n){if(!Number.isFinite(t)||t<=0)throw new e(`INVALID_ARGS`,`gesture sample duration must be a positive finite number`);let r=t/16,i=Math.max(3,n===`android`?Math.round(r):Math.floor(r));return Array.from({length:i+1},(e,n)=>Math.round(t*n/i))}function x(t,n,r=e=>e){let i=t.at(-1)?.offsetMs??0;if(t.length<2||!Number.isFinite(i)||i<=0)throw new e(`INVALID_ARGS`,`gesture path needs at least two timed samples`,{reason:`GESTURE_PATH_TOO_SHORT`});if(t[0].offsetMs!==0||t.some(({offsetMs:e},n)=>!Number.isFinite(e)||n>0&&e<=t[n-1].offsetMs))throw new e(`INVALID_ARGS`,`gesture path needs a strictly increasing timeline from zero`,{reason:`GESTURE_PATH_INVALID_TIMELINE`});let a=[...new Set([...b(i,n),...t.map(({offsetMs:e})=>e)])].sort((e,t)=>e-t),o=0;return a.map(e=>{for(;o<t.length-2&&e>t[o+1].offsetMs;)o+=1;let n=t[o],i=t[o+1],a=i.offsetMs-n.offsetMs,s=a===0?0:(e-n.offsetMs)/a;return{offsetMs:e,point:e===i.offsetMs||a===0?i.point:s===0?n.point:F(n.point,i.point,r(s,a))}})}function S(t,n,r){let i=n.x+n.width-1,a=n.y+n.height-1;for(let o of t){let{x:t,y:s}=o.point;if(!Number.isFinite(t)||!Number.isFinite(s))throw new e(`INVALID_ARGS`,`Gesture motion produces non-finite pointer coordinates`,{...r,reason:`GESTURE_TRAJECTORY_NON_FINITE`,hint:`Reduce the requested rotation, translation, or scale.`});if(t<n.x+1||t>i||s<n.y+1||s>a)throw C({...r,viewport:n,sample:o})}}function C(t){return new e(`INVALID_ARGS`,`Gesture trajectory does not fit inside the viewport`,{...t,reason:`GESTURE_TRAJECTORY_OUT_OF_BOUNDS`,hint:`Move the gesture away from the edge or reduce its translation, scale, or rotation.`})}function w(t){let n={x:A(t.x,`gesture viewport x`),y:A(t.y,`gesture viewport y`),width:A(t.width,`gesture viewport width`),height:A(t.height,`gesture viewport height`)};if(n.width<=0||n.height<=0)throw new e(`INVALID_ARGS`,`gesture viewport width and height must be positive`);return n}function T(t,n,r){let a=t??n;if(!Number.isInteger(a)||a<16||a>1e4)throw new e(`INVALID_ARGS`,`${r} must be an integer between 16 and ${i}`);return a}function E(t,n,r){let a=t??n;if(!Number.isInteger(a)||a<1||a>1e4)throw new e(`INVALID_ARGS`,`${r} must be an integer between 1 and ${i}`);return a}function D(t,n,r){let a=t??n;if(!Number.isInteger(a)||a<0||a>1e4)throw new e(`INVALID_ARGS`,`${r} must be an integer between 0 and ${i}`);return a}function O(e){let t=Math.ceil(Math.abs(e)/3)*16;return Math.min(Math.max(300,t),2400)}function k(e,t){return{x:A(e.x,`${t} x`),y:A(e.y,`${t} y`)}}function A(t,n){if(!Number.isFinite(t))throw new e(`INVALID_ARGS`,`${n} must be a finite number`);return t}function j(t,n){let r=A(t,n);if(r<=0)throw new e(`INVALID_ARGS`,`${n} must be greater than 0`);return r}function M(t,n){let r=A(t,n);if(r<=0)throw new e(`INVALID_ARGS`,`${n} must be a positive number`);return r}function N(e){return{x:e.x+e.width/2,y:e.y+e.height/2}}function P(e,t){return{x:e.x+t.x,y:e.y+t.y}}function F(e,t,n){return{x:e.x+(t.x-e.x)*n,y:e.y+(t.y-e.y)*n}}function I(e,n){let r=t(e,{referenceWidth:n.width,referenceHeight:n.height});return{from:{x:n.x+r.x1,y:n.y+r.y1},to:{x:n.x+r.x2,y:n.y+r.y2}}}function L(e){return e===void 0?a:o[e]}function R(e){return e*Math.PI/180}export{c as i,s as n,x as r,l as t};