Skip to content

Commit 509f8d4

Browse files
authored
enhance: browser_install PR451 (#454)
2 parents de203d7 + 076e232 commit 509f8d4

File tree

2 files changed

+74
-20
lines changed

2 files changed

+74
-20
lines changed

electron/main/init.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,17 @@ export async function startBackend(setPort?: (port: number) => void): Promise<an
153153
setPort(port);
154154
}
155155

156+
const npmCacheDir = path.join(venvPath, '.npm-cache');
157+
if (!fs.existsSync(npmCacheDir)) {
158+
fs.mkdirSync(npmCacheDir, { recursive: true });
159+
}
160+
156161
const env = {
157162
...process.env,
158163
SERVER_URL: "https://dev.eigent.ai/api",
159164
PYTHONIOENCODING: 'utf-8',
160165
UV_PROJECT_ENVIRONMENT: venvPath,
166+
npm_config_cache: npmCacheDir,
161167
}
162168

163169
//Redirect output

electron/main/install-deps.ts

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -333,11 +333,28 @@ export async function installDependencies(version: string): Promise<PromiseRetur
333333
installHybridBrowserDependencies: async (): Promise<boolean> => {
334334
try {
335335
// Find the hybrid_browser_toolkit ts directory in the virtual environment
336-
const sitePackagesPath = path.join(venvPath, 'lib', 'site-packages');
336+
// Need to determine the Python version to construct the correct path
337+
let sitePackagesPath: string | null = null;
338+
const libPath = path.join(venvPath, 'lib');
339+
340+
// Try to find the site-packages directory (it varies by Python version)
341+
if (fs.existsSync(libPath)) {
342+
const libContents = fs.readdirSync(libPath);
343+
const pythonDir = libContents.find(name => name.startsWith('python'));
344+
if (pythonDir) {
345+
sitePackagesPath = path.join(libPath, pythonDir, 'site-packages');
346+
}
347+
}
348+
349+
if (!sitePackagesPath || !fs.existsSync(sitePackagesPath)) {
350+
log.warn('[DEPS INSTALL] site-packages directory not found in venv, skipping npm install');
351+
return true; // Not an error if the venv structure is different
352+
}
353+
337354
const toolkitPath = path.join(sitePackagesPath, 'camel', 'toolkits', 'hybrid_browser_toolkit', 'ts');
338355

339356
if (!fs.existsSync(toolkitPath)) {
340-
log.warn('[DEPS INSTALL] hybrid_browser_toolkit ts directory not found, skipping npm install');
357+
log.warn('[DEPS INSTALL] hybrid_browser_toolkit ts directory not found at ' + toolkitPath + ', skipping npm install');
341358
return true; // Not an error if the toolkit isn't installed
342359
}
343360

@@ -350,8 +367,9 @@ export async function installDependencies(version: string): Promise<PromiseRetur
350367
// Try to find npm - first try system npm, then try uv run npm
351368
let npmCommand: string[];
352369
const testNpm = spawn('npm', ['--version'], { shell: true });
353-
const npmExists = await new Promise(resolve => {
370+
const npmExists = await new Promise<boolean>(resolve => {
354371
testNpm.on('close', (code) => resolve(code === 0));
372+
testNpm.on('error', () => resolve(false));
355373
});
356374

357375
if (npmExists) {
@@ -365,25 +383,35 @@ export async function installDependencies(version: string): Promise<PromiseRetur
365383
}
366384

367385
// Run npm install
386+
const npmCacheDir = path.join(venvPath, '.npm-cache');
387+
if (!fs.existsSync(npmCacheDir)) {
388+
fs.mkdirSync(npmCacheDir, { recursive: true });
389+
}
390+
368391
const npmInstall = spawn(npmCommand[0], [...npmCommand.slice(1), 'install'], {
369392
cwd: toolkitPath,
370393
env: {
371394
...process.env,
372395
UV_PROJECT_ENVIRONMENT: venvPath,
396+
npm_config_cache: npmCacheDir,
373397
},
374398
shell: true // Important for Windows
375399
});
376400

377401
await new Promise<void>((resolve, reject) => {
378-
npmInstall.stdout.on('data', (data) => {
379-
log.info(`[DEPS INSTALL] npm install: ${data}`);
380-
safeMainWindowSend('install-dependencies-log', { type: 'stdout', data: data.toString() });
381-
});
402+
if (npmInstall.stdout) {
403+
npmInstall.stdout.on('data', (data) => {
404+
log.info(`[DEPS INSTALL] npm install: ${data}`);
405+
safeMainWindowSend('install-dependencies-log', { type: 'stdout', data: data.toString() });
406+
});
407+
}
382408

383-
npmInstall.stderr.on('data', (data) => {
384-
log.warn(`[DEPS INSTALL] npm install stderr: ${data}`);
385-
safeMainWindowSend('install-dependencies-log', { type: 'stderr', data: data.toString() });
386-
});
409+
if (npmInstall.stderr) {
410+
npmInstall.stderr.on('data', (data) => {
411+
log.warn(`[DEPS INSTALL] npm install stderr: ${data}`);
412+
safeMainWindowSend('install-dependencies-log', { type: 'stderr', data: data.toString() });
413+
});
414+
}
387415

388416
npmInstall.on('close', (code) => {
389417
if (code === 0) {
@@ -394,6 +422,11 @@ export async function installDependencies(version: string): Promise<PromiseRetur
394422
reject(new Error(`npm install failed with code ${code}`));
395423
}
396424
});
425+
426+
npmInstall.on('error', (err) => {
427+
log.error(`[DEPS INSTALL] npm install process error: ${err}`);
428+
reject(err);
429+
});
397430
});
398431

399432
// Run npm build (use the same npm command as install)
@@ -409,21 +442,26 @@ export async function installDependencies(version: string): Promise<PromiseRetur
409442
env: {
410443
...process.env,
411444
UV_PROJECT_ENVIRONMENT: venvPath,
445+
npm_config_cache: npmCacheDir,
412446
},
413447
shell: true // Important for Windows
414448
});
415449

416450
await new Promise<void>((resolve, reject) => {
417-
npmBuild.stdout.on('data', (data) => {
418-
log.info(`[DEPS INSTALL] npm build: ${data}`);
419-
safeMainWindowSend('install-dependencies-log', { type: 'stdout', data: data.toString() });
420-
});
451+
if (npmBuild.stdout) {
452+
npmBuild.stdout.on('data', (data) => {
453+
log.info(`[DEPS INSTALL] npm build: ${data}`);
454+
safeMainWindowSend('install-dependencies-log', { type: 'stdout', data: data.toString() });
455+
});
456+
}
421457

422-
npmBuild.stderr.on('data', (data) => {
423-
// TypeScript build warnings are common, don't treat as errors
424-
log.info(`[DEPS INSTALL] npm build output: ${data}`);
425-
safeMainWindowSend('install-dependencies-log', { type: 'stdout', data: data.toString() });
426-
});
458+
if (npmBuild.stderr) {
459+
npmBuild.stderr.on('data', (data) => {
460+
// TypeScript build warnings are common, don't treat as errors
461+
log.info(`[DEPS INSTALL] npm build output: ${data}`);
462+
safeMainWindowSend('install-dependencies-log', { type: 'stdout', data: data.toString() });
463+
});
464+
}
427465

428466
npmBuild.on('close', (code) => {
429467
if (code === 0) {
@@ -434,6 +472,11 @@ export async function installDependencies(version: string): Promise<PromiseRetur
434472
reject(new Error(`TypeScript build failed with code ${code}`));
435473
}
436474
});
475+
476+
npmBuild.on('error', (err) => {
477+
log.error(`[DEPS INSTALL] npm build process error: ${err}`);
478+
reject(err);
479+
});
437480
});
438481

439482
// Optionally install Playwright browsers
@@ -461,6 +504,11 @@ export async function installDependencies(version: string): Promise<PromiseRetur
461504
}
462505
resolve();
463506
});
507+
508+
playwrightInstall.on('error', (err) => {
509+
log.warn('[DEPS INSTALL] Playwright installation process error:', err);
510+
resolve(); // Non-critical, continue
511+
});
464512
});
465513
} catch (error) {
466514
log.warn('[DEPS INSTALL] Failed to install Playwright browsers:', error);

0 commit comments

Comments
 (0)