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: read unread scheduled replies aloud when opened (#16847)
oobaboogab2f8dafea3ac
diff --git a/studio/frontend/src/components/app-sidebar.tsx b/studio/frontend/src/components/app-sidebar.tsxindex e5555511e84..48e2750df9e 100644--- a/studio/frontend/src/components/app-sidebar.tsx+++ b/studio/frontend/src/components/app-sidebar.tsx@@ -271,6 +271,7 @@ import {prefersReducedMotion,} from "@/features/settings/stores/appearance-custom-store";import { useSettingsDialogStore } from "@/features/settings/stores/settings-dialog-store";+import { useVoiceSettingsStore } from "@/features/settings/stores/voice-settings-store";import {isSurfaceBackgrounded,useShortcutLabel,@@ -3950,6 +3951,12 @@ export function AppSidebar() {}function clearChatNotifications(item: SidebarItem) {+ // Capture the unread result before Recents clears its persistent unread marker.+ const { ttsEnabled, autoReadAloud } = useVoiceSettingsStore.getState();+ useUnseenScheduledRuns.getState().openThread(+ item.type === "single" ? item.id : null,+ ttsEnabled && autoReadAloud,+ );clearThreadsUnread(getSidebarItemThreadIds(item));}diff --git a/studio/frontend/src/components/assistant-ui/auto-read-aloud.tsx b/studio/frontend/src/components/assistant-ui/auto-read-aloud.tsxindex 9ce4cadc4cd..b3f9782d4dc 100644--- a/studio/frontend/src/components/assistant-ui/auto-read-aloud.tsx+++ b/studio/frontend/src/components/assistant-ui/auto-read-aloud.tsx@@ -3,6 +3,7 @@import { decideAutoRead, useChatActive } from "@/features/chat";import { useVoiceSettingsStore } from "@/features/settings";+import { useUnseenScheduledRuns } from "@/features/scheduled-tasks";import {type AssistantRuntime,useAui,@@ -18,6 +19,9 @@ export const AutoReadAloud: FC = () => {const pendingRef = useRef(false);const lastSpokenRef = useRef<string | null>(null);const threadId = useAuiState(({ threadListItem }) => threadListItem.id);+ const remoteId = useAuiState(({ threadListItem }) => threadListItem.remoteId);+ const loading = useAuiState(({ thread }) => thread.isLoading);+ const scheduledPending = useUnseenScheduledRuns((state) => state.pendingReadAloudThreadId);const lastId = useAuiState(({ thread }) => thread.messages.at(-1)?.id);const lastStatus = useAuiState(({ thread }) => thread.messages.at(-1)?.status?.type,@@ -62,6 +66,38 @@ export const AutoReadAloud: FC = () => {resolvePending();}, [lastId, lastStatus, resolvePending]);+ useEffect(() => {+ if (!active || loading || !remoteId || scheduledPending !== remoteId) return;+ const thread = aui.thread();+ const state = thread.getState();+ // A new runtime starts empty before its history adapter enters the loading state.+ if (state.messages.length === 0) return;+ const decision = decideAutoRead(state.messages);+ if (decision.kind === "wait") return;+ if (!useUnseenScheduledRuns.getState().takeReadAloud(remoteId)) return;+ const { ttsEnabled, autoReadAloud } = useVoiceSettingsStore.getState();+ const source = state.messages.at(-1)?.metadata?.custom?.scheduledTask as+ | { taskId?: unknown; runId?: unknown }+ | undefined;+ if (+ !ttsEnabled ||+ !autoReadAloud ||+ state.speech ||+ decision.kind !== "speak" ||+ typeof source?.taskId !== "string" ||+ typeof source?.runId !== "string" ||+ lastSpokenRef.current === decision.messageId+ ) {+ return;+ }+ lastSpokenRef.current = decision.messageId;+ try {+ thread.message({ id: decision.messageId }).speak();+ } catch {+ // Keep the manual Read aloud action available when speech cannot start.+ }+ }, [active, aui, lastId, lastStatus, loading, remoteId, scheduledPending]);+// The old thread's runtime stays alive, so its playback would overlap the next chat's.useEffect(() => {let bound: ReturnType<AssistantRuntime["threads"]["getById"]> | null = null;diff --git a/studio/frontend/src/features/scheduled-tasks/run-notifier.tsx b/studio/frontend/src/features/scheduled-tasks/run-notifier.tsxindex ede7190628a..c8f495bcd7d 100644--- a/studio/frontend/src/features/scheduled-tasks/run-notifier.tsx+++ b/studio/frontend/src/features/scheduled-tasks/run-notifier.tsx@@ -6,6 +6,7 @@ import { Suspense, lazy, useEffect, useRef, useState } from "react";import { AUTH_SESSION_STORED_EVENT, getAuthSessionEpoch, hasSettledAuthSession } from "@/features/auth";import { notifyChatHistoryUpdated } from "@/features/chat/api/chat-api";import { useChatNavigationStore } from "@/features/chat/stores/chat-navigation-store";+import { useVoiceSettingsStore } from "@/features/settings/stores/voice-settings-store";import { translate } from "@/i18n";import { browserAccountKey } from "@/lib/account-transition";import { isTauri } from "@/lib/api-base";@@ -128,10 +129,16 @@ export function ScheduledTaskRunNotifier() {const pathname = useRouterState({ select: (state) => state.location.pathname });const pathnameRef = useRef(pathname);const openThread = useRouterState({- select: (state) => (state.location.search as { thread?: unknown }).thread,+ select: (state) => {+ const search = state.location.search as { thread?: unknown; compare?: unknown };+ return state.location.pathname === "/chat" && !search.compare && typeof search.thread === "string"+ ? search.thread+ : null;+ },});useEffect(() => {- if (typeof openThread === "string") useUnseenScheduledRuns.getState().clearThread(openThread);+ const { ttsEnabled, autoReadAloud } = useVoiceSettingsStore.getState();+ useUnseenScheduledRuns.getState().openThread(openThread, ttsEnabled && autoReadAloud);}, [openThread]);// Recents' unread marks follow the account's stored unseen results, so every tab and a reload// show them, and opening one anywhere clears it everywhere.diff --git a/studio/frontend/src/features/scheduled-tasks/unseen.ts b/studio/frontend/src/features/scheduled-tasks/unseen.tsindex a143054a031..28c9ab7414f 100644--- a/studio/frontend/src/features/scheduled-tasks/unseen.ts+++ b/studio/frontend/src/features/scheduled-tasks/unseen.ts@@ -21,6 +21,10 @@ interface Stored {}interface UnseenState extends Stored {+ /** This tab's first opening, retained only until the saved messages finish loading. */+ pendingReadAloudThreadId: string | null;+ openThread: (threadId: string | null, readAloud: boolean) => void;+ takeReadAloud: (threadId: string) => boolean;markFinished: (runs: { taskId: string; threadId: string | null }[], onPage: boolean) => void;clearTask: (taskId: string) => void;clearThread: (threadId: string) => void;@@ -77,6 +81,23 @@ export const useUnseenScheduledRuns = create<UnseenState>()((set, get) => {};return {...(typeof window === "undefined" ? NOTHING_UNSEEN : load()),+ pendingReadAloudThreadId: null,+ openThread: (threadId, readAloud) => {+ const current = stored() ?? get();+ const pending = get().pendingReadAloudThreadId;+ set({+ pendingReadAloudThreadId:+ threadId && readAloud && (current.threads.includes(threadId) || pending === threadId)+ ? threadId+ : null,+ });+ if (threadId) get().clearThread(threadId);+ },+ takeReadAloud: (threadId) => {+ if (get().pendingReadAloudThreadId !== threadId) return false;+ set({ pendingReadAloudThreadId: null });+ return true;+ },markFinished: (runs, onPage) =>change((current) => {const tasks = { ...current.tasks };@@ -104,10 +125,10 @@ export const useUnseenScheduledRuns = create<UnseenState>()((set, get) => {if (typeof window !== "undefined") {window.addEventListener(AUTH_SESSION_CLEARED_EVENT, () => {- useUnseenScheduledRuns.setState(NOTHING_UNSEEN);+ useUnseenScheduledRuns.setState({ ...NOTHING_UNSEEN, pendingReadAloudThreadId: null });});window.addEventListener(AUTH_SESSION_STORED_EVENT, () => {- useUnseenScheduledRuns.setState(load());+ useUnseenScheduledRuns.setState({ ...load(), pendingReadAloudThreadId: null });});window.addEventListener("storage", (event) => {if (event.key?.startsWith(UNSEEN_KEY_PREFIX) && event.key === storageKey()) {diff --git a/studio/frontend/tests/scheduled-result-read-aloud.test.ts b/studio/frontend/tests/scheduled-result-read-aloud.test.tsnew file mode 100644index 00000000000..09efa194913--- /dev/null+++ b/studio/frontend/tests/scheduled-result-read-aloud.test.ts@@ -0,0 +1,155 @@+// 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 {+ decideAutoRead,+ type AutoReadMessage,+} from "../src/features/chat/utils/auto-read-aloud.ts";+import type * as Unseen from "../src/features/scheduled-tasks/unseen.ts";+import { installLocalStorageFake } from "./helpers/kit.ts";+import { loadWithStubs } from "./helpers/module-stubs.ts";++const { fireWindowEvent } = installLocalStorageFake();++function unseenStore() {+ return loadWithStubs<typeof Unseen>(+ new URL("../src/features/scheduled-tasks/unseen.ts", import.meta.url),+ {+ zustand,+ "@/features/auth": {+ AUTH_SESSION_CLEARED_EVENT: "out",+ AUTH_SESSION_STORED_EVENT: "in",+ getAuthToken: () => "token",+ },+ "@/lib/account-transition": { browserAccountKey: () => "owner" },+ "./results": { UNSEEN_KEY_PREFIX: `unseen-${Math.random()}:` },+ },+ ).useUnseenScheduledRuns;+}++function openedResult() {+ const store = unseenStore();+ store.getState().markFinished([{ taskId: "task", threadId: "result" }], false);+ store.getState().openThread("result", true);+ return store;+}++const scheduledReply = (custom: unknown = { scheduledTask: { taskId: "task", runId: "run" } }) =>+ ({+ id: "answer",+ role: "assistant",+ status: { type: "complete" },+ content: [{ type: "text", text: "Your scheduled summary." }],+ metadata: { custom },+ }) as AutoReadMessage;++/** Renders AutoReadAloud with hand-run effects against a fake thread. */+function player(store: ReturnType<typeof unseenStore>) {+ const state = {+ active: true,+ threadListItem: { id: "local", remoteId: "result" },+ thread: { isLoading: false, messages: [] as AutoReadMessage[], speech: null as unknown },+ voice: { ttsEnabled: true, autoReadAloud: true },+ };+ const spoken: string[] = [];+ const thread = {+ getState: () => state.thread,+ message: ({ id }: { id: string }) => ({ speak: () => spoken.push(id) }),+ };+ const refs: { current: unknown }[] = [];+ const effects: { deps: unknown[]; cleanup?: unknown }[] = [];+ let index = 0;+ const component = loadWithStubs<{ AutoReadAloud: () => void }>(+ new URL("../src/components/assistant-ui/auto-read-aloud.tsx", import.meta.url),+ {+ "@/features/chat": { decideAutoRead, useChatActive: () => state.active },+ "@/features/settings": { useVoiceSettingsStore: { getState: () => state.voice } },+ "@/features/scheduled-tasks": {+ useUnseenScheduledRuns: Object.assign(+ (select: (s: ReturnType<typeof store.getState>) => unknown) => select(store.getState()),+ { getState: store.getState },+ ),+ },+ "@assistant-ui/react": {+ useAui: () => ({ thread: () => thread, threads: () => ({}) }),+ useAuiState: (select: (s: typeof state) => unknown) => select(state),+ useAuiEvent: () => undefined,+ },+ react: {+ useRef: (initial: unknown) => (refs[index++] ??= { current: initial }),+ useCallback: (callback: unknown) => callback,+ useEffect: (effect: () => unknown, deps: unknown[]) => {+ const i = index++;+ const previous = effects[i];+ if (previous && deps.every((value, d) => Object.is(value, previous.deps[d]))) return;+ if (typeof previous?.cleanup === "function") previous.cleanup();+ effects[i] = { deps, cleanup: effect() };+ },+ },+ },+ );+ const render = () => {+ index = 0;+ component.AutoReadAloud();+ };+ return { state, spoken, render };+}++test("an opened unread result survives the Recents-to-router handoff and plays once", () => {+ const store = openedResult(); // Recents, before it clears the unread marker.+ store.getState().openThread("result", true); // The router sees the same opening.+ assert.equal(store.getState().takeReadAloud("result"), true);+ store.getState().openThread("result", true);+ assert.equal(store.getState().takeReadAloud("result"), false);+});++test("read results, automatic read aloud off, leaving, and signing out drop the playback", () => {+ const store = unseenStore();+ store.getState().markFinished([{ taskId: "task", threadId: "result" }], false);+ store.getState().openThread("result", false);+ store.getState().openThread("result", true);+ assert.equal(store.getState().takeReadAloud("result"), false);++ for (const leave of [+ (s: ReturnType<typeof unseenStore>) => s.getState().openThread(null, true),+ () => fireWindowEvent("out", {}),+ ]) {+ const opened = openedResult();+ leave(opened);+ assert.equal(opened.getState().takeReadAloud("result"), false);+ }+});++test("playback waits for history, needs the matching visible chat, and speaks once", () => {+ const p = player(openedResult());+ p.state.thread.isLoading = true;+ p.state.thread.messages = [scheduledReply()];+ p.render();+ p.state.thread.isLoading = false;+ p.state.active = false;+ p.render();+ p.state.active = true;+ p.state.threadListItem.remoteId = "other";+ p.render();+ assert.deepEqual(p.spoken, []);+ p.state.threadListItem.remoteId = "result";+ p.render();+ p.render();+ assert.deepEqual(p.spoken, ["answer"]);+});++test("an ordinary reply or disabled voice settings consume the opening silently", () => {+ for (const kind of ["ordinary", "auto-off", "tts-off"]) {+ const store = openedResult();+ const p = player(store);+ p.state.thread.messages = [scheduledReply(kind === "ordinary" ? {} : undefined)];+ if (kind === "auto-off") p.state.voice.autoReadAloud = false;+ if (kind === "tts-off") p.state.voice.ttsEnabled = false;+ p.render();+ assert.deepEqual(p.spoken, [], kind);+ assert.equal(store.getState().pendingReadAloudThreadId, null, kind);+ }+});