enhance: browser_install PR451 (#454)

This commit is contained in:
Wendong-Fan 2025-10-06 12:43:15 +08:00 committed by GitHub
commit 509f8d4f6f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 74 additions and 20 deletions

View file

@ -153,11 +153,17 @@ export async function startBackend(setPort?: (port: number) => void): Promise<an
setPort(port);
}
const npmCacheDir = path.join(venvPath, '.npm-cache');
if (!fs.existsSync(npmCacheDir)) {
fs.mkdirSync(npmCacheDir, { recursive: true });
}
const env = {
...process.env,
SERVER_URL: "https://dev.eigent.ai/api",
PYTHONIOENCODING: 'utf-8',
UV_PROJECT_ENVIRONMENT: venvPath,
npm_config_cache: npmCacheDir,
}
//Redirect output

View file

@ -333,11 +333,28 @@ export async function installDependencies(version: string): Promise<PromiseRetur
installHybridBrowserDependencies: async (): Promise<boolean> => {
try {
// Find the hybrid_browser_toolkit ts directory in the virtual environment
const sitePackagesPath = path.join(venvPath, 'lib', 'site-packages');
// Need to determine the Python version to construct the correct path
let sitePackagesPath: string | null = null;
const libPath = path.join(venvPath, 'lib');
// Try to find the site-packages directory (it varies by Python version)
if (fs.existsSync(libPath)) {
const libContents = fs.readdirSync(libPath);
const pythonDir = libContents.find(name => name.startsWith('python'));
if (pythonDir) {
sitePackagesPath = path.join(libPath, pythonDir, 'site-packages');
}
}
if (!sitePackagesPath || !fs.existsSync(sitePackagesPath)) {
log.warn('[DEPS INSTALL] site-packages directory not found in venv, skipping npm install');
return true; // Not an error if the venv structure is different
}
const toolkitPath = path.join(sitePackagesPath, 'camel', 'toolkits', 'hybrid_browser_toolkit', 'ts');
if (!fs.existsSync(toolkitPath)) {
log.warn('[DEPS INSTALL] hybrid_browser_toolkit ts directory not found, skipping npm install');
log.warn('[DEPS INSTALL] hybrid_browser_toolkit ts directory not found at ' + toolkitPath + ', skipping npm install');
return true; // Not an error if the toolkit isn't installed
}
@ -350,8 +367,9 @@ export async function installDependencies(version: string): Promise<PromiseRetur
// Try to find npm - first try system npm, then try uv run npm
let npmCommand: string[];
const testNpm = spawn('npm', ['--version'], { shell: true });
const npmExists = await new Promise(resolve => {
const npmExists = await new Promise<boolean>(resolve => {
testNpm.on('close', (code) => resolve(code === 0));
testNpm.on('error', () => resolve(false));
});
if (npmExists) {
@ -365,25 +383,35 @@ export async function installDependencies(version: string): Promise<PromiseRetur
}
// Run npm install
const npmCacheDir = path.join(venvPath, '.npm-cache');
if (!fs.existsSync(npmCacheDir)) {
fs.mkdirSync(npmCacheDir, { recursive: true });
}
const npmInstall = spawn(npmCommand[0], [...npmCommand.slice(1), 'install'], {
cwd: toolkitPath,
env: {
...process.env,
UV_PROJECT_ENVIRONMENT: venvPath,
npm_config_cache: npmCacheDir,
},
shell: true // Important for Windows
});
await new Promise<void>((resolve, reject) => {
npmInstall.stdout.on('data', (data) => {
log.info(`[DEPS INSTALL] npm install: ${data}`);
safeMainWindowSend('install-dependencies-log', { type: 'stdout', data: data.toString() });
});
if (npmInstall.stdout) {
npmInstall.stdout.on('data', (data) => {
log.info(`[DEPS INSTALL] npm install: ${data}`);
safeMainWindowSend('install-dependencies-log', { type: 'stdout', data: data.toString() });
});
}
npmInstall.stderr.on('data', (data) => {
log.warn(`[DEPS INSTALL] npm install stderr: ${data}`);
safeMainWindowSend('install-dependencies-log', { type: 'stderr', data: data.toString() });
});
if (npmInstall.stderr) {
npmInstall.stderr.on('data', (data) => {
log.warn(`[DEPS INSTALL] npm install stderr: ${data}`);
safeMainWindowSend('install-dependencies-log', { type: 'stderr', data: data.toString() });
});
}
npmInstall.on('close', (code) => {
if (code === 0) {
@ -394,6 +422,11 @@ export async function installDependencies(version: string): Promise<PromiseRetur
reject(new Error(`npm install failed with code ${code}`));
}
});
npmInstall.on('error', (err) => {
log.error(`[DEPS INSTALL] npm install process error: ${err}`);
reject(err);
});
});
// Run npm build (use the same npm command as install)
@ -409,21 +442,26 @@ export async function installDependencies(version: string): Promise<PromiseRetur
env: {
...process.env,
UV_PROJECT_ENVIRONMENT: venvPath,
npm_config_cache: npmCacheDir,
},
shell: true // Important for Windows
});
await new Promise<void>((resolve, reject) => {
npmBuild.stdout.on('data', (data) => {
log.info(`[DEPS INSTALL] npm build: ${data}`);
safeMainWindowSend('install-dependencies-log', { type: 'stdout', data: data.toString() });
});
if (npmBuild.stdout) {
npmBuild.stdout.on('data', (data) => {
log.info(`[DEPS INSTALL] npm build: ${data}`);
safeMainWindowSend('install-dependencies-log', { type: 'stdout', data: data.toString() });
});
}
npmBuild.stderr.on('data', (data) => {
// TypeScript build warnings are common, don't treat as errors
log.info(`[DEPS INSTALL] npm build output: ${data}`);
safeMainWindowSend('install-dependencies-log', { type: 'stdout', data: data.toString() });
});
if (npmBuild.stderr) {
npmBuild.stderr.on('data', (data) => {
// TypeScript build warnings are common, don't treat as errors
log.info(`[DEPS INSTALL] npm build output: ${data}`);
safeMainWindowSend('install-dependencies-log', { type: 'stdout', data: data.toString() });
});
}
npmBuild.on('close', (code) => {
if (code === 0) {
@ -434,6 +472,11 @@ export async function installDependencies(version: string): Promise<PromiseRetur
reject(new Error(`TypeScript build failed with code ${code}`));
}
});
npmBuild.on('error', (err) => {
log.error(`[DEPS INSTALL] npm build process error: ${err}`);
reject(err);
});
});
// Optionally install Playwright browsers
@ -461,6 +504,11 @@ export async function installDependencies(version: string): Promise<PromiseRetur
}
resolve();
});
playwrightInstall.on('error', (err) => {
log.warn('[DEPS INSTALL] Playwright installation process error:', err);
resolve(); // Non-critical, continue
});
});
} catch (error) {
log.warn('[DEPS INSTALL] Failed to install Playwright browsers:', error);