fix(browser): add profile param to tabs routes and browser-tool

- tabs.ts now uses getProfileContext like other routes
- browser-tool threads profile param through all actions
- add tests for profile query param on /tabs endpoints
- update docs with browser tool profile parameter
This commit is contained in:
James Groat
2026-01-06 11:04:33 -07:00
committed by Peter Steinberger
parent 40758b16a9
commit 9b6e2478f5
6 changed files with 130 additions and 23 deletions

View File

@@ -1,18 +1,26 @@
import type express from "express";
import type { BrowserRouteContext } from "../server-context.js";
import { jsonError, toNumber, toStringOrEmpty } from "./utils.js";
import {
getProfileContext,
jsonError,
toNumber,
toStringOrEmpty,
} from "./utils.js";
export function registerBrowserTabRoutes(
app: express.Express,
ctx: BrowserRouteContext,
) {
app.get("/tabs", async (_req, res) => {
app.get("/tabs", async (req, res) => {
const profileCtx = getProfileContext(req, ctx);
if ("error" in profileCtx)
return jsonError(res, profileCtx.status, profileCtx.error);
try {
const reachable = await ctx.isReachable(300);
const reachable = await profileCtx.isReachable(300);
if (!reachable)
return res.json({ running: false, tabs: [] as unknown[] });
const tabs = await ctx.listTabs();
const tabs = await profileCtx.listTabs();
res.json({ running: true, tabs });
} catch (err) {
jsonError(res, 500, String(err));
@@ -20,11 +28,14 @@ export function registerBrowserTabRoutes(
});
app.post("/tabs/open", async (req, res) => {
const profileCtx = getProfileContext(req, ctx);
if ("error" in profileCtx)
return jsonError(res, profileCtx.status, profileCtx.error);
const url = toStringOrEmpty((req.body as { url?: unknown })?.url);
if (!url) return jsonError(res, 400, "url is required");
try {
await ctx.ensureBrowserAvailable();
const tab = await ctx.openTab(url);
await profileCtx.ensureBrowserAvailable();
const tab = await profileCtx.openTab(url);
res.json(tab);
} catch (err) {
jsonError(res, 500, String(err));
@@ -32,14 +43,17 @@ export function registerBrowserTabRoutes(
});
app.post("/tabs/focus", async (req, res) => {
const profileCtx = getProfileContext(req, ctx);
if ("error" in profileCtx)
return jsonError(res, profileCtx.status, profileCtx.error);
const targetId = toStringOrEmpty(
(req.body as { targetId?: unknown })?.targetId,
);
if (!targetId) return jsonError(res, 400, "targetId is required");
try {
if (!(await ctx.isReachable(300)))
if (!(await profileCtx.isReachable(300)))
return jsonError(res, 409, "browser not running");
await ctx.focusTab(targetId);
await profileCtx.focusTab(targetId);
res.json({ ok: true });
} catch (err) {
const mapped = ctx.mapTabError(err);
@@ -49,12 +63,15 @@ export function registerBrowserTabRoutes(
});
app.delete("/tabs/:targetId", async (req, res) => {
const profileCtx = getProfileContext(req, ctx);
if ("error" in profileCtx)
return jsonError(res, profileCtx.status, profileCtx.error);
const targetId = toStringOrEmpty(req.params.targetId);
if (!targetId) return jsonError(res, 400, "targetId is required");
try {
if (!(await ctx.isReachable(300)))
if (!(await profileCtx.isReachable(300)))
return jsonError(res, 409, "browser not running");
await ctx.closeTab(targetId);
await profileCtx.closeTab(targetId);
res.json({ ok: true });
} catch (err) {
const mapped = ctx.mapTabError(err);
@@ -64,37 +81,40 @@ export function registerBrowserTabRoutes(
});
app.post("/tabs/action", async (req, res) => {
const profileCtx = getProfileContext(req, ctx);
if ("error" in profileCtx)
return jsonError(res, profileCtx.status, profileCtx.error);
const action = toStringOrEmpty((req.body as { action?: unknown })?.action);
const index = toNumber((req.body as { index?: unknown })?.index);
try {
if (action === "list") {
const reachable = await ctx.isReachable(300);
const reachable = await profileCtx.isReachable(300);
if (!reachable) return res.json({ ok: true, tabs: [] as unknown[] });
const tabs = await ctx.listTabs();
const tabs = await profileCtx.listTabs();
return res.json({ ok: true, tabs });
}
if (action === "new") {
await ctx.ensureBrowserAvailable();
const tab = await ctx.openTab("about:blank");
await profileCtx.ensureBrowserAvailable();
const tab = await profileCtx.openTab("about:blank");
return res.json({ ok: true, tab });
}
if (action === "close") {
const tabs = await ctx.listTabs();
const tabs = await profileCtx.listTabs();
const target = typeof index === "number" ? tabs[index] : tabs.at(0);
if (!target) return jsonError(res, 404, "tab not found");
await ctx.closeTab(target.targetId);
await profileCtx.closeTab(target.targetId);
return res.json({ ok: true, targetId: target.targetId });
}
if (action === "select") {
if (typeof index !== "number")
return jsonError(res, 400, "index is required");
const tabs = await ctx.listTabs();
const tabs = await profileCtx.listTabs();
const target = tabs[index];
if (!target) return jsonError(res, 404, "tab not found");
await ctx.focusTab(target.targetId);
await profileCtx.focusTab(target.targetId);
return res.json({ ok: true, targetId: target.targetId });
}

View File

@@ -894,6 +894,61 @@ describe("backward compatibility (profile parameter)", () => {
// Should at least have the default clawd profile
expect(result.profiles.some((p) => p.name === "clawd")).toBe(true);
});
it("GET /tabs?profile=clawd returns tabs for specified profile", async () => {
const { startBrowserControlServerFromConfig } = await import("./server.js");
await startBrowserControlServerFromConfig();
const base = `http://127.0.0.1:${testPort}`;
await realFetch(`${base}/start`, { method: "POST" });
const result = (await realFetch(`${base}/tabs?profile=clawd`).then((r) =>
r.json(),
)) as { running: boolean; tabs: unknown[] };
expect(result.running).toBe(true);
expect(Array.isArray(result.tabs)).toBe(true);
});
it("POST /tabs/open?profile=clawd opens tab in specified profile", async () => {
const { startBrowserControlServerFromConfig } = await import("./server.js");
await startBrowserControlServerFromConfig();
const base = `http://127.0.0.1:${testPort}`;
await realFetch(`${base}/start`, { method: "POST" });
const result = (await realFetch(`${base}/tabs/open?profile=clawd`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ url: "https://example.com" }),
}).then((r) => r.json())) as { targetId?: string };
expect(result.targetId).toBe("newtab1");
});
it("GET /tabs?profile=unknown returns 404", async () => {
const { startBrowserControlServerFromConfig } = await import("./server.js");
await startBrowserControlServerFromConfig();
const base = `http://127.0.0.1:${testPort}`;
const result = await realFetch(`${base}/tabs?profile=unknown`);
expect(result.status).toBe(404);
const body = (await result.json()) as { error: string };
expect(body.error).toContain("not found");
});
it("POST /tabs/open?profile=unknown returns 404", async () => {
const { startBrowserControlServerFromConfig } = await import("./server.js");
await startBrowserControlServerFromConfig();
const base = `http://127.0.0.1:${testPort}`;
const result = await realFetch(`${base}/tabs/open?profile=unknown`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ url: "https://example.com" }),
});
expect(result.status).toBe(404);
const body = (await result.json()) as { error: string };
expect(body.error).toContain("not found");
});
});
describe("profile CRUD endpoints", () => {