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
Fix Gallery save toast to use the chosen filename (#15725)
Lee Jacksonfe032baac576
diff --git a/studio/frontend/src/features/audio/save-audio.ts b/studio/frontend/src/features/audio/save-audio.tsindex e650c2d2116..17563195185 100644--- a/studio/frontend/src/features/audio/save-audio.ts+++ b/studio/frontend/src/features/audio/save-audio.ts@@ -17,17 +17,16 @@ export async function saveAudio(load: (() => Promise<Blob>) | null,): Promise<void> {try {+ let savedFileName: string | undefined;if (load && (!url || (isTauri && url.startsWith("blob:")))) {const blob = await load();- if (isTauri) {- await downloadBlobStreaming(blob, filename);- } else {- await downloadFile(blob, filename, blob.type || "audio/wav");- }+ savedFileName = isTauri+ ? await downloadBlobStreaming(blob, filename)+ : await downloadFile(blob, filename, blob.type || "audio/wav");} else if (url) {- await downloadUrl(url, filename);+ savedFileName = await downloadUrl(url, filename);}- if (isTauri) toast.success("Audio saved", { description: filename });+ if (savedFileName !== undefined) toast.success("Audio saved", { description: savedFileName });} catch (error) {if (isDownloadCancelled(error)) return;toast.error("Could not save audio", {diff --git a/studio/frontend/src/features/browser/bookmarks-io.ts b/studio/frontend/src/features/browser/bookmarks-io.tsindex 6e2be386fc4..f8070532721 100644--- a/studio/frontend/src/features/browser/bookmarks-io.ts+++ b/studio/frontend/src/features/browser/bookmarks-io.ts@@ -93,6 +93,6 @@ ${entries("other")}}/** Saves the bookmarks as an HTML file other browsers can import. */-export function exportBookmarksFile(): Promise<void> {- return downloadFile(bookmarksHtml(), "unsloth-bookmarks.html", "text/html");+export async function exportBookmarksFile(): Promise<void> {+ await downloadFile(bookmarksHtml(), "unsloth-bookmarks.html", "text/html");}diff --git a/studio/frontend/src/features/chat/prompt-storage/prompt-storage-dialog.tsx b/studio/frontend/src/features/chat/prompt-storage/prompt-storage-dialog.tsxindex c1b9e97299f..1007e19e146 100644--- a/studio/frontend/src/features/chat/prompt-storage/prompt-storage-dialog.tsx+++ b/studio/frontend/src/features/chat/prompt-storage/prompt-storage-dialog.tsx@@ -134,7 +134,7 @@ async function downloadBlob(filename: string,mimeType: string,): Promise<void> {- return downloadFile(content, filename, mimeType);+ await downloadFile(content, filename, mimeType);}function exportPromptJsonl(entry: PromptEntry): Promise<void> {diff --git a/studio/frontend/src/features/images/images-page.tsx b/studio/frontend/src/features/images/images-page.tsxindex db6acf0abb9..cda5c5b20a4 100644--- a/studio/frontend/src/features/images/images-page.tsx+++ b/studio/frontend/src/features/images/images-page.tsx@@ -678,9 +678,9 @@ async function downloadImage(}}const filename = exportFilename(image, outputFormat);- await downloadFile(output, filename, output.type || "image/png");- if (isTauri) {- toast.success("Image saved", { description: filename });+ const savedFileName = await downloadFile(output, filename, output.type || "image/png");+ if (savedFileName !== undefined) {+ toast.success("Image saved", { description: savedFileName });}} catch (error) {if (isDownloadCancelled(error)) {diff --git a/studio/frontend/src/features/recipe-studio/components/executions/download-execution-dataset.ts b/studio/frontend/src/features/recipe-studio/components/executions/download-execution-dataset.tsindex e4fa7686a93..968fb10be11 100644--- a/studio/frontend/src/features/recipe-studio/components/executions/download-execution-dataset.ts+++ b/studio/frontend/src/features/recipe-studio/components/executions/download-execution-dataset.ts@@ -31,13 +31,13 @@ function buildDownloadFilename(execution: RecipeExecutionRecord): string {return sanitizeFilenameStem(execution.id);}-function triggerClientJsonlDownload(+async function triggerClientJsonlDownload(rows: Record<string, unknown>[],filenameStem: string,): Promise<void> {const body =rows.map((row) => JSON.stringify(row)).join("\n") + (rows.length > 0 ? "\n" : "");- return downloadFile(body, `${filenameStem}.jsonl`, "application/x-ndjson");+ await downloadFile(body, `${filenameStem}.jsonl`, "application/x-ndjson");}export async function downloadExecutionDataset(diff --git a/studio/frontend/src/features/video/video-page.tsx b/studio/frontend/src/features/video/video-page.tsxindex 71c1b944242..5b2630a1f7f 100644--- a/studio/frontend/src/features/video/video-page.tsx+++ b/studio/frontend/src/features/video/video-page.tsx@@ -436,13 +436,12 @@ async function downloadVideo(src: string,video: GalleryVideo,format: VideoExportFormat = "mp4",-) {+): Promise<string | undefined> {if (format === "mp4") {- await downloadUrlStreaming(src, exportFilename(video, format));- return;+ return downloadUrlStreaming(src, exportFilename(video, format));}const blob = await fetchGalleryVideoExport(video.id, format);- await downloadFile(blob, exportFilename(video, format), blob.type);+ return downloadFile(blob, exportFilename(video, format), blob.type);}function formatTimestamp(iso: string): string {@@ -1903,10 +1902,10 @@ function VideoGenerator({const toastId =format === "mp4" ? null : toast.loading(`Converting to ${format.toUpperCase()}…`);try {- await downloadVideo(src, video, format);+ const savedFileName = await downloadVideo(src, video, format);if (toastId !== null) toast.dismiss(toastId);- if (isTauri) {- toast.success("Video saved", { description: exportFilename(video, format) });+ if (savedFileName !== undefined) {+ toast.success("Video saved", { description: savedFileName });}} catch (err) {if (toastId !== null) toast.dismiss(toastId);diff --git a/studio/frontend/src/lib/native-files.ts b/studio/frontend/src/lib/native-files.tsindex 6c0c9eb1e43..5b35f535b3d 100644--- a/studio/frontend/src/lib/native-files.ts+++ b/studio/frontend/src/lib/native-files.ts@@ -82,11 +82,12 @@ async function fetchDownload(url: string): Promise<Response> {return response;}+/** Returns the chosen basename after a native save; browsers do not report the final name. */export async function downloadFile(content: string | Blob | Uint8Array,filename: string,mimeType = "application/octet-stream",-): Promise<void> {+): Promise<string | undefined> {if (isTauri) {const { invoke } = await import("@tauri-apps/api/core");const bytes =@@ -95,15 +96,15 @@ export async function downloadFile(: content instanceof Blob? new Uint8Array(await content.arrayBuffer()): content;- const savedPath = await invoke<string | null>("save_native_file", bytes, {+ const savedFileName = await invoke<string | null>("save_native_file", bytes, {headers: {[NATIVE_FILE_NAME_HEADER]: encodeNativeFilename(filename),},});- if (savedPath === null) {+ if (savedFileName === null) {throw new DownloadCancelledError();}- return;+ return savedFileName;}const browserContent =@@ -121,7 +122,7 @@ export async function downloadFile(export async function downloadBlobStreaming(content: Blob,filename: string,-): Promise<void> {+): Promise<string | undefined> {if (!isTauri) {browserDownload(content, filename);return;@@ -148,7 +149,7 @@ export async function downloadBlobStreaming(headers: { [NATIVE_FILE_SAVE_TOKEN_HEADER]: token },});}- await invoke<string>("finish_native_file_save", { token });+ return await invoke<string>("finish_native_file_save", { token });} catch (error) {await invoke<void>("cancel_native_file_save", { token }).catch(() => undefined,@@ -169,29 +170,29 @@ export async function urlToBlob(url: string): Promise<Blob> {export async function downloadUrlStreaming(url: string,filename: string,-): Promise<void> {+): Promise<string | undefined> {if (!isTauri) {browserUrlDownload(url, filename);return;}const { invoke } = await import("@tauri-apps/api/core");- const savedPath = await invoke<string | null>("save_native_file_from_url", {+ const savedFileName = await invoke<string | null>("save_native_file_from_url", {url,fileName: filename,});- if (savedPath === null) {+ if (savedFileName === null) {throw new DownloadCancelledError();}+ return savedFileName;}export async function downloadUrl(url: string,filename: string,-): Promise<void> {+): Promise<string | undefined> {if (isDataUri(url)) {const { bytes, mimeType } = decodeDataUri(url);- await downloadFile(bytes, filename, mimeType);- return;+ return downloadFile(bytes, filename, mimeType);}if (!isTauri) {browserUrlDownload(url, filename);@@ -199,7 +200,7 @@ export async function downloadUrl(}const response = await fetchDownload(url);const bytes = new Uint8Array(await response.arrayBuffer());- await downloadFile(+ return downloadFile(bytes,filename,response.headers.get("content-type") || "application/octet-stream",diff --git a/studio/frontend/tests/native-file-save.test.ts b/studio/frontend/tests/native-file-save.test.tsnew file mode 100644index 00000000000..d45bd0fd59f--- /dev/null+++ b/studio/frontend/tests/native-file-save.test.ts@@ -0,0 +1,194 @@+// 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 test from "node:test";++import type * as NativeFilesModule from "../src/lib/native-files.ts";+import { loadWithStubs } from "./helpers/module-stubs.ts";++type Invoke = (+ command: string,+ args?: unknown,+ options?: { headers?: Record<string, string> },+) => Promise<unknown>;++function loadNativeFiles(+ isTauri: boolean,+ invoke: Invoke,+): typeof NativeFilesModule {+ return loadWithStubs<typeof NativeFilesModule>(+ new URL("../src/lib/native-files.ts", import.meta.url),+ {+ "@/lib/api-base": { isTauri },+ "@/lib/data-uri": {},+ "@tauri-apps/api/core": { invoke },+ },+ );+}++for (const savedFileName of [+ "renamed-image.png",+ "suggested.png",+ "caf\u00e9.png",+]) {+ test(`native save reports the selected basename: ${savedFileName}`, async () => {+ const bytes = Uint8Array.of(137, 80, 78, 71);+ let calls = 0;+ const files = loadNativeFiles(true, (command, args, options) => {+ calls += 1;+ assert.equal(command, "save_native_file");+ assert.deepEqual(args, bytes);+ assert.equal(+ Buffer.from(+ options?.headers?.["x-unsloth-default-name"] ?? "",+ "base64",+ ).toString("utf8"),+ "suggested.png",+ );+ return Promise.resolve(savedFileName);+ });++ const result = await files.downloadFile(+ new Blob([bytes], { type: "image/png" }),+ "suggested.png",+ );+ assert.equal(result, savedFileName);+ assert.equal(calls, 1);+ });+}++test("native cancellation rejects instead of reporting a saved filename", async () => {+ const files = loadNativeFiles(true, () => Promise.resolve(null));+ await assert.rejects(+ files.downloadFile("contents", "suggested.txt"),+ (error: unknown) => files.isDownloadCancelled(error),+ );+});++test("a failed native save propagates its error instead of reporting success", async () => {+ const failure = new Error("Disk full");+ const files = loadNativeFiles(true, () => Promise.reject(failure));+ await assert.rejects(+ files.downloadFile("contents", "suggested.txt"),+ (error: unknown) => error === failure,+ );+});++test("browser download suggests a name but does not report an unknown final filename", async (t) => {+ const originalDocument = Object.getOwnPropertyDescriptor(+ globalThis,+ "document",+ );+ const originalWindow = Object.getOwnPropertyDescriptor(globalThis, "window");+ t.after(() => {+ for (const [key, descriptor] of [+ ["document", originalDocument],+ ["window", originalWindow],+ ] as const) {+ if (descriptor) {+ Object.defineProperty(globalThis, key, descriptor);+ } else {+ Reflect.deleteProperty(globalThis, key);+ }+ }+ });++ let downloadedBlob: Blob | undefined;+ let clicked = false;+ let removed = false;+ const anchor = {+ href: "",+ download: "",+ click: () => {+ clicked = true;+ },+ remove: () => {+ removed = true;+ },+ };+ const objectUrl = "blob:browser-download";+ t.mock.method(URL, "createObjectURL", (blob: Blob) => {+ downloadedBlob = blob;+ return objectUrl;+ });+ const revoke = t.mock.method(URL, "revokeObjectURL", () => undefined);+ Object.defineProperty(globalThis, "document", {+ configurable: true,+ value: {+ createElement: (tag: string) => {+ assert.equal(tag, "a");+ return anchor;+ },+ body: { appendChild: (child: unknown) => assert.equal(child, anchor) },+ },+ });+ Object.defineProperty(globalThis, "window", {+ configurable: true,+ value: {+ setTimeout: (callback: () => void) => {+ callback();+ return 1;+ },+ },+ });+ const files = loadNativeFiles(false, () =>+ Promise.reject(new Error("Browser must not invoke native save")),+ );++ const result = await files.downloadFile(+ "contents",+ "suggested.txt",+ "text/plain",+ );+ assert.equal(result, undefined);+ assert.equal(anchor.download, "suggested.txt");+ assert.equal(anchor.href, objectUrl);+ assert.equal(clicked, true);+ assert.equal(removed, true);+ assert.equal(downloadedBlob?.type, "text/plain");+ assert.equal(await downloadedBlob?.text(), "contents");+ assert.deepEqual(revoke.mock.calls[0]?.arguments, [objectUrl]);+});++test("a chunked native save reports the basename the dialog wrote", async () => {+ const files = loadNativeFiles(true, (command) => {+ if (command === "begin_native_file_save") return Promise.resolve("token");+ if (command === "append_native_file_save_chunk") return Promise.resolve(undefined);+ assert.equal(command, "finish_native_file_save");+ return Promise.resolve("renamed-stems.zip");+ });+ assert.equal(+ await files.downloadBlobStreaming(new Blob(["stems"]), "stems.zip"),+ "renamed-stems.zip",+ );+});++test("a streamed URL save reports the basename the dialog wrote", async () => {+ const files = loadNativeFiles(true, (command, args) => {+ assert.equal(command, "save_native_file_from_url");+ assert.deepEqual(args, { url: "/clip.mp4", fileName: "clip.mp4" });+ return Promise.resolve("renamed-clip.mp4");+ });+ assert.equal(await files.downloadUrlStreaming("/clip.mp4", "clip.mp4"), "renamed-clip.mp4");+});++test("a native URL save reports the basename the dialog wrote", async (t) => {+ t.mock.method(globalThis, "fetch", () =>+ Promise.resolve(new Response("wav", { headers: { "content-type": "audio/wav" } })),+ );+ const files = loadWithStubs<typeof NativeFilesModule>(+ new URL("../src/lib/native-files.ts", import.meta.url),+ {+ "@/lib/api-base": { isTauri: true },+ "@/lib/data-uri": { isDataUri: () => false },+ "@tauri-apps/api/core": {+ invoke: (command: string) => {+ assert.equal(command, "save_native_file");+ return Promise.resolve("renamed-clip.wav");+ },+ },+ },+ );+ assert.equal(await files.downloadUrl("/clip.wav", "clip.wav"), "renamed-clip.wav");+});diff --git a/tests/studio/test_desktop_reliability_frontend_contract.py b/tests/studio/test_desktop_reliability_frontend_contract.pyindex 9bfbdc89c69..57de410eb69 100644--- a/tests/studio/test_desktop_reliability_frontend_contract.py+++ b/tests/studio/test_desktop_reliability_frontend_contract.py@@ -267,13 +267,15 @@ def test_media_galleries_save_natively_with_feedback():assert "if (src && !isTauri)" in downloadassert "blob.type !== `image/${format}`" in reencodeassert "isDownloadCancelled(error)" in download- assert "if (isTauri)" in download- assert 'toast.success("Image saved", { description: filename });' in download+ assert "const savedFileName = await downloadFile(" in download+ assert "if (savedFileName !== undefined)" in download+ assert 'toast.success("Image saved", { description: savedFileName });' in downloadassert 'document.createElement("a")' not in download- assert "await downloadFile(blob, exportFilename(video, format), blob.type);" in video_page- assert "if (isTauri)" in video_download- assert 'toast.success("Video saved"' in video_download+ assert "return downloadFile(blob, exportFilename(video, format), blob.type);" in video_page+ assert "const savedFileName = await downloadVideo(" in video_download+ assert "if (savedFileName !== undefined)" in video_download+ assert 'toast.success("Video saved", { description: savedFileName });' in video_downloadassert "function saveLink(" not in video_page@@ -285,8 +287,9 @@ def test_audio_clips_and_stems_save_natively():# desktop re-reads blob URLs because the page CSP blocks fetch()assert 'isTauri && url.startsWith("blob:")' in save_audio- assert "await downloadBlobStreaming(blob, filename);" in save_audio- assert "await downloadUrl(url, filename);" in save_audio+ assert "await downloadBlobStreaming(blob, filename)" in save_audio+ assert "savedFileName = await downloadUrl(url, filename);" in save_audio+ assert 'toast.success("Audio saved", { description: savedFileName });' in save_audioassert "if (isDownloadCancelled(error)) return;" in save_audio# tests/audio-stem-mixer-state.test.ts forbids raw anchors in features/audiofor page in ("hooks/use-audio-gallery.tsx", "pages/separate-page.tsx"):@@ -319,7 +322,7 @@ def test_chat_exports_await_native_saves_and_markdown_uses_shared_helper():projects = _ui_source(FRONTEND / "features/chat/projects-page.tsx")assert "async function downloadBlob(" in prompt_storagedownload_blob = prompt_storage.split("async function downloadBlob(", 1)[1].split("\n}\n", 1)[0]- assert "return downloadFile(" in download_blob+ assert "await downloadFile(" in download_blobassert "catch (error)" not in download_blobassert "isDownloadCancelled(error)" in prompt_storage@@ -393,7 +396,7 @@ def test_gallery_video_links_are_absolute_and_saved_natively():assert "downloadUrlStreaming(src, exportFilename(video, format))" in video_pageassert '"save_native_file_from_url"' in helperassert "isDownloadCancelled(err)" in video_page- assert "await downloadFile(blob, exportFilename(video, format), blob.type);" in video_page+ assert "return downloadFile(blob, exportFilename(video, format), blob.type);" in video_pageassert "URL.createObjectURL(blob)" not in video_page# media-src, not just connect-src: the signed link is played by an element.