import { existsSync } from 'node:fs'; import { MIDSCENE_CHROME_PATH, MIDSCENE_MCP_CHROME_PATH, globalConfigManager, } from '../env'; import { getDebug } from '../logger'; const warnChromePath = getDebug('agent-tools:chrome-path', { console: true }); let hasWarnedLegacyChromePath = false; let cachedSystemChromePath: string | undefined; export function getSystemChromePath(): string | undefined { if (cachedSystemChromePath !== undefined) { return cachedSystemChromePath; } const platform = process.platform; const chromePaths: Record = { darwin: [ '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', '/Applications/Chromium.app/Contents/MacOS/Chromium', ], win32: [ 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe', 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe', `C:\\Users\\${process.env.USERNAME ?? process.env.USER}\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe`, ], linux: [ // Prefer actual binaries over wrapper scripts. // Wrappers in /usr/bin may strip --user-data-dir, causing // "DevTools remote debugging requires a non-default data directory" errors. '/opt/google/chrome/chrome', '/opt/google/chrome/google-chrome', '/usr/bin/google-chrome-stable', '/usr/bin/google-chrome', '/usr/bin/chromium-browser', '/usr/bin/chromium', '/snap/bin/chromium', ], }; const paths = chromePaths[platform] ?? []; const foundPath = paths.find((p) => existsSync(p)); if (foundPath) { cachedSystemChromePath = foundPath; } return foundPath; } export function resolveChromePath(): string { const primaryEnvPath = globalConfigManager.getEnvConfigValue(MIDSCENE_CHROME_PATH); const legacyEnvPath = globalConfigManager.getEnvConfigValue( MIDSCENE_MCP_CHROME_PATH, ); const envPath = primaryEnvPath || legacyEnvPath; if (!primaryEnvPath && legacyEnvPath && !hasWarnedLegacyChromePath) { warnChromePath( 'MIDSCENE_MCP_CHROME_PATH is deprecated. Use MIDSCENE_CHROME_PATH instead.', ); hasWarnedLegacyChromePath = true; } if (envPath && envPath !== 'auto' && existsSync(envPath)) { return envPath; } const systemPath = getSystemChromePath(); if (systemPath) return systemPath; throw new Error( 'Chrome not found. Install Google Chrome or set MIDSCENE_CHROME_PATH environment variable.', ); }