Skip to content

Commit 6dc6db7

Browse files
chore: fix replay when self host (#1102)
Co-authored-by: Wendong-Fan <w3ndong.fan@gmail.com>
1 parent 0cab8c7 commit 6dc6db7

1 file changed

Lines changed: 95 additions & 1 deletion

File tree

electron/main/init.ts

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import log from 'electron-log';
1818
import fs from 'fs';
1919
import * as http from 'http';
2020
import * as net from 'net';
21+
import os from 'os';
2122
import path from 'path';
2223
import { promisify } from 'util';
2324
import { PromiseReturnType } from './install-deps';
@@ -35,6 +36,52 @@ import {
3536

3637
const execAsync = promisify(exec);
3738

39+
const DEFAULT_SERVER_URL = 'https://dev.eigent.ai/api';
40+
41+
function readEnvValue(filePath: string, key: string): string | undefined {
42+
try {
43+
if (!fs.existsSync(filePath)) return undefined;
44+
const content = fs.readFileSync(filePath, 'utf-8');
45+
const lines = content.split(/\r?\n/);
46+
const line = lines.find((l) => {
47+
let trimmed = l.trim();
48+
if (!trimmed || trimmed.startsWith('#')) return false;
49+
// Handle lines with 'export' prefix (e.g. export SERVER_URL=value)
50+
if (trimmed.startsWith('export ')) {
51+
trimmed = trimmed.slice(7).trim();
52+
}
53+
return trimmed.startsWith(`${key}=`);
54+
});
55+
if (!line) return undefined;
56+
let raw = line.trim();
57+
// Strip 'export ' prefix before extracting value
58+
if (raw.startsWith('export ')) {
59+
raw = raw.slice(7).trim();
60+
}
61+
let value = raw.slice(key.length + 1).trim();
62+
// Strip surrounding quotes (single or double)
63+
if (
64+
(value.startsWith('"') && value.endsWith('"')) ||
65+
(value.startsWith("'") && value.endsWith("'"))
66+
) {
67+
value = value.slice(1, -1);
68+
}
69+
return value;
70+
} catch (error) {
71+
log.warn(`Failed to read ${key} from ${filePath}:`, error);
72+
return undefined;
73+
}
74+
}
75+
76+
function buildLocalServerUrl(proxyUrl: string | undefined): string | undefined {
77+
if (!proxyUrl) return undefined;
78+
const trimmed = proxyUrl.trim().replace(/\/+$/, '');
79+
if (!trimmed) return undefined;
80+
// Avoid double /api suffix
81+
if (trimmed.endsWith('/api')) return trimmed;
82+
return `${trimmed}/api`;
83+
}
84+
3885
// helper function to get main window
3986
export function getMainWindow(): BrowserWindow | null {
4087
const windows = BrowserWindow.getAllWindows();
@@ -183,10 +230,57 @@ export async function startBackend(
183230
}
184231

185232
const uvEnv = getUvEnv(currentVersion);
233+
const globalEnvPath = path.join(os.homedir(), '.eigent', '.env');
234+
235+
const envProxyEnabled = process.env.VITE_USE_LOCAL_PROXY === 'true';
236+
const envProxyUrl = process.env.VITE_PROXY_URL;
237+
let resolvedServerUrl: string | undefined;
238+
let resolvedSource = 'default';
239+
240+
if (envProxyEnabled) {
241+
resolvedServerUrl = buildLocalServerUrl(envProxyUrl);
242+
if (resolvedServerUrl) {
243+
resolvedSource = 'process.env VITE_*';
244+
} else {
245+
log.warn('VITE_USE_LOCAL_PROXY is true but VITE_PROXY_URL is empty or invalid, ignoring');
246+
}
247+
}
248+
249+
const devServerUrl = process.env.VITE_DEV_SERVER_URL;
250+
if (!resolvedServerUrl && devServerUrl) {
251+
const devEnvPath = path.join(app.getAppPath(), '.env.development');
252+
const devProxyEnabled = readEnvValue(devEnvPath, 'VITE_USE_LOCAL_PROXY') === 'true';
253+
const devProxyUrl = readEnvValue(devEnvPath, 'VITE_PROXY_URL');
254+
if (devProxyEnabled) {
255+
resolvedServerUrl = buildLocalServerUrl(devProxyUrl);
256+
if (resolvedServerUrl) {
257+
resolvedSource = `dev env file (${devEnvPath})`;
258+
} else {
259+
log.warn(`VITE_USE_LOCAL_PROXY is true in ${devEnvPath} but VITE_PROXY_URL is empty or invalid, ignoring`);
260+
}
261+
}
262+
}
263+
264+
if (!resolvedServerUrl && process.env.SERVER_URL) {
265+
resolvedServerUrl = process.env.SERVER_URL;
266+
resolvedSource = 'process.env SERVER_URL';
267+
}
268+
269+
if (!resolvedServerUrl) {
270+
const serverUrlFromFile = readEnvValue(globalEnvPath, 'SERVER_URL');
271+
if (serverUrlFromFile) {
272+
resolvedServerUrl = serverUrlFromFile;
273+
resolvedSource = `global env file (${globalEnvPath})`;
274+
}
275+
}
276+
277+
const serverUrl = resolvedServerUrl || DEFAULT_SERVER_URL;
278+
log.info(`Backend SERVER_URL resolved to: ${serverUrl} (source: ${resolvedSource})`);
279+
186280
const env = {
187281
...process.env,
188282
...uvEnv,
189-
SERVER_URL: 'https://dev.eigent.ai/api',
283+
SERVER_URL: serverUrl,
190284
PYTHONIOENCODING: 'utf-8',
191285
PYTHONUNBUFFERED: '1',
192286
npm_config_cache: npmCacheDir,

0 commit comments

Comments
 (0)