UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

205 lines (204 loc) • 12.8 kB
import { l as normalizeOptionalString } from "../../string-coerce-6kR-86XC.mjs"; import { o as resolveUserPath } from "../../home-dir-CjKH62jd.mjs"; import { r as asNullableRecord } from "../../record-coerce-TIsquoVR.mjs"; import { t as CONFIG_DIR } from "../../utils-DUAwk2p5.mjs"; import { r as isPathInside } from "../../path-guards-CH7noY0Z.mjs"; import { t as formatCliCommand } from "../../command-format-Cte1SWM4.mjs"; import { t as note } from "../../note-BpWs6H5N.mjs"; import "../../string-coerce-runtime-B1b8UNzb.mjs"; import "../../text-utility-runtime-UZWYbiiX.mjs"; import "../../file-access-runtime-ISL--r1N.mjs"; import "../../cli-runtime-D_E6OBH2.mjs"; import { m as DEFAULT_OPENCLAW_BROWSER_PROFILE_NAME } from "../../constants-C-i9f6FL.mjs"; import { i as resolveBrowserConfig } from "../../config-HztiUSlV.mjs"; import { i as readBrowserVersion, n as resolveGoogleChromeExecutableForPlatform, r as parseBrowserMajorVersion, t as resolveBrowserExecutableForPlatform } from "../../chrome.executables-BuNK6O5J.mjs"; import { t as movePathToTrash } from "../../trash-DYVpE258.mjs"; import fs from "node:fs"; import path from "node:path"; //#region extensions/browser/src/doctor-browser.ts const CHROME_MCP_MIN_MAJOR = 144; const LEGACY_CLAWD_BROWSER_PROFILE_NAME = "clawd"; const REMOTE_DEBUGGING_PAGES = [ "chrome://inspect/#remote-debugging", "brave://inspect/#remote-debugging", "edge://inspect/#remote-debugging" ].join(", "); function collectBrowserDoctorProfiles(cfg) { const browser = asNullableRecord(cfg.browser); const managed = /* @__PURE__ */ new Map(); const chromeMcp = /* @__PURE__ */ new Map(); const defaultProfile = normalizeOptionalString(browser?.defaultProfile); if (defaultProfile) (defaultProfile === "user" ? chromeMcp : managed).set(defaultProfile, { name: defaultProfile }); for (const [name, rawProfile] of Object.entries(asNullableRecord(browser?.profiles) ?? {})) { const profile = asNullableRecord(rawProfile); if (normalizeOptionalString(profile?.driver) === "existing-session") chromeMcp.set(name, { name, userDataDir: normalizeOptionalString(profile?.userDataDir) }); else managed.set(name, { name }); } return { managed: [...managed.values()].toSorted((a, b) => a.name.localeCompare(b.name)), chromeMcp: [...chromeMcp.values()].toSorted((a, b) => a.name.localeCompare(b.name)) }; } function isLegacyClawdProfileConfigured(cfg, legacyProfileDir) { const browser = asNullableRecord(cfg.browser); if (!browser) return false; if (normalizeOptionalString(browser.defaultProfile) === LEGACY_CLAWD_BROWSER_PROFILE_NAME) return true; const configuredProfiles = asNullableRecord(browser.profiles); if (!configuredProfiles) return false; if (Object.hasOwn(configuredProfiles, LEGACY_CLAWD_BROWSER_PROFILE_NAME)) return true; for (const rawProfile of Object.values(configuredProfiles)) { const profile = asNullableRecord(rawProfile); const userDataDir = normalizeOptionalString(profile?.userDataDir); if (userDataDir && isPathInside(legacyProfileDir, resolveUserPath(userDataDir))) return true; } return false; } function detectLegacyClawdBrowserProfileResidue(cfg, deps) { const configDir = deps?.configDir ?? CONFIG_DIR; const legacyProfileDir = path.join(configDir, "browser", LEGACY_CLAWD_BROWSER_PROFILE_NAME); const legacyUserDataDir = path.join(legacyProfileDir, "user-data"); const pathExists = deps?.pathExists ?? fs.existsSync; if (!pathExists(legacyProfileDir) && !pathExists(legacyUserDataDir)) return null; if (isLegacyClawdProfileConfigured(cfg, legacyProfileDir)) return null; const resolved = resolveBrowserConfig(cfg.browser, cfg); const defaultProfile = resolved.profiles[resolved.defaultProfile]; if (resolved.defaultProfile !== "openclaw" || defaultProfile?.driver === "existing-session") return null; return { legacyProfileDir, legacyUserDataDir, canonicalUserDataDir: path.join(configDir, "browser", DEFAULT_OPENCLAW_BROWSER_PROFILE_NAME, "user-data") }; } function formatLegacyClawdBrowserProfileResidueNote(residue) { return [ `- Legacy managed browser profile residue was found at ${residue.legacyProfileDir}.`, `- The canonical OpenClaw-managed browser profile is ${residue.canonicalUserDataDir}.`, `- If no browser is using the legacy profile, run ${formatCliCommand("openclaw doctor --fix")} to archive it safely instead of deleting it in place.` ].join("\n"); } async function noteChromeMcpBrowserReadiness(cfg, deps) { const noteFn = deps?.noteFn ?? note; const platform = deps?.platform ?? process.platform; const env = deps?.env ?? process.env; const getUid = deps?.getUid ?? (() => process.getuid?.() ?? -1); const resolveManagedExecutable = deps?.resolveManagedExecutable ?? resolveBrowserExecutableForPlatform; const resolveChromeExecutable = deps?.resolveChromeExecutable ?? resolveGoogleChromeExecutableForPlatform; const readVersion = deps?.readVersion ?? readBrowserVersion; const { managed: managedProfiles, chromeMcp: profiles } = collectBrowserDoctorProfiles(cfg); const managedProfileLabel = managedProfiles.map((profile) => profile.name).join(", "); const resolved = resolveBrowserConfig(cfg.browser, cfg); if (resolved.enabled && resolved.extensionRelay.allowLegacyAuth) noteFn([ "- Legacy Browser Relay Authentication is enabled (browser.extensionRelay.allowLegacyAuth=true).", "- Update paired Chrome extensions and external CDP clients to Browser Relay Authentication v2, then set browser.extensionRelay.allowLegacyAuth=false.", "- V2 clients never downgrade to legacy authentication." ].join("\n"), "Browser relay authentication"); const extensionStateDir = deps?.configDir ?? CONFIG_DIR; const extensionCopyPath = path.join(extensionStateDir, "browser", "chrome-extension"); if (fs.existsSync(extensionCopyPath)) noteFn([ "- Chrome extension native bootstrap was not inspected; registration status is unavailable in Doctor.", `- Run ${formatCliCommand("openclaw browser extension status --json")} to inspect it explicitly; this may request browser profile access.`, `- Run ${formatCliCommand("openclaw browser extension install")} if setup or repair is needed.` ].join("\n"), "Browser extension bootstrap"); const legacyClawdResidue = detectLegacyClawdBrowserProfileResidue(cfg, { configDir: deps?.configDir, pathExists: deps?.pathExists }); if (legacyClawdResidue) noteFn(formatLegacyClawdBrowserProfileResidueNote(legacyClawdResidue), "Browser"); if (platform === "darwin") noteFn([ `- System browser profile cookie import is ${cfg.browser?.allowSystemProfileImport !== false ? "enabled" : "disabled"} (browser.allowSystemProfileImport).`, "- System browser profile discovery skipped by Doctor; importable cookie database count is unavailable.", "- Doctor does not access the macOS Keychain; importing asks for consent separately." ].join("\n"), "Browser"); const browserExecutable = managedProfiles.length > 0 ? resolveManagedExecutable(resolved, platform) : null; const missingDisplay = platform === "linux" && managedProfiles.length > 0 && !resolved.headless && !normalizeOptionalString(env.DISPLAY) && !normalizeOptionalString(env.WAYLAND_DISPLAY); const shouldWarnRootNoSandbox = platform === "linux" && managedProfiles.length > 0 && !resolved.noSandbox && getUid() === 0; if (!browserExecutable && managedProfiles.length > 0) noteFn([ `- OpenClaw-managed browser profile(s) are configured: ${managedProfileLabel}.`, "- No Chromium-based browser executable was found on this host for OpenClaw-managed launch.", "- Install Chrome, Chromium, Brave, Edge, or set browser.executablePath explicitly." ].join("\n"), "Browser"); if (missingDisplay || shouldWarnRootNoSandbox) { const lines = [`- OpenClaw-managed browser profile(s) are configured: ${managedProfileLabel}.`]; if (missingDisplay) lines.push("- No DISPLAY or WAYLAND_DISPLAY is set, and browser.headless is false. Managed browser launch needs a desktop session, Xvfb, or browser.headless: true."); if (shouldWarnRootNoSandbox) lines.push("- The Gateway is running as root and browser.noSandbox is false. Chromium commonly requires browser.noSandbox: true in container/root runtimes."); noteFn(lines.join("\n"), "Browser"); } if (profiles.length === 0) return; const explicitProfiles = profiles.filter((profile) => profile.userDataDir); const autoConnectProfiles = profiles.filter((profile) => !profile.userDataDir); const profileLabel = profiles.map((profile) => profile.name).join(", "); if (autoConnectProfiles.length === 0) { noteFn([ `- Chrome MCP existing-session is configured for profile(s): ${profileLabel}.`, "- These profiles use an explicit Chromium user data directory instead of Chrome's default auto-connect path.", `- Verify the matching Chromium-based browser is version ${CHROME_MCP_MIN_MAJOR}+ on the same host as the Gateway or node.`, `- Enable remote debugging in that browser's inspect page (${REMOTE_DEBUGGING_PAGES}).`, "- Keep the browser running and accept the attach consent prompt the first time OpenClaw connects." ].join("\n"), "Browser"); return; } const chrome = resolveChromeExecutable(platform); const autoProfileLabel = autoConnectProfiles.map((profile) => profile.name).join(", "); if (!chrome) { const lines = [ `- Chrome MCP existing-session is configured for profile(s): ${profileLabel}.`, `- Google Chrome was not found on this host for auto-connect profile(s): ${autoProfileLabel}. OpenClaw does not bundle Chrome.`, `- Install Google Chrome ${CHROME_MCP_MIN_MAJOR}+ on the same host as the Gateway or node, or set browser.profiles.<name>.userDataDir for a different Chromium-based browser.`, `- Enable remote debugging in the browser inspect page (${REMOTE_DEBUGGING_PAGES}).`, "- Keep the browser running and accept the attach consent prompt the first time OpenClaw connects.", "- Docker, headless, and sandbox browser flows stay on raw CDP; this check only applies to host-local Chrome MCP attach." ]; if (explicitProfiles.length > 0) lines.push(`- Profiles with explicit userDataDir skip Chrome auto-detection: ${explicitProfiles.map((profile) => profile.name).join(", ")}.`); noteFn(lines.join("\n"), "Browser"); return; } const versionRaw = readVersion(chrome.path); const major = parseBrowserMajorVersion(versionRaw); const lines = [`- Chrome MCP existing-session is configured for profile(s): ${profileLabel}.`, `- Chrome path: ${chrome.path}`]; if (!versionRaw || major === null) lines.push(`- Could not determine the installed Chrome version. Chrome MCP requires Google Chrome ${CHROME_MCP_MIN_MAJOR}+ on this host.`); else if (major < CHROME_MCP_MIN_MAJOR) lines.push(`- Detected Chrome ${versionRaw}, which is too old for Chrome MCP existing-session attach. Upgrade to Chrome ${CHROME_MCP_MIN_MAJOR}+.`); else lines.push(`- Detected Chrome ${versionRaw}.`); lines.push(`- Enable remote debugging in the browser inspect page (${REMOTE_DEBUGGING_PAGES}).`); lines.push("- Keep the browser running and accept the attach consent prompt the first time OpenClaw connects."); if (explicitProfiles.length > 0) lines.push(`- Profiles with explicit userDataDir still need manual validation of the matching Chromium-based browser: ${explicitProfiles.map((profile) => profile.name).join(", ")}.`); noteFn(lines.join("\n"), "Browser"); } /** Leave discovery-dependent native-host repair to explicit extension setup. */ async function maybeRepairOwnedChromeExtensionNativeHosts() { return { status: "skipped", reason: "Doctor does not inspect personal browser profiles", changes: [], warnings: [`Chrome extension native-host repair skipped: Doctor does not inspect personal browser profiles. Run ${formatCliCommand("openclaw browser extension install")} to repair explicitly.`] }; } async function maybeArchiveLegacyClawdBrowserProfileResidue(cfg, deps) { const residue = detectLegacyClawdBrowserProfileResidue(cfg, deps); if (!residue) return { changes: [], warnings: [] }; const move = deps?.movePathToTrash ?? movePathToTrash; try { const archivedPath = await move(residue.legacyProfileDir); return { changes: [[ "Archived legacy clawd managed browser profile residue.", `- legacy profile: ${residue.legacyProfileDir}`, `- canonical profile: ${residue.canonicalUserDataDir}`, `- archived at: ${archivedPath}` ].join("\n")], warnings: [] }; } catch (error) { return { changes: [], warnings: [`Legacy clawd browser profile residue could not be archived: ${error instanceof Error ? error.message : String(error)}`] }; } } //#endregion export { detectLegacyClawdBrowserProfileResidue, maybeArchiveLegacyClawdBrowserProfileResidue, maybeRepairOwnedChromeExtensionNativeHosts, noteChromeMcpBrowserReadiness };