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.58 kB
JavaScript
import e from"node:path";const t=`/agent-device`,n=`x-agent-device-tenant`,r=`x-agent-device-network-access`,i=`public-only`;function a(e){if(!/^https?:\/\/[^\s/?#]+\/?$/i.test(e)||c(e))throw Error(`Invalid publicOrigin`);let t=e.match(/^https?:\/\/([^/]+)/i)?.[1]??``;if(t.includes(`@`)||t.endsWith(`:`))throw Error(`Invalid publicOrigin`);let n=new URL(e);if(!n.hostname||n.username||n.password||n.pathname!==`/`||n.search||n.hash)throw Error(`Invalid publicOrigin`);return n.origin}function o(e,t,n){if(!e||!e.startsWith(`/`)||e.startsWith(`//`)||e.includes(`#`)||c(e))return null;let r=s(t,n);return r===null?null:new URL(e,r)}function s(e,t){let n=[];for(let t=0;t+1<e.length;t+=2)e[t]?.toLowerCase()===`host`&&n.push(e[t+1]??``);if(n.length!==1||/[/?#]/.test(n[0]??``))return null;try{return a(`${t?`https`:`http`}://${n[0]}`)}catch{return null}}function c(e){for(let t of e){let e=t.charCodeAt(0);if(e<=32||e===127||t===`\\`)return!0}return!1}function l(e){return u(e,t)}function u(e,t){let n=e.endsWith(`/`)?e:`${e}/`;return new URL(t.replace(/^\/+/,``),n).toString()}function d(e){let t=e?.trim();return t?{authorization:`Bearer ${t}`,"x-agent-device-token":t}:{}}function f(e){if(!e)return;let t=e.trim();if(t&&/^[a-zA-Z0-9._-]{1,128}$/.test(t))return t}function p(e,t){return t.startsWith(`${e}:`)}function m(e){let t=e?.trim();return t?{[n]:t}:{}}function h(e){let t=e?.trim();return t?{"x-agent-device-lease-id":t}:{}}const g=67108864,_=`x-agent-device-instance`,v=`x-agent-device-upstream-instance`,y=`x-agent-device-instance-mismatch`;function b(e,t,n){return{jsonrpc:`2.0`,id:e,error:{code:-32001,message:t,data:n}}}function x(e,t,n={}){return{ok:!0,service:e,version:t,rpcProtocolVersion:3,...n.instanceId===void 0?{}:{instanceId:n.instanceId},...n.hostArch===void 0?{}:{hostArch:n.hostArch},...n.capabilities===void 0?{}:{capabilities:n.capabilities},...n.leaseBackends===void 0?{}:{leaseBackends:n.leaseBackends},...n.upstream===void 0?{}:{upstream:n.upstream}}}const S=`/tmp`;function C(t,n){return e.posix.join(S,`${D(t)}${E(n)}`)}function w(t){return e.posix.join(S,D(t))}function T(t,n,r){let i=E(r),a=e.posix.basename(t,i);return t===e.posix.join(S,`${a}${i}`)&&a.startsWith(`agent-device-${n}-`)&&/^\d+-[a-z0-9]+$/.test(a.slice(`agent-device-${n}-`.length))}function E(e){return e.startsWith(`.`)?e:`.${e}`}function D(e){return`agent-device-${e}-${Date.now()}-${Math.random().toString(36).slice(2,8)}`}export{f as S,C as _,i as a,o as b,g as c,l as d,h as f,w as g,b as h,r as i,x as l,u as m,_ as n,n as o,m as p,y as r,v as s,t,d as u,T as v,a as x,p as y};