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 • 2.65 kB
JavaScript
import{pt as e,rt as t,x as n,z as r}from"./sdk-io.js";import i from"node:path";import{readlink as a}from"node:fs/promises";import{clearTimeout as o,setTimeout as s}from"node:timers";const c=4096;async function l(i,a,o=1e3){let s=r(i).filter(e=>e<=2147483647);if(s.length===0)return new Map;let c=n();return c===`linux`?await d(s,o):c!==`darwin`&&c!==`win32`?new Map:await e(async()=>{try{let e=c===`win32`,n=e?u(a.env):`/usr/bin/osascript`;if(!n)return new Map;let r=await t(n,e?p(s):f(s),{allowFailure:!0,timeoutMs:e?Math.max(o,2e3):o});return r.exitCode===0?m(r.stdout,s,e):new Map}catch{return new Map}})}function u(e){let t=Object.keys(e).find(e=>e.toUpperCase()===`SYSTEMROOT`),n=t===void 0?void 0:e[t];if(!(!n||!i.win32.isAbsolute(n)||i.win32.parse(n).root.length<3))return i.win32.join(n,`System32`,`WindowsPowerShell`,`v1.0`,`powershell.exe`)}async function d(e,t){let n=Promise.all(e.map(async e=>{try{let t=await a(`/proc/${e}/exe`,{encoding:`buffer`});if(t.length>=c)return;let n=new TextDecoder(`utf-8`,{fatal:!0}).decode(t);return h(n,!1)&&!n.endsWith(` (deleted)`)?[e,n]:void 0}catch{return}})),r=()=>{};try{let e=await Promise.race([n,new Promise(e=>{let n=s(e,t);r=()=>o(n)})]);return new Map(e?.filter(e=>e!==void 0)??[])}finally{r()}}function f(e){return[`-l`,`JavaScript`,`-e`,`ObjC.import("Foundation"); ObjC.bindFunction("proc_pidpath", ["int", ["int", "void *", "uint32"]]); JSON.stringify(${JSON.stringify(e)}.map(function(pid) { var b=$.NSMutableData.dataWithLength(${c}); var n=$.proc_pidpath(pid,b.mutableBytes,${c}); if(n<=0 || n>=${c}) return {pid:pid}; var d=$.NSData.dataWithBytesLength(b.bytes,n); try { var s=$.NSString.alloc.initWithDataEncoding(d,$.NSUTF8StringEncoding); return {pid:pid,path:ObjC.unwrap(s)}; } catch (_) { return {pid:pid}; } }));`]}function p(e){return[`-NoProfile`,`-NonInteractive`,`-Command`,`$OutputEncoding=[Console]::OutputEncoding=[System.Text.UTF8Encoding]::new(); ConvertTo-Json -Compress -InputObject @(Get-CimInstance Win32_Process -Filter '${e.map(e=>`ProcessId=${e}`).join(` OR `)}' | ForEach-Object { @{pid=$_.ProcessId; path=$_.ExecutablePath} })`]}function m(e,t,n){let r=new Map;try{let i=JSON.parse(e.replace(/^\uFEFF/,``));if(!Array.isArray(i))return r;let a=new Set(t),o=new Set;for(let e of i){if(!e||typeof e!=`object`)continue;let{pid:t,path:i}=e;if(!(typeof t!=`number`||!a.has(t))){if(o.has(t)){r.delete(t);continue}o.add(t),typeof i==`string`&&h(i,n)&&r.set(t,i)}}}catch{return new Map}return r}function h(e,t){return e.length>0&&Buffer.byteLength(e,`utf8`)<c&&!/[\0\uFFFD]/.test(e)&&(t?i.win32.isAbsolute(e):i.posix.isAbsolute(e))}export{l as readProcessExecutablePaths};