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: decode native attachment base64 with Uint8Array.fromBase64 when available (#16319)
Daniel Han3e0c2efca1bd
diff --git a/studio/frontend/src/features/chat/utils/clipboard-files.ts b/studio/frontend/src/features/chat/utils/clipboard-files.tsindex e9999a9bd28..91ab103ad0c 100644--- a/studio/frontend/src/features/chat/utils/clipboard-files.ts+++ b/studio/frontend/src/features/chat/utils/clipboard-files.ts@@ -3,6 +3,7 @@import { isTauri } from "@/lib/api-base";import { MAX_AUDIO_SIZE } from "@/lib/audio-utils";+import { decodeBase64Bytes } from "@/lib/base64-bytes";import {browserClipboardFiles,clipboardAdvertisesFiles,@@ -74,11 +75,7 @@ async function readNativeClipboardFiles(): Promise<File[]> {if (file.base64.length > Math.ceil((maxFileBytes * 4) / 3) + 4) {return [];}- const binary = globalThis.atob(file.base64);- const bytes = new Uint8Array(binary.length);- for (let index = 0; index < binary.length; index += 1) {- bytes[index] = binary.charCodeAt(index);- }+ const bytes = decodeBase64Bytes(file.base64);if (bytes.byteLength > maxFileBytes) return [];totalBytes += bytes.byteLength;if (totalBytes > MAX_CLIPBOARD_BYTES) return [];diff --git a/studio/frontend/src/features/native-intents/native-attachment-file.ts b/studio/frontend/src/features/native-intents/native-attachment-file.tsindex 41b6f7d11d4..bf70c189696 100644--- a/studio/frontend/src/features/native-intents/native-attachment-file.ts+++ b/studio/frontend/src/features/native-intents/native-attachment-file.ts@@ -1,6 +1,7 @@// 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 { decodeBase64Bytes } from "@/lib/base64-bytes";import { readNativeAttachmentFile } from "./api";import type { NativeIntent } from "./types";@@ -8,11 +9,7 @@ export async function nativeAttachmentIntentToFile(intent: NativeIntent,): Promise<File> {const payload = await readNativeAttachmentFile(intent.path.token);- const binary = globalThis.atob(payload.base64);- const bytes = new Uint8Array(binary.length);- for (let index = 0; index < binary.length; index += 1) {- bytes[index] = binary.charCodeAt(index);- }+ const bytes = decodeBase64Bytes(payload.base64);return new File([bytes], payload.name, {type: payload.mimeType,lastModified: intent.path.modifiedMs ?? Date.now(),diff --git a/studio/frontend/src/lib/base64-bytes.ts b/studio/frontend/src/lib/base64-bytes.tsnew file mode 100644index 00000000000..aa22d15e537--- /dev/null+++ b/studio/frontend/src/lib/base64-bytes.ts@@ -0,0 +1,23 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++type FromBase64 = (base64: string) => Uint8Array<ArrayBuffer>;++// Inputs fromBase64 rejects go through atob so callers keep its InvalidCharacterError.+export function decodeBase64Bytes(base64: string): Uint8Array<ArrayBuffer> {+ const fromBase64 = (Uint8Array as unknown as { fromBase64?: FromBase64 })+ .fromBase64;+ if (typeof fromBase64 === "function") {+ try {+ return fromBase64.call(Uint8Array, base64);+ } catch {+ // Fall through to atob.+ }+ }+ const binary = globalThis.atob(base64);+ const bytes = new Uint8Array(binary.length);+ for (let index = 0; index < binary.length; index += 1) {+ bytes[index] = binary.charCodeAt(index);+ }+ return bytes;+}diff --git a/studio/frontend/tests/base64-bytes.test.ts b/studio/frontend/tests/base64-bytes.test.tsnew file mode 100644index 00000000000..22d3686c9e6--- /dev/null+++ b/studio/frontend/tests/base64-bytes.test.ts@@ -0,0 +1,138 @@+// 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 { randomBytes } from "node:crypto";+import { test } from "node:test";++import { decodeBase64Bytes } from "../src/lib/base64-bytes.ts";++// The decoder both native readers used before.+function atobLoop(base64: string): Uint8Array {+ const binary = globalThis.atob(base64);+ const bytes = new Uint8Array(binary.length);+ for (let index = 0; index < binary.length; index += 1) {+ bytes[index] = binary.charCodeAt(index);+ }+ return bytes;+}++function outcome(decode: (value: string) => Uint8Array, value: string): string {+ try {+ return `ok:${Buffer.from(decode(value)).toString("hex")}`;+ } catch (error) {+ return `err:${(error as Error).name}`;+ }+}++const edgeInputs = [+ "",+ "=",+ "==",+ "A",+ "AA",+ "AAA",+ "AAAA",+ "AA=",+ "AA==",+ "AAA=",+ "AB==",+ "AF==",+ "AAA==",+ "A===",+ "AAAA=",+ "AAAA==",+ " AAAA ",+ "A A\nA\tA",+ "AA==AA==",+ "QQ",+ "QQ=",+ "QQ==",+ "QUI",+ "QUI=",+ "QUJD",+ "-_-_",+ "AAA.",+ "%%%%",+ "ÿÿ",+];++function randomInputs(): string[] {+ const alphabet = "AB/+=-_ \n\tz09.%Q";+ const inputs: string[] = [];+ for (let index = 0; index < 5000; index += 1) {+ let value = "";+ const length = Math.floor(Math.random() * 9);+ for (let j = 0; j < length; j += 1) {+ value += alphabet[Math.floor(Math.random() * alphabet.length)];+ }+ inputs.push(value);+ }+ for (const length of [0, 1, 2, 3, 4, 5, 255, 256, 257, 65_537]) {+ inputs.push(randomBytes(length).toString("base64"));+ }+ return inputs;+}++function withFromBase64<T>(impl: unknown, run: () => T): T {+ const target = Uint8Array as unknown as { fromBase64?: unknown };+ const had = Object.prototype.hasOwnProperty.call(target, "fromBase64");+ const previous = target.fromBase64;+ target.fromBase64 = impl;+ try {+ return run();+ } finally {+ if (had) target.fromBase64 = previous;+ else delete target.fromBase64;+ }+}++test("decodeBase64Bytes matches the atob loop without fromBase64", () => {+ withFromBase64(undefined, () => {+ for (const value of [...edgeInputs, ...randomInputs()]) {+ assert.equal(+ outcome(decodeBase64Bytes, value),+ outcome(atobLoop, value),+ JSON.stringify(value),+ );+ }+ });+});++test("decodeBase64Bytes round-trips padded payloads byte for byte", () => {+ for (const length of [0, 1, 2, 3, 4, 1023, 1024, 1025, 1 << 20]) {+ const source = randomBytes(length);+ const decoded = decodeBase64Bytes(source.toString("base64"));+ assert.ok(decoded instanceof Uint8Array);+ assert.equal(decoded.byteLength, length);+ assert.ok(Buffer.from(decoded).equals(source));+ }+});++test("decodeBase64Bytes uses fromBase64 and keeps atob's error when it throws", () => {+ const calls: string[] = [];+ withFromBase64(+ function (this: unknown, value: string) {+ assert.equal(this, Uint8Array);+ calls.push(value);+ return new Uint8Array(Buffer.from(value, "base64"));+ },+ () => {+ assert.deepEqual(Array.from(decodeBase64Bytes("QUJD")), [65, 66, 67]);+ },+ );+ assert.deepEqual(calls, ["QUJD"]);++ withFromBase64(+ () => {+ throw new SyntaxError("bad base64");+ },+ () => {+ assert.equal(outcome(decodeBase64Bytes, "A"), outcome(atobLoop, "A"));+ assert.equal(+ outcome(decodeBase64Bytes, "A"),+ "err:InvalidCharacterError",+ );+ },+ );+});diff --git a/tests/studio/test_desktop_reliability_frontend_contract.py b/tests/studio/test_desktop_reliability_frontend_contract.pyindex b5c90b2ff13..4c507589ec3 100644--- a/tests/studio/test_desktop_reliability_frontend_contract.py+++ b/tests/studio/test_desktop_reliability_frontend_contract.py@@ -447,7 +447,7 @@ def test_clipboard_file_paste_is_bounded_and_wired_to_both_composers():'types.includes("files")','type.includes("uri-list")','"read_native_clipboard_files"',- "globalThis.atob(file.base64)",+ "decodeBase64Bytes(file.base64)","new File([bytes], file.name","MAX_CLIPBOARD_BYTES",'import("@tauri-apps/plugin-clipboard-manager")',