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: preserve browser panel drafts across tab switches (#14280)
Nilay3c8ffe39db96
diff --git a/studio/backend/routes/browser.py b/studio/backend/routes/browser.pyindex 72d4a9c0b60..933977af99f 100644--- a/studio/backend/routes/browser.py+++ b/studio/backend/routes/browser.py@@ -389,6 +389,9 @@ _FRAME_HTML = r"""<!doctype html><body><script>const boot = (cfg) => {+ const inputChannel = new MessageChannel();+ const edited = inputChannel.port1.postMessage.bind(inputChannel.port1);+ parent.postMessage({ source: "unsloth-browser-input" }, "*", [inputChannel.port2]);// Captured before the page's scripts run, so they can't swap it.const compile = Function;// about:srcdoc has an opaque origin, and data: and blob: URLs share it.@@ -526,6 +529,7 @@ _FRAME_HTML = r"""<!doctype html>try { original.call(this, state, title, next ? shellPath(next) : undefined); }catch { original.call(this, state, title); }if (next) {+ if (!samePage(next)) edited(false);pageUrl = next;routedBase?.setAttribute("href", pageUrl);reportUrl();@@ -627,6 +631,25 @@ _FRAME_HTML = r"""<!doctype html>unlessCancelled(event, () => submitForm(target, submitter));}, true);HTMLFormElement.prototype.submit = function () { submitForm(this, null); };+ let scriptedEdit = 0;+ let editingKey = false;+ // execCommand produces trusted input even when a page calls it on load.+ const execCommand = Function.prototype.call.bind(Document.prototype.execCommand);+ Document.prototype.execCommand = function (...args) {+ scriptedEdit += 1;+ try { return execCommand(this, ...args); }+ finally { scriptedEdit -= 1; }+ };+ document.addEventListener("keydown", (event) => {+ const target = event.composedPath()[0];+ if (!event.isTrusted || !(target instanceof Element) || !(target.matches("input, textarea") || target.isContentEditable)) return;+ if (event.key.length !== 1 && !["Backspace", "Delete", "Enter"].includes(event.key)) return;+ editingKey = true;+ setTimeout(() => { editingKey = false; });+ }, true);+ for (const type of ["input", "paste", "cut"]) document.addEventListener(type, (event) => {+ if (event.isTrusted && (!scriptedEdit || editingKey)) edited(true);+ }, true);const report = () => {const icon = document.querySelector("link[rel~='icon'][href], link[rel='shortcut icon'][href], link[rel='apple-touch-icon'][href]");@@ -944,21 +967,30 @@ _FRAME_HTML = r"""<!doctype html>};const escapeAttr = (value) => value.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<");const inject = (html, tags) => {- const at = (match) => match.index + match[0].length;- const head = /<head\b[^<>]*>/i.exec(html);- if (head) return html.slice(0, at(head)) + tags + html.slice(at(head));- const root = /<html\b[^<>]*>/i.exec(html) || /^\s*<!doctype[^>]*>/i.exec(html);- if (root) return html.slice(0, at(root)) + "<head>" + tags + "</head>" + html.slice(at(root));- return "<head>" + tags + "</head>" + html;+ // Bootstrap before even malformed pages' scripts, preserving their document mode.+ const doctype = /^(?:\s|<!--[\s\S]*?-->)*<!doctype[^>]*>/i.exec(html)?.[0] || "";+ return doctype + "<html><head>" + tags + "</head>" + html.slice(doctype.length);};const FOLLOW_CLICK_MS = 3000;const sameOrigin = (a, b) => { try { return new URL(a).origin === new URL(b).origin; } catch { return false; } };let page = null;+ let inputPort = null;+ let lastInput = -Infinity;let clicked = null;window.addEventListener("message", (event) => {// Relay the page's messages; the panel only trusts this window.if (page && event.source === page.contentWindow) {- if (!event.data || event.data.source !== "unsloth-browser") return;+ if (event.data?.source === "unsloth-browser-input" && !inputPort && event.ports.length === 1) {+ inputPort = event.ports[0];+ inputPort.onmessage = ({ data }) => {+ if (typeof data !== "boolean") return;+ const now = performance.now();+ if (data && now - lastInput < 1000) return;+ lastInput = data ? now : -Infinity;+ parent.postMessage({ source: "unsloth-browser", type: "typed", reset: !data }, "*");+ };+ }+ if (!event.data || event.data.source !== "unsloth-browser" || event.data.type === "typed") return;if (event.data.type === "linkClick") clicked = { url: event.data.url, at: Date.now() };else parent.postMessage(event.data, "*");return;diff --git a/studio/backend/tests/test_browser_routes.py b/studio/backend/tests/test_browser_routes.pyindex 2ee61f19a74..74d0a3db46d 100644--- a/studio/backend/tests/test_browser_routes.py+++ b/studio/backend/tests/test_browser_routes.py@@ -101,6 +101,15 @@ def test_the_shell_guards():assert "requestSubmit = " not in shell+def test_trusted_input_uses_a_private_channel():+ shell = browser_mod._FRAME_HTML+ boot = shell[shell.index("const boot = ") : shell.index("const escapeAttr")]+ assert "if (event.isTrusted && (!scriptedEdit || editingKey)) edited(true);" in boot+ assert "inputChannel.port1.postMessage.bind(inputChannel.port1)" in boot+ assert "if (!samePage(next)) edited(false);" in boot+ assert 'removeEventListener("input"' not in boot++def test_the_shell_tells_the_panel_when_the_sandbox_refuses_a_feature():# Checked in Chromium, Firefox and WebKit, as this suite has no browser to run the shell in.shell = browser_mod._FRAME_HTMLdiff --git a/studio/frontend/scripts/check-bundle-budget.ts b/studio/frontend/scripts/check-bundle-budget.tsindex c5b13916952..0e2f06a1a54 100644--- a/studio/frontend/scripts/check-bundle-budget.ts+++ b/studio/frontend/scripts/check-bundle-budget.ts@@ -298,8 +298,8 @@ export const BUDGET = {// The Desktop chat-only install choice (#14245) adds 679 B raw / 205 B transfer (6,842,169 -> 6,842,848 B raw,// 2,060,319 -> 2,060,524 B transfer against 26e246d407, one machine and build, Node 24, 96 chunks): the// first-run checkbox lives in the eager startup screen and the Train hint in the eager sidebar. Raised by that.- transferBytes: 2_061_486,- rawBytes: 6_845_747,+ transferBytes: 2_061_610,+ rawBytes: 6_846_068,};// The chunk count is reported but not budgeted. Splitting a page out of the entry raises it while lowering thediff --git a/studio/frontend/src/features/browser/browser-panel.tsx b/studio/frontend/src/features/browser/browser-panel.tsxindex fbc7805eab0..7415b615199 100644--- a/studio/frontend/src/features/browser/browser-panel.tsx+++ b/studio/frontend/src/features/browser/browser-panel.tsx@@ -3,6 +3,16 @@import { ATTACHMENT_PAGE_SCALES } from "@/components/assistant-ui/attachment-viewer-meta";import { ScaleMenu } from "@/components/media-viewer";+import {+ AlertDialog,+ AlertDialogAction,+ AlertDialogCancel,+ AlertDialogContent,+ AlertDialogDescription,+ AlertDialogFooter,+ AlertDialogHeader,+ AlertDialogTitle,+} from "@/components/ui/alert-dialog";import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";import {ContextMenu,@@ -2372,17 +2382,31 @@ function DeviceBar() {}const MAX_MOUNTED_TABS = 4;-// Hidden web pages run scripts on the chat's thread, so only the newest stays live.+// Hidden pages share the chat's thread; keep only one ordinary page and three drafts.const MAX_HIDDEN_WEB_PAGES = 1;+const MAX_TYPED_PAGES = 3;++function typedTabIds(tabs: BrowserTab[], activeTabId: string | null): Set<string> {+ const typed = tabs.filter((tab) => tab.typedAt !== undefined && tab.id !== activeTabId).sort((a, b) => (b.typedAt ?? 0) - (a.typedAt ?? 0));+ return new Set(typed.slice(0, MAX_TYPED_PAGES).map((tab) => tab.id));+}++function mountedTabIds(mounted: readonly string[], tabs: BrowserTab[], activeTabId: string): string[] {+ const open = new Set(tabs.map((tab) => tab.id));+ const typed = new Set(tabs.filter((tab) => tab.typedAt !== undefined).map((tab) => tab.id));+ const recent = [...mounted.filter((id) => id !== activeTabId && open.has(id)), activeTabId];+ return recent.filter((id, index) => index >= recent.length - MAX_MOUNTED_TABS || typed.has(id));+}function liveTabIds(mounted: readonly string[], tabs: BrowserTab[], activeTabId: string | null): Set<string> {const byId = new Map(tabs.map((tab) => [tab.id, tab]));+ const typed = typedTabIds(tabs, activeTabId);const live = new Set<string>();let hiddenWeb = 0;for (const id of [...mounted].reverse()) {const tab = byId.get(id);if (!tab) continue;- if (id !== activeTabId && currentEntry(tab).kind === "web") {+ if (id !== activeTabId && currentEntry(tab).kind === "web" && !typed.has(id)) {if (hiddenWeb >= MAX_HIDDEN_WEB_PAGES) continue;hiddenWeb += 1;}@@ -2391,6 +2415,22 @@ function liveTabIds(mounted: readonly string[], tabs: BrowserTab[], activeTabId:return live;}+function retainedTabs(mounted: readonly string[], tabs: BrowserTab[], activeTabId: string | null) {+ const live = liveTabIds(mounted, tabs, activeTabId);+ const pendingDrafts = tabs.filter((tab) => tab.typedAt !== undefined && mounted.includes(tab.id) && !live.has(tab.id));+ if (pendingDrafts.length) {+ // The pending draft takes the ordinary hidden page's slot while the reader decides.+ for (const tab of tabs) {+ if (tab.id !== activeTabId && tab.typedAt === undefined && currentEntry(tab).kind === "web") live.delete(tab.id);+ }+ for (const tab of pendingDrafts) live.add(tab.id);+ if (tabs.filter((tab) => live.has(tab.id) && currentEntry(tab).kind === "web").length > MAX_TYPED_PAGES + MAX_HIDDEN_WEB_PAGES + 1 && activeTabId) {+ live.delete(activeTabId);+ }+ }+ return { live, pendingDrafts };+}+/** 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();@@ -2425,16 +2465,19 @@ export const BrowserPanel = memo(function BrowserPanel({ active = true }: { acticonst [mounted, setMounted] = useState<readonly string[]>(() =>activeTabId ? [activeTabId] : [],);+ const [selection, setSelection] = useState({ id: activeTabId, previous: activeTabId, mounted });+ if (selection.id !== activeTabId) setSelection({ id: activeTabId, previous: selection.id, mounted });if (activeTabId && mounted[mounted.length - 1] !== activeTabId) {- const open = new Set(tabs.map((tab) => tab.id));- setMounted(- [- ...mounted.filter((id) => id !== activeTabId && open.has(id)),- activeTabId,- ].slice(-MAX_MOUNTED_TABS),- );+ setMounted(mountedTabIds(mounted, tabs, activeTabId));}- const live = liveTabIds(mounted, tabs, activeTabId);+ const { live, pendingDrafts } = retainedTabs(mounted, tabs, activeTabId);+ const cancelEviction = () => {+ const previous = tabs.find((tab) => tab.id === selection.previous) ?? pendingDrafts[0];+ if (previous) {+ setMounted(selection.mounted);+ useBrowserStore.getState().activateTab(previous.id);+ }+ };const fileTab = activeEntry?.kind === "file";const floatingFileControls = activeEntry?.kind === "file" && !usesBrowserChrome(activeEntry);const documentShown = fileTab || Boolean(activeTab?.documentType);@@ -2468,6 +2511,25 @@ export const BrowserPanel = memo(function BrowserPanel({ active = true }: { acti: "[--browser-surface:var(--card)]",)}>+ <AlertDialog open={active && pendingDrafts.length > 0} onOpenChange={(open) => { if (!open) cancelEviction(); }}>+ <AlertDialogContent>+ <AlertDialogHeader>+ <AlertDialogTitle>{t("browser.drafts.title")}</AlertDialogTitle>+ <AlertDialogDescription>+ {t("browser.drafts.description", { pages: pendingDrafts.map((tab) => tabTitle(tab, currentEntry(tab))).join(", ") })}+ </AlertDialogDescription>+ </AlertDialogHeader>+ <AlertDialogFooter>+ <AlertDialogCancel>{t("browser.clearData.cancel")}</AlertDialogCancel>+ <AlertDialogAction onClick={(event) => {+ event.preventDefault();+ for (const tab of pendingDrafts) useBrowserStore.getState().updateTab(tab.id, { typedAt: undefined });+ }}>+ {t("browser.drafts.confirm")}+ </AlertDialogAction>+ </AlertDialogFooter>+ </AlertDialogContent>+ </AlertDialog><TabStrip tabs={tabs} activeTabId={activeTabId} active={active} /><div className="relative flex min-h-0 flex-1 flex-col overflow-hidden bg-[var(--browser-surface)]"><divdiff --git a/studio/frontend/src/features/browser/frame-message.ts b/studio/frontend/src/features/browser/frame-message.tsindex 25749f44aa2..216957ed13e 100644--- a/studio/frontend/src/features/browser/frame-message.ts+++ b/studio/frontend/src/features/browser/frame-message.ts@@ -21,6 +21,7 @@ export type FrameMessage =| { type: "upload" }| { type: "scriptNavigation" }| { type: "needsBrowser" }+ | { type: "typed"; reset?: boolean }/** Find in page: how many matches the query has, and which one is shown (-1: none). */| { type: "findResult"; count: number; active: number }/** The page's current markup, for printing; null when it couldn't be copied or was too large. */@@ -211,6 +212,8 @@ export function parseFrameMessage(data: unknown): FrameMessage | null {case "scriptNavigation":case "needsBrowser":return { type: message.type };+ case "typed":+ return message.reset === true ? { type: "typed", reset: true } : { type: "typed" };case "findResult": {const { count, active } = message;return Number.isInteger(count) && Number.isInteger(active) && (count as number) >= 0 && (active as number) >= -1 && (active as number) < Math.max(count as number, 1)diff --git a/studio/frontend/src/features/browser/page-frame.tsx b/studio/frontend/src/features/browser/page-frame.tsxindex 262b56406f7..37b7d012ec9 100644--- a/studio/frontend/src/features/browser/page-frame.tsx+++ b/studio/frontend/src/features/browser/page-frame.tsx@@ -214,6 +214,7 @@ export function PageFrame({);return;}+ if (message.type === "typed" && left) return;if (isUserAction(message)) {if (!userActive(frame, message)) return;// Shortcuts too: a script can queue many before the first one moves focus off the page.@@ -257,6 +258,7 @@ export function PageFrame({stopClearing();stopSyncing();requests.stop();+ onMessageRef.current({ type: "typed", reset: true });};}, [origin]);diff --git a/studio/frontend/src/features/browser/store.ts b/studio/frontend/src/features/browser/store.tsindex 8ca97f30eda..15b7cc99aa6 100644--- a/studio/frontend/src/features/browser/store.ts+++ b/studio/frontend/src/features/browser/store.ts@@ -89,6 +89,7 @@ export type BrowserTab = {nativeError: string | null;/** The proxied page failed to load, so an error shows instead of its frame. */pageError?: boolean;+ typedAt?: number;/** The name the reader gave the tab; kept as it navigates. */customTitle: string | null;muted: boolean;@@ -356,7 +357,7 @@ type BrowserState = {patch: Partial<Pick<BrowserTab,- "title" | "favicon" | "documentType" | "displayUrl" | "loading" | "nativeHistory" | "nativeError" | "pageError"+ "title" | "favicon" | "documentType" | "displayUrl" | "loading" | "nativeHistory" | "nativeError" | "pageError" | "typedAt">>,) => void;@@ -421,6 +422,7 @@ function pushEntry(tab: BrowserTab, entry: BrowserEntry, replace = false): BrowsopenKey: null,nativeError: null,pageError: false,+ typedAt: undefined,zoom,};}@@ -439,6 +441,7 @@ function moveTo(tab: BrowserTab, index: number): BrowserTab {openKey: entry.kind === "file" ? (entry.openKey ?? null) : null,nativeError: null,pageError: false,+ typedAt: undefined,};}@@ -726,7 +729,7 @@ export const useBrowserStore = create<BrowserState>((set, get) => {set((state) => ({tabs: patchTab(state.tabs, tabId, (tab) => {const moved = routerEntry(tab);- if (!moved) return { ...tab, reloadKey: tab.reloadKey + 1 };+ if (!moved) return { ...tab, reloadKey: tab.reloadKey + 1, typedAt: undefined };// Swapped in place, as a browser's reload: Forward keeps its pages.const next = pushEntry(tab, moved, true);return { ...next, history: [...next.history, ...tab.history.slice(tab.index + 1)], reloadKey: tab.reloadKey + 1 };diff --git a/studio/frontend/src/features/browser/tab-view.tsx b/studio/frontend/src/features/browser/tab-view.tsxindex e1a1a8bc715..e574d3dccad 100644--- a/studio/frontend/src/features/browser/tab-view.tsx+++ b/studio/frontend/src/features/browser/tab-view.tsx@@ -139,6 +139,9 @@ function useFrameMessages(tabId: string, origin: string | null) {case "reload":store.reload(tabId);break;+ case "typed":+ store.updateTab(tabId, { typedAt: message.reset ? undefined : Date.now() });+ break;case "upload":case "scriptNavigation":case "needsBrowser": {diff --git a/studio/frontend/src/i18n/locales/ar.ts b/studio/frontend/src/i18n/locales/ar.tsindex 31ea82e72ec..296440e3ee3 100644--- a/studio/frontend/src/i18n/locales/ar.ts+++ b/studio/frontend/src/i18n/locales/ar.ts@@ -50,6 +50,11 @@ export const ar = {removeFromMessage: "إزالة من الرسالة",},browser: {+ drafts: {+ title: "إلغاء تحميل صفحة تحتوي على نص مُدخل؟",+ description: "للحفاظ على سرعة استجابة الدردشة، يستمر تشغيل ثلاث صفحات مخفية فقط تحتوي على نص مُدخل. ستؤدي المتابعة إلى إلغاء تحميل {pages}، وقد يُفقد النص غير المحفوظ. يعيدك الإلغاء إلى علامة التبويب السابقة.",+ confirm: "إلغاء التحميل والمتابعة",+ },agentSettings: {title: "استخدام المتصفح",enable: "السماح للنماذج باستخدام المتصفح",diff --git a/studio/frontend/src/i18n/locales/de.ts b/studio/frontend/src/i18n/locales/de.tsindex 75896384333..58653929917 100644--- a/studio/frontend/src/i18n/locales/de.ts+++ b/studio/frontend/src/i18n/locales/de.ts@@ -50,6 +50,11 @@ export const de = {removeFromMessage: "Aus Nachricht entfernen",},browser: {+ drafts: {+ title: "Seite mit eingegebenem Text entladen?",+ description: "Damit der Chat reaktionsschnell bleibt, laufen nur drei ausgeblendete Seiten mit eingegebenem Text weiter. Beim Fortfahren wird {pages} entladen. Nicht gespeicherter Text kann verloren gehen. Abbrechen kehrt zum vorherigen Tab zurück.",+ confirm: "Entladen und fortfahren",+ },agentSettings: {title: "Browsernutzung",enable: "Modelle den Browser nutzen lassen",diff --git a/studio/frontend/src/i18n/locales/en.ts b/studio/frontend/src/i18n/locales/en.tsindex 58e938fd8ce..7749dc3882e 100644--- a/studio/frontend/src/i18n/locales/en.ts+++ b/studio/frontend/src/i18n/locales/en.ts@@ -47,6 +47,11 @@ export const en = {removeFromMessage: "Remove from message",},browser: {+ drafts: {+ title: "Unload a page with entered text?",+ description: "To keep the chat responsive, only three hidden pages with entered text stay running. Continuing will unload {pages}, which may lose unsaved text. Cancel returns to the previous tab.",+ confirm: "Unload and continue",+ },agentSettings: {title: "Browser use",enable: "Let models use the browser",diff --git a/studio/frontend/src/i18n/locales/es.ts b/studio/frontend/src/i18n/locales/es.tsindex bbb32688c0f..984b005e3a8 100644--- a/studio/frontend/src/i18n/locales/es.ts+++ b/studio/frontend/src/i18n/locales/es.ts@@ -50,6 +50,11 @@ export const es = {removeFromMessage: "Quitar del mensaje",},browser: {+ drafts: {+ title: "¿Descargar una página con texto introducido?",+ description: "Para mantener la capacidad de respuesta del chat, solo siguen ejecutándose tres páginas ocultas con texto introducido. Continuar descargará {pages} y podría perder texto sin guardar. Cancelar vuelve a la pestaña anterior.",+ confirm: "Descargar y continuar",+ },agentSettings: {title: "Uso del navegador",enable: "Permitir que los modelos usen el navegador",diff --git a/studio/frontend/src/i18n/locales/fr.ts b/studio/frontend/src/i18n/locales/fr.tsindex f8deba55276..524d1c956cb 100644--- a/studio/frontend/src/i18n/locales/fr.ts+++ b/studio/frontend/src/i18n/locales/fr.ts@@ -50,6 +50,11 @@ export const fr = {removeFromMessage: "Retirer du message",},browser: {+ drafts: {+ title: "Décharger une page contenant du texte saisi ?",+ description: "Pour préserver la réactivité du chat, seules trois pages masquées contenant du texte saisi restent actives. Continuer déchargera {pages} et risque de perdre le texte non enregistré. Annuler revient à l’onglet précédent.",+ confirm: "Décharger et continuer",+ },agentSettings: {title: "Utilisation du navigateur",enable: "Autoriser les modèles à utiliser le navigateur",diff --git a/studio/frontend/src/i18n/locales/he.ts b/studio/frontend/src/i18n/locales/he.tsindex 06f4256bed1..2f9f8f3b6bf 100644--- a/studio/frontend/src/i18n/locales/he.ts+++ b/studio/frontend/src/i18n/locales/he.ts@@ -51,6 +51,11 @@ export const he = {removeFromMessage: "הסר מההודעה",},browser: {+ drafts: {+ title: "לפרוק דף עם טקסט שהוזן?",+ description: "כדי לשמור על תגובתיות הצ׳אט, רק שלושה דפים מוסתרים עם טקסט שהוזן ממשיכים לפעול. המשך יפרוק את {pages}, וטקסט שלא נשמר עלול ללכת לאיבוד. ביטול מחזיר לכרטיסייה הקודמת.",+ confirm: "פריקה והמשך",+ },agentSettings: {title: "שימוש בדפדפן",enable: "לאפשר למודלים להשתמש בדפדפן",diff --git a/studio/frontend/src/i18n/locales/hi.ts b/studio/frontend/src/i18n/locales/hi.tsindex 78683efe28f..6b2968d041c 100644--- a/studio/frontend/src/i18n/locales/hi.ts+++ b/studio/frontend/src/i18n/locales/hi.ts@@ -50,6 +50,11 @@ export const hi = {removeFromMessage: "मैसेज से हटाएँ",},browser: {+ drafts: {+ title: "दर्ज किए गए टेक्स्ट वाला पेज अनलोड करें?",+ description: "चैट को तेज़ बनाए रखने के लिए दर्ज किए गए टेक्स्ट वाले केवल तीन छिपे पेज चलते रहते हैं। जारी रखने पर {pages} अनलोड होगा और बिना सहेजा टेक्स्ट खो सकता है। रद्द करने पर पिछले टैब पर लौटेंगे।",+ confirm: "अनलोड करें और जारी रखें",+ },agentSettings: {title: "ब्राउज़र उपयोग",enable: "मॉडल को ब्राउज़र इस्तेमाल करने दें",diff --git a/studio/frontend/src/i18n/locales/it.ts b/studio/frontend/src/i18n/locales/it.tsindex 87a1cc112db..0936ddddcf3 100644--- a/studio/frontend/src/i18n/locales/it.ts+++ b/studio/frontend/src/i18n/locales/it.ts@@ -49,6 +49,11 @@ export const it = {removeFromMessage: "Rimuovi dal messaggio",},browser: {+ drafts: {+ title: "Scaricare una pagina con testo inserito?",+ description: "Per mantenere la chat reattiva, solo tre pagine nascoste con testo inserito restano in esecuzione. Continuando verrà scaricata {pages} e il testo non salvato potrebbe andare perso. Annulla torna alla scheda precedente.",+ confirm: "Scarica e continua",+ },agentSettings: {title: "Uso del browser",enable: "Consenti ai modelli di usare il browser",diff --git a/studio/frontend/src/i18n/locales/ja.ts b/studio/frontend/src/i18n/locales/ja.tsindex 827f3f3a664..2b0a1e74864 100644--- a/studio/frontend/src/i18n/locales/ja.ts+++ b/studio/frontend/src/i18n/locales/ja.ts@@ -51,6 +51,11 @@ export const ja = {removeFromMessage: "メッセージから削除",},browser: {+ drafts: {+ title: "入力したテキストがあるページを終了しますか?",+ description: "チャットの応答性を保つため、入力したテキストがある非表示ページは3つまで実行を続けます。続行すると {pages} を終了し、未保存のテキストが失われる可能性があります。キャンセルすると前のタブに戻ります。",+ confirm: "終了して続行",+ },agentSettings: {title: "ブラウザ操作",enable: "モデルにブラウザを使わせる",diff --git a/studio/frontend/src/i18n/locales/ko.ts b/studio/frontend/src/i18n/locales/ko.tsindex da209ee363b..c4bd58a8a84 100644--- a/studio/frontend/src/i18n/locales/ko.ts+++ b/studio/frontend/src/i18n/locales/ko.ts@@ -50,6 +50,11 @@ export const ko = {removeFromMessage: "메시지에서 제거",},browser: {+ drafts: {+ title: "입력한 텍스트가 있는 페이지를 종료할까요?",+ description: "채팅의 응답성을 유지하기 위해 입력한 텍스트가 있는 숨겨진 페이지는 세 개까지만 계속 실행됩니다. 계속하면 {pages} 페이지가 종료되며 저장하지 않은 텍스트가 사라질 수 있습니다. 취소하면 이전 탭으로 돌아갑니다.",+ confirm: "종료하고 계속",+ },agentSettings: {title: "브라우저 사용",enable: "모델이 브라우저를 사용하도록 허용",diff --git a/studio/frontend/src/i18n/locales/pt-br.ts b/studio/frontend/src/i18n/locales/pt-br.tsindex 056ba96c9f6..eb1be25b469 100644--- a/studio/frontend/src/i18n/locales/pt-br.ts+++ b/studio/frontend/src/i18n/locales/pt-br.ts@@ -50,6 +50,11 @@ export const ptBR = {removeFromMessage: "Remover da mensagem",},browser: {+ drafts: {+ title: "Descarregar uma página com texto inserido?",+ description: "Para manter o chat responsivo, apenas três páginas ocultas com texto inserido continuam em execução. Continuar descarregará {pages}, podendo perder texto não salvo. Cancelar volta à aba anterior.",+ confirm: "Descarregar e continuar",+ },agentSettings: {title: "Uso do navegador",enable: "Permitir que os modelos usem o navegador",diff --git a/studio/frontend/src/i18n/locales/ru.ts b/studio/frontend/src/i18n/locales/ru.tsindex 571f19dfefb..111b9c4bd25 100644--- a/studio/frontend/src/i18n/locales/ru.ts+++ b/studio/frontend/src/i18n/locales/ru.ts@@ -50,6 +50,11 @@ export const ru = {removeFromMessage: "Убрать из сообщения",},browser: {+ drafts: {+ title: "Выгрузить страницу с введённым текстом?",+ description: "Чтобы чат оставался отзывчивым, работают не более трёх скрытых страниц с введённым текстом. При продолжении страница {pages} будет выгружена, и несохранённый текст может быть потерян. Отмена вернёт на предыдущую вкладку.",+ confirm: "Выгрузить и продолжить",+ },agentSettings: {title: "Работа с браузером",enable: "Разрешить моделям пользоваться браузером",diff --git a/studio/frontend/src/i18n/locales/sv.ts b/studio/frontend/src/i18n/locales/sv.tsindex cbc82395658..05f305d8552 100644--- a/studio/frontend/src/i18n/locales/sv.ts+++ b/studio/frontend/src/i18n/locales/sv.ts@@ -48,6 +48,11 @@ export const sv = {removeFromMessage: "Ta bort från meddelandet",},browser: {+ drafts: {+ title: "Ladda ur en sida med inmatad text?",+ description: "För att hålla chatten responsiv fortsätter bara tre dolda sidor med inmatad text att köras. Om du fortsätter laddas {pages} ur och osparad text kan gå förlorad. Avbryt återgår till föregående flik.",+ confirm: "Ladda ur och fortsätt",+ },agentSettings: {title: "Webbläsaranvändning",enable: "Låt modeller använda webbläsaren",diff --git a/studio/frontend/src/i18n/locales/zh-CN.ts b/studio/frontend/src/i18n/locales/zh-CN.tsindex 4212f7d8642..c90ea69a7f7 100644--- a/studio/frontend/src/i18n/locales/zh-CN.ts+++ b/studio/frontend/src/i18n/locales/zh-CN.ts@@ -50,6 +50,11 @@ export const zhCN = {removeFromMessage: "从消息中移除",},browser: {+ drafts: {+ title: "卸载含有已输入文本的页面?",+ description: "为保持聊天响应流畅,最多仅保留三个含有已输入文本的隐藏页面继续运行。继续将卸载 {pages},其中未保存的文本可能丢失。取消将返回上一个标签页。",+ confirm: "卸载并继续",+ },agentSettings: {title: "浏览器操作",enable: "允许模型使用浏览器",diff --git a/studio/frontend/tests/browser-frame-message.test.ts b/studio/frontend/tests/browser-frame-message.test.tsindex 448d397ed5c..c3f680245a9 100644--- a/studio/frontend/tests/browser-frame-message.test.ts+++ b/studio/frontend/tests/browser-frame-message.test.ts@@ -58,6 +58,12 @@ test("a page the sandbox refused a feature reaches the panel as a bare notice",assert.deepEqual(from({ type: "needsBrowser", url: "https://evil.example/" }), { type: "needsBrowser" });});+test("typing in a page reaches the panel without the text", () => {+ assert.deepEqual(from({ type: "typed", value: "secret" }), { type: "typed" });+ assert.deepEqual(from({ type: "typed", reset: true, value: "secret" }), { type: "typed", reset: true });+ assert.deepEqual(from({ type: "typed", reset: "true" }), { type: "typed" });+});+test("the sandbox notice stays until the page goes, the others time out", () => {assert.deepEqual(frameNotice("needsBrowser", "t1"), { id: "browser-needsBrowser-t1", duration: Number.POSITIVE_INFINITY });assert.deepEqual(frameNotice("upload", "t1"), { id: "browser-upload-t1" });diff --git a/studio/frontend/tests/browser-typed-tab-stays-live.test.ts b/studio/frontend/tests/browser-typed-tab-stays-live.test.tsnew file mode 100644index 00000000000..688477571d2--- /dev/null+++ b/studio/frontend/tests/browser-typed-tab-stays-live.test.ts@@ -0,0 +1,304 @@+// 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 { readFileSync } from "node:fs";+import { register } from "node:module";+import { test } from "node:test";+import ts from "@typescript/typescript6";+import { readSrc } from "./helpers/kit.ts";++register("./helpers/browser-store-resolver.mjs", import.meta.url);+const { chatTabs, currentEntry, useBrowserStore } = await import("../src/features/browser/store.ts");+type Tab = ReturnType<typeof chatTabs>[number];++function sourceCode(file: string, names: string[], globals: Record<string, unknown>): Record<string, unknown> {+ const source = ts.createSourceFile(file, readSrc(file), ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);+ const parts = names.map((name) => {+ const node = source.statements.find(+ (statement) =>+ (ts.isFunctionDeclaration(statement) && statement.name?.text === name) ||+ (ts.isVariableStatement(statement) &&+ statement.declarationList.declarations.some((declaration) => declaration.name.getText(source) === name)),+ );+ assert.ok(node, `${name} in ${file}`);+ return node.getText(source);+ });+ const code = ts.transpileModule(`${parts.join("\n")}\nreturn { ${names.join(", ")} };`, {+ compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.None },+ }).outputText;+ return new Function(...Object.keys(globals), code)(...Object.values(globals));+}++const { liveTabIds, mountedTabIds, retainedTabs } = sourceCode(+ "features/browser/browser-panel.tsx",+ ["MAX_MOUNTED_TABS", "MAX_HIDDEN_WEB_PAGES", "MAX_TYPED_PAGES", "typedTabIds", "mountedTabIds", "liveTabIds", "retainedTabs"],+ { currentEntry },+) as {+ liveTabIds: (mounted: readonly string[], tabs: Tab[], activeTabId: string | null) => Set<string>;+ mountedTabIds: (mounted: readonly string[], tabs: Tab[], activeTabId: string) => string[];+ retainedTabs: (mounted: readonly string[], tabs: Tab[], activeTabId: string | null) => { live: Set<string>; pendingDrafts: Tab[] };+};++function openTabs(urls: string[]): string[] {+ const store = useBrowserStore.getState();+ for (const tab of chatTabs(store)) store.closeTab(tab.id);+ return urls.map((url) => {+ useBrowserStore.getState().openUrl(url, { newTab: true });+ return useBrowserStore.getState().activeTabId ?? "";+ });+}++const tabs = () => chatTabs(useBrowserStore.getState());+let typedAt = 0;+const type = (id: string) => useBrowserStore.getState().updateTab(id, { typedAt: ++typedAt });++function visit(ids: string[]): readonly string[] {+ let mounted: readonly string[] = [];+ for (const id of ids) mounted = mountedTabIds(mounted, tabs(), id);+ return mounted;+}++test("a page typed into stays live when two more tabs are opened", () => {+ const [form, example, banana] = openTabs([+ "https://httpbin.org/forms/post",+ "https://example.com/",+ "https://en.wikipedia.org/wiki/Banana",+ ]);+ type(form);+ const live = liveTabIds([form, example, banana], tabs(), banana);+ assert.ok(live.has(form));+ assert.ok(live.has(example));+});++test("pages not typed into still leave only the newest hidden one live", () => {+ const [a, b, c] = openTabs(["https://a.example/", "https://b.example/", "https://c.example/"]);+ assert.deepEqual([...liveTabIds([a, b, c], tabs(), c)], [c, b]);+});++test("a page typed into stays mounted past the mounted tab limit", () => {+ const ids = openTabs(["https://httpbin.org/forms/post", "https://a.example/", "https://b.example/", "https://c.example/", "https://d.example/"]);+ const [form] = ids;+ type(form);+ const mounted = visit(ids);+ assert.ok(mounted.includes(form));+ assert.ok(liveTabIds(mounted, tabs(), ids[ids.length - 1]).has(form));+});++test("the fourth most recent draft leaves the live set only after consent", () => {+ const ids = openTabs(["https://a.example/", "https://b.example/", "https://c.example/", "https://d.example/", "https://e.example/", "https://f.example/"]);+ const [a, b, c, d, e, f] = ids;+ for (const id of [a, b, c, d]) type(id);+ const mounted = visit(ids);+ assert.deepEqual(mounted, [a, b, c, d, e, f]);+ assert.deepEqual([...liveTabIds(mounted, tabs(), f)], [f, e, d, c, b]);+ const pending = retainedTabs(mounted, tabs(), f);+ assert.deepEqual(pending.pendingDrafts.map((tab) => tab.id), [a]);+ assert.deepEqual([...pending.live], [f, d, c, b, a]);+ useBrowserStore.getState().updateTab(a, { typedAt: undefined });+ const confirmed = retainedTabs(mounted, tabs(), f);+ assert.deepEqual(confirmed.pendingDrafts, []);+ assert.deepEqual([...confirmed.live], [f, e, d, c, b]);+ assert.ok(!mountedTabIds(mounted, tabs(), f).includes(a));+});++test("the active draft is separate from the three hidden draft slots", () => {+ const ids = openTabs(["https://a.example/", "https://b.example/", "https://c.example/", "https://d.example/", "https://e.example/"]);+ for (const id of ids.slice(0, 4)) type(id);+ for (const active of [ids[3], ids[1]]) {+ const result = retainedTabs(visit([...ids, active]), tabs(), active);+ assert.equal(result.live.size, 5);+ assert.deepEqual(result.pendingDrafts, []);+ }+});++test("re-editing an older draft changes eviction order", () => {+ const ids = openTabs(["https://a.example/", "https://b.example/", "https://c.example/", "https://d.example/", "https://e.example/", "https://f.example/"]);+ let now = 0;+ const { useFrameMessages } = sourceCode("features/browser/tab-view.tsx", ["useFrameMessages"], {+ useBrowserStore, useCallback: (fn: unknown) => fn, useT: () => () => "", Date: { now: () => ++now },+ }) as { useFrameMessages: (id: string, origin: string) => (message: { type: "typed" }) => void };+ const edit = (id: string) => useFrameMessages(id, "https://a.example")({ type: "typed" });+ for (const id of ids.slice(0, 4)) edit(id);+ const mounted = visit(ids);+ edit(ids[0]);+ assert.deepEqual(retainedTabs(mounted, tabs(), ids[5]).pendingDrafts.map((tab) => tab.id), [ids[1]]);+ edit(ids[1]);+ assert.deepEqual(retainedTabs(mounted, tabs(), ids[5]).pendingDrafts.map((tab) => tab.id), [ids[2]]);+});++test("pending drafts defer a new page rather than exceed five live web pages", () => {+ const ids = openTabs(["https://a.example/", "https://b.example/", "https://c.example/", "https://d.example/", "https://e.example/", "https://f.example/"]);+ for (const id of ids.slice(0, 5)) type(id);+ const result = retainedTabs(visit(ids), tabs(), ids[5]);+ assert.deepEqual(result.pendingDrafts.map((tab) => tab.id), [ids[0]]);+ assert.deepEqual([...result.live], [ids[4], ids[3], ids[2], ids[1], ids[0]]);+});++test("leaving or reloading the page forgets that it was typed into", () => {+ const [form] = openTabs(["https://httpbin.org/forms/post"]);+ const typed = () => tabs().find((tab) => tab.id === form)?.typedAt !== undefined;+ const store = useBrowserStore.getState();+ type(form);+ store.reload(form);+ assert.equal(typed(), false);+ type(form);+ store.navigate(form, { url: "https://httpbin.org/post", method: "POST", body: "custname=a" });+ assert.equal(typed(), false);+ type(form);+ store.goBack(form);+ assert.equal(typed(), false);+ type(form);+ store.goForward(form);+ assert.equal(typed(), false);+ store.navigate(form, { url: "https://httpbin.org/forms/post" });+ store.updateTab(form, { displayUrl: "https://httpbin.org/other", typedAt: ++typedAt });+ store.reload(form);+ assert.equal(typed(), false);+ assert.equal(currentEntry(tabs()[0]).kind === "web" && (currentEntry(tabs()[0]) as { url: string }).url, "https://httpbin.org/other");+});++test("a typed notice sent after the page left does not mark the next page", () => {+ const source = readSrc("features/browser/page-frame.tsx");+ assert.ok(source.includes('if (message.type === "typed" && left) return;'));+});++test("discarding a frame clears only its own draft mark", () => {+ const ids = openTabs(["https://a.example/", "https://b.example/", "https://c.example/"]);+ for (const id of ids) type(id);+ const { useFrameMessages } = sourceCode("features/browser/tab-view.tsx", ["useFrameMessages"], {+ useBrowserStore, useCallback: (fn: unknown) => fn, useT: () => () => "",+ }) as { useFrameMessages: (id: string, origin: string) => (message: unknown) => void };+ const file = "features/browser/page-frame.tsx";+ const source = ts.createSourceFile(file, readSrc(file), ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);+ const component = source.statements.find((node) => ts.isFunctionDeclaration(node) && node.name?.text === "PageFrame");+ assert.ok(component && ts.isFunctionDeclaration(component));+ const effect = component.body?.statements.find((node) => ts.isExpressionStatement(node) && ts.isCallExpression(node.expression)+ && node.expression.expression.getText(source) === "useEffect" && node.getText(source).includes("requests.stop()"));+ assert.ok(effect && ts.isExpressionStatement(effect) && ts.isCallExpression(effect.expression));+ const code = ts.transpileModule(`return (${effect.expression.arguments[0].getText(source)});`, {+ compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.None },+ }).outputText;+ for (const id of [ids[1], ids[0]]) {+ const noop = () => {};+ const globals = {+ pageRequests: () => ({ stop: noop }), requestForPage: noop, urlRef: { current: null }, frameRef: { current: null },+ window: { addEventListener: noop, removeEventListener: noop }, onPageStorageClear: () => noop,+ onPageStorageWrite: () => noop, origin: "https://a.example", tabIdRef: { current: id },+ onMessageRef: { current: useFrameMessages(id, "https://a.example") },+ };+ const mount = new Function(...Object.keys(globals), code)(...Object.values(globals)) as () => () => void;+ const unmount = mount();+ assert.notEqual(tabs().find((tab) => tab.id === id)?.typedAt, undefined);+ unmount();+ assert.equal(tabs().find((tab) => tab.id === id)?.typedAt, undefined);+ assert.notEqual(tabs().find((tab) => tab.id === ids[2])?.typedAt, undefined);+ }+});++test("only the first private input channel from this page can report edits", () => {+ const backend = readFileSync(new URL("../../backend/routes/browser.py", import.meta.url), "utf8");+ const start = backend.indexOf(" const FOLLOW_CLICK_MS = ");+ const relay = backend.slice(start, backend.indexOf(" </script>", start));+ type Port = { onmessage?: (event: { data: unknown }) => void };+ const child = {};+ const forwarded: unknown[] = [];+ const parent = { postMessage: (data: unknown) => forwarded.push(data) };+ let listener: (event: { source: unknown; data: unknown; ports: Port[] }) => void;+ let now = 0;+ let violation: (event: { effectiveDirective: string; blockedURI: string }) => void;+ const document = {+ createElement: () => ({ contentWindow: child, setAttribute() {} }),+ body: { appendChild() {} }, head: { appendChild() {} },+ addEventListener: (_: string, callback: typeof violation) => { violation = callback; },+ };+ new Function("window", "document", "parent", "performance", "boot", "escapeAttr", "inject", relay)(+ { addEventListener: (_: string, callback: typeof listener) => { listener = callback; } },+ document, parent, { now: () => now }, () => {}, (text: string) => text, () => "",+ );+ const send = (source: unknown, data: unknown, ports: Port[] = []) => listener({ source, data, ports });+ send(parent, { type: "unsloth:browser-html", html: "" });+ const foreign: Port = {};+ send({}, { source: "unsloth-browser-input" }, [foreign]);+ assert.equal(foreign.onmessage, undefined);+ send(child, { source: "unsloth-browser", type: "typed", tabId: "another-tab" });+ assert.deepEqual(forwarded, []);+ const port: Port = {};+ send(child, { source: "unsloth-browser-input" }, [port]);+ assert.ok(port.onmessage);+ const replacement: Port = {};+ send(child, { source: "unsloth-browser-input" }, [replacement]);+ assert.equal(replacement.onmessage, undefined);+ port.onmessage({ data: "true" });+ assert.deepEqual(forwarded, []);+ port.onmessage({ data: true });+ now = 500;+ port.onmessage({ data: true });+ assert.deepEqual(forwarded, [{ source: "unsloth-browser", type: "typed", reset: false }]);+ port.onmessage({ data: false });+ port.onmessage({ data: true });+ now = 1500;+ port.onmessage({ data: true });+ assert.deepEqual(forwarded.slice(1), [+ { source: "unsloth-browser", type: "typed", reset: true },+ { source: "unsloth-browser", type: "typed", reset: false },+ { source: "unsloth-browser", type: "typed", reset: false },+ ]);+ forwarded.length = 0;+ send(child, { source: "unsloth-browser", type: "linkClick", url: "https://docs.example/next" });+ assert.deepEqual(forwarded, []);+ violation!({ effectiveDirective: "frame-src", blockedURI: "https://docs.example/" });+ assert.deepEqual(forwarded, [{ source: "unsloth-browser", type: "navigate", url: "https://docs.example/next" }]);+});++test("bootstrap precedes page scripts without moving a leading doctype", () => {+ const backend = readFileSync(new URL("../../backend/routes/browser.py", import.meta.url), "utf8");+ const injection = backend.slice(backend.indexOf(" const inject = "), backend.indexOf(" let page = null;"));+ const inject = new Function(`${injection}\nreturn inject;`)() as (html: string, tags: string) => string;+ for (const prefix of ["<!doctype html>", "<!-- page -->\n<!doctype html>", ""]) {+ const result = inject(`${prefix}<script>hostile()</script><html lang="en"><head></head><body></body>`, "<script>boot()</script>");+ assert.ok(result.startsWith(`${prefix}<html><head><script>boot()</script></head>`));+ assert.ok(result.indexOf("boot()") < result.indexOf("hostile()"));+ }+});++test("scripted editing cannot claim typing but keyboard editors and paste can", () => {+ const backend = readFileSync(new URL("../../backend/routes/browser.py", import.meta.url), "utf8");+ const start = backend.indexOf(" let scriptedEdit = 0;");+ const code = backend.slice(start, backend.indexOf(" const report = ", start));+ const listeners = new Map<string, (event: unknown) => void>();+ const notices: boolean[] = [];+ const timers: Array<() => void> = [];+ class Element { matches() { return true; } }+ class Document {+ execCommand() { listeners.get("input")?.({ isTrusted: true }); }+ addEventListener(type: string, listener: (event: unknown) => void) { listeners.set(type, listener); }+ }+ const document = new Document();+ new Function("Document", "document", "Element", "edited", "setTimeout", code)(+ Document, document, Element, (value: boolean) => notices.push(value), (callback: () => void) => timers.push(callback),+ );+ document.execCommand();+ listeners.get("input")?.({ isTrusted: false });+ assert.deepEqual(notices, []);+ const apply = Function.prototype.apply;+ let escaped: ((this: Document) => void) | undefined;+ try {+ Function.prototype.apply = function () { escaped = this as typeof escaped; };+ document.execCommand();+ } finally {+ Function.prototype.apply = apply;+ }+ escaped?.call(document);+ assert.deepEqual(notices, []);+ listeners.get("input")?.({ isTrusted: true });+ listeners.get("paste")?.({ isTrusted: true });+ listeners.get("cut")?.({ isTrusted: true });+ assert.deepEqual(notices, [true, true, true]);+ listeners.get("keydown")?.({ isTrusted: true, key: "a", composedPath: () => [new Element()] });+ document.execCommand();+ assert.equal(notices.length, 4);+ for (const timer of timers) timer();+ document.execCommand();+ assert.equal(notices.length, 4);+});diff --git a/studio/frontend/tests/browser-url.test.ts b/studio/frontend/tests/browser-url.test.tsindex f5c65553c2f..2c059286ffe 100644--- a/studio/frontend/tests/browser-url.test.ts+++ b/studio/frontend/tests/browser-url.test.ts@@ -5,6 +5,7 @@ import assert from "node:assert/strict";import { readFileSync } from "node:fs";import { test } from "node:test";import { runInNewContext } from "node:vm";+import { MessageChannel } from "node:worker_threads";// Run the shell's boot through URL installation, before the rest needs a DOM.const source = readFileSync(@@ -26,6 +27,8 @@ function setup(location: window.location,cfg: { url: pageUrl },document: { querySelector: () => null },+ MessageChannel,+ parent: { postMessage() {} },});return window;}