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 • 4.14 kB
import{t as e}from"./platform-runtime-android-mechanics.js";import{o as t,s as n}from"./device-shell.js";import{n as r}from"./video.js";import i from"node:path";const a={medium:8e6,high:2e7};async function o(o,c){let{resolveAndroidAdbExecutor:l,resolveScopedAndroidAdbBackgroundTransport:u,runAdbShell:d}=await e(),f=l(o),p=u(o),m=async(e,t)=>await d(f,e,{allowFailure:!0,timeoutMs:5e3,signal:t}),h=async(e,t)=>{let n=await m([`test`,`-e`,e],t);return n.exitCode===0?`present`:n.exitCode===1&&n.stderr.trim()===``?`absent`:`uncertain`};return Object.freeze({mode:p.mode,start:async({remotePath:e,quality:r=`medium`},i)=>{let o=(await m([t(`screenrecord --bit-rate ${a[r]} ${n(e)} >/dev/null 2>&1 & echo $!`)],i)).stdout.split(/\s+/).find(e=>/^\d+$/.test(e));if(!o)throw Error(`Android screenrecord did not return a process id`);let c=await s(m,{pid:o,remotePath:e,startTime:``},i);if(c.status!==`owned-alive`||!c.process)throw Error(`Android screenrecord did not expose a complete process identity`);return Object.freeze({process:c.process})},inspect:async(e,t)=>(await s(m,e,t)).status,stop:async(e,t,n)=>{let r=await s(m,e,n);return r.status===`missing`?`already-missing`:r.status===`foreign-writer`?`ownership-lost`:r.status===`owned-alive`?(await m([`kill`,t?.force?`-9`:`-2`,e.pid],n)).exitCode===0?`stopped`:`uncertain`:r.status},exists:async(e,t)=>{let n=await h(e,t);return n===`present`||n!==`absent`&&`uncertain`},size:async(e,t)=>{let n=await h(e,t);if(n!==`present`)return n===`absent`?void 0:`uncertain`;let r=await m([`stat`,`-c`,`%s`,e],t);if(r.exitCode!==0)return`uncertain`;let i=Number(r.stdout.trim());return Number.isSafeInteger(i)&&i>=0?i:`uncertain`},probeRunningWriters:async(e,t)=>{let n=await m([`ps`,`-A`,`-o`,`pid=`],t);if(n.exitCode!==0)return{writers:[],conclusive:!1};let r=n.stdout.split(/\s+/).filter(e=>/^\d+$/.test(e)),i=await Promise.all(r.map(async n=>await s(m,{pid:n,remotePath:e,startTime:``},t)));return{writers:i.flatMap(e=>e.status===`owned-alive`&&e.process?[e.process]:[]),conclusive:!i.some(e=>e.status===`uncertain`)}},pullPlayable:async({remotePath:e,outputPath:t},n)=>{let i=await f([`pull`,e,t],{allowFailure:!0,signal:n});return{...i,playable:i.exitCode===0&&await r(t,c)}},remove:async(e,t)=>(await m([`rm`,`-f`,e],t)).exitCode===0,manifestPathFor:e=>`${i.posix.dirname(e)}/agent-device-recording-active.json`,readManifest:async(e,t)=>{let n=await m([`test`,`-e`,e],t);if(n.exitCode!==0)return n.exitCode===1&&n.stderr.trim()===``?{status:`missing`}:{status:`unavailable`,message:n.stderr.trim()||`Android recording manifest probe failed`};let r=await m([`cat`,e],t);return r.exitCode===0?{status:`read`,contents:r.stdout}:{status:`unavailable`,message:r.stderr.trim()||`Android recording manifest could not be read`}},writeManifest:async({manifestPath:e,contents:r},i)=>{let a=`${e}.tmp`;if((await m([t(`printf %s ${n(r)} > ${n(a)} && mv -f ${n(a)} ${n(e)}`)],i)).exitCode!==0)throw Error(`failed to write Android recording manifest`)},removeManifest:async(e,t)=>(await m([`rm`,`-f`,e],t)).exitCode===0})}async function s(e,t,n){let r=await c(e,t.pid,n);if(r!==`present`)return{status:r};let i=await e([`cat`,`/proc/${t.pid}/cmdline`],n),a=await e([`cat`,`/proc/${t.pid}/stat`],n),o=a.exitCode===0?u(a.stdout):void 0;return i.exitCode!==0||!o?{status:await c(e,t.pid,n)===`missing`?`missing`:`uncertain`}:o.state===`Z`?{status:`missing`}:l(i.stdout,t.remotePath)?t.startTime.length>0&&t.startTime!==o.startTime?{status:`foreign-writer`}:{status:`owned-alive`,process:Object.freeze({pid:t.pid,remotePath:t.remotePath,startTime:o.startTime})}:{status:`ownership-lost`}}async function c(e,t,n){let r=await e([`test`,`-d`,`/proc/${t}`],n);return r.signal?`uncertain`:r.exitCode===0?`present`:r.exitCode===1&&r.stderr.trim()===``?`missing`:`uncertain`}function l(e,t){let n=e.split(`\0`).filter(e=>e.length>0),r=n[0];return r!==void 0&&i.posix.basename(r)===`screenrecord`&&n.at(-1)===t}function u(e){let t=e.lastIndexOf(`)`);if(t<0)return;let n=e.slice(t+1).trim().split(/\s+/),r=n[19];return r&&/^\d+$/.test(r)?{startTime:r,state:n[0]}:void 0}export{o as createAndroidScreenRecordingTransport};