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.
2 lines • 4.32 kB
JavaScript
import{k as e,v as t}from"./sdk-contracts.js";import{q as n}from"./sdk-io.js";import{t as r}from"./scroll-gesture.js";import"./retry.js";import{r as i}from"./tool-provider2.js";async function a(e=i()){let n=e.display.detectDisplayServer();if(n===`wayland`){if(await e.whichCommand(`ydotool`))return{tool:`ydotool`,display:n};throw new t(`TOOL_MISSING`,`ydotool is required for input synthesis on Wayland (xdotool does not work on Wayland). Install it via your package manager.`)}if(await e.whichCommand(`xdotool`))return{tool:`xdotool`,display:n};throw new t(`TOOL_MISSING`,`xdotool is required for input synthesis on X11. Install it via your package manager.`)}const o=1e4;async function s(...e){await i().runCommand(`xdotool`,e,{allowFailure:!1,timeoutMs:o})}async function c(...e){await i().runCommand(`ydotool`,e,{allowFailure:!1,timeoutMs:o})}function l(){return i().input}async function u(e,t){let{tool:n}=await a();if(n===`xdotool`){if(await d(e,t))return;await s(`mousemove`,`--sync`,String(e),String(t))}else await c(`mousemove`,`--absolute`,`-x`,String(e),`-y`,String(t))}async function d(e,t){try{let n=await i().runCommand(`xdotool`,[`getmouselocation`,`--shell`],{allowFailure:!0,timeoutMs:o}),r=new Map(n.stdout.split(`
`).map(e=>e.trim().split(`=`)).filter(e=>e.length===2).map(([e,t])=>[e,Number(t)]));return r.get(`X`)===Math.round(e)&&r.get(`Y`)===Math.round(t)}catch{return!1}}async function f(e,t){let n=l();if(n){await n.key(e,t);return}let{tool:r}=await a();r===`xdotool`?await s(`key`,`--clearmodifiers`,e):await c(`key`,...t)}async function p(e,t,n,r){await u(e,t);let{tool:i}=await a();i===`xdotool`?await s(`click`,n):await c(`click`,r)}async function m(e,t,n,r,i){let a=l();if(a){await a.click(e,t,n);return}await p(e,t,r,i)}async function h(e,t){await m(e,t,`primary`,`1`,`0xC0`)}async function g(e,t){await m(e,t,`secondary`,`3`,`0xC1`)}async function _(e,t){await m(e,t,`middle`,`2`,`0xC2`)}async function v(e,t){let n=l();if(n){await n.doubleClick(e,t);return}let{tool:r}=await a();await u(e,t),r===`xdotool`?await s(`click`,`--repeat`,`2`,`1`):(await c(`click`,`0xC0`),await c(`click`,`0xC0`))}async function y(e,t,r=800){let i=l();if(i){await i.longPress(e,t,r);return}let{tool:o}=await a();await u(e,t),o===`xdotool`?(await s(`mousedown`,`1`),await n(r),await s(`mouseup`,`1`)):(await c(`click`,`--down`,`0xC0`),await n(r),await c(`click`,`--up`,`0xC0`))}async function b(e,t){await h(e,t)}async function x(e,t,r,i,o=300){let d=l();if(d){await d.drag(e,t,r,i,o);return}let{tool:f}=await a();await u(e,t),f===`xdotool`?(await s(`mousedown`,`1`),await s(`mousemove`,`--sync`,String(r),String(i)),await n(o),await s(`mouseup`,`1`)):(await c(`click`,`--down`,`0xC0`),await c(`mousemove`,`--absolute`,`-x`,String(r),`-y`,String(i)),await n(o),await c(`click`,`--up`,`0xC0`))}async function S(e,t){let n=l();if(n)return await n.scroll(e,t),w(t);let{tool:i}=await a(),o=5;if(t?.pixels==null?t?.amount!=null&&(o=Math.max(1,Math.round(5*(t.amount/r)))):o=i===`xdotool`?Math.max(1,Math.round(t.pixels/15)):Math.max(1,Math.round(t.pixels/40)),i===`xdotool`){let n=e===`up`?`4`:e===`down`?`5`:e===`left`?`6`:`7`;await C(o,t?.durationMs,async e=>{await s(`click`,`--repeat`,String(e),n)})}else e===`up`||e===`down`?await C(o,t?.durationMs,async t=>{await c(`mousemove`,`--wheel`,`-y`,String(e===`up`?-t:t))}):await C(o,t?.durationMs,async t=>{await c(`mousemove`,`--wheel`,`-x`,String(e===`left`?-t:t))});return w(t)}async function C(t,r,i){if(r===void 0||r<=0||t<=1){await i(t);return}let a=r/Math.max(1,t-1),o=0;try{for(let e=0;e<t;e+=1)await i(1),o+=1,e<t-1&&await n(a)}catch(t){throw e(t,o)}}function w(e){return e?.durationMs===void 0?{}:{durationMs:e.durationMs}}async function T(e,t=0){let n=l();if(n){await n.typeText(e,{delayMs:t});return}let{tool:r}=await a();if(r===`xdotool`){let n=[`type`];t>0&&n.push(`--delay`,String(t)),n.push(`--clearmodifiers`,`--`,e),await s(...n)}else await c(`type`,`--`,e)}async function E(e,t,r,i=0){if(await h(e,t),await n(100),await f(`ctrl+a`,[`29:1`,`30:1`,`30:0`,`29:0`]),await n(50),r.length===0){await f(`BackSpace`,[`14:1`,`14:0`]);return}await T(r,i)}export{v as doubleClickLinux,E as fillLinux,b as focusLinux,y as longPressLinux,_ as middleClickLinux,h as pressLinux,g as rightClickLinux,S as scrollLinux,f as sendKey,x as swipeLinux,T as typeLinux};