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 • 1.51 kB
JavaScript
import{xt as e}from"./sdk-io.js";import"./diagnostics.js";import{t}from"./catalog.js";import{a as n,c as r}from"./transport.js";import i from"node:net";async function a(e,t={}){let n=Date.now()+1e3,r=[];return e.httpPort&&r.push(s(e.httpPort,n)),e.port&&r.push(c(e.port,e.token,t.session,n)),r.length!==0&&await new Promise(e=>{let t=r.length,n=n=>{if(n){for(let e of r)e.cancel();e(!0);return}--t===0&&e(!1)};for(let e of r)e.answer.then(n,()=>n(!1))})}function o(e){let t=!1,n,r=()=>{},i=new Promise(e=>{r=e}),a=setTimeout(()=>o(!1),Math.max(1,e-Date.now()));a.unref?.();function o(e){t||(t=!0,clearTimeout(a),n?.(),r(e))}return{leg:{answer:i,cancel:()=>o(!1)},settle:o,attach:e=>{n=e,t&&e()}}}function s(e,t){let n=o(t);return(async()=>{try{let t=(await r(`http:`)).request({host:`127.0.0.1`,port:String(e),path:`/health`,method:`GET`,agent:!1},e=>{e.resume(),n.settle(!0)});t.on(`error`,()=>n.settle(!1)),n.attach(()=>t.destroy()),t.end()}catch{n.settle(!1)}})(),n.leg}function c(e,t,r,a){let s=o(a),c;try{c=i.createConnection({host:`127.0.0.1`,port:e})}catch{return s.settle(!1),s.leg}s.attach(()=>c.destroy());let u=l(t,r),d=``;return c.setEncoding(`utf8`),c.on(`connect`,()=>{c.write(`${JSON.stringify(u)}\n`)}),c.on(`data`,e=>{let t=n(d,e);d=t.buffer,t.lines.length>0&&s.settle(!0)}),c.on(`error`,()=>s.settle(!1)),c.on(`close`,()=>s.settle(!1)),s.leg}function l(n,r){return{token:n,session:r||`default`,command:t.sessionList,positionals:[],flags:{},meta:{requestId:e()}}}export{a as probeDaemonResponsive};