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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: keep a chat's HTML pages with that chat (#12985)
Nilay6a0f1e85b3b2
diff --git a/studio/frontend/src/features/browser/store.ts b/studio/frontend/src/features/browser/store.tsindex c6a8ef33d16..e39460d0940 100644--- a/studio/frontend/src/features/browser/store.ts+++ b/studio/frontend/src/features/browser/store.ts@@ -16,7 +16,7 @@ export type BrowserEntry =url: string;method?: "GET" | "POST";body?: string;- /** The page that sent the tab here (link, form, script, refresh); a file here downloads on its behalf. */+ /** source page for link, form, script, or refresh; files reached here download on its behalf. */from?: string;}| {@@ -24,10 +24,11 @@ export type BrowserEntry =fileId: string;name: string;contentType: string;- /** Show as text even if named .html (text extracted from a document). */+ /** show document-extracted text as text even when named .html. */plainText?: boolean;- /** The tab's openKey while this entry shows, so Back restores it. */+ /** the tab's openKey while this entry shows, so Back restores it. */openKey?: string;+ chatPage?: boolean;};export type InternalPage = "history" | "downloads" | "bookmarks";@@ -40,8 +41,7 @@ export type ChatDock = "minimized" | "composer" | "expanded";export type RequestEdits = (prompt: string) => void;-/** Resolves false when the composer refused them (it says why), so the marks stay.- * `files` (an annotation screenshot) go in the same message. */+/** false keeps marks when the composer rejects them and explains why; `files` carries the annotation screenshot. */export type SendAnnotations = (annotations: DocumentAnnotations, files?: File[]) => Promise<boolean>;/** Stages a file in the chat's composer; false when it refused it (it says why). */@@ -275,20 +275,20 @@ type BrowserState = {closePanel: () => void;togglePanel: () => void;newTab: () => void;- /** A new tab just after `tabId`, as its menu's New tab to the right opens. */newTabAfter: (tabId: string) => void;- /** A copy of the tab and its history, just after it. */+ /** copies the tab and its history immediately after it. */duplicateTab: (tabId: string) => void;- /** Opens a pinned page in a tab of its own, or shows the tab already showing it. */+ /** opens a pinned page in its own tab or focuses the tab already showing it. */openPinned: (pinnedId: string, url: string, title: string) => void;setTabPinned: (tabId: string, pinnedId: string | null) => void;renamingTabId: string | null;setRenamingTab: (tabId: string | null) => void;- /** A name for the tab, or null for the page's own title. */+ /** sets a tab name, or null to use the page title. */renameTab: (tabId: string, title: string | null) => void;setMuted: (tabId: string, muted: boolean) => void;closeOtherTabs: (tabId: string) => void;closeTabsToRight: (tabId: string) => void;+ closeChatPages: () => void;openUrl: (url: string,options?: { newTab?: boolean; background?: boolean; method?: "GET" | "POST"; body?: string; from?: string },@@ -299,7 +299,7 @@ type BrowserState = {request: { url: string; method?: "GET" | "POST"; body?: string; from?: string },options?: { replace?: boolean },) => void;- /** A page-sent entry that became a download: back to that page and out of history, unless the tab moved on. */+ /** removes a page-sent download from history and returns to its sending page unless the tab moved on. */leaveDownload: (tabId: string, entry: BrowserEntry) => void;goBack: (tabId: string) => void;goForward: (tabId: string) => void;@@ -481,6 +481,11 @@ export const useBrowserStore = create<BrowserState>((set, get) => {if (index < 0) return;for (const tab of tabs.slice(index + 1)) get().closeTab(tab.id);},+ closeChatPages: () => {+ for (const tab of get().tabs) {+ if (tab.history.some((entry) => entry.kind === "file" && entry.chatPage)) get().closeTab(tab.id);+ }+ },openUrl: (url, options) => {if (!isWeb(url)) return;if (options?.method === "POST") {@@ -509,11 +514,12 @@ export const useBrowserStore = create<BrowserState>((set, get) => {contentType: contentType || blob.type,plainText,...(openKey ? { openKey } : {}),+ ...(key?.startsWith("html:") ? { chatPage: true } : {}),};const existing = openKey ? get().tabs.find((tab) => tab.openKey === openKey) : undefined;if (openKey && existing) {focusExisting(openKey);- // The file may have changed since it opened: refresh in order per tab, so the last reopen wins.+ // refresh changed files in order per tab so the last reopen wins.const previous = refreshes.get(existing.id) ?? Promise.resolve();queuedFiles.add(fileId);const next = previous.then(async () => {diff --git a/studio/frontend/src/features/chat/chat-page.tsx b/studio/frontend/src/features/chat/chat-page.tsxindex f7d708023dd..3e993196a2a 100644--- a/studio/frontend/src/features/chat/chat-page.tsx+++ b/studio/frontend/src/features/chat/chat-page.tsx@@ -137,6 +137,7 @@ import {useCallback,useContext,useEffect,+ useLayoutEffect,useMemo,useRef,useState,@@ -2858,9 +2859,7 @@ export function ChatPage({const persistedActiveThreadId = isAssistantLocalThreadId(activeThreadId)? null: activeThreadId;- // A ?new=<nonce> chat has no thread in the URL before or after its first send, and for the first render the- // store still holds the PREVIOUS chat's id until ThreadNewChatSwitch blanks it, so latch on having seen it- // blanked for this nonce.+ // ?new=<nonce> lacks a URL thread; wait for ThreadNewChatSwitch to clear stale activeThreadId.const newChatBlankedRef = useRef<string | null>(null);if (search.new &&@@ -2872,6 +2871,17 @@ export function ChatPage({search.new && newChatBlankedRef.current === search.new? persistedActiveThreadId: null;+ // leaving Chat clears activeThreadId and restores it later, so the shown thread id is latched.+ const newChatIdentityBlankedRef = useRef<string | null>(null);+ if (search.new && activeThreadId === null) {+ newChatIdentityBlankedRef.current = search.new;+ }+ // re-latch after every blank: a nonce whose chat was deleted while hidden comes back on a fresh thread.+ const newChatRef = useRef<{ nonce: string; threadId: string } | null>(null);+ if (search.new && activeThreadId && newChatIdentityBlankedRef.current === search.new) {+ newChatRef.current = { nonce: search.new, threadId: activeThreadId };+ newChatIdentityBlankedRef.current = null;+ }const modelOperationInProgress = useChatRuntimeStore((state) => state.modelLoading,);@@ -3391,6 +3401,50 @@ export function ChatPage({clearAutoOpenedArtifacts();}, [artifactViewKey]);+ const newChat = newChatRef.current;+ const newChatShownId =+ newChat && view.mode === "single" && view.newThreadNonce === newChat.nonce ? newChat.threadId : null;+ const projectChatBlankedRef = useRef<{ projectId: string; nonce: string } | null>(null);+ if (view.mode === "project" && activeThreadId === null) {+ projectChatBlankedRef.current = { projectId: view.projectId, nonce: projectNewThreadNonce };+ }+ const projectChatRef = useRef<{ projectId: string; nonce: string; threadId: string } | null>(null);+ const projectChatBlanked = projectChatBlankedRef.current;+ if (+ view.mode === "project" &&+ activeThreadId &&+ projectChatBlanked?.projectId === view.projectId &&+ projectChatBlanked.nonce === projectNewThreadNonce &&+ (projectChatRef.current?.projectId !== view.projectId ||+ projectChatRef.current.nonce !== projectNewThreadNonce)+ ) {+ projectChatRef.current = {+ projectId: view.projectId,+ nonce: projectNewThreadNonce,+ threadId: activeThreadId,+ };+ }+ const projectChat = projectChatRef.current;+ const projectChatShownId =+ projectChat &&+ view.mode === "project" &&+ projectChat.projectId === view.projectId &&+ projectChat.nonce === projectNewThreadNonce+ ? projectChat.threadId+ : null;+ const shownChatKey =+ view.mode === "single"+ ? `single:${view.threadId ?? newChatShownId ?? activeThreadId ?? view.newThreadNonce ?? "new"}`+ : view.mode === "project"+ ? projectChatShownId+ ? `single:${projectChatShownId}`+ : `project:${view.projectId}:${projectNewThreadNonce}`+ : artifactViewKey;+ // biome-ignore lint/correctness/useExhaustiveDependencies: another chat on screen is the reset+ useLayoutEffect(() => {+ useBrowserStore.getState().closeChatPages();+ }, [shownChatKey]);+const hasActiveModel = Boolean(inferenceParams.checkpoint);const chatContextKey = `${view.mode}|${activeThreadId ?? ""}|${search.new ?? ""}|${search.project ?? ""}`;const [pendingHubAutoLoad, setPendingHubAutoLoad] =diff --git a/studio/frontend/tests/artifact-panel-reopen.test.ts b/studio/frontend/tests/artifact-panel-reopen.test.tsindex 8fbe55fbae4..6671c16b11e 100644--- a/studio/frontend/tests/artifact-panel-reopen.test.ts+++ b/studio/frontend/tests/artifact-panel-reopen.test.ts@@ -108,3 +108,62 @@ test("a project's browser overlay stages Request edits in the project composer",assert.match(landing, /useStagedFixPrompt\(\s*useChatArtifactsStore\(\(state\) => state\.pendingFixPrompt\),\s*active,\s*\);/);assert.match(page, /useStagedFixPrompt\(pendingFixPrompt, chatActive\);/);});++test("leaving Chat and coming back keeps a new chat's pages open", () => {+ const page = read("../src/features/chat/chat-page.tsx");+ // outside Chat, activeThreadId is cleared and restored, so ?new uses the first shown thread id.+ assert.match(page, /if \(search\.new && activeThreadId === null\) \{\s*newChatIdentityBlankedRef\.current = search\.new;/);+ assert.match(page, /if \(search\.new && activeThreadId && newChatIdentityBlankedRef\.current === search\.new\) \{/);+ assert.match(page, /view\.newThreadNonce === newChat\.nonce \? newChat\.threadId : null;/);+ const key = page.slice(page.indexOf("const shownChatKey ="), page.indexOf("closeChatPages();"));+ assert.match(key, /view\.threadId \?\? newChatShownId \?\? activeThreadId/);+});++test("a new chat deleted while Chat is hidden re-latches onto its replacement thread", () => {+ const page = read("../src/features/chat/chat-page.tsx");+ const identity = page.slice(page.indexOf("const newChatIdentityBlankedRef"), page.indexOf("const modelOperationInProgress"));+ // ThreadNewChatSwitch starts a fresh thread under the same nonce when the recorded one is tombstoned.+ assert.match(identity, /newChatRef\.current = \{ nonce: search\.new, threadId: activeThreadId \};\s*newChatIdentityBlankedRef\.current = null;/);+ assert.doesNotMatch(identity, /newChatRef\.current\?\.nonce !== search\.new/);+});++test("Back and Forward cannot assign an outgoing local thread to an incoming new-chat nonce", () => {+ const page = read("../src/features/chat/chat-page.tsx");+ const identity = page.slice(page.indexOf("const newChatIdentityBlankedRef"), page.indexOf("const modelOperationInProgress"));+ assert.match(identity, /search\.new && activeThreadId === null/);+ assert.match(identity, /newChatIdentityBlankedRef\.current === search\.new/);+ assert.doesNotMatch(identity, /isAssistantLocalThreadId/);+});++test("a chat switch removes stale pages before destination artifacts auto-open", () => {+ const page = read("../src/features/chat/chat-page.tsx");+ const cleanup = page.slice(page.indexOf("const shownChatKey ="), page.indexOf("const hasActiveModel"));+ assert.match(cleanup, /useLayoutEffect\(\(\) => \{\s*useBrowserStore\.getState\(\)\.closeChatPages\(\);/);+ const card = read("../src/features/chat/artifacts/artifact-card.tsx");+ const autoOpen = card.slice(card.indexOf("const autoOpenAttemptedRef"), card.indexOf("const pageTitle"));+ assert.match(autoOpen, /useEffect\(\(\) => \{/);+});++test("a new chat started inside a project closes the previous project chat's pages", () => {+ const page = read("../src/features/chat/chat-page.tsx");+ const key = page.slice(page.indexOf("const shownChatKey ="), page.indexOf("closeChatPages();"));+ // the project URL stays ?project=, so projectNewThreadNonce distinguishes its chats.+ assert.match(key, /`project:\$\{view\.projectId\}:\$\{projectNewThreadNonce\}`/);+});++test("a project chat keeps its pages when its new thread gains a URL", () => {+ const page = read("../src/features/chat/chat-page.tsx");+ const key = page.slice(page.indexOf("const shownChatKey ="), page.indexOf("closeChatPages();"));+ // ProjectLanding shows a newly-created thread before its row changes the route to ?thread=.+ // Both representations use the same identity once the runtime has assigned the thread id.+ assert.match(key, /view\.mode === "project"\s*\? projectChatShownId\s*\? `single:\$\{projectChatShownId\}`/);+ assert.match(key, /view\.threadId \?\? newChatShownId \?\? activeThreadId/);+});++test("leaving Chat and coming back keeps a new project chat's pages open", () => {+ const page = read("../src/features/chat/chat-page.tsx");+ const key = page.slice(page.indexOf("const projectChatBlankedRef"), page.indexOf("closeChatPages();"));+ // RootLayout clears activeThreadId while Chat is hidden, so retain the first created thread by nonce.+ assert.match(key, /projectChatRef\.current = \{\s*projectId: view\.projectId,\s*nonce: projectNewThreadNonce,\s*threadId: activeThreadId,/);+ assert.match(key, /projectChat\.nonce === projectNewThreadNonce\s*\? projectChat\.threadId\s*:\s*null/);+});diff --git a/studio/frontend/tests/browser-store-reopen.test.ts b/studio/frontend/tests/browser-store-reopen.test.tsindex 7ff3a1a1a86..01b408b1bdd 100644--- a/studio/frontend/tests/browser-store-reopen.test.ts+++ b/studio/frontend/tests/browser-store-reopen.test.ts@@ -218,3 +218,56 @@ test("a page's link that turns out to be a download leaves that page showing", (assert.equal(after.kind === "web" && after.url, "https://b.example/");store.closeTab(tabId);});++test("leaving a chat closes tabs containing its pages in history, and only those", () => {+ const store = useBrowserStore.getState();+ for (const tab of useBrowserStore.getState().tabs) store.closeTab(tab.id);+ const html = (name: string, key: string) =>+ store.openFile({ blob: new Blob(["<p>hi</p>"], { type: "text/html" }), name, contentType: "text/html", key: `html:${key}` });+ html("shown.html", "a1");+ html("left.html", "a2");+ store.navigate(useBrowserStore.getState().activeTabId ?? "", { url: "https://example.com/" });+ html("back.html", "a3");+ const back = useBrowserStore.getState().activeTabId ?? "";+ store.navigate(back, { url: "https://other.example/" });+ store.goBack(back);+ store.openUrl("https://site.example/", { newTab: true });+ store.openFile({ blob: new Blob(["notes"]), name: "notes.txt", key: "sandbox:s:notes.txt" });+ store.closeChatPages();+ const shown = useBrowserStore.getState().tabs.map((tab) => {+ const entry = currentEntry(tab);+ return entry.kind === "file" ? entry.name : entry.kind === "web" ? entry.url : entry.kind;+ });+ assert.deepEqual(shown, ["https://site.example/", "notes.txt"]);+ assert.equal(+ useBrowserStore.getState().tabs.some((tab) =>+ tab.history.some((entry) => entry.kind === "file" && entry.chatPage),+ ),+ false,+ );+ assert.equal(useBrowserStore.getState().open, true);+ for (const tab of useBrowserStore.getState().tabs) store.closeTab(tab.id);+ html("only.html", "a4");+ store.closeChatPages();+ assert.equal(useBrowserStore.getState().tabs.length, 0);+ assert.equal(useBrowserStore.getState().open, false);+});++test("leaving a chat closes a duplicated page that no longer claims the card's key", () => {+ const store = useBrowserStore.getState();+ for (const tab of useBrowserStore.getState().tabs) store.closeTab(tab.id);+ store.openFile({+ blob: new Blob(["<p>hi</p>"], { type: "text/html" }),+ name: "page.html",+ contentType: "text/html",+ key: "html:duplicate",+ });+ store.duplicateTab(useBrowserStore.getState().activeTabId ?? "");+ assert.equal(useBrowserStore.getState().tabs.length, 2);+ assert.equal(+ useBrowserStore.getState().tabs.filter((tab) => tab.openKey === "file:html:duplicate").length,+ 1,+ );+ store.closeChatPages();+ assert.equal(useBrowserStore.getState().tabs.length, 0);+});