@@ -18,8 +18,9 @@ import * as path from "node:path";
1818import { fileURLToPath } from "node:url" ;
1919import { loadLocalEnv } from "./shared/load-env" ;
2020
21- type Logger = typeof import ( "../../lib/utils/logger" ) . logger ;
22- type Repo = typeof import ( "../../db/repositories/agent-sandboxes" ) . agentSandboxesRepository ;
21+ type Logger = typeof import ( "@elizaos/cloud-shared/lib/utils/logger" ) . logger ;
22+ type Repo =
23+ typeof import ( "@elizaos/cloud-shared/db/repositories/agent-sandboxes" ) . agentSandboxesRepository ;
2324
2425interface RouterDeps {
2526 logger : Logger ;
@@ -40,7 +41,9 @@ function parsePositiveInt(value: string | undefined, fallback: number): number {
4041 return Number . isFinite ( parsed ) && parsed > 0 ? parsed : fallback ;
4142}
4243
43- export function readRouterConfig ( env : NodeJS . ProcessEnv = process . env ) : AgentRouterConfig {
44+ export function readRouterConfig (
45+ env : NodeJS . ProcessEnv = process . env ,
46+ ) : AgentRouterConfig {
4447 return {
4548 port : parsePositiveInt ( env . AGENT_ROUTER_PORT , DEFAULT_PORT ) ,
4649 bindHost : env . AGENT_ROUTER_BIND_HOST ?. trim ( ) || DEFAULT_BIND_HOST ,
@@ -52,8 +55,8 @@ let depsPromise: Promise<RouterDeps> | null = null;
5255async function loadDeps ( ) : Promise < RouterDeps > {
5356 if ( ! depsPromise ) {
5457 depsPromise = Promise . all ( [
55- import ( "../.. /db/repositories/agent-sandboxes" ) ,
56- import ( "../.. /lib/utils/logger" ) ,
58+ import ( "@elizaos/cloud-shared /db/repositories/agent-sandboxes" ) ,
59+ import ( "@elizaos/cloud-shared /lib/utils/logger" ) ,
5760 ] ) . then ( ( [ repoModule , loggerModule ] ) => ( {
5861 agentSandboxesRepository : repoModule . agentSandboxesRepository ,
5962 logger : loggerModule . logger ,
@@ -69,7 +72,9 @@ interface RoutingResponse {
6972 target : string ;
7073}
7174
72- export async function resolveAgentRouting ( agentId : string ) : Promise < RoutingResponse | null > {
75+ export async function resolveAgentRouting (
76+ agentId : string ,
77+ ) : Promise < RoutingResponse | null > {
7378 const { agentSandboxesRepository } = await loadDeps ( ) ;
7479 const sandbox = await agentSandboxesRepository . findById ( agentId ) ;
7580 if ( ! sandbox || sandbox . status !== "running" ) return null ;
@@ -95,15 +100,18 @@ export async function resolveAgentRouting(agentId: string): Promise<RoutingRespo
95100 } ;
96101}
97102
98- const AGENT_ID_RE = / ^ [ a - f 0 - 9 ] { 8 } - [ a - f 0 - 9 ] { 4 } - [ a - f 0 - 9 ] { 4 } - [ a - f 0 - 9 ] { 4 } - [ a - f 0 - 9 ] { 12 } $ / i;
103+ const AGENT_ID_RE =
104+ / ^ [ a - f 0 - 9 ] { 8 } - [ a - f 0 - 9 ] { 4 } - [ a - f 0 - 9 ] { 4 } - [ a - f 0 - 9 ] { 4 } - [ a - f 0 - 9 ] { 12 } $ / i;
99105
100106async function handleRequest ( url : URL ) : Promise < Response > {
101107 if ( url . pathname === "/healthz" ) {
102108 return Response . json ( { ok : true } , { status : 200 } ) ;
103109 }
104110 // /headscale-ip is the path nginx Lua already calls; /routing is the alias
105111 // for new callers.
106- const match = url . pathname . match ( / ^ \/ a g e n t s \/ ( [ ^ / ] + ) \/ ( h e a d s c a l e - i p | r o u t i n g ) $ / ) ;
112+ const match = url . pathname . match (
113+ / ^ \/ a g e n t s \/ ( [ ^ / ] + ) \/ ( h e a d s c a l e - i p | r o u t i n g ) $ / ,
114+ ) ;
107115 if ( ! match ) {
108116 return Response . json ( { error : "not found" } , { status : 404 } ) ;
109117 }
@@ -113,7 +121,10 @@ async function handleRequest(url: URL): Promise<Response> {
113121 }
114122 const routing = await resolveAgentRouting ( agentId ) ;
115123 if ( ! routing ) {
116- return Response . json ( { error : "agent not found or not running" } , { status : 404 } ) ;
124+ return Response . json (
125+ { error : "agent not found or not running" } ,
126+ { status : 404 } ,
127+ ) ;
117128 }
118129 return Response . json ( routing , { status : 200 } ) ;
119130}
@@ -127,11 +138,16 @@ async function main(): Promise<void> {
127138
128139 const { createServer } = await import ( "node:http" ) ;
129140 server = createServer ( ( req , res ) => {
130- const url = new URL ( req . url ?? "/" , `http://${ req . headers . host || "localhost" } ` ) ;
141+ const url = new URL (
142+ req . url ?? "/" ,
143+ `http://${ req . headers . host || "localhost" } ` ,
144+ ) ;
131145 handleRequest ( url )
132146 . then ( ( response ) => {
133147 res . statusCode = response . status ;
134- response . headers . forEach ( ( v , k ) => res . setHeader ( k , v ) ) ;
148+ response . headers . forEach ( ( v , k ) => {
149+ res . setHeader ( k , v ) ;
150+ } ) ;
135151 return response . text ( ) ;
136152 } )
137153 . then ( ( body ) => {
@@ -206,7 +222,8 @@ process.on("unhandledRejection", (reason) => {
206222} ) ;
207223
208224function isMainModule ( ) : boolean {
209- return path . resolve ( process . argv [ 1 ] ) === fileURLToPath ( import . meta. url ) ;
225+ const entry = process . argv [ 1 ] ;
226+ return entry ? path . resolve ( entry ) === fileURLToPath ( import . meta. url ) : false ;
210227}
211228
212229if ( isMainModule ( ) ) {
0 commit comments