fix(browser): default chrome profile to host

This commit is contained in:
Peter Steinberger
2026-01-15 10:34:57 +00:00
parent 725c340257
commit 6042485367
2 changed files with 41 additions and 5 deletions

View File

@@ -134,6 +134,18 @@ describe("browser tool snapshot maxChars", () => {
}),
);
});
it("defaults to host when using profile=chrome (even in sandboxed sessions)", async () => {
const tool = createBrowserTool({ defaultControlUrl: "http://127.0.0.1:9999" });
await tool.execute?.(null, { action: "snapshot", profile: "chrome", format: "ai" });
expect(browserClientMocks.browserSnapshot).toHaveBeenCalledWith(
"http://127.0.0.1:18791",
expect.objectContaining({
profile: "chrome",
}),
);
});
});
describe("browser tool snapshot labels", () => {

View File

@@ -141,8 +141,12 @@ export function createBrowserTool(opts?: {
const params = args as Record<string, unknown>;
const action = readStringParam(params, "action", { required: true });
const controlUrl = readStringParam(params, "controlUrl");
const target = readStringParam(params, "target") as "sandbox" | "host" | "custom" | undefined;
const profile = readStringParam(params, "profile");
let target = readStringParam(params, "target") as "sandbox" | "host" | "custom" | undefined;
if (profile === "chrome" && !target && !controlUrl?.trim()) {
// Chrome extension relay takeover is a host Chrome feature; default to host even in sandboxed sessions.
target = "host";
}
const baseUrl = resolveBrowserBaseUrl({
target,
controlUrl,
@@ -345,10 +349,30 @@ export function createBrowserTool(opts?: {
if (!request || typeof request !== "object") {
throw new Error("request required");
}
const result = await browserAct(baseUrl, request as Parameters<typeof browserAct>[1], {
profile,
});
return jsonResult(result);
try {
const result = await browserAct(
baseUrl,
request as Parameters<typeof browserAct>[1],
{
profile,
},
);
return jsonResult(result);
} catch (err) {
const msg = String(err);
if (msg.includes("404:") && msg.includes("tab not found") && profile === "chrome") {
const tabs = await browserTabs(baseUrl, { profile }).catch(() => []);
if (!tabs.length) {
throw new Error(
'No Chrome tabs are attached via the Clawdbot Browser Relay extension. Click the toolbar icon on the tab you want to control (badge ON), then retry.',
);
}
throw new Error(
`Chrome tab not found (stale targetId?). Run action=tabs profile="chrome" and use one of the returned targetIds.`,
);
}
throw err;
}
}
default:
throw new Error(`Unknown action: ${action}`);