From 4f50630291469fd125bd1fb3ed7cdfa8d899ac59 Mon Sep 17 00:00:00 2001 From: Haze <709547807@qq.com> Date: Thu, 26 Feb 2026 14:47:36 +0800 Subject: [PATCH] feat(gateway, openclaw-auth): add browser config synchronization (#184) --- electron/gateway/manager.ts | 8 +++++- electron/utils/openclaw-auth.ts | 48 +++++++++++++++++++++++++++++++ resources/context/AGENTS.clawx.md | 2 +- resources/context/TOOLS.clawx.md | 8 ++++-- scripts/bundle-openclaw.mjs | 1 - 5 files changed, 62 insertions(+), 5 deletions(-) diff --git a/electron/gateway/manager.ts b/electron/gateway/manager.ts index 6284d39f3..786e8aa6f 100644 --- a/electron/gateway/manager.ts +++ b/electron/gateway/manager.ts @@ -30,7 +30,7 @@ import { buildDeviceAuthPayload, type DeviceIdentity, } from '../utils/device-identity'; -import { syncGatewayTokenToConfig } from '../utils/openclaw-auth'; +import { syncGatewayTokenToConfig, syncBrowserConfigToOpenClaw } from '../utils/openclaw-auth'; /** * Gateway connection status @@ -637,6 +637,12 @@ export class GatewayManager extends EventEmitter { } catch (err) { logger.warn('Failed to sync gateway token to openclaw.json:', err); } + + try { + syncBrowserConfigToOpenClaw(); + } catch (err) { + logger.warn('Failed to sync browser config to openclaw.json:', err); + } let command: string; let args: string[]; diff --git a/electron/utils/openclaw-auth.ts b/electron/utils/openclaw-auth.ts index b6f60952c..15bef5cff 100644 --- a/electron/utils/openclaw-auth.ts +++ b/electron/utils/openclaw-auth.ts @@ -443,6 +443,54 @@ export function syncGatewayTokenToConfig(token: string): void { console.log('Synced gateway token to openclaw.json'); } +/** + * Ensure browser automation is enabled in ~/.openclaw/openclaw.json with the + * "openclaw" managed profile as the default. + * + * Only sets values that are not already present so existing user + * customisation (e.g. switching to a remote CDP profile) is preserved. + */ +export function syncBrowserConfigToOpenClaw(): void { + const configPath = join(homedir(), '.openclaw', 'openclaw.json'); + let config: Record = {}; + try { + if (existsSync(configPath)) { + config = JSON.parse(readFileSync(configPath, 'utf-8')) as Record; + } + } catch { + // start from a blank config if the file is corrupt + } + + const browser = ( + config.browser && typeof config.browser === 'object' + ? { ...(config.browser as Record) } + : {} + ) as Record; + + let changed = false; + + if (browser.enabled === undefined) { + browser.enabled = true; + changed = true; + } + + if (browser.defaultProfile === undefined) { + browser.defaultProfile = 'openclaw'; + changed = true; + } + + if (!changed) return; + + config.browser = browser; + + const dir = join(configPath, '..'); + if (!existsSync(dir)) { + mkdirSync(dir, { recursive: true }); + } + writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf-8'); + console.log('Synced browser config to openclaw.json'); +} + /** * Update a provider entry in every discovered agent's models.json. * diff --git a/resources/context/AGENTS.clawx.md b/resources/context/AGENTS.clawx.md index d30cea345..a5f2231c5 100644 --- a/resources/context/AGENTS.clawx.md +++ b/resources/context/AGENTS.clawx.md @@ -3,6 +3,6 @@ You are ClawX, a desktop AI assistant application based on OpenClaw. - **Python**: Always use `uv` to run Python commands. The `uv` binary is bundled and available on PATH. Examples: `uv run python script.py`, `uv pip install package`. -- **Browser**: When asked to open URLs or web pages, use the browser tool to open them in the user's system default browser. +- **Browser**: Full browser automation is available via the `browser` tool. The default "openclaw" profile uses an isolated browser instance. Use it for web scraping, form filling, testing, and any browser automation task. For simply opening a URL for the user to view, use `shell:openExternal` instead. - **Shell**: You have full shell access on the user's machine. Prefer using tools directly over asking the user to run commands manually. - Always confirm before running destructive operations. diff --git a/resources/context/TOOLS.clawx.md b/resources/context/TOOLS.clawx.md index 72977554d..359b054f2 100644 --- a/resources/context/TOOLS.clawx.md +++ b/resources/context/TOOLS.clawx.md @@ -9,5 +9,9 @@ ### Browser -- Use the `browser` tool to open URLs in the user's default browser. -- When the user asks to "open" a link, default to opening it in the browser. +- The `browser` tool provides full browser automation via OpenClaw's browser control server. +- Default profile is "openclaw" (isolated managed browser using system Chrome/Brave/Edge). +- Use `action="start"` to launch the browser, then `action="snapshot"` to see the page, `action="act"` to interact. +- Use `action="open"` with `targetUrl` to open new tabs. +- Refs from snapshots (e.g. `e12`) are used in `act` actions to click/type on specific elements. +- For simple "open a URL for the user to see", use `shell:openExternal` instead. diff --git a/scripts/bundle-openclaw.mjs b/scripts/bundle-openclaw.mjs index 15d8bbefe..35230089f 100644 --- a/scripts/bundle-openclaw.mjs +++ b/scripts/bundle-openclaw.mjs @@ -129,7 +129,6 @@ queue.push({ nodeModulesDir: openclawVirtualNM, skipPkg: 'openclaw' }); const SKIP_PACKAGES = new Set([ 'typescript', - 'playwright-core', '@playwright/test', ]); const SKIP_SCOPES = ['@cloudflare/', '@types/'];