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 • 3.02 kB
import{B as e,v as t}from"../sdk-contracts.js";import{A as n}from"../sdk-batch-runner.js";import{ct as r}from"../sdk-io.js";import"../command.js";import{n as i,t as a}from"../run-script-http.js";import{r as o,t as s}from"../run-script-execution.js";import c,{readFileSync as l}from"node:fs";import u from"node:vm";const d=1e3,f=a*6+1048576;function p(n){try{return{outputEnv:m(JSON.parse(n))}}catch(n){if(n instanceof t)return{error:{code:n.code,message:n.message,details:n.details}};let r=e(n);return{error:{code:r.code,message:r.message}}}}function m(r){let{scriptPath:i,env:a,publicNetworkOnly:o}=r,l=c.readFileSync(i,`utf8`),d=Object.create(null);try{u.runInNewContext(l,h(a,d,o),{filename:i,timeout:s})}catch(r){let a=e(r);throw new t(a.code,`Maestro runScript failed: ${a.message}`,n({...a.details??{},hint:a.hint,diagnosticId:a.diagnosticId,logPath:a.logPath,retriable:a.retriable,supportedOn:a.supportedOn,scriptPath:i}),r instanceof Error?r:void 0)}return S(d,i),Object.fromEntries(Object.entries(d).map(([e,t])=>[`output.${e}`,C(t)]))}function h(e,t,n){return{...e,output:t,json:g,http:{post:(e,t)=>v(`POST`,e,n,t)}}}function g(e){if(typeof e!=`string`)throw new t(`COMMAND_FAILED`,`Maestro runScript json() expected a string body, received ${typeof e}.`);if(e.trim().length===0)throw new t(`COMMAND_FAILED`,`Maestro runScript json() received an empty body.`,{hint:`Check the preceding HTTP response status and setup server output.`});try{return JSON.parse(e,_)}catch(n){throw new t(`COMMAND_FAILED`,`Maestro runScript json() could not parse response body: ${n instanceof Error?n.message:String(n)}`,{bodyPreview:e.slice(0,d)},n instanceof Error?n:void 0)}}function _(e,t){return e===`__proto__`||e===`constructor`||e===`prototype`?void 0:t}function v(e,n,a,o){let s=r(process.execPath,b(),{stdin:JSON.stringify(x(e,n,o,a)),timeoutMs:i+5e3,allowFailure:!0,maxBuffer:f}),c=y(s.stdout);if(c&&`response`in c)return c.response;throw c?new t(c.error.code,c.error.message,{...c.error.details,url:n}):s.exitCode===0?new t(`COMMAND_FAILED`,`Maestro runScript http.${e.toLowerCase()} returned invalid JSON for ${n}`,{stdout:s.stdout.slice(0,d),stderr:s.stderr.slice(0,d)}):new t(`COMMAND_FAILED`,`Maestro runScript http.${e.toLowerCase()} failed for ${n}: ${w(s.stderr)}`,{exitCode:s.exitCode,stderr:s.stderr})}function y(e){try{let t=JSON.parse(e);return t&&typeof t==`object`&&(`response`in t||`error`in t)?t:void 0}catch{return}}function b(){return o(`run-script-http-child`)}function x(e,t,n,r){return{method:e,url:t,headers:n?.headers??{},body:n?.body??``,publicNetworkOnly:r}}function S(e,n){for(let r of Object.keys(e))if(r.includes(`.`))throw new t(`INVALID_ARGS`,`Maestro runScript output key cannot contain ".": ${r}`,{scriptPath:n,key:r})}function C(e){return typeof e==`string`?e:typeof e==`number`||typeof e==`boolean`?String(e):JSON.stringify(e)}function w(e){let t=e.trim();return t.length>0?t.slice(0,d):`request process exited without stderr`}process.stdout.write(JSON.stringify(p(l(0,`utf8`))));export{};