diff --git a/packages/shared/src/mcp/chrome-path.ts b/packages/shared/src/mcp/chrome-path.ts index 75f3b84827..521da2780c 100644 --- a/packages/shared/src/mcp/chrome-path.ts +++ b/packages/shared/src/mcp/chrome-path.ts @@ -1,7 +1,17 @@ import { existsSync } from 'node:fs'; import { MIDSCENE_MCP_CHROME_PATH, globalConfigManager } from '../env'; +let cachedSystemChromePath: string | undefined = undefined; + +export function clearSystemChromePathCache() { + cachedSystemChromePath = undefined; +} + export function getSystemChromePath(): string | undefined { + if (cachedSystemChromePath !== undefined) { + return cachedSystemChromePath; + } + const platform = process.platform; const chromePaths: Record = { @@ -29,7 +39,13 @@ export function getSystemChromePath(): string | undefined { }; const paths = chromePaths[platform] ?? []; - return paths.find((p) => existsSync(p)); + const foundPath = paths.find((p) => existsSync(p)); + + if (foundPath) { + cachedSystemChromePath = foundPath; + } + + return foundPath; } export function resolveChromePath(): string { diff --git a/packages/shared/tests/unit-test/chrome-path.test.ts b/packages/shared/tests/unit-test/chrome-path.test.ts index fa784ca5cf..f436cf0982 100644 --- a/packages/shared/tests/unit-test/chrome-path.test.ts +++ b/packages/shared/tests/unit-test/chrome-path.test.ts @@ -1,6 +1,7 @@ import { existsSync } from 'node:fs'; import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; import { + clearSystemChromePathCache, getSystemChromePath, resolveChromePath, } from '../../src/mcp/chrome-path'; @@ -22,6 +23,7 @@ describe('Chrome Path Resolution', () => { beforeEach(() => { vi.clearAllMocks(); vi.mocked(existsSync).mockReturnValue(false); + clearSystemChromePathCache(); }); afterEach(() => {