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: keep custom spellings when another window saves voice settings (#16577)

Nilayf7eeafb0fc36
diff --git a/studio/frontend/src/features/audio/stores/audio-voices-store.ts b/studio/frontend/src/features/audio/stores/audio-voices-store.ts
index 175b8160884..d55d9132050 100644
--- a/studio/frontend/src/features/audio/stores/audio-voices-store.ts
+++ b/studio/frontend/src/features/audio/stores/audio-voices-store.ts
@@ -23,10 +23,12 @@ interface AudioVoicesState {
patch: Pick<AudioVoice, "name" | "transcript" | "language">,
) => Promise<void>;
remove: (id: string) => Promise<void>;
+ confirmGone: (id: string) => Promise<boolean>;
}
// Bumped by each save, rename and delete: a list fetched across one is stale and is fetched again.
let mutations = 0;
+let listing: Promise<void> | null = null;
const message = (error: unknown, fallback: string) =>
error instanceof Error ? error.message : fallback;
@@ -36,25 +38,29 @@ export const useAudioVoicesStore = create<AudioVoicesState>()((set, get) => ({
loaded: false,
loading: false,
error: null,
- refresh: async () => {
- if (get().loading) return;
- set({ loading: true });
- const started = mutations;
- try {
- const voices = await listVoices();
- if (started !== mutations) {
- set({ loading: false });
- return get().refresh();
+ // Callers share one request, so awaiting it always sees the list it fetched.
+ refresh: () =>
+ (listing ??= (async () => {
+ set({ loading: true });
+ try {
+ for (;;) {
+ const started = mutations;
+ const voices = await listVoices();
+ if (started === mutations) {
+ set({ voices, loaded: true, loading: false, error: null });
+ return;
+ }
+ }
+ } catch (error) {
+ set({
+ loading: false,
+ loaded: true,
+ error: message(error, "Could not list saved voices."),
+ });
+ } finally {
+ listing = null;
}
- set({ voices, loaded: true, loading: false, error: null });
- } catch (error) {
- set({
- loading: false,
- loaded: true,
- error: message(error, "Could not list saved voices."),
- });
- }
- },
+ })()),
save: async (request) => {
const voice = await createVoice(request);
mutations += 1;
@@ -95,6 +101,10 @@ export const useAudioVoicesStore = create<AudioVoicesState>()((set, get) => ({
try {
await deleteVoice(id);
mutations += 1;
+ // A list fetched before the delete landed may have brought it back.
+ set((state) => ({
+ voices: state.voices.filter((voice) => voice.id !== id),
+ }));
} catch (error) {
// Put back only this voice: another deletion may have finished meanwhile.
const failed = before[index];
@@ -109,4 +119,11 @@ export const useAudioVoicesStore = create<AudioVoicesState>()((set, get) => ({
throw error;
}
},
+ // Another window may have saved this voice after our list was fetched, so check a fresh list.
+ confirmGone: async (id) => {
+ if (get().loading) await get().refresh();
+ await get().refresh();
+ const { voices, error } = get();
+ return !error && !voices.some((voice) => voice.id === id);
+ },
}));
diff --git a/studio/frontend/src/features/chat/external-providers.ts b/studio/frontend/src/features/chat/external-providers.ts
index a817a623592..ede1749ae08 100644
--- a/studio/frontend/src/features/chat/external-providers.ts
+++ b/studio/frontend/src/features/chat/external-providers.ts
@@ -535,9 +535,9 @@ export function toExternalBackendProviderType(
: providerType;
}
-const EXTERNAL_PROVIDERS_KEY = "unsloth_chat_external_providers";
+export const EXTERNAL_PROVIDERS_KEY = "unsloth_chat_external_providers";
const EXTERNAL_PROVIDER_KEYS_KEY = "unsloth_chat_external_provider_keys";
-const CONNECTIONS_ENABLED_KEY = "unsloth_chat_connections_enabled";
+export const CONNECTIONS_ENABLED_KEY = "unsloth_chat_connections_enabled";
const EXTERNAL_MODEL_PREFIX = "external::";
function canUseStorage(): boolean {
diff --git a/studio/frontend/src/features/chat/stores/external-providers-store.ts b/studio/frontend/src/features/chat/stores/external-providers-store.ts
index 22accc5a23a..c2c809221b0 100644
--- a/studio/frontend/src/features/chat/stores/external-providers-store.ts
+++ b/studio/frontend/src/features/chat/stores/external-providers-store.ts
@@ -3,6 +3,8 @@
import { create } from "zustand";
import {
+ CONNECTIONS_ENABLED_KEY,
+ EXTERNAL_PROVIDERS_KEY,
loadConnectionsEnabled,
loadExternalProviders,
saveConnectionsEnabled,
@@ -34,3 +36,17 @@ export const useExternalProvidersStore = create<ExternalProvidersState>(
},
}),
);
+
+// Voice settings sync across tabs and drop a provider id this tab does not list.
+if (typeof window !== "undefined") {
+ window.addEventListener("storage", (event) => {
+ if (event.key === EXTERNAL_PROVIDERS_KEY || event.key === null) {
+ useExternalProvidersStore.setState({ providers: loadExternalProviders() });
+ }
+ if (event.key === CONNECTIONS_ENABLED_KEY || event.key === null) {
+ useExternalProvidersStore.setState({
+ connectionsEnabled: loadConnectionsEnabled(),
+ });
+ }
+ });
+}
diff --git a/studio/frontend/src/features/settings/stores/voice-settings-store.ts b/studio/frontend/src/features/settings/stores/voice-settings-store.ts
index 56b45e715b9..bff4a50a41e 100644
--- a/studio/frontend/src/features/settings/stores/voice-settings-store.ts
+++ b/studio/frontend/src/features/settings/stores/voice-settings-store.ts
@@ -420,6 +420,13 @@ export const useVoiceSettingsStore = create<VoiceSettingsState>()(
},
),
);
+if (typeof window !== "undefined") {
+ window.addEventListener("storage", (event) => {
+ if (event.key === useVoiceSettingsStore.persist.getOptions().name) {
+ void useVoiceSettingsStore.persist.rehydrate();
+ }
+ });
+}
function asString(value: unknown, fallback: string): string {
return typeof value === "string" && value ? value : fallback;
diff --git a/studio/frontend/src/features/settings/tabs/voice-tab.tsx b/studio/frontend/src/features/settings/tabs/voice-tab.tsx
index 2ce848cd3d0..81fb911ef74 100644
--- a/studio/frontend/src/features/settings/tabs/voice-tab.tsx
+++ b/studio/frontend/src/features/settings/tabs/voice-tab.tsx
@@ -649,9 +649,19 @@ export function VoiceTab() {
(voice) => voice.id === ttsStudioVoiceId,
);
useEffect(() => {
- if (ttsStudioVoiceId && savedVoicesListed && !hasSelectedStudioVoice) {
- setTtsStudioVoiceId("");
+ if (!ttsStudioVoiceId || !savedVoicesListed || hasSelectedStudioVoice) {
+ return;
}
+ let cancelled = false;
+ void useAudioVoicesStore
+ .getState()
+ .confirmGone(ttsStudioVoiceId)
+ .then((gone) => {
+ if (gone && !cancelled) setTtsStudioVoiceId("");
+ });
+ return () => {
+ cancelled = true;
+ };
}, [
hasSelectedStudioVoice,
savedVoicesListed,
diff --git a/studio/frontend/tests/audio-clone-store.test.ts b/studio/frontend/tests/audio-clone-store.test.ts
index d930876e5ea..402f98a0d89 100644
--- a/studio/frontend/tests/audio-clone-store.test.ts
+++ b/studio/frontend/tests/audio-clone-store.test.ts
@@ -200,7 +200,7 @@ test("voice previews are bounded, stale ones dropped, and the menu follows disab
test("a voice list fetched across a save, rename or delete is fetched again", () => {
const src = readSrc("features/audio/stores/audio-voices-store.ts");
- assert.match(src, /const started = mutations;[\s\S]*?if \(started !== mutations\) \{\s*set\(\{ loading: false \}\);\s*return get\(\)\.refresh\(\);/);
+ assert.match(src, /for \(;;\) \{\s*const started = mutations;\s*const voices = await listVoices\(\);\s*if \(started === mutations\) \{/);
for (const call of ["createVoice\\(request\\)", "updateVoice\\(id, patch\\)", "deleteVoice\\(id\\)"]) {
assert.match(src, new RegExp(`await ${call};\\s*mutations \\+= 1;`), call);
}
diff --git a/studio/frontend/tests/audio-voices-confirm-gone.test.ts b/studio/frontend/tests/audio-voices-confirm-gone.test.ts
new file mode 100644
index 00000000000..9cdfaecbb04
--- /dev/null
+++ b/studio/frontend/tests/audio-voices-confirm-gone.test.ts
@@ -0,0 +1,95 @@
+// 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 * as zustand from "zustand";
+
+import { loadWithStubs } from "./helpers/module-stubs.ts";
+
+type Voice = { id: string };
+let server: Voice[] = [];
+let gate: Promise<void> = Promise.resolve();
+let deleteGate: Promise<void> = Promise.resolve();
+let offline = false;
+
+const { useAudioVoicesStore } = loadWithStubs<
+ typeof import("../src/features/audio/stores/audio-voices-store.ts")
+>(new URL("../src/features/audio/stores/audio-voices-store.ts", import.meta.url), {
+ zustand,
+ "../api": {
+ listVoices: async () => {
+ if (offline) throw new Error("offline");
+ const snapshot = [...server];
+ await gate;
+ return snapshot;
+ },
+ createVoice: async () => ({ id: "x" }),
+ updateVoice: async () => ({ id: "x" }),
+ deleteVoice: async (id: string) => {
+ await deleteGate;
+ server = server.filter((voice) => voice.id !== id);
+ },
+ },
+});
+
+const store = () => useAudioVoicesStore.getState();
+
+test("a list fetched before a delete lands does not bring the voice back", async () => {
+ server = [{ id: "v-del" }];
+ await store().refresh();
+ let finishDelete!: () => void;
+ deleteGate = new Promise((resolve) => (finishDelete = resolve));
+ const removal = store().remove("v-del");
+ await store().refresh();
+ finishDelete();
+ await removal;
+ deleteGate = Promise.resolve();
+ assert.deepEqual(store().voices.map((v) => v.id), []);
+});
+
+test("a voice saved in another window after our list loaded is not treated as deleted", async () => {
+ server = [];
+ await store().refresh();
+ server = [{ id: "v-new" }];
+ assert.equal(await store().confirmGone("v-new"), false);
+ assert.deepEqual(store().voices.map((v) => v.id), ["v-new"]);
+});
+
+test("a voice that is really gone is confirmed gone", async () => {
+ server = [];
+ assert.equal(await store().confirmGone("v-new"), true);
+});
+
+test("a list request already in flight is not trusted for a newer voice", async () => {
+ server = [];
+ let release!: () => void;
+ gate = new Promise((resolve) => (release = resolve));
+ const stale = store().refresh();
+ server = [{ id: "v-late" }];
+ const check = store().confirmGone("v-late");
+ gate = Promise.resolve();
+ release();
+ await stale;
+ assert.equal(await check, false);
+});
+
+test("two checks waiting on the same old list both see a fresh one", async () => {
+ server = [];
+ let release!: () => void;
+ gate = new Promise((resolve) => (release = resolve));
+ const stale = store().refresh();
+ server = [{ id: "v-late" }];
+ const first = store().confirmGone("v-late");
+ const second = store().confirmGone("v-late");
+ gate = Promise.resolve();
+ release();
+ await stale;
+ assert.deepEqual([await first, await second], [false, false]);
+});
+
+test("a failed list keeps the selection", async () => {
+ offline = true;
+ assert.equal(await store().confirmGone("anything"), false);
+ offline = false;
+});
diff --git a/studio/frontend/tests/external-providers-storage-sync.test.ts b/studio/frontend/tests/external-providers-storage-sync.test.ts
new file mode 100644
index 00000000000..97a4444b433
--- /dev/null
+++ b/studio/frontend/tests/external-providers-storage-sync.test.ts
@@ -0,0 +1,45 @@
+// 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 { installLocalStorageFake, registerBundlerResolver } from "./helpers/kit.ts";
+
+registerBundlerResolver();
+const { store, fireWindowEvent } = installLocalStorageFake();
+
+const { useExternalProvidersStore } = await import(
+ "../src/features/chat/stores/external-providers-store.ts"
+);
+
+test("a connection added in another window shows up here", () => {
+ assert.deepEqual(useExternalProvidersStore.getState().providers, []);
+ store.set(
+ "unsloth_chat_external_providers",
+ JSON.stringify([
+ {
+ id: "conn-new",
+ providerType: "openrouter",
+ name: "Router",
+ baseUrl: "https://example.com/v1",
+ models: ["vendor/model"],
+ createdAt: 1,
+ updatedAt: 1,
+ },
+ ]),
+ );
+ fireWindowEvent("storage", { key: "unsloth_chat_external_providers" });
+ assert.deepEqual(
+ useExternalProvidersStore.getState().providers.map((p) => p.id),
+ ["conn-new"],
+ );
+ assert.equal(store.get("unsloth_chat_external_providers")?.includes("conn-new"), true);
+});
+
+test("turning connections off in another window applies here", () => {
+ assert.equal(useExternalProvidersStore.getState().connectionsEnabled, true);
+ store.set("unsloth_chat_connections_enabled", "false");
+ fireWindowEvent("storage", { key: "unsloth_chat_connections_enabled" });
+ assert.equal(useExternalProvidersStore.getState().connectionsEnabled, false);
+});
diff --git a/studio/frontend/tests/read-aloud-studio-voice.test.ts b/studio/frontend/tests/read-aloud-studio-voice.test.ts
index b0b2982bf4e..6d9d64bbb9a 100644
--- a/studio/frontend/tests/read-aloud-studio-voice.test.ts
+++ b/studio/frontend/tests/read-aloud-studio-voice.test.ts
@@ -302,7 +302,7 @@ test("the voice setting persists and Settings lists saved voices for the studio
// A voice deleted on the Audio page is cleared once the list is known.
assert.match(
tab,
- /if \(ttsStudioVoiceId && savedVoicesListed && !hasSelectedStudioVoice\) \{\s*setTtsStudioVoiceId\(""\);/,
+ /if \(!ttsStudioVoiceId \|\| !savedVoicesListed \|\| hasSelectedStudioVoice\) \{[\s\S]*?\.confirmGone\(ttsStudioVoiceId\)[\s\S]*?if \(gone && !cancelled\) setTtsStudioVoiceId\(""\);/,
);
assert.match(
tab,
diff --git a/studio/frontend/tests/voice-settings-storage-sync.test.ts b/studio/frontend/tests/voice-settings-storage-sync.test.ts
new file mode 100644
index 00000000000..987d2e463dc
--- /dev/null
+++ b/studio/frontend/tests/voice-settings-storage-sync.test.ts
@@ -0,0 +1,61 @@
+// 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 * as zustand from "zustand";
+import * as zustandMiddleware from "zustand/middleware";
+
+import * as audioCppCatalog from "../src/features/audio/audio-cpp-catalog.ts";
+import * as sttModelCatalog from "../src/features/settings/stores/stt-model-catalog.ts";
+import { installLocalStorageFake } from "./helpers/kit.ts";
+import { loadWithStubs } from "./helpers/module-stubs.ts";
+
+const { store, fireWindowEvent } = installLocalStorageFake();
+const KEY = "unsloth_voice_settings";
+
+const { useVoiceSettingsStore } = loadWithStubs<
+ typeof import("../src/features/settings/stores/voice-settings-store.ts")
+>(
+ new URL("../src/features/settings/stores/voice-settings-store.ts", import.meta.url),
+ {
+ "@/lib/api-base": { isTauri: false },
+ "../../audio/audio-cpp-catalog": audioCppCatalog,
+ zustand,
+ "zustand/middleware": zustandMiddleware,
+ "./stt-model-catalog": sttModelCatalog,
+ "../api/stt-device": { syncSttDevicePreference: () => undefined },
+ },
+);
+
+const saved = () => JSON.parse(store.get(KEY) ?? "{}").state;
+
+test("a save in this window keeps what another window added", () => {
+ useVoiceSettingsStore.getState().setTtsRate(1.5);
+ store.set(
+ KEY,
+ JSON.stringify({
+ state: {
+ ...saved(),
+ dictionary: ["Zephyrine"],
+ recentDictations: [{ id: "d1", text: "hello there", at: 1 }],
+ },
+ version: 1,
+ }),
+ );
+ fireWindowEvent("storage", { key: KEY });
+ useVoiceSettingsStore.getState().setAutoReadAloud(true);
+ assert.deepEqual(saved().dictionary, ["Zephyrine"]);
+ assert.deepEqual(
+ saved().recentDictations.map((d: { text: string }) => d.text),
+ ["hello there"],
+ );
+ assert.equal(saved().ttsRate, 1.5);
+ assert.equal(saved().autoReadAloud, true);
+});
+
+test("writes to other keys leave the settings alone", () => {
+ const before = useVoiceSettingsStore.getState().dictionary;
+ fireWindowEvent("storage", { key: "unsloth_theme" });
+ assert.equal(useVoiceSettingsStore.getState().dictionary, before);
+});