Skip to content

Commit 6026eb8

Browse files
HemantSudarshansallyom
authored andcommitted
fix(browser): honor cdp url for default session profile
1 parent ac21e89 commit 6026eb8

4 files changed

Lines changed: 66 additions & 3 deletions

File tree

extensions/browser/src/browser/config.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,39 @@ describe("browser config", () => {
974974
expect(profile?.mcpArgs).toEqual(["--no-usage-statistics", "--performanceCrux", "false"]);
975975
});
976976

977+
it("applies top-level cdpUrl to an existing-session default profile", () => {
978+
const resolved = resolveBrowserConfig({
979+
defaultProfile: "user",
980+
cdpUrl: "http://127.0.0.1:9222/",
981+
});
982+
983+
const profile = resolveProfile(resolved, resolved.defaultProfile);
984+
expect(resolved.defaultProfile).toBe("user");
985+
expect(profile?.driver).toBe("existing-session");
986+
expect(profile?.cdpUrl).toBe("http://127.0.0.1:9222");
987+
expect(profile?.cdpHost).toBe("127.0.0.1");
988+
expect(profile?.cdpIsLoopback).toBe(true);
989+
});
990+
991+
it("keeps explicit existing-session profile cdpUrl over the top-level cdpUrl", () => {
992+
const resolved = resolveBrowserConfig({
993+
defaultProfile: "chrome-live",
994+
cdpUrl: "http://127.0.0.1:9222",
995+
profiles: {
996+
"chrome-live": {
997+
driver: "existing-session",
998+
attachOnly: true,
999+
cdpUrl: "http://127.0.0.1:9333",
1000+
color: "#00AA00",
1001+
},
1002+
},
1003+
});
1004+
1005+
const profile = resolveProfile(resolved, resolved.defaultProfile);
1006+
expect(profile?.driver).toBe("existing-session");
1007+
expect(profile?.cdpUrl).toBe("http://127.0.0.1:9333");
1008+
});
1009+
9771010
it("preserves direct websocket cdpUrl for existing-session profiles", () => {
9781011
const resolved = resolveBrowserConfig({
9791012
profiles: {

extensions/browser/src/browser/config.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,31 @@ function ensureDefaultUserBrowserProfile(
345345
return result;
346346
}
347347

348+
function applyLegacyCdpUrlToExistingSessionDefaultProfile(
349+
profiles: Record<string, BrowserProfileConfig>,
350+
defaultProfile: string,
351+
legacyCdpUrl: string | undefined,
352+
): Record<string, BrowserProfileConfig> {
353+
if (!legacyCdpUrl) {
354+
return profiles;
355+
}
356+
const profile = profiles[defaultProfile];
357+
if (
358+
!profile ||
359+
profile.driver !== "existing-session" ||
360+
normalizeOptionalString(profile.cdpUrl)
361+
) {
362+
return profiles;
363+
}
364+
return {
365+
...profiles,
366+
[defaultProfile]: {
367+
...profile,
368+
cdpUrl: legacyCdpUrl,
369+
},
370+
};
371+
}
372+
348373
/** Resolve raw browser config into runtime browser defaults. */
349374
export function resolveBrowserConfig(
350375
cfg: BrowserConfig | undefined,
@@ -417,7 +442,7 @@ export function resolveBrowserConfig(
417442
const legacyCdpPort = rawCdpUrl ? cdpInfo.port : undefined;
418443
const isWsUrl = cdpInfo.parsed.protocol === "ws:" || cdpInfo.parsed.protocol === "wss:";
419444
const legacyCdpUrl = rawCdpUrl && isWsUrl ? cdpInfo.normalized : undefined;
420-
const profiles = ensureDefaultUserBrowserProfile(
445+
let profiles = ensureDefaultUserBrowserProfile(
421446
ensureDefaultProfile(
422447
cfg?.profiles,
423448
defaultColor,
@@ -435,6 +460,11 @@ export function resolveBrowserConfig(
435460
: profiles[DEFAULT_OPENCLAW_BROWSER_PROFILE_NAME]
436461
? DEFAULT_OPENCLAW_BROWSER_PROFILE_NAME
437462
: "user");
463+
profiles = applyLegacyCdpUrlToExistingSessionDefaultProfile(
464+
profiles,
465+
defaultProfile,
466+
rawCdpUrl ? cdpInfo.normalized : undefined,
467+
);
438468

439469
const extraArgs = Array.isArray(cfg?.extraArgs)
440470
? cfg.extraArgs.filter(

src/config/schema.help.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ export const FIELD_HELP: Record<string, string> = {
347347
"browser.profiles.*.cdpPort":
348348
"Per-profile local CDP port used when connecting to browser instances by port instead of URL. Use unique ports per profile to avoid connection collisions.",
349349
"browser.profiles.*.cdpUrl":
350-
"Per-profile CDP websocket URL used for explicit remote browser routing by profile name. Use this when profile connections terminate on remote hosts or tunnels.",
350+
"Per-profile CDP/DevTools endpoint URL used for explicit browser routing by profile name. Use this for remote CDP hosts, tunnels, or existing-session profiles that should attach through a running Chrome DevTools endpoint.",
351351
"browser.profiles.*.userDataDir":
352352
"Per-profile Chromium user data directory for existing-session attachment through Chrome DevTools MCP. Use this for Brave, Edge, Chromium, or non-default Chrome profiles when the built-in auto-connect path would pick the wrong browser data directory on the selected host or browser node. Paths starting with ~ expand to the OS home directory.",
353353
"browser.profiles.*.mcpCommand":

src/config/types.browser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
export type BrowserProfileConfig = {
33
/** CDP port for this profile. Allocated once at creation, persisted permanently. */
44
cdpPort?: number;
5-
/** CDP URL for this profile (use for remote Chrome). */
5+
/** CDP/DevTools endpoint URL for this profile (remote CDP or existing-session endpoint attach). */
66
cdpUrl?: string;
77
/** Explicit user data directory for existing-session Chrome MCP attachment. */
88
userDataDir?: string;

0 commit comments

Comments
 (0)