Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: each chat keeps its own browser tabs, and the browser agent works on in chats off screen (#13800)

Daniel Hane16e7fcb84ce
diff --git a/studio/frontend/scripts/check-bundle-budget.ts b/studio/frontend/scripts/check-bundle-budget.ts
index 4e5e028adda..aa181879c6d 100644
--- a/studio/frontend/scripts/check-bundle-budget.ts
+++ b/studio/frontend/scripts/check-bundle-budget.ts
@@ -120,8 +120,12 @@ export const BUDGET = {
// Naming the Windows update that adds MXC's built-in container (#13798) adds 358 B raw / 93 B transfer, its
// string in the eager English locale (6,765,003 -> 6,765,361 B raw against 3c0061e16, one machine and build,
// Node 24, chunk count unchanged at 95), which put main 55 B raw over. Raised by that growth.
- transferBytes: 2_033_777,
- rawBytes: 6_765_664,
+ // Each chat keeping its own browser tabs (#13800: the store's per-chat state, background pages in the
+ // native view, and the off-screen question strings) adds 5,278 B raw / 1,928 B transfer (6,765,003 ->
+ // 6,770,281 B raw, 2,033,563 -> 2,035,491 B transfer against a8585676d4, one machine and build, Node 24,
+ // chunk count unchanged at 95), raised by that.
+ transferBytes: 2_035_705,
+ rawBytes: 6_770_942,
};
// The chunk count is reported but not budgeted. Splitting a page out of the entry raises it while lowering the
diff --git a/studio/frontend/src/features/browser/agent/agent-bar.tsx b/studio/frontend/src/features/browser/agent/agent-bar.tsx
index 6fb927d2c62..a1a45cb9795 100644
--- a/studio/frontend/src/features/browser/agent/agent-bar.tsx
+++ b/studio/frontend/src/features/browser/agent/agent-bar.tsx
@@ -5,15 +5,16 @@ import { Button } from "@/components/ui/button";
import { Spinner } from "@/components/ui/spinner";
import { useT } from "@/i18n";
import { useCallback, useState } from "react";
-import { answerQuestion, stopBrowserAgent, useBrowserAgentStore } from "./agent-store";
+import { useBrowserStore } from "../store";
+import { IDLE_AGENT, answerQuestion, stopBrowserAgent, useBrowserAgentStore } from "./agent-store";
-/** Above the agent's tab while it works: what it is doing, its questions, and Stop. */
+/** Above the agent's tab while it works in the chat on screen: what it is doing, its questions, and Stop. */
export function AgentBar({ activeTabId }: { activeTabId: string | null }) {
const t = useT();
- const tabId = useBrowserAgentStore((state) => state.tabId);
+ const chat = useBrowserStore((state) => state.chat);
+ const { tabId, working, runKey, stoppedRun, question } = useBrowserAgentStore((state) => state.chats[chat] ?? IDLE_AGENT);
// The whole run, not just each action: Stop between calls stops the next one before it starts.
- const running = useBrowserAgentStore((state) => state.working || (!!state.runKey && state.stoppedRun !== state.runKey));
- const question = useBrowserAgentStore((state) => state.question);
+ const running = working || (!!runKey && stoppedRun !== runKey);
const [readId, setReadId] = useState<number | null>(null);
const script = question?.ask === "script";
if (!question && !(running && tabId === activeTabId)) return null;
@@ -51,7 +52,7 @@ export function AgentBar({ activeTabId }: { activeTabId: string | null }) {
</Button>
</>
) : null}
- <Button type="button" size="xs" variant="outline" onClick={stopBrowserAgent}>
+ <Button type="button" size="xs" variant="outline" onClick={() => stopBrowserAgent(chat)}>
{t("browser.agent.stop")}
</Button>
</div>
diff --git a/studio/frontend/src/features/browser/agent/agent-store.ts b/studio/frontend/src/features/browser/agent/agent-store.ts
index 9fb1d015739..6688db63b55 100644
--- a/studio/frontend/src/features/browser/agent/agent-store.ts
+++ b/studio/frontend/src/features/browser/agent/agent-store.ts
@@ -18,6 +18,22 @@ import {
export type AgentPrompt = Question & { id: number };
+/** One chat's use of the browser tools; chats run theirs side by side, each in its own tab. */
+export type AgentChat = {
+ /** The tab the agent drives in this chat; the model can't reach any other. */
+ tabId: string | null;
+ working: boolean;
+ question: AgentPrompt | null;
+ /** The chat run the agent is acting for, until it ends; Stop applies to it. */
+ runKey: string | null;
+ /** The chat run the user stopped from the agent bar; its actions answer "stopped". */
+ stoppedRun: string | null;
+ /** When its last run ended (ms), for downloads that land after it. */
+ endedAt: number;
+};
+
+export const IDLE_AGENT: AgentChat = { tabId: null, working: false, question: null, runKey: null, stoppedRun: null, endedAt: 0 };
+
type BrowserAgentState = {
/** The composer's Browser tool: offer the model the browser tools. */
enabled: boolean;
@@ -29,14 +45,8 @@ type BrowserAgentState = {
setHistoryAccess: (access: HistoryAccess) => void;
scriptAccess: ScriptAccess;
setScriptAccess: (access: ScriptAccess) => void;
- /** The tab the agent drives; the model can't reach any other. */
- tabId: string | null;
- working: boolean;
- question: AgentPrompt | null;
- /** The chat run the agent is acting for, until it ends; Stop applies to it. */
- runKey: string | null;
- /** The chat run the user stopped from the agent bar; its actions answer "stopped". */
- stoppedRun: string | null;
+ /** By chat key (browser store `chat`). */
+ chats: Record<string, AgentChat>;
};
export const useBrowserAgentStore = create<BrowserAgentState>()(
@@ -59,11 +69,7 @@ export const useBrowserAgentStore = create<BrowserAgentState>()(
setHistoryAccess: (historyAccess) => set({ historyAccess }),
scriptAccess: "ask",
setScriptAccess: (scriptAccess) => set({ scriptAccess }),
- tabId: null,
- working: false,
- question: null,
- runKey: null,
- stoppedRun: null,
+ chats: {},
}),
{
name: "unsloth_browser_agent",
@@ -91,17 +97,52 @@ export const useBrowserAgentStore = create<BrowserAgentState>()(
),
);
+// A new chat's key -> its saved one, so a run that captured the old key still reaches its chat.
+const adopted = new Map<string, string>();
+
+export function liveChat(chat: string): string {
+ let key = chat;
+ for (let hops = 0; hops < 8 && adopted.has(key); hops++) key = adopted.get(key) ?? key;
+ return key;
+}
+
+export function agentChat(chat: string): AgentChat {
+ return useBrowserAgentStore.getState().chats[liveChat(chat)] ?? IDLE_AGENT;
+}
+
+export function patchAgentChat(chat: string, patch: Partial<AgentChat>): void {
+ const key = liveChat(chat);
+ useBrowserAgentStore.setState((state) => ({
+ chats: { ...state.chats, [key]: { ...(state.chats[key] ?? IDLE_AGENT), ...patch } },
+ }));
+}
+
+/** A new chat saved under its id keeps its run, tab, pending question and the action in flight. */
+export function adoptAgentChat(from: string, to: string): void {
+ if (from === to) return;
+ adopted.set(from, to);
+ const action = activeActions.get(from);
+ activeActions.delete(from);
+ if (action && !activeActions.has(to)) activeActions.set(to, action);
+ useBrowserAgentStore.setState((state) => {
+ const moved = state.chats[from];
+ if (!moved) return state;
+ const { [from]: _moved, ...chats } = state.chats;
+ return { chats: chats[to] ? chats : { ...chats, [to]: moved } };
+ });
+}
+
const answers = new Map<number, (allow: boolean) => void>();
let nextQuestion = 1;
-/** Ask the user in the agent bar; false when they decline or the run ends first. */
-export function askUser(question: Question, signal?: AbortSignal): Promise<boolean> {
+/** Ask the user in `chat`'s agent bar; false when they decline or the run ends first. */
+export function askUser(chat: string, question: Question, signal?: AbortSignal): Promise<boolean> {
return new Promise((resolve) => {
const id = nextQuestion++;
const finish = (allow: boolean) => {
answers.delete(id);
signal?.removeEventListener("abort", onAbort);
- if (useBrowserAgentStore.getState().question?.id === id) useBrowserAgentStore.setState({ question: null });
+ if (agentChat(chat).question?.id === id) patchAgentChat(chat, { question: null });
resolve(allow);
};
const onAbort = () => finish(false);
@@ -110,11 +151,11 @@ export function askUser(question: Question, signal?: AbortSignal): Promise<boole
return;
}
signal?.addEventListener("abort", onAbort, { once: true });
- // One at a time: actions run in order, so an older question can only be a leftover.
- const current = useBrowserAgentStore.getState().question;
+ // One at a time per chat: its actions run in order, so an older question can only be a leftover.
+ const current = agentChat(chat).question;
if (current) answers.get(current.id)?.(false);
answers.set(id, finish);
- useBrowserAgentStore.setState({ question: { ...question, id } });
+ patchAgentChat(chat, { question: { ...question, id } });
});
}
@@ -144,35 +185,45 @@ function creator(url: string): string {
* file's own host only when it has a rule of its own, and then the stricter one wins. */
export function agentDownloadRule(tabId: string, site: string, fileUrl: string): DownloadsRule | null {
const state = useBrowserAgentStore.getState();
+ const agent = Object.values(state.chats).find((chat) => chat.tabId === tabId);
// Between runs the tab is the user's again, under their own download settings.
- if (state.tabId !== tabId || (!state.runKey && Date.now() - runEndedAt > DOWNLOAD_GRACE_MS)) return null;
+ if (!agent || (!agent.runKey && Date.now() - agent.endedAt > DOWNLOAD_GRACE_MS)) return null;
const rule = ruleFor(creator(site || fileUrl), state.permissions).rule.downloads;
const file = ruleFor(creator(fileUrl), state.permissions);
if (!file.site) return rule;
return DOWNLOAD_STRICTNESS[file.rule.downloads] > DOWNLOAD_STRICTNESS[rule] ? file.rule.downloads : rule;
}
-let activeAction: AbortController | null = null;
+const activeActions = new Map<string, AbortController>();
-export function setActiveAction(controller: AbortController | null): void {
- activeAction = controller;
+export function setActiveAction(chat: string, controller: AbortController | null): void {
+ if (controller) activeActions.set(liveChat(chat), controller);
+ else activeActions.delete(liveChat(chat));
}
// A download the agent started can be reported after its run ends (a slow response, a native
// view's prompt), so the agent's rule outlasts the run by this much.
const DOWNLOAD_GRACE_MS = 120_000;
-let runEndedAt = 0;
+
+const runEndListeners = new Set<(chat: string) => void>();
+
+export function onBrowserAgentRunEnd(listener: (chat: string) => void): () => void {
+ runEndListeners.add(listener);
+ return () => runEndListeners.delete(listener);
+}
export function endBrowserAgentRun(runKey: string): void {
- if (useBrowserAgentStore.getState().runKey !== runKey) return;
- runEndedAt = Date.now();
- useBrowserAgentStore.setState({ runKey: null });
+ for (const [chat, agent] of Object.entries(useBrowserAgentStore.getState().chats)) {
+ if (agent.runKey !== runKey) continue;
+ patchAgentChat(chat, { runKey: null, endedAt: Date.now() });
+ for (const listener of runEndListeners) listener(chat);
+ }
}
/** The agent bar's Stop: the action running now ends too, not just the ones after it. */
-export function stopBrowserAgent(): void {
- const { runKey, question } = useBrowserAgentStore.getState();
- useBrowserAgentStore.setState({ stoppedRun: runKey, working: false });
+export function stopBrowserAgent(chat: string): void {
+ const { runKey, question } = agentChat(chat);
+ patchAgentChat(chat, { stoppedRun: runKey, working: false });
if (question) answers.get(question.id)?.(false);
- activeAction?.abort();
+ activeActions.get(liveChat(chat))?.abort();
}
diff --git a/studio/frontend/src/features/browser/agent/executor.ts b/studio/frontend/src/features/browser/agent/executor.ts
index c508626446f..24e74a29ad0 100644
--- a/studio/frontend/src/features/browser/agent/executor.ts
+++ b/studio/frontend/src/features/browser/agent/executor.ts
@@ -5,15 +5,18 @@
* each result back. Every action goes through the gates in gate.ts first. */
import { authFetch } from "@/features/auth";
+import { openChatItemById, useChatNavigationStore, visibleChatItems } from "@/features/chat";
+import { getLocale, translate } from "@/i18n";
+import { toast } from "@/lib/toast";
import { isTauri } from "@/lib/api-base";
-import { screenshotPage } from "../capture";
+import { captureHiddenPage, screenshotPage } from "../capture";
import { useBrowserHistoryStore } from "../history-store";
-import { hasNativeView, nativeAction } from "../native-view";
+import { hasNativeView, keepInBackground, nativeAction, releaseBackground, whenViewOpen } from "../native-view";
import { callNative, useNativeBrowser } from "../native-support";
import { requestFrameAgent } from "../page-frame";
import { browserPanelAvailable } from "../panel-availability";
-import { type BrowserTab, currentEntry, useBrowserStore } from "../store";
-import { askUser, setActiveAction, useBrowserAgentStore } from "./agent-store";
+import { type BrowserTab, chatKeyOfThread, currentEntry, onChatAdopted, useBrowserStore } from "../store";
+import { adoptAgentChat, agentChat, askUser, liveChat, onBrowserAgentRunEnd, patchAgentChat, setActiveAction, useBrowserAgentStore } from "./agent-store";
import { type Gate, type Question, type SitePolicy, gateElement, gateNavigation, hostOfUrl, navigationBlocks } from "./gate";
import { type SiteRule, ruleFor } from "./permissions";
import { type AgentAnswer, type ElementInfo, pageAgentSource } from "./page-agent";
@@ -44,8 +47,18 @@ export type BrowserAction = {
policy: SitePolicy;
};
-/** `scope`: the chat the run belongs to; sites approved in one chat aren't approved in another. */
-export type ActionContext = { runKey: string; scope?: string; userText: string; fullAccess?: boolean; signal?: AbortSignal };
+/** `scope`: the chat the run belongs to; sites approved in one chat aren't approved in another.
+ * `chat`: its browser chat when that isn't the thread's own (a compare pane's pair). */
+export type ActionContext = {
+ runKey: string;
+ scope?: string;
+ chat?: string;
+ userText: string;
+ fullAccess?: boolean;
+ signal?: AbortSignal;
+};
+
+type Run = ActionContext & { session: Session };
type Result = { ok: boolean; error?: string; url?: string; title?: string; text?: string; note?: string; image?: string };
@@ -99,62 +112,101 @@ export function parseBrowserAction(value: unknown): BrowserAction | null {
};
}
-// Sites the user approved or named, where the model may open addresses it made up; every site the
-// agent has been on (a page's link reaches its site, but the page chose it, not the user); and the
-// links on the page the agent last read. All per chat.
-const knownHosts = new Set<string>();
-const reachedHosts = new Set<string>();
-const opaqueHosts = new Set<string>();
-let pageLinks = new Set<string>();
-let scope: string | undefined;
-// Where the agent's own moves in this chat landed: a native view's Back or Forward doesn't say where
-// it goes, so this is how it is checked before it loads.
-let trail: string[] = [];
-let at = -1;
-
-function enterScope(next: string | undefined): void {
- if (next === scope) return;
- knownHosts.clear();
- reachedHosts.clear();
- opaqueHosts.clear();
- pageLinks = new Set();
- trail = [];
- at = -1;
- // Another chat never saw this chat's screenshots: its x and y are CSS pixels until it takes one.
- pointScale.clear();
- scope = next;
+type Session = {
+ chat: string;
+ // Sites the user approved or named, where the model may open addresses it made up; every site the
+ // agent has been on (a page's link reaches its site, but the page chose it, not the user); and the
+ // links on the page the agent last read.
+ knownHosts: Set<string>;
+ reachedHosts: Set<string>;
+ opaqueHosts: Set<string>;
+ pageLinks: Set<string>;
+ // Where the agent's own moves landed: a native view's Back or Forward doesn't say where it goes,
+ // so this is how it is checked before it loads.
+ trail: string[];
+ at: number;
+ // A chat never saw another's screenshots: its x and y are CSS pixels until it takes one.
+ pointScale: Map<string, ShotView & { scaleX: number; scaleY: number }>;
+ queue: Promise<void>;
+};
+
+const sessions = new Map<string, Session>();
+const MAX_SESSIONS = 32;
+
+function sessionFor(chat: string): Session {
+ let session = sessions.get(chat);
+ if (!session) {
+ session = {
+ chat,
+ knownHosts: new Set(),
+ reachedHosts: new Set(),
+ opaqueHosts: new Set(),
+ pageLinks: new Set(),
+ trail: [],
+ at: -1,
+ pointScale: new Map(),
+ queue: Promise.resolve(),
+ };
+ sessions.set(chat, session);
+ for (const old of sessions.keys()) {
+ if (sessions.size <= MAX_SESSIONS) break;
+ if (old !== chat && !agentChat(old).runKey) sessions.delete(old);
+ }
+ }
+ return session;
+}
+
+function chatOf(context: ActionContext): string {
+ if (context.chat) return liveChat(context.chat);
+ const scope = context.scope;
+ return liveChat(scope && scope !== "__default" ? chatKeyOfThread(scope) : useBrowserStore.getState().chat);
}
-function record(op: string, url: string): void {
- if (op === "back" && trail[at - 1] === url) at -= 1;
- else if (op === "forward" && trail[at + 1] === url) at += 1;
+onChatAdopted((from, to) => {
+ const session = sessions.get(from);
+ sessions.delete(from);
+ if (session && !sessions.has(to)) sessions.set(to, Object.assign(session, { chat: to }));
+ adoptAgentChat(from, to);
+});
+
+// A chat's page stays open in the background only while its run lasts.
+onBrowserAgentRunEnd((chat) => {
+ const tabId = agentChat(chat).tabId;
+ if (tabId) releaseBackground(tabId);
+});
+
+function record(session: Session, op: string, url: string): void {
+ const { trail, at } = session;
+ if (op === "back" && trail[at - 1] === url) session.at -= 1;
+ else if (op === "forward" && trail[at + 1] === url) session.at += 1;
else if (op !== "reload" || trail[at] !== url) {
- trail = [...trail.slice(0, at + 1), url];
- at = trail.length - 1;
+ session.trail = [...trail.slice(0, at + 1), url];
+ session.at = session.trail.length - 1;
}
}
-function admit(url: string, gate: Gate, context: ActionContext): void {
+function admit(url: string, gate: Gate, context: Run): void {
if (context.fullAccess === true) return;
const host = hostOfUrl(url);
if (!host) return;
- reachedHosts.add(host);
- if (!(gate.kind === "allow" && gate.linked)) knownHosts.add(host);
+ context.session.reachedHosts.add(host);
+ if (!(gate.kind === "allow" && gate.linked)) context.session.knownHosts.add(host);
}
const TEXT_ROLES = new Set(["textbox", "searchbox", "password", "combobox", "spinbutton"]);
-let queue: Promise<void> = Promise.resolve();
// A replayed stream frame must not click twice.
const seen = new Set<string>();
-/** Runs actions one at a time, in the order the model asked for them. */
-export function runBrowserAction(action: BrowserAction, context: ActionContext): Promise<void> {
- if (seen.has(action.id)) return queue;
+export function runBrowserAction(action: BrowserAction, actionContext: ActionContext): Promise<void> {
+ const session = sessionFor(chatOf(actionContext));
+ const context: Run = { ...actionContext, session };
+ const { chat } = session;
+ if (seen.has(action.id)) return session.queue;
seen.add(action.id);
// The server's wait starts when it sends the action, not when an earlier one lets this one run.
const expires = action.timeoutS ? Date.now() + (action.timeoutS - 2) * 1000 : null;
- const next = queue.then(async () => {
+ const next = session.queue.then(async () => {
// Ends with the chat run, with Stop, or when the server stops waiting: a question still open
// then must not be answerable, or a late Allow would act for nobody.
const controller = new AbortController();
@@ -169,7 +221,7 @@ export function runBrowserAction(action: BrowserAction, context: ActionContext):
};
if (expires !== null && expires <= Date.now()) expire();
const deadline = expires !== null ? setTimeout(expire, Math.max(0, expires - Date.now())) : null;
- setActiveAction(controller);
+ setActiveAction(chat, controller);
let result: Result;
try {
result = await perform(action, { ...context, signal: controller.signal });
@@ -178,14 +230,14 @@ export function runBrowserAction(action: BrowserAction, context: ActionContext):
} finally {
if (deadline) clearTimeout(deadline);
context.signal?.removeEventListener("abort", abort);
- setActiveAction(null);
- useBrowserAgentStore.setState({ working: false });
+ setActiveAction(chat, null);
+ patchAgentChat(chat, { working: false });
}
if (expired && !result.ok) result = { ok: false, error: EXPIRED };
- if (useBrowserAgentStore.getState().stoppedRun === context.runKey) result = { ok: false, error: STOPPED };
+ if (agentChat(chat).stoppedRun === context.runKey) result = { ok: false, error: STOPPED };
await postResult(action, result);
});
- queue = next.catch(() => undefined);
+ session.queue = next.catch(() => undefined);
return next;
}
@@ -213,8 +265,13 @@ const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
const findTab = (tabId: string) => useBrowserStore.getState().tabs.find((candidate) => candidate.id === tabId);
-function agentTab(): BrowserTab | null {
- return findTab(useBrowserAgentStore.getState().tabId ?? "") ?? null;
+function agentTab(context: Run): BrowserTab | null {
+ return findTab(agentChat(context.session.chat).tabId ?? "") ?? null;
+}
+
+/** The run's chat is on screen with the panel beside it, so the user can watch and answer there. */
+function chatOnScreen(context: Run): boolean {
+ return context.session.chat === useBrowserStore.getState().chat && browserPanelAvailable();
}
function tabUrl(tab: BrowserTab): string {
@@ -222,11 +279,15 @@ function tabUrl(tab: BrowserTab): string {
return entry.kind === "web" ? (tab.displayUrl ?? entry.url) : "";
}
-/** The agent's tab in front, so the user sees what it does and its page is mounted. */
-function showTab(tab: BrowserTab): void {
+/** The agent's tab in front when its chat is on screen, so the user sees what it does; else its
+ * page works on in the background, with the user's own tabs left where they were. */
+function showTab(tab: BrowserTab, context: Run): void {
+ // Kept from the start: the user can switch chats mid-run, and closing the panel then must leave it.
+ if (useNativeBrowser.getState().enabled) keepInBackground(tab.id);
+ // Off screen it shows when the user goes back to that chat; their tabs here stay put. A chat on
+ // screen without room for the panel isn't given one.
const store = useBrowserStore.getState();
- if (store.activeTabId !== tab.id) store.activateTab(tab.id);
- if (!store.open) store.openPanel();
+ if (context.session.chat !== store.chat || browserPanelAvailable()) store.revealIn(context.session.chat, tab.id);
}
async function waitForLoad(tabId: string, signal?: AbortSignal): Promise<BrowserTab | null> {
@@ -398,11 +459,11 @@ async function untilFilled(tab: BrowserTab, since: number, signal?: AbortSignal)
return (await waitOnPage(tab.id, (now) => currentEntry(now) !== entry, since, FILL_QUIET_MAX_MS, signal, FILL_IDLE_QUIET_MS)) === "loading";
}
-async function pass(gate: Gate, signal?: AbortSignal): Promise<string | null> {
+async function pass(gate: Gate, context: Run): Promise<string | null> {
if (gate.kind === "allow") return null;
if (gate.kind === "block") return `Not done: ${gate.reason}.`;
const { ask, host, name, detail } = gate;
- const allowed = await askInPanel({ ask, host, name, detail }, signal);
+ const allowed = await askInPanel({ ask, host, name, detail }, context);
// Not gate.prompt: it quotes the page's label, and errors reach the model outside the fence.
return allowed ? null : `The user declined ${DECLINED[ask]} on ${host || "this page"}. Do something else, or ask them.`;
}
@@ -417,14 +478,30 @@ const DECLINED: Record<Question["ask"], string> = {
pointer: "clicking where the page doesn't say what is there",
};
-/** Questions show in the panel's agent bar, so the panel has to be on screen to ask. */
-function askInPanel(question: Question, signal?: AbortSignal): Promise<boolean> {
+/** A question waits in a chat that isn't on screen: say so, with a way there. One toast per chat. */
+function notifyOffScreenQuestion(chat: string): void {
+ // A single chat's key is `single:<thread>`, a compare's `compare:<pair>`; the sidebar row has that id.
+ const [type, id] = [chat.slice(0, chat.indexOf(":")), chat.slice(chat.indexOf(":") + 1)];
+ const item =
+ visibleChatItems(useChatNavigationStore.getState()).find((candidate) => candidate.id === id && candidate.type === type) ?? null;
+ toast(translate("browser.agent.waiting", { chat: item?.title || translate("browser.agent.otherChat", undefined, getLocale()) }, getLocale()), {
+ id: `browser-agent-question:${chat}`,
+ action: item ? { label: translate("browser.agent.show", undefined, getLocale()), onClick: () => openChatItemById(item) } : undefined,
+ });
+}
+
+/** Questions show in the chat's agent bar: the panel opens for one in the chat on screen, and one
+ * from a chat off screen waits there, with a toast to go answer it. */
+function askInPanel(question: Question, context: Run): Promise<boolean> {
+ const tabId = agentChat(context.session.chat).tabId;
const store = useBrowserStore.getState();
- if (!store.open) store.openPanel();
- return askUser(question, signal);
+ if (tabId) store.revealIn(context.session.chat, tabId);
+ else if (chatOnScreen(context) && !store.open) store.openPanel();
+ if (!chatOnScreen(context)) notifyOffScreenQuestion(context.session.chat);
+ return askUser(context.session.chat, question, context.signal);
}
-async function observe(tab: BrowserTab, context: ActionContext, note?: string, op: Record<string, unknown> = { op: "snapshot" }): Promise<Result> {
+async function observe(tab: BrowserTab, context: Run, note?: string, op: Record<string, unknown> = { op: "snapshot" }): Promise<Result> {
const entry = currentEntry(tab);
if (entry.kind !== "web") return { ok: false, error: "No web page is open. Use browser_navigate first." };
if (tab.pageError || tab.nativeError) {
@@ -441,7 +518,7 @@ async function observe(tab: BrowserTab, context: ActionContext, note?: string, o
if (fresh.loading || tabUrl(fresh) !== tabUrl(tab)) {
return { ok: false, error: "the page changed while it was being read; take a snapshot again." };
}
- if (op.op === "snapshot" || op.op === "find") pageLinks = new Set(context.fullAccess === true ? [] : answer.links ?? []);
+ if (op.op === "snapshot" || op.op === "find") context.session.pageLinks = new Set(context.fullAccess === true ? [] : answer.links ?? []);
return {
ok: true,
url: tabUrl(fresh),
@@ -451,24 +528,28 @@ async function observe(tab: BrowserTab, context: ActionContext, note?: string, o
};
}
-async function open(url: string, signal?: AbortSignal): Promise<BrowserTab | null> {
+async function open(url: string, context: Run): Promise<BrowserTab | null> {
const store = useBrowserStore.getState();
- const existing = agentTab();
+ const existing = agentTab(context);
if (existing) {
store.navigate(existing.id, { url }, { replace: false });
- showTab(existing);
- return waitForLoad(existing.id, signal);
+ showTab(existing, context);
+ return waitForLoad(existing.id, context.signal);
}
- store.openUrl(url, { newTab: true });
- const tabId = useBrowserStore.getState().activeTabId;
+ // In its own chat's tabs: a chat off screen gets a tab there, and the panel on screen stays put.
+ const tabId = store.openTabIn(context.session.chat, url);
if (!tabId) return null;
- useBrowserAgentStore.setState({ tabId });
- trail = [];
- at = -1;
- return waitForLoad(tabId, signal);
+ // Off screen the view opens a beat later: loading until it reports in, so the wait below doesn't read the blank view.
+ if (!chatOnScreen(context)) store.updateTab(tabId, { loading: true });
+ patchAgentChat(context.session.chat, { tabId });
+ context.session.trail = [];
+ context.session.at = -1;
+ const tab = findTab(tabId);
+ if (tab) showTab(tab, context);
+ return waitForLoad(tabId, context.signal);
}
-function permissions(context: ActionContext) {
+function permissions(context: Pick<ActionContext, "fullAccess">) {
const saved = useBrowserAgentStore.getState().permissions;
if (!context.fullAccess) return saved;
const allow = (rule: SiteRule): SiteRule => ({
@@ -480,9 +561,10 @@ function permissions(context: ActionContext) {
return { defaults: allow(saved.defaults), sites: Object.fromEntries(Object.entries(saved.sites).map(([site, rule]) => [site, allow(rule)])) };
}
-function navigationContext(action: BrowserAction, context: ActionContext) {
- const tab = agentTab();
+function navigationContext(action: BrowserAction, context: Run) {
+ const tab = agentTab(context);
const currentUrl = tab ? tabUrl(tab) : "";
+ const { reachedHosts, knownHosts, pageLinks } = context.session;
return {
policy: action.policy,
permissions: permissions(context),
@@ -493,28 +575,35 @@ function navigationContext(action: BrowserAction, context: ActionContext) {
};
}
-async function perform(action: BrowserAction, context: ActionContext): Promise<Result> {
- const { signal } = context;
- if (useBrowserAgentStore.getState().stoppedRun === context.runKey) return { ok: false, error: STOPPED };
+async function perform(action: BrowserAction, context: Run): Promise<Result> {
+ const { signal, session } = context;
+ const { chat, reachedHosts } = session;
+ if (agentChat(chat).stoppedRun === context.runKey) return { ok: false, error: STOPPED };
if (signal?.aborted) return { ok: false, error: "the action expired before it could run." };
- if (!browserPanelAvailable()) {
- return { ok: false, error: "the browser panel can't be shown in this window; ask the user to widen it or open the chat." };
+ // Off screen, only a native page can work on: a framed one runs in the panel.
+ if (!chatOnScreen(context) && !useNativeBrowser.getState().enabled) {
+ return {
+ ok: false,
+ error:
+ chat === useBrowserStore.getState().chat
+ ? "the browser panel can't be shown in this window; ask the user to widen it or open the chat."
+ : "this chat's browser only runs while the chat is open here; ask the user to switch back to it.",
+ };
}
- const owner = useBrowserAgentStore.getState().runKey;
+ const owner = agentChat(chat).runKey;
if (owner && owner !== context.runKey) {
- return { ok: false, error: "Another chat is using the browser right now. Wait for it to finish, or tell the user." };
+ return { ok: false, error: "Another reply in this chat is using the browser right now. Wait for it to finish, or tell the user." };
}
- useBrowserAgentStore.setState({ working: true, runKey: context.runKey });
- enterScope(context.scope);
+ patchAgentChat(chat, { working: true, runKey: context.runKey });
if (action.op === "navigate") {
const url = action.url ?? "";
const gate = gateNavigation(url, navigationContext(action, context));
- const refused = await pass(gate, signal);
+ const refused = await pass(gate, context);
if (refused) return { ok: false, error: refused };
admit(url, gate, context);
const since = Date.now();
- const tab = await open(url, signal);
+ const tab = await open(url, context);
if (!tab) return { ok: false, error: "the browser tab closed." };
return arrive(tab, url, action, context, since);
}
@@ -524,9 +613,15 @@ async function perform(action: BrowserAction, context: ActionContext): Promise<R
return { ok: false, error: SCRIPTS_OFF };
}
- const tab = agentTab();
- if (!tab) return { ok: false, error: "No page is open yet. Call browser_navigate first." };
- showTab(tab);
+ const shown = agentTab(context);
+ if (!shown) return { ok: false, error: "No page is open yet. Call browser_navigate first." };
+ showTab(shown, context);
+ // A kept page the cap closed reopens a beat later; its history flags are only good once it has.
+ if (useNativeBrowser.getState().enabled && currentEntry(shown).kind === "web" && !hasNativeView(shown.id)) {
+ await whenViewOpen(shown.id);
+ }
+ const tab = findTab(shown.id);
+ if (!tab) return { ok: false, error: "the browser tab closed." };
const before = tabUrl(tab);
if (action.op === "back" || action.op === "forward" || action.op === "reload") {
@@ -539,7 +634,7 @@ async function perform(action: BrowserAction, context: ActionContext): Promise<R
action.op === "reload"
? before
: nativeMove
- ? (trail[at] === before && trail[at + step]) || ""
+ ? (session.trail[session.at] === before && session.trail[session.at + step]) || ""
: entry?.kind === "web"
? entry.url
: "";
@@ -550,13 +645,13 @@ async function perform(action: BrowserAction, context: ActionContext): Promise<R
: hostOfUrl(destination) === hostOfUrl(before) && reachedHosts.has(hostOfUrl(before))
? { kind: "allow" }
: gateNavigation(destination, navigationContext(action, context));
- const refused = await pass(gate, signal);
+ const refused = await pass(gate, context);
if (refused) return { ok: false, error: refused };
admit(destination, gate, context);
} else if (nativeMove) {
// History from before this chat, or from the user browsing in the tab: where it leads is unknown.
const where = action.op === "back" ? "the previous page" : "the next page";
- const refused = await pass({ kind: "ask", ask: "open", host: where, name: "", detail: "", prompt: `Open ${where}?` }, signal);
+ const refused = await pass({ kind: "ask", ask: "open", host: where, name: "", detail: "", prompt: `Open ${where}?` }, context);
if (refused) return { ok: false, error: refused };
}
const since = Date.now();
@@ -594,7 +689,7 @@ async function perform(action: BrowserAction, context: ActionContext): Promise<R
return observe(after, context, moved.note);
}
case "screenshot":
- return screenshot(ready);
+ return screenshot(ready, context);
case "move":
return moveTo(ready, action, context, here);
case "drag":
@@ -605,7 +700,7 @@ async function perform(action: BrowserAction, context: ActionContext): Promise<R
// Acting on an element: what it is decides whether the user is asked first.
const ref = action.ref ?? "";
- const point = action.op === "click" ? await pagePoint(ready, action.x, action.y) : null;
+ const point = action.op === "click" ? await pagePoint(ready, session, action.x, action.y) : null;
if (typeof point === "string") return { ok: false, error: point };
const described = await pageCall(ready, {
op: "describe",
@@ -662,11 +757,11 @@ async function perform(action: BrowserAction, context: ActionContext): Promise<R
if (formGate) gates.push(formGate);
}
for (const gate of gates) {
- const refused = await pass(gate, signal);
+ const refused = await pass(gate, context);
if (refused) return { ok: false, error: refused };
}
if (signal?.aborted) return { ok: false, error: STOPPED };
- if (opaque) opaqueHosts.add(opaque.host);
+ if (opaque) session.opaqueHosts.add(opaque.host);
// A point's control can be relabelled or rewired around the same leaf while the user decides.
if (point && gates.some((gate) => gate.kind === "ask")) {
if (!(await unchanged(ready, point, described.hit ?? "", JSON.stringify(info)))) {
@@ -692,7 +787,7 @@ async function perform(action: BrowserAction, context: ActionContext): Promise<R
// A link: opened in this tab, under the same rules as any other address. Its href is read
// now, so it may not be one the last snapshot listed.
const gate = gateNavigation(clicked.navigate, navigationContext(action, context));
- const refused = await pass(gate, signal);
+ const refused = await pass(gate, context);
if (refused) return { ok: false, error: refused };
admit(clicked.navigate, gate, context);
const sent = Date.now();
@@ -728,7 +823,7 @@ async function afterward(
tab: BrowserTab,
before: string,
action: BrowserAction,
- context: ActionContext,
+ context: Run,
since: number,
note?: string,
): Promise<Result> {
@@ -749,12 +844,11 @@ type ShotView = {
scrolls?: number;
doc?: string;
};
-const pointScale = new Map<string, ShotView & { scaleX: number; scaleY: number }>();
const MAX_SHOT_EDGE = 1024;
-async function pagePoint(tab: BrowserTab, x?: number, y?: number): Promise<{ x: number; y: number } | string | null> {
+async function pagePoint(tab: BrowserTab, session: Session, x?: number, y?: number): Promise<{ x: number; y: number } | string | null> {
if (x === undefined || y === undefined) return null;
- const shot = pointScale.get(tab.id);
+ const shot = session.pointScale.get(tab.id);
if (!shot) return { x, y };
if (tabUrl(tab) !== shot.url) return STALE_SHOT;
const status = await pageCall(tab, { op: "status" });
@@ -767,12 +861,13 @@ const STALE_SHOT = "the page changed since the screenshot those x and y come fro
async function onScreen(
tab: BrowserTab,
+ session: Session,
ref: string | undefined,
x?: number,
y?: number,
scroll = true,
): Promise<{ x: number; y: number } | string> {
- const point = await pagePoint(tab, x, y);
+ const point = await pagePoint(tab, session, x, y);
if (point) return point;
const found = await pageCall(tab, { op: "locate", ref: ref ?? "", scroll });
if (!found.ok) return found.error ?? `${ref} could not be found.`;
@@ -780,8 +875,8 @@ async function onScreen(
return { x: found.x, y: found.y };
}
-async function moveTo(tab: BrowserTab, action: BrowserAction, context: ActionContext, before: string): Promise<Result> {
- const at = await onScreen(tab, action.ref, action.x, action.y);
+async function moveTo(tab: BrowserTab, action: BrowserAction, context: Run, before: string): Promise<Result> {
+ const at = await onScreen(tab, context.session, action.ref, action.x, action.y);
if (typeof at === "string") return { ok: false, error: at };
const since = Date.now();
const moved = await pageCall(tab, { op: "pointer", phase: "move", ...at });
@@ -793,14 +888,14 @@ const DRAG_STEP_PX = 20;
const DRAG_FRAME_MS = 16;
/** browser_drag: both ends are asked about as clicks, since a drop can delete, send or buy too. */
-async function dragBetween(tab: BrowserTab, action: BrowserAction, context: ActionContext, before: string): Promise<Result> {
+async function dragBetween(tab: BrowserTab, action: BrowserAction, context: Run, before: string): Promise<Result> {
const { signal } = context;
const scroll = Boolean(action.ref && action.toRef);
- const start = await onScreen(tab, action.ref, action.x, action.y, scroll);
+ const start = await onScreen(tab, context.session, action.ref, action.x, action.y, scroll);
if (typeof start === "string") return { ok: false, error: start };
- const end = await onScreen(tab, action.toRef, action.toX, action.toY, scroll);
+ const end = await onScreen(tab, context.session, action.toRef, action.toX, action.toY, scroll);
if (typeof end === "string") return { ok: false, error: end };
- const from = scroll ? await onScreen(tab, action.ref, undefined, undefined, false) : start;
+ const from = scroll ? await onScreen(tab, context.session, action.ref, undefined, undefined, false) : start;
if (typeof from === "string") {
return { ok: false, error: "the start and end don't fit on screen together; drag by x and y, or in shorter steps." };
}
@@ -815,9 +910,9 @@ async function dragBetween(tab: BrowserTab, action: BrowserAction, context: Acti
for (const gate of [gateElement("click", described.info, before, action.policy, permissions(context)), opaqueGate(described.info, before, context)]) {
if (!gate) continue;
asked ||= gate.kind === "ask";
- const refused = await pass(gate, signal);
+ const refused = await pass(gate, context);
if (refused) return { ok: false, error: refused };
- if (gate.kind === "ask" && gate.ask === "pointer") opaqueHosts.add(gate.host);
+ if (gate.kind === "ask" && gate.ask === "pointer") context.session.opaqueHosts.add(gate.host);
}
hits.push(described.hit ?? "");
sigs.push(described.sig ?? "");
@@ -870,9 +965,9 @@ async function dragBetween(tab: BrowserTab, action: BrowserAction, context: Acti
if (target.hit !== hits[1] || JSON.stringify(target.info) !== infos[1]) {
for (const gate of [gateElement("click", target.info, before, action.policy, permissions(context)), opaqueGate(target.info, before, context)]) {
if (!gate) continue;
- const refused = await pass(gate, signal);
+ const refused = await pass(gate, context);
if (refused) return cancel(refused);
- if (gate.kind === "ask" && gate.ask === "pointer") opaqueHosts.add(gate.host);
+ if (gate.kind === "ask" && gate.ask === "pointer") context.session.opaqueHosts.add(gate.host);
}
const again = await pageCall(tab, { op: "describe", ref: "", ...end }).catch(() => null);
if (!sameTab() || again?.doc !== doc) return left(again?.doc);
@@ -885,11 +980,11 @@ async function dragBetween(tab: BrowserTab, action: BrowserAction, context: Acti
}
/** An unlabelled point (a canvas) may be a Buy button nothing can read: ask once per site per chat. */
-function opaqueGate(info: ElementInfo, pageUrl: string, context: ActionContext): (Gate & { kind: "ask" }) | null {
+function opaqueGate(info: ElementInfo, pageUrl: string, context: Run): (Gate & { kind: "ask" }) | null {
if (info.role !== "element" || info.name.trim() || info.href) return null;
const host = hostOfUrl(pageUrl);
const rule = ruleFor(pageUrl, permissions(context)).rule;
- if (!host || opaqueHosts.has(host) || rule.actions === "allow" || rule.unlabelled === "allow") return null;
+ if (!host || context.session.opaqueHosts.has(host) || rule.actions === "allow" || rule.unlabelled === "allow") return null;
return { kind: "ask", ask: "pointer", host, name: "", detail: info.tag, prompt: `Click unlabelled parts of ${host}?` };
}
@@ -904,14 +999,14 @@ const SCRIPT_WAIT_MS = 15_000;
const SCRIPTS_OFF = "The user doesn't let the agent run scripts on pages.";
/** A script bypasses every rule above, so it is shown verbatim and asked every time, whatever the site permissions. */
-async function runScript(tab: BrowserTab, action: BrowserAction, context: ActionContext, before: string): Promise<Result> {
+async function runScript(tab: BrowserTab, action: BrowserAction, context: Run, before: string): Promise<Result> {
const { signal } = context;
const script = action.script ?? "";
if (!script.trim()) return { ok: false, error: "the script is empty." };
// The document the user sees as they approve: a reload or a page at the same address is another one.
const doc = (await pageCall(tab, { op: "status" })).doc;
if (!doc) return { ok: false, error: "the page is not answering; take a snapshot." };
- const refused = await pass({ kind: "ask", ask: "script", host: hostOfUrl(before), name: "", detail: script, prompt: "" }, signal);
+ const refused = await pass({ kind: "ask", ask: "script", host: hostOfUrl(before), name: "", detail: script, prompt: "" }, context);
if (refused) return { ok: false, error: refused };
if (signal?.aborted) return { ok: false, error: STOPPED };
// Settings can change while the question is open.
@@ -968,7 +1063,7 @@ async function arrive(
tab: BrowserTab,
requested: string,
action: BrowserAction,
- context: ActionContext,
+ context: Run,
since: number,
note?: string,
): Promise<Result> {
@@ -977,35 +1072,35 @@ async function arrive(
if (url) {
const blocked = navigationBlocks(url, action.policy, permissions(context));
if (blocked) return { ok: false, error: `The page went to ${host}, but ${blocked}. Go back.` };
- if (host && host !== hostOfUrl(requested) && !reachedHosts.has(host)) {
+ if (host && host !== hostOfUrl(requested) && !context.session.reachedHosts.has(host)) {
const gate = gateNavigation(url, { ...navigationContext(action, context), currentUrl: requested });
- const refused = await pass(gate, context.signal);
+ const refused = await pass(gate, context);
if (refused) return { ok: false, error: `The page went to ${host}. ${refused}` };
admit(url, gate, context);
}
- if (host && context.fullAccess !== true) reachedHosts.add(host);
- record(action.op, url);
+ if (host && context.fullAccess !== true) context.session.reachedHosts.add(host);
+ record(context.session, action.op, url);
}
return observeLoaded(tab, context, since, note);
}
/** The page the tab shows now, before the model reads or acts on it: the page may have moved on
* its own, or the user declined where a redirect landed and it is still loaded. */
-async function stillAllowed(tab: BrowserTab, action: BrowserAction, context: ActionContext): Promise<string | null> {
+async function stillAllowed(tab: BrowserTab, action: BrowserAction, context: Run): Promise<string | null> {
const url = tabUrl(tab);
const host = hostOfUrl(url);
if (!host) return null;
const blocked = navigationBlocks(url, action.policy, permissions(context));
if (blocked) return `The page is on ${host}, but ${blocked}. Use browser_navigate to go elsewhere.`;
- if (reachedHosts.has(host)) return null;
+ if (context.session.reachedHosts.has(host)) return null;
const gate = gateNavigation(url, { ...navigationContext(action, context), currentUrl: "" });
- const refused = await pass(gate, context.signal);
+ const refused = await pass(gate, context);
if (refused) return `The page is on ${host}. ${refused}`;
admit(url, gate, context);
return null;
}
-async function observeLoaded(tab: BrowserTab, context: ActionContext, since: number, note?: string): Promise<Result> {
+async function observeLoaded(tab: BrowserTab, context: Run, since: number, note?: string): Promise<Result> {
const entry = currentEntry(tab);
if (entry.kind === "web" && tab.documentType && !/html/i.test(tab.documentType)) {
return {
@@ -1023,18 +1118,20 @@ async function observeLoaded(tab: BrowserTab, context: ActionContext, since: num
return observe(fresh && !fresh.loading && currentEntry(fresh) === entry ? fresh : tab, context, note);
}
-async function screenshot(tab: BrowserTab): Promise<Result> {
+async function screenshot(tab: BrowserTab, context: Run): Promise<Result> {
if (!isTauri) return { ok: false, error: "screenshots of the browser panel need the desktop app; use browser_snapshot." };
+ // A page at work off screen is captured on its own; one on screen as the panel shows it.
+ const hidden = !chatOnScreen(context) && hasNativeView(tab.id);
const pages = document.querySelectorAll<HTMLElement>("[data-browser-page]");
const element = pages[pages.length - 1];
- if (!element) return { ok: false, error: "the browser panel is not on screen." };
+ if (!hidden && !element) return { ok: false, error: "the browser panel is not on screen." };
// What the user typed into card, code and password fields stays out of the picture, as out of the text.
const masked = await pageCall(tab, { op: "mask", on: true }).catch(() => null);
if (!masked?.ok) return { ok: false, error: "the page could not be prepared for a screenshot; use browser_snapshot." };
let blob: Blob | null;
try {
await sleep(100);
- blob = await screenshotPage(tab, element);
+ blob = hidden || !element ? await captureHiddenPage(tab) : await screenshotPage(tab, element);
} finally {
await pageCall(tab, { op: "mask", on: false }).catch(() => null);
}
@@ -1044,7 +1141,7 @@ async function screenshot(tab: BrowserTab): Promise<Result> {
if (!now || now.loading || tabUrl(now) !== tabUrl(tab)) {
return { ok: false, error: "the page changed while the screenshot was taken; take a snapshot first." };
}
- blob = await toPointScale(tab, blob, masked);
+ blob = await toPointScale(tab, context.session, blob, masked);
const image = await new Promise<string>((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(String(reader.result));
@@ -1055,7 +1152,7 @@ async function screenshot(tab: BrowserTab): Promise<Result> {
}
/** At CSS size within mcp_images.MAX_IMAGE_EDGE, so x and y read off it map back exactly. */
-async function toPointScale(tab: BrowserTab, blob: Blob, size: AgentAnswer): Promise<Blob> {
+async function toPointScale(tab: BrowserTab, session: Session, blob: Blob, size: AgentAnswer): Promise<Blob> {
let bitmap: ImageBitmap;
try {
bitmap = await createImageBitmap(blob);
@@ -1077,7 +1174,7 @@ async function toPointScale(tab: BrowserTab, blob: Blob, size: AgentAnswer): Pro
scrolls: size.scrolls,
doc: size.doc,
};
- pointScale.set(tab.id, { ...at, scaleX: cssWidth / bitmap.width, scaleY: cssHeight / bitmap.height });
+ session.pointScale.set(tab.id, { ...at, scaleX: cssWidth / bitmap.width, scaleY: cssHeight / bitmap.height });
if (width === bitmap.width && height === bitmap.height) return blob;
const canvas = document.createElement("canvas");
canvas.width = width;
@@ -1085,7 +1182,7 @@ async function toPointScale(tab: BrowserTab, blob: Blob, size: AgentAnswer): Pro
canvas.getContext("2d")?.drawImage(bitmap, 0, 0, width, height);
const scaled = await new Promise<Blob | null>((resolve) => canvas.toBlob(resolve, "image/png"));
// Width and height round apart, so each axis keeps its own scale.
- if (scaled) pointScale.set(tab.id, { ...at, scaleX: cssWidth / width, scaleY: cssHeight / height });
+ if (scaled) session.pointScale.set(tab.id, { ...at, scaleX: cssWidth / width, scaleY: cssHeight / height });
return scaled ?? blob;
} finally {
bitmap.close();
@@ -1095,13 +1192,13 @@ async function toPointScale(tab: BrowserTab, blob: Blob, size: AgentAnswer): Pro
const HISTORY_LIMIT = 50;
/** The browser_history tool: the user's visits, by their Settings choice (never, ask, allow). */
-async function readHistory(action: BrowserAction, context: ActionContext): Promise<Result> {
+async function readHistory(action: BrowserAction, context: Run): Promise<Result> {
const saved = useBrowserAgentStore.getState().historyAccess;
const access = context.fullAccess && saved === "ask" ? "allow" : saved;
if (access === "never") return { ok: false, error: "The user doesn't let the agent read their browsing history." };
if (access === "ask") {
const query = action.query ?? "";
- const allowed = await askInPanel({ ask: "history", host: "", name: query, detail: query }, context.signal);
+ const allowed = await askInPanel({ ask: "history", host: "", name: query, detail: query }, context);
if (!allowed) return { ok: false, error: "The user declined to share their browsing history. Do something else, or ask them." };
}
const terms = (action.query ?? "").toLowerCase().split(/\s+/).filter(Boolean);
diff --git a/studio/frontend/src/features/browser/browser-panel.tsx b/studio/frontend/src/features/browser/browser-panel.tsx
index 59573d2d8d7..024423514d2 100644
--- a/studio/frontend/src/features/browser/browser-panel.tsx
+++ b/studio/frontend/src/features/browser/browser-panel.tsx
@@ -153,6 +153,7 @@ import {
type FileViewState,
browserFile,
cachedPage,
+ chatTabs,
currentEntry,
entryKey,
pageDownload,
@@ -160,6 +161,7 @@ import {
} from "./store";
import { TabView } from "./tab-view";
import { ZOOM_STEPS, canZoom, homeZoom, stepZoom, zoomTab } from "./zoom";
+import { useShallow } from "zustand/shallow";
function tabAddress(tab: BrowserTab | undefined): string {
if (!tab) return "";
@@ -2392,7 +2394,7 @@ function liveTabIds(mounted: readonly string[], tabs: BrowserTab[], activeTabId:
/** The chat's in-app browser, memoized so chat renders skip it. `active`: its chat is shown. */
export const BrowserPanel = memo(function BrowserPanel({ active = true }: { active?: boolean }) {
const t = useT();
- const tabs = useBrowserStore((state) => state.tabs);
+ const tabs = useBrowserStore(useShallow(chatTabs));
const activeTabId = useBrowserStore((state) => state.activeTabId);
const device = useBrowserStore((state) => state.device);
const annotateTabId = useBrowserStore((state) => state.annotateTabId);
diff --git a/studio/frontend/src/features/browser/capture.ts b/studio/frontend/src/features/browser/capture.ts
index ef4dfd99b5d..2a3d384e51f 100644
--- a/studio/frontend/src/features/browser/capture.ts
+++ b/studio/frontend/src/features/browser/capture.ts
@@ -86,6 +86,12 @@ export async function screenshotPage(tab: BrowserTab, element: HTMLElement): Pro
const png = (bytes: ArrayBuffer) => (bytes.byteLength > 0 ? new Blob([bytes], { type: "image/png" }) : null);
+/** A PNG of a native page that isn't on screen (its chat isn't shown); the app shows it off the window for the shot. */
+export async function captureHiddenPage(tab: BrowserTab): Promise<Blob | null> {
+ if (!nativePageOf(tab)) return null;
+ return png(await callNative<ArrayBuffer>("browser_capture", { tabId: tab.id }));
+}
+
async function nativeScreenshot(tab: BrowserTab, element: HTMLElement): Promise<Blob | null> {
if (nativePageOf(tab)) {
// A native view sits over the DOM and hides under the menu: capture it once it's back.
diff --git a/studio/frontend/src/features/browser/index.ts b/studio/frontend/src/features/browser/index.ts
index 5650968554c..0ae77057a8a 100644
--- a/studio/frontend/src/features/browser/index.ts
+++ b/studio/frontend/src/features/browser/index.ts
@@ -32,7 +32,7 @@ export {
HISTORY_RETENTION_DAYS,
useBrowserPrefsStore,
} from "./prefs-store";
-export { type FileViewMode, type OpenFileInput, useBrowserStore } from "./store";
+export { type FileViewMode, type OpenFileInput, chatKeyOfThread, useBrowserStore } from "./store";
export { useBrowserAgentStore } from "./agent/agent-store";
export {
DEFAULT_RULE as DEFAULT_AGENT_RULE,
diff --git a/studio/frontend/src/features/browser/native-view.ts b/studio/frontend/src/features/browser/native-view.ts
index fb01c6e4abf..4517f4b746f 100644
--- a/studio/frontend/src/features/browser/native-view.ts
+++ b/studio/frontend/src/features/browser/native-view.ts
@@ -118,6 +118,7 @@ function closeView(tabId: string): void {
loadingPages.delete(tabId);
recency = recency.filter((id) => id !== tabId);
if (parkedView === tabId) parkedView = null;
+ background = background.filter((id) => id !== tabId);
void call("browser_view_close", { tabId }).catch(() => undefined);
}
@@ -255,11 +256,17 @@ function onNativeEvent(event: NativeEvent): void {
newTabTimes = newTabTimes.filter((time) => now - time < NEW_TAB_WINDOW_MS);
if (newTabTimes.length < NEW_TABS_PER_WINDOW) {
newTabTimes.push(now);
- store.openUrl(event.url, { newTab: true, from: shownUrl(tab) });
+ // A page at work in a chat off screen opens its popups there, not over this chat's tabs.
+ if (tab.chat !== store.chat) store.openTabIn(tab.chat, event.url, { from: shownUrl(tab), background: true });
+ else store.openUrl(event.url, { newTab: true, from: shownUrl(tab) });
} else {
prompt(t("browser.native.externalPrompt", { host: hostOf(shownUrl(tab)), url: event.url }), {
label: t("browser.native.open"),
- onClick: () => store.openUrl(event.url, { newTab: true, from: shownUrl(tab) }),
+ onClick: () => {
+ const now = useBrowserStore.getState();
+ if (tab.chat !== now.chat) now.openTabIn(tab.chat, event.url, { from: shownUrl(tab), background: true });
+ else now.openUrl(event.url, { newTab: true, from: shownUrl(tab) });
+ },
});
}
break;
@@ -692,8 +699,8 @@ function pruneViews(shown: string | null): void {
resume.delete(tabId);
}
}
- while (views.size > MAX_VIEWS) {
- const oldest = recency.find((id) => id !== shown && views.has(id));
+ while (views.size - background.length > MAX_VIEWS) {
+ const oldest = recency.find((id) => id !== shown && views.has(id) && !background.includes(id));
if (!oldest) break;
closeView(oldest);
}
@@ -729,6 +736,94 @@ let snapshot: { tabId: string; element: HTMLElement; url: string } | null = null
let shownBounds: Bounds | null = null;
const viewBounds = new Map<string, Bounds>();
+// Pages kept open while their chat is off screen: the agent at work in another chat. Newest last;
+// past the cap the oldest closes (and reopens where it got to, like any pruned view).
+const MAX_BACKGROUND = 3;
+let background: string[] = [];
+const openingInBackground = new Set<string>();
+let watchingBackground = false;
+// A panel is mounted (startNativeViews running), which prunes and closes hidden pages itself.
+let panelUp = false;
+
+/** Keeps `tabId`'s page open, and following its tab, while the panel shows another chat or none. */
+export function keepInBackground(tabId: string): void {
+ background = [...background.filter((id) => id !== tabId), tabId];
+ while (background.length > MAX_BACKGROUND) {
+ const oldest = background.shift();
+ if (oldest && oldest !== shownView && oldest !== parkedView && views.has(oldest)) closeView(oldest);
+ }
+ listenOnce();
+ if (!watchingBackground) {
+ watchingBackground = true;
+ useBrowserStore.subscribe(syncBackground);
+ }
+ syncBackground();
+}
+
+/** The page can close with the panel again; with no panel up, it closes now. */
+export function releaseBackground(tabId: string): void {
+ background = background.filter((id) => id !== tabId);
+ if (!panelUp && tabId !== shownView && views.has(tabId)) closeView(tabId);
+}
+
+/** Opens or moves each kept page its tab no longer matches; the panel owns the one it shows. */
+function syncBackground(): void {
+ if (background.length === 0) return;
+ const tabs = new Map(useBrowserStore.getState().tabs.map((tab) => [tab.id, tab]));
+ for (const tabId of [...background]) {
+ const tab = tabs.get(tabId);
+ if (!tab) {
+ releaseBackground(tabId);
+ continue;
+ }
+ const entry = currentEntry(tab);
+ if (tabId === shownView || tabId === parkedView || entry.kind !== "web" || openingInBackground.has(tabId)) continue;
+ const key = entryKey(entry);
+ if (views.get(tabId) === key) continue;
+ void openInBackground(tabId, entry.url, key);
+ }
+}
+
+async function openInBackground(tabId: string, url: string, key: number): Promise<void> {
+ openingInBackground.add(tabId);
+ const resumed = resume.get(tabId);
+ try {
+ if (views.has(tabId)) {
+ await call("browser_view_navigate", { tabId, url });
+ } else {
+ const bounds = viewBounds.get(tabId) ??
+ shownBounds ?? { x: 0, y: 0, width: Math.round(window.innerWidth / 2), height: window.innerHeight, viewportWidth: window.innerWidth };
+ openedTabs.add(tabId);
+ await call("browser_view_open_background", { tabId, url: resumed?.entry === key ? resumed.url : url, bounds });
+ viewBounds.set(tabId, bounds);
+ recency = [...recency.filter((id) => id !== tabId), tabId];
+ }
+ views.set(tabId, key);
+ if (useBrowserStore.getState().tabs.find((tab) => tab.id === tabId)?.nativeError) {
+ useBrowserStore.getState().updateTab(tabId, { nativeError: null });
+ }
+ } catch (cause) {
+ useBrowserStore.getState().updateTab(tabId, { loading: false, nativeError: error(cause) });
+ // Not retried here: the agent's next action asks again.
+ background = background.filter((id) => id !== tabId);
+ } finally {
+ openingInBackground.delete(tabId);
+ }
+ // Let go (run over, tab closed, cap) while it opened: nothing owns it now.
+ if (!background.includes(tabId) && !panelUp && tabId !== shownView && views.has(tabId)) closeView(tabId);
+ syncBackground();
+}
+
+/** True once `tabId`'s page is open (a kept page reopening after the cap closed it), false after `timeoutMs`. */
+export async function whenViewOpen(tabId: string, timeoutMs = 5000): Promise<boolean> {
+ const until = Date.now() + timeoutMs;
+ while (!views.has(tabId) || openingInBackground.has(tabId)) {
+ if (Date.now() > until) return false;
+ await new Promise((resolve) => setTimeout(resolve, 50));
+ }
+ return true;
+}
+
function clearSnapshot(): void {
if (!snapshot) return;
const { style } = snapshot.element;
@@ -953,6 +1048,7 @@ let motionUntil = 0;
export function startNativeViews(): () => void {
listenOnce();
+ panelUp = true;
let frame = 0;
let sent = "";
let resized: HTMLElement | null = null;
@@ -1029,8 +1125,10 @@ export function startNativeViews(): () => void {
document.removeEventListener("pointerover", reachedPage, true);
document.removeEventListener("pointerup", schedule, true);
window.clearInterval(interval);
+ panelUp = false;
tooltipHold = 0;
- // close pages because hidden native views keep scripts and media running.
+ // close pages because hidden native views keep scripts and media running; a page at work
+ // in the background is only hidden.
generation += 1;
pending = null;
parkedView = null;
@@ -1039,6 +1137,7 @@ export function startNativeViews(): () => void {
closeHole();
sendInput(NO_INPUT);
insetToasts(null);
- for (const tabId of [...views.keys()]) closeView(tabId);
+ for (const tabId of [...views.keys()]) if (!background.includes(tabId)) closeView(tabId);
+ if (views.size > 0) void call("browser_view_show", { tabId: null }).catch(() => undefined);
};
}
diff --git a/studio/frontend/src/features/browser/panel-availability.ts b/studio/frontend/src/features/browser/panel-availability.ts
index a24a2fd369e..d0d90cc5ddc 100644
--- a/studio/frontend/src/features/browser/panel-availability.ts
+++ b/studio/frontend/src/features/browser/panel-availability.ts
@@ -17,6 +17,18 @@ export function browserPanelAvailable(): boolean {
return panelAvailable;
}
+// The chat whose browser is on screen, as the browser store names it; the store sets it.
+let shownChat = "new";
+
+export function setShownBrowserChat(chat: string): void {
+ shownChat = chat;
+}
+
+/** A new chat's run reads this when it starts, so its actions stay in that chat after a switch. */
+export function shownBrowserChat(): string {
+ return shownChat;
+}
+
const subscribe = (listener: () => void) => {
listeners.add(listener);
return () => void listeners.delete(listener);
diff --git a/studio/frontend/src/features/browser/store.ts b/studio/frontend/src/features/browser/store.ts
index ef5d962bb8c..68bd2b03e3f 100644
--- a/studio/frontend/src/features/browser/store.ts
+++ b/studio/frontend/src/features/browser/store.ts
@@ -1,11 +1,12 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-import { type DocumentAnnotations, useChatRuntimeStore } from "@/features/chat";
+import { type DocumentAnnotations, onChatThreadsDeleted, useChatRuntimeStore } from "@/features/chat";
import { create } from "zustand";
import { unwrapRedirect } from "./address";
import type { BrowserPage } from "./api";
import { PageCache, cacheLimits, reportedDeviceMemory } from "./page-cache";
+import { setShownBrowserChat } from "./panel-availability";
import { defaultZoom } from "./prefs-store";
import { REACT_PREVIEW_KEY_PREFIX, REACT_PREVIEW_TYPE } from "./react-preview-type";
@@ -92,8 +93,20 @@ export type BrowserTab = {
muted: boolean;
/** The sidebar pin this tab shows (pinned-pages-store), kept as it navigates. */
pinnedId: string | null;
+ /** The chat the tab belongs to (`BrowserState.chat`); the panel shows only that chat's tabs. */
+ chat: string;
};
+type ChatBrowser = { open: boolean; activeTabId: string | null; fullView: boolean };
+
+/** The chat on screen before the chat page names it (chat-page.tsx `shownChatKey`). */
+export const NEW_CHAT = "new";
+
+/** A saved chat's key, as chat-page.tsx names the chat on screen. */
+export function chatKeyOfThread(threadId: string): string {
+ return `single:${threadId}`;
+}
+
export type OpenFileInput = {
blob: Blob;
name: string;
@@ -175,6 +188,7 @@ const newId = (prefix: string) => `${prefix}-${Date.now().toString(36)}-${(nextI
function createTab(entry: BrowserEntry, openKey: string | null = null): BrowserTab {
return {
id: newId("tab"),
+ chat: "",
history: [entry],
index: 0,
title: entry.kind === "file" ? entry.name : "",
@@ -266,8 +280,22 @@ function releaseFiles(tabs: BrowserTab[]): void {
type BrowserState = {
open: boolean;
+ /** Every chat's tabs; the panel lists `chatTabs`, the shown chat's. */
tabs: BrowserTab[];
activeTabId: string | null;
+ /** The chat on screen. `open`, `activeTabId` and `fullView` are its; other chats keep theirs in `chats`. */
+ chat: string;
+ chats: Record<string, ChatBrowser>;
+ /** Puts `chat`'s tabs on screen, keeping the last chat's for when it comes back. A chat with none
+ * gets a new tab while the panel is open, so the browser stays where the reader left it. */
+ showChat: (chat: string) => void;
+ /** A new chat got its id on its first save: its tabs go with it. */
+ adoptChat: (from: string, to: string) => void;
+ /** A web tab for `chat`, in front only when that chat is on screen (`background`: behind its front tab); its id. */
+ openTabIn: (chat: string, url: string, options?: { from?: string; background?: boolean }) => string | null;
+ forgetChat: (chat: string) => void;
+ /** `tabId` in front of `chat`'s open panel: now if it is on screen, else when it comes back. */
+ revealIn: (chat: string, tabId: string) => void;
/** Bumped on open, so the panel re-expands after a drag shut. */
openSequence: number;
focusAddressSequence: number;
@@ -413,22 +441,33 @@ function moveTo(tab: BrowserTab, index: number): BrowserTab {
};
}
+export function chatTabs(state: Pick<BrowserState, "tabs" | "chat">): BrowserTab[] {
+ return state.tabs.filter((tab) => tab.chat === state.chat);
+}
+
export const useBrowserStore = create<BrowserState>((set, get) => {
- const openTab = (tab: BrowserTab, background = false, after?: string) => {
+ const openTab = (tab: BrowserTab, background = false, after?: string, chat = get().chat) => {
set((state) => {
+ const owned = { ...tab, chat };
const at = after ? state.tabs.findIndex((other) => other.id === after) : -1;
const tabs = [...state.tabs];
- tabs.splice(at < 0 ? tabs.length : at + 1, 0, tab);
+ tabs.splice(at < 0 ? tabs.length : at + 1, 0, owned);
+ if (chat !== state.chat) {
+ // Another chat's: it shows there next time, without moving this chat's panel.
+ const saved = state.chats[chat];
+ const activeTabId = background && saved?.activeTabId ? saved.activeTabId : owned.id;
+ return { tabs, chats: { ...state.chats, [chat]: { open: true, fullView: saved?.fullView ?? false, activeTabId } } };
+ }
return {
open: true,
tabs,
- activeTabId: background && state.activeTabId ? state.activeTabId : tab.id,
+ activeTabId: background && state.activeTabId ? state.activeTabId : owned.id,
openSequence: state.openSequence + 1,
};
});
};
const focusExisting = (openKey: string): boolean => {
- const existing = get().tabs.find((tab) => tab.openKey === openKey);
+ const existing = chatTabs(get()).find((tab) => tab.openKey === openKey);
if (!existing) return false;
set((state) => ({ open: true, activeTabId: existing.id, openSequence: state.openSequence + 1 }));
return true;
@@ -438,6 +477,73 @@ export const useBrowserStore = create<BrowserState>((set, get) => {
open: false,
tabs: [],
activeTabId: null,
+ chat: NEW_CHAT,
+ chats: {},
+ showChat: (chat) => {
+ forgetDeletedChats();
+ const state = get();
+ if (chat === state.chat) return;
+ const chats = { ...state.chats };
+ if (state.tabs.some((tab) => tab.chat === state.chat)) {
+ chats[state.chat] = { open: state.open, activeTabId: state.activeTabId, fullView: state.fullView };
+ } else {
+ delete chats[state.chat];
+ }
+ const saved = chats[chat];
+ delete chats[chat];
+ const own = state.tabs.filter((tab) => tab.chat === chat);
+ const activeTabId =
+ saved && own.some((tab) => tab.id === saved.activeTabId) ? saved.activeTabId : (own.at(-1)?.id ?? null);
+ const open = saved ? saved.open : state.open;
+ // Not openTab: no address focus out of the composer, and no open bump, which closes the chat's research.
+ const fresh = open && own.length === 0 ? { ...createTab({ kind: "newtab" }), chat } : null;
+ set({
+ chat,
+ chats,
+ tabs: fresh ? [...state.tabs, fresh] : state.tabs,
+ activeTabId: fresh?.id ?? activeTabId,
+ open,
+ fullView: open && (saved ? saved.fullView : state.fullView),
+ annotateTabId: null,
+ renamingTabId: null,
+ });
+ },
+ adoptChat: (from, to) => {
+ if (from === to) return;
+ set((state) => {
+ const tabs = state.tabs.map((tab) => (tab.chat === from ? { ...tab, chat: to } : tab));
+ const chats = { ...state.chats };
+ const saved = chats[from];
+ delete chats[from];
+ if (saved && !chats[to]) chats[to] = saved;
+ return { tabs, chats, chat: state.chat === from ? to : state.chat };
+ });
+ for (const listener of adoptListeners) listener(from, to);
+ },
+ revealIn: (chat, tabId) => {
+ if (chat === get().chat) {
+ if (get().activeTabId !== tabId) get().activateTab(tabId);
+ if (!get().open) get().openPanel();
+ return;
+ }
+ set((state) => ({
+ chats: { ...state.chats, [chat]: { open: true, fullView: state.chats[chat]?.fullView ?? false, activeTabId: tabId } },
+ }));
+ },
+ openTabIn: (chat, url, options) => {
+ if (!isWeb(url)) return null;
+ const tab = createTab(webEntry(unwrapRedirect(url), undefined, undefined, options?.from));
+ openTab(tab, options?.background, undefined, chat);
+ return tab.id;
+ },
+ forgetChat: (chat) => {
+ for (const tab of get().tabs) if (tab.chat === chat) get().closeTab(tab.id);
+ set((state) => {
+ if (!(chat in state.chats)) return state;
+ const { [chat]: _forgotten, ...chats } = state.chats;
+ return { chats };
+ });
+ },
openSequence: 0,
focusAddressSequence: 0,
bookmarkSequence: 0,
@@ -459,7 +565,7 @@ export const useBrowserStore = create<BrowserState>((set, get) => {
return { fileViews: { ...state.fileViews, [tabId]: next } };
}),
openPanel: () => {
- if (get().tabs.length === 0) {
+ if (chatTabs(get()).length === 0) {
get().newTab();
return;
}
@@ -478,10 +584,10 @@ export const useBrowserStore = create<BrowserState>((set, get) => {
duplicateTab: (tabId) => {
const tab = get().tabs.find((candidate) => candidate.id === tabId);
if (!tab) return;
- openTab(copyTab(tab), false, tabId);
+ openTab(copyTab(tab), false, tabId, tab.chat);
},
openPinned: (pinnedId, url, title) => {
- const shown = get().tabs.find((tab) => tab.pinnedId === pinnedId);
+ const shown = chatTabs(get()).find((tab) => tab.pinnedId === pinnedId);
if (shown) {
set((state) => ({ open: true, activeTabId: shown.id, openSequence: state.openSequence + 1 }));
return;
@@ -500,16 +606,16 @@ export const useBrowserStore = create<BrowserState>((set, get) => {
setMuted: (tabId, muted) =>
set((state) => ({ tabs: patchTab(state.tabs, tabId, (tab) => (tab.muted === muted ? tab : { ...tab, muted })) })),
closeOtherTabs: (tabId) => {
- for (const tab of get().tabs) if (tab.id !== tabId) get().closeTab(tab.id);
+ for (const tab of chatTabs(get())) if (tab.id !== tabId) get().closeTab(tab.id);
},
closeTabsToRight: (tabId) => {
- const { tabs } = get();
+ const tabs = chatTabs(get());
const index = tabs.findIndex((tab) => tab.id === tabId);
if (index < 0) return;
for (const tab of tabs.slice(index + 1)) get().closeTab(tab.id);
},
closeChatPages: () => {
- for (const tab of get().tabs) {
+ for (const tab of chatTabs(get())) {
if (tab.history.some((entry) => entry.kind === "file" && entry.chatPage)) get().closeTab(tab.id);
}
},
@@ -545,7 +651,7 @@ export const useBrowserStore = create<BrowserState>((set, get) => {
...(openKey ? { openKey } : {}),
...(key?.startsWith("html:") ? { chatPage: true } : {}),
};
- const existing = openKey ? get().tabs.find((tab) => tab.openKey === openKey) : undefined;
+ const existing = openKey ? chatTabs(get()).find((tab) => tab.openKey === openKey) : undefined;
if (openKey && existing) {
focusExisting(openKey);
if (page) get().setFileView(existing.id, { pageTarget: { page } });
@@ -627,32 +733,45 @@ export const useBrowserStore = create<BrowserState>((set, get) => {
activateTab: (tabId) => set({ activeTabId: tabId, annotateTabId: null }),
moveTab: (tabId, index) =>
set((state) => {
- const from = state.tabs.findIndex((tab) => tab.id === tabId);
- const to = Math.max(0, Math.min(index, state.tabs.length - 1));
- if (from < 0 || from === to) return state;
- const tabs = [...state.tabs];
- const [moved] = tabs.splice(from, 1);
- if (moved) tabs.splice(to, 0, moved);
+ // `index` is in the strip, which lists only the shown chat's tabs.
+ const strip = chatTabs(state);
+ const from = strip.findIndex((tab) => tab.id === tabId);
+ const to = Math.max(0, Math.min(index, strip.length - 1));
+ const moved = strip[from];
+ const target = strip[to];
+ if (!moved || !target || from === to) return state;
+ const tabs = state.tabs.filter((tab) => tab !== moved);
+ const at = tabs.indexOf(target);
+ tabs.splice(to > from ? at + 1 : at, 0, moved);
return { tabs };
}),
closeTab: (tabId) => {
- const { tabs, activeTabId } = get();
- const index = tabs.findIndex((tab) => tab.id === tabId);
- if (index < 0) return;
+ const { tabs, activeTabId, chat } = get();
+ const closing = tabs.find((tab) => tab.id === tabId);
+ if (!closing) return;
const remaining = tabs.filter((tab) => tab.id !== tabId);
- for (const entry of tabs[index]?.history ?? []) pageCache.delete(entry);
+ const siblings = tabs.filter((tab) => tab.chat === closing.chat);
+ const index = siblings.indexOf(closing);
+ const left = siblings.filter((tab) => tab !== closing);
+ for (const entry of closing.history) pageCache.delete(entry);
releaseFiles(remaining);
pageDownloads.delete(tabId);
const { [tabId]: _closed, ...fileViews } = get().fileViews;
const annotateTabId = get().annotateTabId === tabId ? null : get().annotateTabId;
const renamingTabId = get().renamingTabId === tabId ? null : get().renamingTabId;
- if (remaining.length === 0) {
- set({ tabs: [], activeTabId: null, open: false, fullView: false, annotateTabId: null, renamingTabId, fileViews });
+ const next = (current: string | null) =>
+ current === tabId ? (left[Math.min(index, left.length - 1)]?.id ?? null) : current;
+ if (closing.chat !== chat) {
+ const { [closing.chat]: saved, ...chats } = get().chats;
+ if (saved && left.length > 0) chats[closing.chat] = { ...saved, activeTabId: next(saved.activeTabId) };
+ set({ tabs: remaining, chats, annotateTabId, renamingTabId, fileViews });
+ return;
+ }
+ if (left.length === 0) {
+ set({ tabs: remaining, activeTabId: null, open: false, fullView: false, annotateTabId: null, renamingTabId, fileViews });
return;
}
- const nextActive =
- activeTabId === tabId ? (remaining[Math.min(index, remaining.length - 1)]?.id ?? null) : activeTabId;
- set({ tabs: remaining, activeTabId: nextActive, annotateTabId, renamingTabId, fileViews });
+ set({ tabs: remaining, activeTabId: next(activeTabId), annotateTabId, renamingTabId, fileViews });
},
updateTab: (tabId, patch) =>
set((state) => {
@@ -678,3 +797,26 @@ export const useBrowserStore = create<BrowserState>((set, get) => {
splitWithChatOn: (chatSide) => set({ fullView: false, chatSide }),
};
});
+
+useBrowserStore.subscribe((state) => setShownBrowserChat(state.chat));
+
+const adoptListeners = new Set<(from: string, to: string) => void>();
+
+/** `listener(from, to)` when a new chat's state moves to its saved id, so state kept per chat elsewhere follows. */
+export function onChatAdopted(listener: (from: string, to: string) => void): () => void {
+ adoptListeners.add(listener);
+ return () => adoptListeners.delete(listener);
+}
+
+let watchingDeletes = false;
+
+/** A deleted chat's tabs go with it. Hooked on first use: the chat feature imports this module. */
+function forgetDeletedChats(): void {
+ if (watchingDeletes) return;
+ watchingDeletes = true;
+ onChatThreadsDeleted((threadIds, pairIds) => {
+ const store = useBrowserStore.getState();
+ for (const threadId of threadIds) store.forgetChat(chatKeyOfThread(threadId));
+ for (const pairId of pairIds) store.forgetChat(`compare:${pairId}`);
+ });
+}
diff --git a/studio/frontend/src/features/browser/tab-menu.tsx b/studio/frontend/src/features/browser/tab-menu.tsx
index 088061eb484..ad18fb7fdba 100644
--- a/studio/frontend/src/features/browser/tab-menu.tsx
+++ b/studio/frontend/src/features/browser/tab-menu.tsx
@@ -48,7 +48,8 @@ import { hostOf } from "./address";
import { callNative, useNativeBrowser } from "./native-support";
import { nativeAction, hasNativeView } from "./native-view";
import { type PinnedPage, usePinnedPagesStore } from "./pinned-pages-store";
-import { type BrowserTab, currentEntry, useBrowserStore } from "./store";
+import { type BrowserTab, chatTabs, currentEntry, useBrowserStore } from "./store";
+import { useShallow } from "zustand/shallow";
const ICON = "size-icon";
@@ -89,7 +90,7 @@ export function tabAddress(tab: BrowserTab | undefined): string | null {
/** The tab showing a pinned page, if one is open. */
export function usePinnedTab(pinnedId: string | undefined): BrowserTab | undefined {
return useBrowserStore((state) =>
- pinnedId ? state.tabs.find((tab) => tab.pinnedId === pinnedId) : undefined,
+ pinnedId ? chatTabs(state).find((tab) => tab.pinnedId === pinnedId) : undefined,
);
}
@@ -195,7 +196,7 @@ export function TabMenuItems({
}) {
const t = useT();
const navigate = useNavigate();
- const tabs = useBrowserStore((state) => state.tabs);
+ const tabs = useBrowserStore(useShallow(chatTabs));
const url = (tab && tabAddress(tab)) ?? pinned?.url ?? null;
const pinnedId = pinned?.id ?? tab?.pinnedId ?? null;
const index = tab ? tabs.findIndex((other) => other.id === tab.id) : -1;
diff --git a/studio/frontend/src/features/chat/api/chat-adapter.ts b/studio/frontend/src/features/chat/api/chat-adapter.ts
index 87cd402c387..01707d09108 100644
--- a/studio/frontend/src/features/chat/api/chat-adapter.ts
+++ b/studio/frontend/src/features/chat/api/chat-adapter.ts
@@ -76,6 +76,7 @@ function userTexts(
}
import { endBrowserAgentRun } from "@/features/browser/agent/agent-store";
import { browserAgentToolsForTurn } from "@/features/browser/agent/tools";
+import { shownBrowserChat } from "@/features/browser/panel-availability";
import { projectHasSources } from "@/features/rag/api/rag-api";
import {
IMAGE_SENTINEL_TOOLS,
@@ -5638,6 +5639,12 @@ export function createOpenAIStreamAdapter(
);
const threadKey = resolvedThreadId || "__default";
+ // A new chat's browser is the one on screen as it sends; a switch mid-run must not move it.
+ const browserChat = options.pairId
+ ? `compare:${options.pairId}`
+ : threadKey === "__default"
+ ? shownBrowserChat()
+ : undefined;
// A first turn files its handles under "__default" and autosave re-keys them mid-run, so resolve per use.
const liveThreadKey = (owner: () => void) =>
threadKey === "__default"
@@ -7567,6 +7574,7 @@ export function createOpenAIStreamAdapter(
if (action) {
void runBrowserAction(action, {
runKey: cancelId,
+ chat: browserChat,
// Resolved per action: a first turn is re-keyed from "__default" to its id.
scope: liveThreadKey(serverCancel),
userText,
diff --git a/studio/frontend/src/features/chat/chat-page.tsx b/studio/frontend/src/features/chat/chat-page.tsx
index 22388b00d3c..fccc08b8749 100644
--- a/studio/frontend/src/features/chat/chat-page.tsx
+++ b/studio/frontend/src/features/chat/chat-page.tsx
@@ -94,6 +94,7 @@ import { cn } from "@/lib/utils";
import { setInAppLinkHandler } from "@/lib/open-link";
import {
BrowserToggleButton,
+ chatKeyOfThread,
openUrlInBrowser,
pinBrowserPage,
setBrowserPanelAvailable,
@@ -120,7 +121,7 @@ import {
PencilEdit02Icon,
Telescope02Icon,
} from "@hugeicons/core-free-icons";
-import { useAui, useAuiState } from "@assistant-ui/react";
+import { useAui } from "@assistant-ui/react";
import { HugeiconsIcon } from "@hugeicons/react";
import { useNavigate } from "@tanstack/react-router";
import {
@@ -764,16 +765,13 @@ const SingleContent = memo(function SingleContent({
useChatRuntimeStore.getState().setSettingsPanelOpen(false);
}, [researchMatchesThread, closeBrowser]);
- // Close the browser on leaving the chat; the runtime thread id survives a new chat's first save.
- const shownThreadId = useAuiState(({ threads }) => threads.mainThreadId);
- const shownThreadIdRef = useRef(shownThreadId);
+ // Leaving Chat closes this chat's browser. Compare keeps this view mounted but hidden, and by
+ // then the panel on screen is the compare chat's: that one stays as it is.
+ const browserChat = useBrowserStore((state) => state.chat);
+ const browserChatRef = useRef<string | null>(null);
+ if (chatActive) browserChatRef.current = browserChat;
useEffect(() => {
- if (shownThreadIdRef.current === shownThreadId) return;
- shownThreadIdRef.current = shownThreadId;
- closeBrowser();
- }, [shownThreadId, closeBrowser]);
- useEffect(() => {
- if (!chatActive) closeBrowser();
+ if (!chatActive && useBrowserStore.getState().chat === browserChatRef.current) closeBrowser();
}, [chatActive, closeBrowser]);
// Width on header/notice only: on the root it would restyle the whole thread per resize.
@@ -3494,6 +3492,26 @@ export function ChatPage({
useLayoutEffect(() => {
useBrowserStore.getState().closeChatPages();
}, [shownChatKey]);
+ // Each chat keeps its own browser tabs, so a run in another chat keeps its page. A new chat's key
+ // changes on its first save, and its tabs go with it.
+ const unsavedChatKey =
+ view.mode === "single"
+ ? `single:${view.newThreadNonce ?? "new"}`
+ : view.mode === "project"
+ ? `project:${view.projectId}:${projectNewThreadNonce}`
+ : null;
+ const browserChatRef = useRef<string | null>(null);
+ // biome-ignore lint/correctness/useExhaustiveDependencies: only another chat on screen moves the tabs
+ useLayoutEffect(() => {
+ const previous = browserChatRef.current;
+ browserChatRef.current = shownChatKey;
+ const store = useBrowserStore.getState();
+ if (previous !== null && previous === unsavedChatKey && activeThreadId && shownChatKey === chatKeyOfThread(activeThreadId)) {
+ store.adoptChat(previous, shownChatKey);
+ } else {
+ store.showChat(shownChatKey);
+ }
+ }, [shownChatKey]);
const hasActiveModel = Boolean(inferenceCheckpoint);
const chatContextKey = `${view.mode}|${activeThreadId ?? ""}|${search.new ?? ""}|${search.project ?? ""}`;
diff --git a/studio/frontend/src/features/chat/hooks/use-chat-sidebar-items.ts b/studio/frontend/src/features/chat/hooks/use-chat-sidebar-items.ts
index fdcc58a09a4..e12fe69e580 100644
--- a/studio/frontend/src/features/chat/hooks/use-chat-sidebar-items.ts
+++ b/studio/frontend/src/features/chat/hooks/use-chat-sidebar-items.ts
@@ -21,6 +21,7 @@ import { offerToDeleteKeptSandboxes } from "../utils/offer-kept-sandbox-files";
import { stopChatThread } from "../utils/stop-chat-thread";
import {
markChatThreadsDeleted,
+ notifyChatThreadsDeleted,
removeChatThreadTombstones,
} from "../utils/chat-thread-tombstones";
import { requestPromptQueueStop } from "../utils/prompt-queue-boundary";
@@ -348,7 +349,8 @@ export async function deleteChatItems(
// clear composer drafts so deleted threads leave no orphan keys.
for (const id of threadIds) clearComposerDraft(id);
- markChatThreadsDeleted(threadIds);
+ // Quiet until the delete lands: a failed one restores the chats, browser tabs and all.
+ markChatThreadsDeleted(threadIds, { quiet: true });
notifyChatHistoryUpdated();
if (activeId !== undefined && items.some((item) => item.id === activeId)) {
@@ -358,6 +360,10 @@ export async function deleteChatItems(
try {
const kept = await deleteStoredChatThreads(threadIds, args);
+ notifyChatThreadsDeleted(
+ threadIds,
+ items.filter((item) => item.type === "compare").map((item) => item.id),
+ );
// retain the head until deletion succeeds so a restored chat reopens on the same branch.
for (const id of threadIds) clearBranchHead(id);
// offer recovery for sandbox files that outlive a deleted chat and lose their sidebar entry.
diff --git a/studio/frontend/src/features/chat/index.ts b/studio/frontend/src/features/chat/index.ts
index 07c41292bfc..e6088b433bc 100644
--- a/studio/frontend/src/features/chat/index.ts
+++ b/studio/frontend/src/features/chat/index.ts
@@ -463,6 +463,7 @@ export {
export { allRecordedSandboxSessionIds } from "./utils/recorded-sandbox-session";
export {
markChatThreadDeleted,
+ onChatThreadsDeleted,
removeChatThreadTombstones,
} from "./utils/chat-thread-tombstones";
export { emitChatAttachmentDeleted } from "./utils/chat-attachment-events";
diff --git a/studio/frontend/src/features/chat/utils/chat-thread-tombstones.ts b/studio/frontend/src/features/chat/utils/chat-thread-tombstones.ts
index 0c15f094c39..70b7a3cbf30 100644
--- a/studio/frontend/src/features/chat/utils/chat-thread-tombstones.ts
+++ b/studio/frontend/src/features/chat/utils/chat-thread-tombstones.ts
@@ -84,19 +84,35 @@ for (const t of loadTombstones()) {
}
gc();
+const deletedListeners = new Set<(threadIds: string[], pairIds: string[]) => void>();
+
export function markChatThreadDeleted(threadId: string): void {
deletedThreads.set(threadId, { id: threadId, deletedAt: nowMs() });
gc();
persist();
+ for (const listener of deletedListeners) listener([threadId], []);
+}
+
+/** `listener(threadIds, pairIds)` when chats are deleted, so state kept per chat elsewhere can go too. */
+export function onChatThreadsDeleted(listener: (threadIds: string[], pairIds: string[]) => void): () => void {
+ deletedListeners.add(listener);
+ return () => deletedListeners.delete(listener);
}
-export function markChatThreadsDeleted(threadIds: Iterable<string>): void {
+/** `quiet` hides the rows without telling listeners: a delete that can still fail and roll back. */
+export function markChatThreadsDeleted(threadIds: Iterable<string>, { quiet = false } = {}): void {
const now = nowMs();
- for (const id of threadIds) {
+ const ids = [...threadIds];
+ for (const id of ids) {
deletedThreads.set(id, { id, deletedAt: now });
}
gc();
persist();
+ if (!quiet) notifyChatThreadsDeleted(ids, []);
+}
+
+export function notifyChatThreadsDeleted(threadIds: string[], pairIds: string[]): void {
+ for (const listener of deletedListeners) listener(threadIds, pairIds);
}
export function isChatThreadDeleted(threadId: string): boolean {
diff --git a/studio/frontend/src/i18n/locales/ar.ts b/studio/frontend/src/i18n/locales/ar.ts
index d528e90ca85..0e06ee4f458 100644
--- a/studio/frontend/src/i18n/locales/ar.ts
+++ b/studio/frontend/src/i18n/locales/ar.ts
@@ -83,6 +83,9 @@ export const ar = {
},
agent: {
working: "يتصفح Unsloth في علامة التبويب هذه",
+ waiting: "يحتاج Unsloth إلى ردك في المتصفح من أجل {chat}",
+ otherChat: "محادثة أخرى",
+ show: "عرض",
stop: "إيقاف",
allow: "السماح",
deny: "الرفض",
diff --git a/studio/frontend/src/i18n/locales/de.ts b/studio/frontend/src/i18n/locales/de.ts
index 235b90a233e..f0cb158a216 100644
--- a/studio/frontend/src/i18n/locales/de.ts
+++ b/studio/frontend/src/i18n/locales/de.ts
@@ -83,6 +83,9 @@ export const de = {
},
agent: {
working: "Unsloth surft in diesem Tab",
+ waiting: "Unsloth braucht im Browser deine Antwort für {chat}",
+ otherChat: "einen anderen Chat",
+ show: "Anzeigen",
stop: "Stopp",
allow: "Erlauben",
deny: "Ablehnen",
diff --git a/studio/frontend/src/i18n/locales/en.ts b/studio/frontend/src/i18n/locales/en.ts
index 7fbb4a7605f..4764435e52f 100644
--- a/studio/frontend/src/i18n/locales/en.ts
+++ b/studio/frontend/src/i18n/locales/en.ts
@@ -80,6 +80,9 @@ export const en = {
},
agent: {
working: "Unsloth is browsing in this tab",
+ waiting: "Unsloth needs your answer in the browser for {chat}",
+ otherChat: "another chat",
+ show: "Show",
stop: "Stop",
allow: "Allow",
deny: "Deny",
diff --git a/studio/frontend/src/i18n/locales/es.ts b/studio/frontend/src/i18n/locales/es.ts
index bd703d32a06..2ab0c1f49c3 100644
--- a/studio/frontend/src/i18n/locales/es.ts
+++ b/studio/frontend/src/i18n/locales/es.ts
@@ -83,6 +83,9 @@ export const es = {
},
agent: {
working: "Unsloth está navegando en esta pestaña",
+ waiting: "Unsloth necesita tu respuesta en el navegador para {chat}",
+ otherChat: "otro chat",
+ show: "Mostrar",
stop: "Detener",
allow: "Permitir",
deny: "Denegar",
diff --git a/studio/frontend/src/i18n/locales/fr.ts b/studio/frontend/src/i18n/locales/fr.ts
index ae7729a3ec6..6519ec31556 100644
--- a/studio/frontend/src/i18n/locales/fr.ts
+++ b/studio/frontend/src/i18n/locales/fr.ts
@@ -83,6 +83,9 @@ export const fr = {
},
agent: {
working: "Unsloth navigue dans cet onglet",
+ waiting: "Unsloth attend votre réponse dans le navigateur pour {chat}",
+ otherChat: "une autre discussion",
+ show: "Afficher",
stop: "Arrêter",
allow: "Autoriser",
deny: "Refuser",
diff --git a/studio/frontend/src/i18n/locales/he.ts b/studio/frontend/src/i18n/locales/he.ts
index c883dde522b..722b44e80b0 100644
--- a/studio/frontend/src/i18n/locales/he.ts
+++ b/studio/frontend/src/i18n/locales/he.ts
@@ -84,6 +84,9 @@ export const he = {
},
agent: {
working: "Unsloth גולש בכרטיסייה הזו",
+ waiting: "Unsloth צריך את התשובה שלך בדפדפן עבור {chat}",
+ otherChat: "צ׳אט אחר",
+ show: "הצג",
stop: "עצירה",
allow: "אישור",
deny: "דחייה",
diff --git a/studio/frontend/src/i18n/locales/hi.ts b/studio/frontend/src/i18n/locales/hi.ts
index f471afa9e0d..2bc957311f2 100644
--- a/studio/frontend/src/i18n/locales/hi.ts
+++ b/studio/frontend/src/i18n/locales/hi.ts
@@ -83,6 +83,9 @@ export const hi = {
},
agent: {
working: "Unsloth इस टैब में ब्राउज़ कर रहा है",
+ waiting: "Unsloth को {chat} के लिए ब्राउज़र में आपका जवाब चाहिए",
+ otherChat: "किसी अन्य चैट",
+ show: "दिखाएँ",
stop: "रोकें",
allow: "अनुमति दें",
deny: "अस्वीकार करें",
diff --git a/studio/frontend/src/i18n/locales/it.ts b/studio/frontend/src/i18n/locales/it.ts
index 3119ca7f095..54f546b1224 100644
--- a/studio/frontend/src/i18n/locales/it.ts
+++ b/studio/frontend/src/i18n/locales/it.ts
@@ -82,6 +82,9 @@ export const it = {
},
agent: {
working: "Unsloth sta navigando in questa scheda",
+ waiting: "Unsloth ha bisogno della tua risposta nel browser per {chat}",
+ otherChat: "un'altra chat",
+ show: "Mostra",
stop: "Interrompi",
allow: "Consenti",
deny: "Nega",
diff --git a/studio/frontend/src/i18n/locales/ja.ts b/studio/frontend/src/i18n/locales/ja.ts
index 340c4a99f40..ae7ac56037a 100644
--- a/studio/frontend/src/i18n/locales/ja.ts
+++ b/studio/frontend/src/i18n/locales/ja.ts
@@ -84,6 +84,9 @@ export const ja = {
},
agent: {
working: "Unsloth がこのタブで操作しています",
+ waiting: "{chat} のブラウザで Unsloth があなたの回答を待っています",
+ otherChat: "別のチャット",
+ show: "表示",
stop: "停止",
allow: "許可",
deny: "拒否",
diff --git a/studio/frontend/src/i18n/locales/ko.ts b/studio/frontend/src/i18n/locales/ko.ts
index f2baf0c3bc3..8a8175f865d 100644
--- a/studio/frontend/src/i18n/locales/ko.ts
+++ b/studio/frontend/src/i18n/locales/ko.ts
@@ -83,6 +83,9 @@ export const ko = {
},
agent: {
working: "Unsloth가 이 탭에서 탐색 중입니다",
+ waiting: "{chat}의 브라우저에서 Unsloth가 응답을 기다리고 있습니다",
+ otherChat: "다른 채팅",
+ show: "보기",
stop: "중지",
allow: "허용",
deny: "거부",
diff --git a/studio/frontend/src/i18n/locales/pt-br.ts b/studio/frontend/src/i18n/locales/pt-br.ts
index f75b34fc161..0d102006f3a 100644
--- a/studio/frontend/src/i18n/locales/pt-br.ts
+++ b/studio/frontend/src/i18n/locales/pt-br.ts
@@ -83,6 +83,9 @@ export const ptBR = {
},
agent: {
working: "O Unsloth está navegando nesta aba",
+ waiting: "O Unsloth precisa da sua resposta no navegador para {chat}",
+ otherChat: "outro chat",
+ show: "Mostrar",
stop: "Parar",
allow: "Permitir",
deny: "Negar",
diff --git a/studio/frontend/src/i18n/locales/ru.ts b/studio/frontend/src/i18n/locales/ru.ts
index c388a325b59..50fbecd3674 100644
--- a/studio/frontend/src/i18n/locales/ru.ts
+++ b/studio/frontend/src/i18n/locales/ru.ts
@@ -83,6 +83,9 @@ export const ru = {
},
agent: {
working: "Unsloth работает в этой вкладке",
+ waiting: "Unsloth ждёт вашего ответа в браузере для {chat}",
+ otherChat: "другого чата",
+ show: "Показать",
stop: "Остановить",
allow: "Разрешить",
deny: "Запретить",
diff --git a/studio/frontend/src/i18n/locales/sv.ts b/studio/frontend/src/i18n/locales/sv.ts
index 4f3cf813992..70d72e8b3f6 100644
--- a/studio/frontend/src/i18n/locales/sv.ts
+++ b/studio/frontend/src/i18n/locales/sv.ts
@@ -81,6 +81,9 @@ export const sv = {
},
agent: {
working: "Unsloth surfar i den här fliken",
+ waiting: "Unsloth behöver ditt svar i webbläsaren för {chat}",
+ otherChat: "en annan chatt",
+ show: "Visa",
stop: "Stoppa",
allow: "Tillåt",
deny: "Neka",
diff --git a/studio/frontend/src/i18n/locales/zh-CN.ts b/studio/frontend/src/i18n/locales/zh-CN.ts
index cf91c609d87..9753a23f55e 100644
--- a/studio/frontend/src/i18n/locales/zh-CN.ts
+++ b/studio/frontend/src/i18n/locales/zh-CN.ts
@@ -83,6 +83,9 @@ export const zhCN = {
},
agent: {
working: "Unsloth 正在此标签页中浏览",
+ waiting: "Unsloth 需要你在 {chat} 的浏览器中回答",
+ otherChat: "另一个聊天",
+ show: "显示",
stop: "停止",
allow: "允许",
deny: "拒绝",
diff --git a/studio/frontend/tests/browser-agent-chats.test.ts b/studio/frontend/tests/browser-agent-chats.test.ts
new file mode 100644
index 00000000000..879874442f3
--- /dev/null
+++ b/studio/frontend/tests/browser-agent-chats.test.ts
@@ -0,0 +1,177 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import { register } from "node:module";
+import { test } from "node:test";
+
+// Chats use the browser tools side by side, each in its own tab. A chat off screen works on in the
+// background (native pages), leaving the user's tabs alone, and its questions wait in its own bar.
+register("./helpers/browser-executor-resolver.mjs", import.meta.url);
+const { runBrowserAction, STOPPED } = await import("../src/features/browser/agent/executor.ts");
+const { agentChat, endBrowserAgentRun, stopBrowserAgent, useBrowserAgentStore } = await import(
+ "../src/features/browser/agent/agent-store.ts"
+);
+const { chatKeyOfThread, chatTabs, currentEntry, useBrowserStore } = await import("../src/features/browser/store.ts");
+
+const globals = globalThis as Record<string, unknown>;
+const policy = { allowedDomains: [], blockedDomains: [] };
+type Posted = { action_id: string; result: { ok: boolean; url?: string; error?: string } };
+const posted: Posted[] = [];
+globals.__authFetch = async (_url: string, init: { body: string }) => {
+ posted.push(JSON.parse(init.body));
+ return new Response(null, { status: 200 });
+};
+const answer = (op: Record<string, unknown>) =>
+ op.op === "status"
+ ? { ok: true, quietMs: 1000, doc: "d1" }
+ : { ok: true, text: "page", links: [], width: 800, height: 600, doc: "d1" };
+// The page of a native view answers through browser_view_agent; a framed one through the frame.
+globals.__callNative = async (command: string, args: { command: { op: Record<string, unknown> } }) =>
+ command === "browser_view_agent" ? { installed: true, result: answer(args.command.op) } : null;
+globals.__frameAgent = async (_tabId: string, op: Record<string, unknown>) => ({ installed: true, result: answer(op) });
+const kept: string[] = [];
+// A page opened in the background reports its load a beat later.
+globals.__keep = (tabId: string) => {
+ kept.push(tabId);
+ setTimeout(() => useBrowserStore.getState().updateTab(tabId, { loading: false }), 30);
+};
+
+const navigate = (id: string, scope: string, url: string) =>
+ runBrowserAction({ id, sessionId: "s", op: "navigate", url, direction: "down", policy }, { runKey: `run-${scope}`, scope, userText: `open ${url}` });
+const resultOf = (id: string) => posted.find((post) => post.action_id === id)?.result;
+const urlOf = (tabId: string | null) => {
+ const tab = useBrowserStore.getState().tabs.find((candidate) => candidate.id === tabId);
+ const entry = tab && currentEntry(tab);
+ return entry?.kind === "web" ? entry.url : null;
+};
+
+test("two chats browse at once, each in its own tab; the one off screen leaves the user's tabs alone", async () => {
+ globals.__native = true;
+ const store = useBrowserStore.getState();
+ store.showChat(chatKeyOfThread("shown"));
+ store.openUrl("https://mine.example/");
+ const mine = useBrowserStore.getState().activeTabId;
+
+ await Promise.all([navigate("p1", "away", "https://away.example/"), navigate("p2", "shown", "https://here.example/")]);
+ assert.equal(resultOf("p1")?.ok, true, resultOf("p1")?.error);
+ assert.equal(resultOf("p2")?.ok, true, resultOf("p2")?.error);
+ const away = agentChat(chatKeyOfThread("away")).tabId;
+ const here = agentChat(chatKeyOfThread("shown")).tabId;
+ assert.equal(urlOf(away), "https://away.example/");
+ assert.equal(urlOf(here), "https://here.example/");
+ // The chat on screen shows its own agent tab beside the user's; the other chat's tab is not in its strip.
+ assert.deepEqual(
+ chatTabs(useBrowserStore.getState()).map((tab) => tab.id),
+ [mine, here],
+ );
+ assert.equal(useBrowserStore.getState().activeTabId, here);
+ // The background page stays open while its run lasts, and is let go when it ends.
+ assert.ok(kept.includes(away ?? ""));
+ endBrowserAgentRun("run-away");
+ assert.ok((globals.__released as string[]).includes(away ?? ""));
+ // Going back to that chat shows its agent tab in an open panel.
+ store.showChat(chatKeyOfThread("away"));
+ assert.equal(useBrowserStore.getState().activeTabId, away);
+ assert.equal(useBrowserStore.getState().open, true);
+ store.showChat(chatKeyOfThread("shown"));
+});
+
+test("a question in a chat off screen waits in that chat, with a toast; Stop there ends only its run", async () => {
+ globals.__native = true;
+ useBrowserAgentStore.setState({ scriptAccess: "ask" });
+ const away = chatKeyOfThread("away");
+ await navigate("q0", "away", "https://away.example/next");
+ const pending = runBrowserAction(
+ { id: "q1", sessionId: "s", op: "evaluate", script: "return 1;", direction: "down", policy },
+ { runKey: "run-away", scope: "away", userText: "" },
+ );
+ let question = agentChat(away).question;
+ for (let i = 0; !question && i < 200; i++) {
+ await new Promise((resolve) => setTimeout(resolve, 10));
+ question = agentChat(away).question;
+ }
+ assert.equal(question?.ask, "script");
+ assert.equal(agentChat(chatKeyOfThread("shown")).question, null, "the chat on screen isn't asked");
+ const toasts = (globals.__toasts ?? []) as { options?: { id?: string } }[];
+ assert.ok(toasts.some((toast) => toast.options?.id === `browser-agent-question:${away}`));
+ // Stop in the shown chat's bar leaves the other chat's question open.
+ stopBrowserAgent(chatKeyOfThread("shown"));
+ assert.equal(agentChat(away).question?.id, question?.id);
+ stopBrowserAgent(away);
+ await pending;
+ assert.equal(resultOf("q1")?.error, STOPPED);
+});
+
+test("without native pages a chat off screen is told to wait for its chat", async () => {
+ globals.__native = false;
+ await navigate("w1", "elsewhere", "https://elsewhere.example/");
+ assert.equal(resultOf("w1")?.ok, false);
+ assert.match(resultOf("w1")?.error ?? "", /only runs while the chat is open here/);
+ assert.equal(agentChat(chatKeyOfThread("elsewhere")).tabId, null);
+});
+
+test("a compare pane's run drives its pair's browser, whatever the sidebar shows", async () => {
+ globals.__native = true;
+ await runBrowserAction(
+ { id: "c1", sessionId: "s", op: "navigate", url: "https://pair.example/", direction: "down", policy },
+ { runKey: "run-pane", scope: "pane-thread", chat: "compare:pair1", userText: "open https://pair.example/" },
+ );
+ assert.equal(resultOf("c1")?.ok, true, resultOf("c1")?.error);
+ const tabId = agentChat("compare:pair1").tabId;
+ assert.equal(useBrowserStore.getState().tabs.find((tab) => tab.id === tabId)?.chat, "compare:pair1");
+ assert.equal(agentChat(chatKeyOfThread("pane-thread")).tabId, null);
+});
+
+test("a new chat's run keeps its page when the chat is saved between actions", async () => {
+ globals.__native = true;
+ const store = useBrowserStore.getState();
+ store.closePanel();
+ store.showChat("single:draft");
+ await navigate("d1", "__default", "https://draft.example/");
+ assert.equal(resultOf("d1")?.ok, true, resultOf("d1")?.error);
+ const tabId = agentChat("single:draft").tabId;
+ assert.ok(tabId);
+ store.adoptChat("single:draft", chatKeyOfThread("saved"));
+ assert.equal(agentChat(chatKeyOfThread("saved")).tabId, tabId);
+ assert.equal("single:draft" in useBrowserAgentStore.getState().chats, false);
+ // The next action names the saved chat and reads the same page, not a second one.
+ await runBrowserAction(
+ { id: "d2", sessionId: "s", op: "snapshot", direction: "down", policy },
+ { runKey: "run-__default", scope: "saved", userText: "" },
+ );
+ assert.equal(resultOf("d2")?.ok, true, resultOf("d2")?.error);
+ assert.equal(useBrowserStore.getState().tabs.filter((tab) => tab.chat === chatKeyOfThread("saved")).length, 1);
+});
+
+test("a new chat's run stays in it after a switch, and Stop in the saved chat ends the action in flight", async () => {
+ globals.__native = true;
+ const store = useBrowserStore.getState();
+ store.closePanel();
+ store.showChat("single:draft2");
+ store.showChat(chatKeyOfThread("elsewhere2"));
+ // The run started in the new chat, which the user has since left: its page opens there, not here.
+ await runBrowserAction(
+ { id: "b1", sessionId: "s", op: "navigate", url: "https://bound.example/", direction: "down", policy },
+ { runKey: "run-bound", scope: "__default", chat: "single:draft2", userText: "open https://bound.example/" },
+ );
+ assert.equal(resultOf("b1")?.ok, true, resultOf("b1")?.error);
+ const tabId = agentChat("single:draft2").tabId;
+ assert.equal(useBrowserStore.getState().tabs.find((tab) => tab.id === tabId)?.chat, "single:draft2");
+ assert.equal(chatTabs(useBrowserStore.getState()).length, 0);
+
+ // Saved while an action waits on the user: Stop there ends it, and its question goes with it.
+ useBrowserAgentStore.setState({ scriptAccess: "ask" });
+ const pending = runBrowserAction(
+ { id: "b2", sessionId: "s", op: "evaluate", script: "return 1;", direction: "down", policy },
+ { runKey: "run-bound", scope: "__default", chat: "single:draft2", userText: "" },
+ );
+ for (let i = 0; !agentChat("single:draft2").question && i < 200; i++) await new Promise((resolve) => setTimeout(resolve, 10));
+ store.adoptChat("single:draft2", chatKeyOfThread("bound"));
+ assert.ok(agentChat(chatKeyOfThread("bound")).question);
+ stopBrowserAgent(chatKeyOfThread("bound"));
+ await pending;
+ assert.equal(resultOf("b2")?.error, STOPPED);
+ assert.equal(agentChat(chatKeyOfThread("bound")).question, null);
+});
+
diff --git a/studio/frontend/tests/browser-agent-drag.test.ts b/studio/frontend/tests/browser-agent-drag.test.ts
index 01d8c94e54d..6fd3f379c78 100644
--- a/studio/frontend/tests/browser-agent-drag.test.ts
+++ b/studio/frontend/tests/browser-agent-drag.test.ts
@@ -9,6 +9,8 @@ register("./helpers/browser-executor-resolver.mjs", import.meta.url);
const { runBrowserAction } = await import("../src/features/browser/agent/executor.ts");
const { useBrowserStore } = await import("../src/features/browser/store.ts");
const { stopBrowserAgent } = await import("../src/features/browser/agent/agent-store.ts");
+// Actions without a scope act for the chat on screen.
+const CHAT = "new";
const globals = globalThis as Record<string, unknown>;
const policy = { allowedDomains: [], blockedDomains: [] };
@@ -105,7 +107,7 @@ test("a drag whose page moves its address without a new document is cancelled, n
test("Stop during the checks before the drop cancels the drag instead of releasing it", async () => {
const { sent, result } = await drag("p2", "r", (op, sent) => {
- if (op.op === "describe" && sent.includes("pointer:move")) stopBrowserAgent();
+ if (op.op === "describe" && sent.includes("pointer:move")) stopBrowserAgent(CHAT);
});
assert.equal(result?.ok, false);
assert.ok(sent.includes("pointer:cancel"), sent.join(", "));
diff --git a/studio/frontend/tests/browser-agent-point.test.ts b/studio/frontend/tests/browser-agent-point.test.ts
index 9e83f50b919..49d64309607 100644
--- a/studio/frontend/tests/browser-agent-point.test.ts
+++ b/studio/frontend/tests/browser-agent-point.test.ts
@@ -7,7 +7,9 @@ import { test } from "node:test";
register("./helpers/browser-executor-resolver.mjs", import.meta.url);
const { runBrowserAction } = await import("../src/features/browser/agent/executor.ts");
-const { answerQuestion, stopBrowserAgent, useBrowserAgentStore } = await import("../src/features/browser/agent/agent-store.ts");
+const { agentChat, answerQuestion, stopBrowserAgent } = await import("../src/features/browser/agent/agent-store.ts");
+// Actions without a scope act for the chat on screen.
+const CHAT = "new";
const globals = globalThis as Record<string, unknown>;
const policy = { allowedDomains: [], blockedDomains: [] };
@@ -26,7 +28,7 @@ test("Stop pressed while an approved point click is rechecked keeps the click fr
if (op.op === "status") return { installed: true, result: { ok: true, quietMs: 1000, doc: "r1" } };
if (op.op === "describe") {
// The second describe is the recheck after the user's answer: Stop lands during it.
- if (++describes === 2) stopBrowserAgent();
+ if (++describes === 2) stopBrowserAgent(CHAT);
return { installed: true, result: { ok: true, info: buy, hit: "r5", sig: "s" } };
}
return { installed: true, result: { ok: true, text: "page", links: [] } };
@@ -34,10 +36,10 @@ test("Stop pressed while an approved point click is rechecked keeps the click fr
const run = { runKey: "r", userText: "open https://shop.example.com/" };
await runBrowserAction({ id: "c0", sessionId: "s", op: "navigate", url: "https://shop.example.com/", direction: "down", policy }, run);
const pending = runBrowserAction({ id: "c1", sessionId: "s", op: "click", x: 40, y: 20, direction: "down", policy }, run);
- let question = useBrowserAgentStore.getState().question;
+ let question = agentChat(CHAT).question;
for (let i = 0; !question && i < 200; i++) {
await new Promise((resolve) => setTimeout(resolve, 10));
- question = useBrowserAgentStore.getState().question;
+ question = agentChat(CHAT).question;
}
assert.ok(question, "the Buy button was not asked about");
answerQuestion(question.id, true);
diff --git a/studio/frontend/tests/browser-agent-script.test.ts b/studio/frontend/tests/browser-agent-script.test.ts
index 86114b412d4..5f2e904aa66 100644
--- a/studio/frontend/tests/browser-agent-script.test.ts
+++ b/studio/frontend/tests/browser-agent-script.test.ts
@@ -7,7 +7,9 @@ import { test } from "node:test";
register("./helpers/browser-executor-resolver.mjs", import.meta.url);
const { runBrowserAction } = await import("../src/features/browser/agent/executor.ts");
-const { answerQuestion, useBrowserAgentStore } = await import("../src/features/browser/agent/agent-store.ts");
+const { agentChat, answerQuestion, useBrowserAgentStore } = await import("../src/features/browser/agent/agent-store.ts");
+// Actions without a scope act for the chat on screen.
+const CHAT = "new";
const globals = globalThis as Record<string, unknown>;
const policy = { allowedDomains: [], blockedDomains: [] };
@@ -28,10 +30,10 @@ test("a script approved after Run scripts was set to Never does not run", async
await runBrowserAction({ id: "e0", sessionId: "s", op: "navigate", url: "https://example.com/", direction: "down", policy }, run);
useBrowserAgentStore.setState({ scriptAccess: "ask" });
const pending = runBrowserAction({ id: "e1", sessionId: "s", op: "evaluate", script: "return 1;", direction: "down", policy }, run);
- let question = useBrowserAgentStore.getState().question;
+ let question = agentChat(CHAT).question;
for (let i = 0; !question && i < 200; i++) {
await new Promise((resolve) => setTimeout(resolve, 10));
- question = useBrowserAgentStore.getState().question;
+ question = agentChat(CHAT).question;
}
assert.equal(question?.ask, "script");
useBrowserAgentStore.setState({ scriptAccess: "never" });
diff --git a/studio/frontend/tests/browser-native-view-layered.test.ts b/studio/frontend/tests/browser-native-view-layered.test.ts
index 07a6a44bf09..9c92fdd420d 100644
--- a/studio/frontend/tests/browser-native-view-layered.test.ts
+++ b/studio/frontend/tests/browser-native-view-layered.test.ts
@@ -102,8 +102,8 @@ const calls: Call[] = [];
register("./helpers/browser-store-resolver.mjs", import.meta.url);
register("./helpers/native-view-resolver.mjs", import.meta.url);
-const { useBrowserStore } = await import("../src/features/browser/store.ts");
-const { startNativeViews } = await import("../src/features/browser/native-view.ts");
+const { currentEntry, useBrowserStore } = await import("../src/features/browser/store.ts");
+const { keepInBackground, releaseBackground, startNativeViews } = await import("../src/features/browser/native-view.ts");
const settle = () => new Promise((resolve) => setTimeout(resolve, 5));
async function frame(): Promise<void> {
@@ -274,3 +274,71 @@ test("a menu over a layered page neither captures nor hides it, and owns its inp
assert.equal(placeholder.attributes.has("data-native-hole"), false, "closing the panel fills the hole");
assert.equal(html.vars.has("--native-hole-x"), false);
});
+
+test("a page at work in a chat off screen opens hidden, follows its tab, and outlives the panel", async () => {
+ const store = useBrowserStore.getState();
+ store.closePanel();
+ store.showChat("single:shown");
+ const tabId = store.openTabIn("single:away", "https://away.example/") ?? "";
+ calls.length = 0;
+ keepInBackground(tabId);
+ await settle();
+ const opened = calls.find(({ command }) => command === "browser_view_open_background");
+ assert.deepEqual([opened?.args?.tabId, opened?.args?.url], [tabId, "https://away.example/"]);
+ // The agent moves it on: the hidden page follows, and is never shown over this chat.
+ store.navigate(tabId, { url: "https://away.example/next" }, { replace: false });
+ await settle();
+ assert.ok(calls.some(({ command, args }) => command === "browser_view_navigate" && args?.url === "https://away.example/next"));
+ // A panel opening and closing over this chat leaves it open (closed pages go; a kept one is hidden).
+ store.openUrl("https://mine.example/");
+ const stop = startNativeViews();
+ await frame();
+ await frame();
+ stop();
+ assert.ok(!calls.some(({ command, args }) => command === "browser_view_show" && args?.tabId === tabId));
+ assert.ok(!calls.some(({ command, args }) => command === "browser_view_close" && args?.tabId === tabId));
+ assert.deepEqual(calls.at(-1), { command: "browser_view_show", args: { tabId: null } });
+ // Let go with no panel up, it closes.
+ releaseBackground(tabId);
+ assert.ok(calls.some(({ command, args }) => command === "browser_view_close" && args?.tabId === tabId));
+});
+
+test("a background page's popup opens in its own chat, and a page that fails to open isn't retried in a loop", async () => {
+ const store = useBrowserStore.getState();
+ store.closePanel();
+ store.showChat("single:viewer");
+ const tabId = store.openTabIn("single:worker", "https://worker.example/") ?? "";
+ // A target=_blank link the agent clicked there: the popup joins that chat, behind its front tab.
+ const listener = (globalThis as { nativeViewListener?: (event: { payload: unknown }) => void }).nativeViewListener;
+ listener?.({ payload: { kind: "newTab", tabId, url: "https://popup.example/" } });
+ const popup = useBrowserStore.getState().tabs.find((tab) => {
+ const entry = currentEntry(tab);
+ return entry.kind === "web" && entry.url === "https://popup.example/";
+ });
+ assert.equal(popup?.chat, "single:worker");
+ assert.equal(useBrowserStore.getState().tabs.filter((tab) => tab.chat === "single:viewer").length, 0);
+
+ // Opening it keeps failing: one try per request, not a busy loop.
+ const failing = store.openTabIn("single:worker", "https://broken.example/") ?? "";
+ const call = (globalThis as { nativeViewCall?: unknown }).nativeViewCall;
+ let opens = 0;
+ (globalThis as { nativeViewCall?: unknown }).nativeViewCall = (command: string, args?: Record<string, unknown>) => {
+ if (command === "browser_view_open_background" && args?.tabId === failing) {
+ opens += 1;
+ return Promise.reject(new Error("refused"));
+ }
+ return (call as (command: string, args?: Record<string, unknown>) => Promise<unknown>)(command, args);
+ };
+ keepInBackground(failing);
+ await settle();
+ await settle();
+ assert.equal(opens, 1);
+ assert.match(useBrowserStore.getState().tabs.find((tab) => tab.id === failing)?.nativeError ?? "", /refused/);
+ (globalThis as { nativeViewCall?: unknown }).nativeViewCall = call;
+ // The agent's next action opens it after all: the old failure no longer marks the page broken.
+ keepInBackground(failing);
+ await settle();
+ assert.equal(useBrowserStore.getState().tabs.find((tab) => tab.id === failing)?.nativeError, null);
+ releaseBackground(failing);
+ releaseBackground(tabId);
+});
diff --git a/studio/frontend/tests/browser-settings.test.ts b/studio/frontend/tests/browser-settings.test.ts
index 265bdf9a1d6..e7295a0522b 100644
--- a/studio/frontend/tests/browser-settings.test.ts
+++ b/studio/frontend/tests/browser-settings.test.ts
@@ -859,11 +859,12 @@ test("closing the last Downloads button dismisses its result, but a button swapp
});
test("the agent's Downloads rule covers its tab while a chat run is using it, and just after", async () => {
- const { agentDownloadRule, endBrowserAgentRun, useBrowserAgentStore } = await import(
+ const { agentDownloadRule, endBrowserAgentRun, patchAgentChat, useBrowserAgentStore } = await import(
"../src/features/browser/agent/agent-store.ts"
);
const blocked = { browsing: "ask", actions: "ask", downloads: "block", unlabelled: "ask" } as const;
- useBrowserAgentStore.setState({ tabId: "t1", runKey: "run1", permissions: { defaults: blocked, sites: {} } });
+ useBrowserAgentStore.setState({ permissions: { defaults: blocked, sites: {} } });
+ patchAgentChat("single:c1", { tabId: "t1", runKey: "run1" });
assert.equal(agentDownloadRule("t1", "https://example.com/", "https://example.com/f.pdf"), "block");
assert.equal(agentDownloadRule("t2", "https://example.com/", "https://example.com/f.pdf"), null);
// Another run ending leaves this one alone; this one ending hands the tab back to the user.
diff --git a/studio/frontend/tests/browser-store-chats.test.ts b/studio/frontend/tests/browser-store-chats.test.ts
new file mode 100644
index 00000000000..258744d6fd3
--- /dev/null
+++ b/studio/frontend/tests/browser-store-chats.test.ts
@@ -0,0 +1,145 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import { register } from "node:module";
+import { test } from "node:test";
+
+// Each chat keeps its own browser tabs: switching chats swaps them, and a run in a chat off screen
+// opens its tab there without moving the panel on screen.
+register("./helpers/browser-store-resolver.mjs", import.meta.url);
+const { chatKeyOfThread, chatTabs, currentEntry, useBrowserStore } = await import("../src/features/browser/store.ts");
+
+const urls = () =>
+ chatTabs(useBrowserStore.getState()).map((tab) => {
+ const entry = currentEntry(tab);
+ return entry.kind === "web" ? entry.url : entry.kind;
+ });
+
+test("switching chats keeps each chat's tabs, panel and active tab; a new chat gets a new tab", () => {
+ const store = useBrowserStore.getState();
+ const a = chatKeyOfThread("a");
+ const b = chatKeyOfThread("b");
+ store.showChat(a);
+ store.openUrl("https://one.example/");
+ store.openUrl("https://two.example/");
+ const two = useBrowserStore.getState().activeTabId;
+ store.activateTab(useBrowserStore.getState().tabs[0]?.id ?? "");
+ const one = useBrowserStore.getState().activeTabId;
+ assert.notEqual(one, two);
+
+ // The panel was open: the next chat opens on a new tab of its own, not on chat a's pages.
+ store.showChat(b);
+ assert.equal(useBrowserStore.getState().open, true);
+ assert.deepEqual(urls(), ["newtab"]);
+ store.openUrl("https://three.example/", { newTab: false });
+ assert.deepEqual(urls(), ["https://three.example/"]);
+ store.closePanel();
+
+ // Back in chat a: its two pages, the one it had in front, and its panel open.
+ store.showChat(a);
+ assert.deepEqual(urls(), ["https://one.example/", "https://two.example/"]);
+ assert.equal(useBrowserStore.getState().activeTabId, one);
+ assert.equal(useBrowserStore.getState().open, true);
+ // Chat b closed its panel, and comes back closed with its page.
+ store.showChat(b);
+ assert.equal(useBrowserStore.getState().open, false);
+ assert.deepEqual(urls(), ["https://three.example/"]);
+ // A chat's last tab closing closes only its panel; the other chat's tabs stay.
+ store.openPanel();
+ store.closeTab(chatTabs(useBrowserStore.getState())[0]?.id ?? "");
+ assert.equal(useBrowserStore.getState().open, false);
+ store.showChat(a);
+ assert.deepEqual(urls(), ["https://one.example/", "https://two.example/"]);
+});
+
+test("a tab opened for a chat off screen waits there; the chat on screen is untouched", () => {
+ const store = useBrowserStore.getState();
+ const shown = chatKeyOfThread("shown");
+ const away = chatKeyOfThread("away");
+ store.closePanel();
+ store.showChat(shown);
+ store.openUrl("https://mine.example/");
+ const mine = useBrowserStore.getState().activeTabId;
+ const sequence = useBrowserStore.getState().openSequence;
+
+ const agentTab = store.openTabIn(away, "https://agent.example/");
+ assert.ok(agentTab);
+ assert.equal(useBrowserStore.getState().activeTabId, mine);
+ assert.equal(useBrowserStore.getState().openSequence, sequence, "the panel on screen is not re-opened");
+ assert.deepEqual(urls(), ["https://mine.example/"]);
+ assert.equal(useBrowserStore.getState().tabs.find((tab) => tab.id === agentTab)?.chat, away);
+
+ // The user's own tab in front of that chat when they come back is the agent's.
+ store.revealIn(away, agentTab ?? "");
+ store.showChat(away);
+ assert.equal(useBrowserStore.getState().open, true);
+ assert.equal(useBrowserStore.getState().activeTabId, agentTab);
+ assert.deepEqual(urls(), ["https://agent.example/"]);
+
+ // Closing a tab of a chat off screen moves that chat's front tab, not this one's.
+ store.openUrl("https://second.example/");
+ const second = useBrowserStore.getState().activeTabId;
+ store.showChat(shown);
+ store.closeTab(second ?? "");
+ store.showChat(away);
+ assert.equal(useBrowserStore.getState().activeTabId, agentTab);
+});
+
+test("a new chat's tabs follow it to its id on the first save, and moving a tab stays within the chat", () => {
+ const store = useBrowserStore.getState();
+ store.closePanel();
+ store.showChat("single:nonce-1");
+ store.openUrl("https://draft.example/");
+ store.openUrl("https://draft2.example/");
+ store.adoptChat("single:nonce-1", chatKeyOfThread("saved"));
+ assert.equal(useBrowserStore.getState().chat, chatKeyOfThread("saved"));
+ assert.deepEqual(urls(), ["https://draft.example/", "https://draft2.example/"]);
+ // Other chats' tabs sit between in the full list; the strip order is what moves.
+ const [first, second] = chatTabs(useBrowserStore.getState());
+ store.moveTab(second?.id ?? "", 0);
+ assert.deepEqual(
+ chatTabs(useBrowserStore.getState()).map((tab) => tab.id),
+ [second?.id, first?.id],
+ );
+ // Opening the same address again focuses this chat's tab, never another chat's.
+ store.closePanel();
+ store.showChat(chatKeyOfThread("other"));
+ store.openUrl("https://draft.example/");
+ assert.equal(chatTabs(useBrowserStore.getState()).length, 1);
+});
+
+test("a deleted chat's tabs and panel state go with it; the chat on screen keeps its own", () => {
+ const store = useBrowserStore.getState();
+ store.closePanel();
+ store.showChat(chatKeyOfThread("keep"));
+ store.openUrl("https://kept.example/");
+ const gone = chatKeyOfThread("gone");
+ const popup = store.openTabIn(gone, "https://gone.example/", { background: true });
+ assert.ok(popup);
+ assert.ok(gone in useBrowserStore.getState().chats);
+ // Chat deletion reaches the store through the chat feature's listener.
+ (globalThis as { __chatDeleted?: (ids: string[], pairIds: string[]) => void }).__chatDeleted?.(["gone"], []);
+ assert.equal(useBrowserStore.getState().tabs.some((tab) => tab.chat === gone), false);
+ assert.equal(gone in useBrowserStore.getState().chats, false);
+ assert.deepEqual(urls(), ["https://kept.example/"]);
+});
+
+test("deleting the chat on screen drops its tabs; deleting a comparison drops its pair's browser", () => {
+ const store = useBrowserStore.getState();
+ const deleted = (globalThis as { __chatDeleted?: (threadIds: string[], pairIds: string[]) => void }).__chatDeleted;
+ store.closePanel();
+ store.showChat(chatKeyOfThread("current"));
+ store.openUrl("https://current.example/");
+ deleted?.(["current"], []);
+ assert.equal(useBrowserStore.getState().tabs.some((tab) => tab.chat === chatKeyOfThread("current")), false);
+ assert.equal(useBrowserStore.getState().open, false);
+ // The next chat shown does not bring the deleted one's state back.
+ store.showChat(chatKeyOfThread("next"));
+ assert.equal(chatKeyOfThread("current") in useBrowserStore.getState().chats, false);
+
+ store.openTabIn("compare:pair9", "https://pair.example/", { background: true });
+ deleted?.(["left", "right"], ["pair9"]);
+ assert.equal(useBrowserStore.getState().tabs.some((tab) => tab.chat === "compare:pair9"), false);
+ assert.equal("compare:pair9" in useBrowserStore.getState().chats, false);
+});
diff --git a/studio/frontend/tests/helpers/browser-executor-resolver.mjs b/studio/frontend/tests/helpers/browser-executor-resolver.mjs
index c180551b76d..a860d8abf82 100644
--- a/studio/frontend/tests/helpers/browser-executor-resolver.mjs
+++ b/studio/frontend/tests/helpers/browser-executor-resolver.mjs
@@ -9,11 +9,17 @@ const stub = (source) => ({ url: `data:text/javascript,${encodeURIComponent(sour
const STUBS = {
"../page-frame": "export const requestFrameAgent = (tabId, op, code) => globalThis.__frameAgent(tabId, op, code);",
- "../capture": "export const screenshotPage = async () => null;",
- "../native-view": "export const hasNativeView = () => false; export const nativeAction = () => {};",
+ "../capture": "export const screenshotPage = async () => null; export const captureHiddenPage = async () => null;",
+ // Native pages only when a test sets globalThis.__native; it answers their calls via __callNative.
+ "../native-view":
+ "export const hasNativeView = () => globalThis.__native === true; export const nativeAction = () => {};" +
+ " export const keepInBackground = (tabId) => globalThis.__keep?.(tabId);" +
+ " export const releaseBackground = (tabId) => void (globalThis.__released ??= []).push(tabId);" +
+ " export const whenViewOpen = async () => true;",
"../native-support":
- "export const useNativeBrowser = { getState: () => ({ enabled: false }) }; export const callNative = async () => null;",
- "../panel-availability": "export const browserPanelAvailable = () => true;",
+ "export const useNativeBrowser = { getState: () => ({ enabled: globalThis.__native === true }) };" +
+ " export const callNative = async (command, args) => (globalThis.__callNative ? globalThis.__callNative(command, args) : null);",
+ "../panel-availability": "export const browserPanelAvailable = () => globalThis.__panel !== false;",
"../history-store": "export const useBrowserHistoryStore = { getState: () => ({ recordVisit() {} }) };",
};
diff --git a/studio/frontend/tests/helpers/browser-store-resolver.mjs b/studio/frontend/tests/helpers/browser-store-resolver.mjs
index 5dda4050896..8557b9483d9 100644
--- a/studio/frontend/tests/helpers/browser-store-resolver.mjs
+++ b/studio/frontend/tests/helpers/browser-store-resolver.mjs
@@ -7,7 +7,11 @@ const CHAT_STUB =
encodeURIComponent(
"export const useChatArtifactsStore = { getState: () => ({ closeArtifactSurface() {} }) };" +
" const runtime = { incognito: false, setIncognito: (incognito) => void (runtime.incognito = incognito) };" +
- " export const useChatRuntimeStore = { getState: () => runtime };",
+ " export const useChatRuntimeStore = { getState: () => runtime };" +
+ " export const useChatNavigationStore = { getState: () => ({}) };" +
+ " export const onChatThreadsDeleted = (listener) => void (globalThis.__chatDeleted = listener);" +
+ " export const visibleChatItems = () => globalThis.__chatItems ?? [];" +
+ " export const openChatItemById = (item) => void (globalThis.__openedChats ??= []).push(item);",
);
const I18N_STUB =
diff --git a/studio/frontend/tests/helpers/store-stubs/sidebar-items-deps.ts b/studio/frontend/tests/helpers/store-stubs/sidebar-items-deps.ts
index 630f2840510..a760f18a2b2 100644
--- a/studio/frontend/tests/helpers/store-stubs/sidebar-items-deps.ts
+++ b/studio/frontend/tests/helpers/store-stubs/sidebar-items-deps.ts
@@ -86,6 +86,7 @@ export function clearComposerDraft(): void {}
export async function offerToDeleteKeptSandboxes(): Promise<void> {}
export async function stopChatThread(): Promise<void> {}
export function markChatThreadsDeleted(): void {}
+export function notifyChatThreadsDeleted(): void {}
export function removeChatThreadTombstones(): void {}
export function requestPromptQueueStop(): void {}
export async function repairLegacyChatTitles(): Promise<void> {}
diff --git a/studio/frontend/tests/saved-branch-head.test.ts b/studio/frontend/tests/saved-branch-head.test.ts
index cc6a0e9d7ff..b47f1c2939f 100644
--- a/studio/frontend/tests/saved-branch-head.test.ts
+++ b/studio/frontend/tests/saved-branch-head.test.ts
@@ -91,6 +91,7 @@ type DeleteModule = {
function sidebar(deleteResult: () => Promise<string[]>) {
const cleared: string[] = [];
const restored: string[][] = [];
+ const notified: string[][] = [];
const module = loadWithStubs<DeleteModule>(
new URL("../src/features/chat/hooks/use-chat-sidebar-items.ts", import.meta.url),
{
@@ -109,23 +110,25 @@ function sidebar(deleteResult: () => Promise<string[]>) {
"../utils/stop-chat-thread": { stopChatThread: () => {} },
"../utils/chat-thread-tombstones": {
markChatThreadsDeleted: () => {},
+ notifyChatThreadsDeleted: (ids: string[]) => notified.push(ids),
removeChatThreadTombstones: (ids: string[]) => restored.push(ids),
},
"../utils/prompt-queue-boundary": { requestPromptQueueStop: () => {} },
"../utils/repair-legacy-chat-titles": { repairLegacyChatTitles: () => {} },
},
);
- return { ...module, cleared, restored };
+ return { ...module, cleared, restored, notified };
}
test("deleting a chat clears its saved branch once the delete holds", async () => {
- const { deleteChatItems, cleared } = sidebar(async () => []);
+ const { deleteChatItems, cleared, notified } = sidebar(async () => []);
await deleteChatItems([{ type: "single", id: "t" }], undefined, () => {});
assert.deepEqual(cleared, ["t"]);
+ assert.deepEqual(notified, [["t"]]);
});
test("a rejected delete keeps the saved branch of the chat it brings back", async () => {
- const { deleteChatItems, cleared, restored } = sidebar(async () => {
+ const { deleteChatItems, cleared, restored, notified } = sidebar(async () => {
throw new Error("delete failed");
});
await assert.rejects(
@@ -134,4 +137,6 @@ test("a rejected delete keeps the saved branch of the chat it brings back", asyn
);
assert.deepEqual(restored, [["t"]]);
assert.deepEqual(cleared, []);
+ // State kept per chat elsewhere (its browser tabs) is told only once the delete holds.
+ assert.deepEqual(notified, []);
});
diff --git a/studio/src-tauri/src/browser_capture.rs b/studio/src-tauri/src/browser_capture.rs
index 106a4067e48..ae32a14721a 100644
--- a/studio/src-tauri/src/browser_capture.rs
+++ b/studio/src-tauri/src/browser_capture.rs
@@ -6,13 +6,16 @@
//! is captured from the app's webview, cropped to the page area. Native snapshots need no
//! screen-share prompt, which the web build has to ask for.
-use crate::browser_webview::{require_main, view, ViewBounds};
+use crate::browser_webview::{
+ end_capture, require_main, show_for_capture, view, BrowserViews, ViewBounds,
+};
use std::sync::Mutex;
use std::time::Duration;
use tauri::ipc::Response;
use tauri::{Manager, Runtime, Webview};
const CAPTURE_TIMEOUT: Duration = Duration::from_secs(10);
+const SHOW_FOR_CAPTURE_PAINT: Duration = Duration::from_millis(150);
/// Larger than any screen's worth of pixels; a bigger answer is not a screenshot.
const MAX_PNG_BYTES: usize = 64 * 1024 * 1024;
@@ -36,6 +39,8 @@ pub async fn browser_capture<R: Runtime>(
bounds: Option<ViewBounds>,
) -> Result<Response, String> {
require_main(&webview)?;
+ let app = webview.app_handle().clone();
+ let tab = tab_id.clone();
let (target, clip) = match (tab_id, bounds) {
(Some(tab_id), _) => (view(webview.app_handle(), &tab_id)?, None),
(None, Some(bounds)) => {
@@ -61,10 +66,25 @@ pub async fn browser_capture<R: Runtime>(
let _ = sender.send(result);
}
});
- target
+ let views = app.state::<BrowserViews>();
+ let shown_for_shot = tab
+ .as_deref()
+ .is_some_and(|tab| show_for_capture(&views, &target, tab));
+ if shown_for_shot {
+ // A frame for the page to paint where it now sits.
+ tokio::time::sleep(SHOW_FOR_CAPTURE_PAINT).await;
+ }
+ let captured = target
.with_webview(move |platform| platform_capture(platform, clip, done))
- .map_err(|error| error.to_string())?;
- let png = match tokio::time::timeout(CAPTURE_TIMEOUT, receiver).await {
+ .map_err(|error| error.to_string());
+ let png = match captured {
+ Ok(()) => tokio::time::timeout(CAPTURE_TIMEOUT, receiver).await,
+ Err(error) => Ok(Ok(Err(error))),
+ };
+ if let (true, Some(tab)) = (shown_for_shot, tab.as_deref()) {
+ end_capture(&views, &target, tab);
+ }
+ let png = match png {
Ok(Ok(result)) => result?,
_ => return Err("The screenshot didn't finish".into()),
};
diff --git a/studio/src-tauri/src/browser_webview.rs b/studio/src-tauri/src/browser_webview.rs
index 2fd6e07eef7..02dbe9f2629 100644
--- a/studio/src-tauri/src/browser_webview.rs
+++ b/studio/src-tauri/src/browser_webview.rs
@@ -160,6 +160,10 @@ struct ViewsState {
muted: HashSet<String>,
/// Tabs whose view committed a page of its own since it opened or was last sent somewhere; until then a download's site is the address asked for (or the opener).
committed: HashSet<String>,
+ /// Hidden tabs shown just off the window for a screenshot (`show_for_capture`); a switch leaves them be.
+ capturing: HashSet<String>,
+ /// Where `browser_view_show` last put each tab, for a capture that a switch overtook (`end_capture`).
+ placed: HashMap<String, Rect>,
}
pub fn new_browser_views() -> BrowserViews {
@@ -1421,11 +1425,17 @@ pub async fn browser_view_show<R: Runtime>(
if parked {
position.x = -size.width - PARK_GAP;
}
- view.set_bounds(Rect {
+ let rect = Rect {
position: position.into(),
size: size.into(),
- })
- .map_err(|error| error.to_string())?;
+ };
+ state
+ .inner
+ .lock()
+ .unwrap()
+ .placed
+ .insert(tab_id.clone(), rect);
+ view.set_bounds(rect).map_err(|error| error.to_string())?;
view
}
None if parked => return Err("no page to park".into()),
@@ -1446,8 +1456,18 @@ pub async fn browser_view_show<R: Runtime>(
if let Some(view) = &target {
view.show().map_err(|error| error.to_string())?;
}
+ let capturing: HashSet<String> = state
+ .inner
+ .lock()
+ .unwrap()
+ .capturing
+ .iter()
+ .filter_map(|tab_id| label_for(tab_id).ok())
+ .collect();
for view in browser_views(&app) {
- if Some(view.label()) != target.as_ref().map(|view| view.label()) {
+ if Some(view.label()) != target.as_ref().map(|view| view.label())
+ && !capturing.contains(view.label())
+ {
let _ = view.hide();
}
}
@@ -1455,6 +1475,79 @@ pub async fn browser_view_show<R: Runtime>(
Ok(())
}
+/// Opens a tab's page without showing it: the agent at work in a chat that isn't on screen. Made
+/// just off the window and hidden, so nothing flashes and the page on screen stays.
+#[tauri::command]
+pub async fn browser_view_open_background<R: Runtime>(
+ webview: Webview<R>,
+ tab_id: String,
+ url: String,
+ bounds: ViewBounds,
+) -> Result<(), String> {
+ require_main(&webview)?;
+ let label = label_for(&tab_id)?;
+ if webview.app_handle().get_webview(&label).is_some() {
+ return Ok(());
+ }
+ let url = parse_page_url(&url)?;
+ #[cfg(target_os = "macos")]
+ crate::browser_layer::install(&webview);
+ let off_window = ViewBounds {
+ x: -bounds.width - PARK_GAP,
+ ..bounds
+ };
+ let page = create_view(&webview, &tab_id, url, &off_window)?;
+ if let Err(error) = page.hide() {
+ // The frontend never records a view that failed to open, so it would never close this one.
+ let _ = close_view(&page);
+ return Err(error.to_string());
+ }
+ Ok(())
+}
+
+/// A hidden page paints nothing to capture (WebView2 doesn't render it; WebKit's snapshot waits
+/// on a paint that never comes), so it shows just off the window for the shot. True if it did.
+pub(crate) fn show_for_capture<R: Runtime>(
+ views: &BrowserViews,
+ page: &Webview<R>,
+ tab_id: &str,
+) -> bool {
+ {
+ let mut inner = views.inner.lock().unwrap();
+ if inner.shown.as_deref() == Some(tab_id) || !inner.capturing.insert(tab_id.to_string()) {
+ return false;
+ }
+ }
+ let size = page
+ .bounds()
+ .ok()
+ .zip(page.window().scale_factor().ok())
+ .map(|(rect, factor)| rect.size.to_logical::<f64>(factor))
+ .unwrap_or(LogicalSize::new(1024.0, 768.0));
+ let position = LogicalPosition::new(-size.width - PARK_GAP, 0.0);
+ let _ = page.set_bounds(Rect {
+ position: position.into(),
+ size: size.into(),
+ });
+ if page.show().is_err() {
+ views.inner.lock().unwrap().capturing.remove(tab_id);
+ return false;
+ }
+ true
+}
+
+/// Hides a page `show_for_capture` showed. One that became the shown page meanwhile goes back
+/// where the switch put it, not off the window.
+pub(crate) fn end_capture<R: Runtime>(views: &BrowserViews, page: &Webview<R>, tab_id: &str) {
+ let mut inner = views.inner.lock().unwrap();
+ inner.capturing.remove(tab_id);
+ if inner.shown.as_deref() != Some(tab_id) {
+ let _ = page.hide();
+ } else if let Some(rect) = inner.placed.get(tab_id) {
+ let _ = page.set_bounds(*rect);
+ }
+}
+
#[tauri::command]
pub fn browser_view_navigate<R: Runtime>(
webview: Webview<R>,
@@ -1851,6 +1944,7 @@ pub fn browser_view_close<R: Runtime>(
inner.urls.remove(&tab_id);
inner.committed.remove(&tab_id);
inner.download_starts.remove(&tab_id);
+ inner.placed.remove(&tab_id);
// `muted` stays: a pruned view reopens muted; unmuting is what forgets it.
if inner.shown.as_deref() == Some(tab_id.as_str()) {
set_shown(&state, &mut inner, None);
diff --git a/studio/src-tauri/src/main.rs b/studio/src-tauri/src/main.rs
index 7b8b1b0d87d..ab4e1055963 100644
--- a/studio/src-tauri/src/main.rs
+++ b/studio/src-tauri/src/main.rs
@@ -2269,6 +2269,7 @@ fn main() {
app_menu::set_app_menu_actions,
browser_webview::browser_view_supported,
browser_webview::browser_view_show,
+ browser_webview::browser_view_open_background,
browser_webview::browser_view_navigate,
browser_webview::browser_view_action,
browser_webview::browser_view_zoom,