@@ -33,7 +33,7 @@ import {
3333 CloudAuthWindowManager ,
3434 readNavigationEventUrl ,
3535} from "./cloud-auth-window" ;
36- import { ensureDesktopApiToken , getAgentManager } from "./native/agent" ;
36+ import { configureDesktopLocalApiAuth , getAgentManager } from "./native/agent" ;
3737import { getDesktopManager } from "./native/desktop" ;
3838import { disposeNativeModules , initializeNativeModules } from "./native/index" ;
3939import {
@@ -124,7 +124,8 @@ function buildApiRequestHeaders(contentType?: string): Record<string, string> {
124124 if ( contentType ) {
125125 headers [ "Content-Type" ] = contentType ;
126126 }
127- const apiToken = process . env . MILADY_API_TOKEN ?. trim ( ) ;
127+ const apiToken =
128+ process . env . MILADY_API_TOKEN ?. trim ( ) ?? process . env . ELIZA_API_TOKEN ?. trim ( ) ;
128129 if ( apiToken ) {
129130 headers . Authorization = `Bearer ${ apiToken } ` ;
130131 }
@@ -415,13 +416,20 @@ async function startRendererServer(): Promise<string> {
415416 const initialApiBase = resolveInitialApiBase (
416417 process . env as Record < string , string | undefined > ,
417418 ) ;
419+ const initialApiToken =
420+ resolveDesktopRuntimeMode ( process . env as Record < string , string | undefined > )
421+ . mode === "local"
422+ ? configureDesktopLocalApiAuth ( )
423+ : ( process . env . MILADY_API_TOKEN ?. trim ( ) ??
424+ process . env . ELIZA_API_TOKEN ?. trim ( ) ??
425+ "" ) ;
418426
419427 // Inject the API base into index.html so it's available before React mounts.
420428 function injectApiBaseIntoHtml ( html : string ) : string {
421429 if ( ! initialApiBase ) {
422430 return html ;
423431 }
424- const script = `<script>window.__MILADY_API_BASE__=${ JSON . stringify ( initialApiBase ) } ;</script>` ;
432+ const script = `<script>window.__MILADY_API_BASE__=${ JSON . stringify ( initialApiBase ) } ;${ initialApiToken ? `Object.defineProperty(window,"__MILADY_API_TOKEN__",{value: ${ JSON . stringify ( initialApiToken ) } ,configurable:true,writable:true,enumerable:false});` : "" } </script>` ;
425433 // Inject before </head> if present, otherwise before <body>
426434 if ( html . includes ( "</head>" ) ) {
427435 return html . replace ( "</head>" , `${ script } </head>` ) ;
@@ -924,7 +932,7 @@ function injectApiBase(win: BrowserWindow): void {
924932 const agent = getAgentManager ( ) ;
925933 const port =
926934 agent . getPort ( ) ?? ( Number ( process . env . MILADY_PORT ) || DEFAULT_PORT ) ;
927- const apiToken = ensureDesktopApiToken ( ) ;
935+ const apiToken = configureDesktopLocalApiAuth ( ) ;
928936 pushApiBaseToRenderer ( win , `http://127.0.0.1:${ port } ` , apiToken ) ;
929937 setAgentReady ( true ) ;
930938}
@@ -967,7 +975,7 @@ async function _startAgent(win: BrowserWindow): Promise<void> {
967975 }
968976
969977 const agent = getAgentManager ( ) ;
970- const apiToken = ensureDesktopApiToken ( ) ;
978+ const apiToken = configureDesktopLocalApiAuth ( ) ;
971979
972980 try {
973981 const status = await agent . start ( ) ;
0 commit comments