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: add HTTP recording fallback to Dictate (#11075)

Etherlc68b28aa7094
diff --git a/studio/frontend/src/components/assistant-ui/thread.tsx b/studio/frontend/src/components/assistant-ui/thread.tsx
index 87f2e786b38..fc1c8f5dc40 100644
--- a/studio/frontend/src/components/assistant-ui/thread.tsx
+++ b/studio/frontend/src/components/assistant-ui/thread.tsx
@@ -55,6 +55,7 @@ import { TerminalToolUI } from "@/components/assistant-ui/tool-ui-terminal";
import { WebSearchToolUI } from "@/components/assistant-ui/tool-ui-web-search";
import { ChatDictationBar } from "@/components/assistant-ui/chat-dictation-bar";
import {
+ ChatAudioUploadMount,
ChatSkillsDialog,
composerSubmitIntent,
composerFollowUpBehavior,
@@ -77,11 +78,13 @@ import {
pasteLongTextAsFile,
isPlainPasteChord,
plainPasteStillCounts,
+ currentDictationEntryMode,
isStudioDictationAvailable,
notifyStudioDictationUnavailable,
YoutubeTranscriptPrompt,
stripSearchImageTokens,
useChatActive,
+ useChatAudioUpload,
useInComparePane,
refreshSkillsCatalog,
} from "@/features/chat";
@@ -3551,6 +3554,31 @@ const Composer: FC<{
({ threadListItem }) => threadListItem.remoteId,
);
const referenceThreadId = threadId ?? activeThreadId ?? null;
+ // Not referenceThreadId: it moves null -> remote id on first persist of the same composer.
+ const composerIdentity = threadListItemId ?? "";
+ composerIdentityRef.current = composerIdentity;
+ const chatActive = useChatActive();
+ const readAudioUploadDraft = useCallback(
+ () => aui.composer().getState().text,
+ [aui],
+ );
+ const writeAudioUploadDraft = useCallback(
+ (value: string) => aui.composer().setText(value),
+ [aui],
+ );
+ const focusAudioUploadDraft = useCallback(() => {
+ inputRef.current?.focus({ preventScroll: true });
+ }, []);
+ const dictationEntryDisabled = !chatActive;
+ const audioUpload = useChatAudioUpload({
+ owner: composerIdentity,
+ chatId: referenceThreadId,
+ disabled: dictationEntryDisabled || isDictating,
+ readDraft: readAudioUploadDraft,
+ writeDraft: writeAudioUploadDraft,
+ focusDraft: focusAudioUploadDraft,
+ });
+ const cancelAudioUpload = audioUpload.cancel;
// Read at Send time, so a send that materializes after a project switch is still filed
// where it was made.
const projectScope = useChatProjectScope();
@@ -4241,6 +4269,7 @@ const Composer: FC<{
promptQueueStartPendingRef.current.get(reservationKey) ===
reservation
) {
+ cancelAudioUpload();
startPromptQueue(
items,
target,
@@ -4274,7 +4303,12 @@ const Composer: FC<{
});
return true;
},
- [createPromptQueueTarget, pendingQueueStartIsStale, referenceThreadId],
+ [
+ cancelAudioUpload,
+ createPromptQueueTarget,
+ pendingQueueStartIsStale,
+ referenceThreadId,
+ ],
);
// The queue carries text, and a long paste is text the composer parked in a
@@ -4592,6 +4626,8 @@ const Composer: FC<{
}
preStreamRunReservationRef.current = reservationToken;
try {
+ // Only after reservation succeeds: a refused send keeps the in-flight transcript.
+ cancelAudioUpload();
const sentText = aui.composer().getState().text;
// Stamp the send BEFORE send() starts awaiting every incomplete attachment: a document
// send reaches initialize() seconds later, by which time navigation may have moved the
@@ -4617,7 +4653,14 @@ const Composer: FC<{
error instanceof Error ? error.message : "Please retry the send.",
});
}
- }, [aui, armJustSent, preStreamThreadIds, projectScope, referenceThreadId]);
+ }, [
+ aui,
+ armJustSent,
+ cancelAudioUpload,
+ preStreamThreadIds,
+ projectScope,
+ referenceThreadId,
+ ]);
// Gate for both form submit and the Send button. Returns true when it handled
// the event (blocked or queued) so callers stop.
@@ -4788,12 +4831,6 @@ const Composer: FC<{
usePublishedFrame(composerEl);
const dictationBaseTextRef = useRef("");
const dictationComposerRef = useRef("");
- // Thread switches reuse this composer, so the send has to know where it
- // started to avoid submitting the destination thread's draft. The list item
- // id, not referenceThreadId: that one moves from null to the remote id when
- // a new chat first persists, which is the same composer.
- const composerIdentity = threadListItemId ?? "";
- composerIdentityRef.current = composerIdentity;
useEffect(() => {
setIsWritingExpanded(false);
}, [composerIdentity]);
@@ -4818,6 +4855,11 @@ const Composer: FC<{
// Keep the mic clickable: if the engine can't run here, explain and point to
// the local model instead of disabling the button.
const startDictation = useCallback(() => {
+ if (audioUpload.busy || dictationEntryDisabled) return;
+ if (currentDictationEntryMode() === "recording-file") {
+ audioUpload.openDialog();
+ return;
+ }
if (!isStudioDictationAvailable()) {
notifyStudioDictationUnavailable();
return;
@@ -4827,7 +4869,7 @@ const Composer: FC<{
} catch {
notifyStudioDictationUnavailable();
}
- }, [aui]);
+ }, [aui, audioUpload, dictationEntryDisabled]);
const sendAfterDictation = useCallback(() => {
sendAfterDictationRef.current = true;
dictationComposerRef.current = composerIdentity;
@@ -4853,7 +4895,6 @@ const Composer: FC<{
// Both chords live here, not with the controls below: the recording bar
// replaces those while dictation runs, so a chord registered there could
// start dictation and never stop it.
- const chatActive = useChatActive();
useShortcut(
"startDictation",
() => {
@@ -5349,6 +5390,7 @@ const Composer: FC<{
isComposing ||
hasPendingAttachments
}
+ dictationDisabled={dictationEntryDisabled}
// disableQueue (project new-chat composer) also blocks the queue
// button, so a running thread shows Stop instead of Queue.
queueDisabled={
@@ -5362,6 +5404,7 @@ const Composer: FC<{
onStopClick={stopQueue}
onResumeClick={resumeQueue}
onDictateClick={startDictation}
+ audioUpload={audioUpload}
pendingSend={pendingSend}
menuSide={effectiveMenuSide}
queueThreadIds={promptQueueThreadIds}
@@ -5373,6 +5416,7 @@ const Composer: FC<{
open={researchWebsiteAccessOpen && effectiveDeepResearchEnabled}
onOpenChange={setResearchWebsiteAccessOpen}
/>
+ <ChatAudioUploadMount audioUpload={audioUpload} />
</>
);
@@ -6883,23 +6927,27 @@ const PromptQueueStack: FC<{ queueThreadIds: string[] }> = ({
const ComposerRightControls: FC<{
disabled?: boolean;
+ dictationDisabled?: boolean;
queueDisabled?: boolean;
onQueueClick?: () => void;
onSendClick?: (event: { preventDefault: () => void }) => void;
onStopClick?: () => void;
onResumeClick?: () => void;
onDictateClick?: () => void;
+ audioUpload: ReturnType<typeof useChatAudioUpload>;
pendingSend?: boolean;
menuSide?: "top" | "bottom";
queueThreadIds: string[];
}> = ({
disabled,
+ dictationDisabled,
queueDisabled,
onQueueClick,
onSendClick,
onStopClick,
onResumeClick,
onDictateClick,
+ audioUpload,
pendingSend,
menuSide,
queueThreadIds,
@@ -6993,16 +7041,33 @@ const ComposerRightControls: FC<{
{/* Starts dictation; the recording bar then covers the input row and owns
the stop and send actions. */}
<ComposerPrimitive.If dictation={false}>
- <TooltipIconButton
- tooltip="Dictate"
- aria-label="Dictate"
- type="button"
- variant="ghost"
- className="size-9 rounded-full text-foreground"
- onClick={onDictateClick}
- >
- <MicIcon className="unsloth-dictate-icon size-6" />
- </TooltipIconButton>
+ {audioUpload.busy ? (
+ <Button
+ type="button"
+ variant="ghost"
+ size="sm"
+ className="h-9 gap-1.5 rounded-full px-2.5 text-muted-foreground"
+ aria-label={t("settings.voice.dictation.audioUploadCancel")}
+ title={t("settings.voice.dictation.audioUploadCancel")}
+ onClick={audioUpload.cancel}
+ >
+ <Spinner className="size-4" />
+ <span>{t("settings.voice.dictation.audioUploadTranscribing")}</span>
+ <XIcon className="size-3.5" aria-hidden="true" />
+ </Button>
+ ) : (
+ <TooltipIconButton
+ tooltip="Dictate"
+ aria-label="Dictate"
+ type="button"
+ variant="ghost"
+ className="size-9 rounded-full text-foreground"
+ disabled={dictationDisabled}
+ onClick={onDictateClick}
+ >
+ <MicIcon className="unsloth-dictate-icon size-6" />
+ </TooltipIconButton>
+ )}
</ComposerPrimitive.If>
<AuiIf
condition={({ thread }) =>
diff --git a/studio/frontend/src/features/chat/adapters/studio-dictation-adapter.tsx b/studio/frontend/src/features/chat/adapters/studio-dictation-adapter.tsx
index ee997b7f879..b3ca0f62f6a 100644
--- a/studio/frontend/src/features/chat/adapters/studio-dictation-adapter.tsx
+++ b/studio/frontend/src/features/chat/adapters/studio-dictation-adapter.tsx
@@ -9,6 +9,10 @@ import {
import { toast } from "@/lib/toast";
import type { DictationAdapter } from "@assistant-ui/react";
import { useExternalProvidersStore } from "../stores/external-providers-store";
+import {
+ currentDictationEntryMode,
+ insecureDictationGuidance,
+} from "../utils/dictation-entry";
import {
StudioModelDictationAdapter,
fetchSttStatus,
@@ -45,9 +49,9 @@ function customSttConfigured(): boolean {
const providerId = sttProviderId.trim();
return Boolean(
connectionsEnabled &&
- providerId &&
- sttProviderModel.trim() &&
- providers.some((provider) => provider.id === providerId),
+ providerId &&
+ sttProviderModel.trim() &&
+ providers.some((provider) => provider.id === providerId),
);
}
@@ -98,7 +102,9 @@ export class StudioDictationAdapter implements DictationAdapter {
);
}
if (StudioModelDictationAdapter.isSupported()) {
- return new StudioModelDictationAdapter({ chatId: this.chatId }).listen();
+ return new StudioModelDictationAdapter({
+ chatId: this.chatId,
+ }).listen();
}
throw new Error(
dictationEngine === "custom"
@@ -107,7 +113,9 @@ export class StudioDictationAdapter implements DictationAdapter {
);
}
if (StudioWebSpeechDictationAdapter.isSupported()) {
- return new StudioWebSpeechDictationAdapter({ chatId: this.chatId }).listen();
+ return new StudioWebSpeechDictationAdapter({
+ chatId: this.chatId,
+ }).listen();
}
throw new Error("Browser dictation is not supported in this browser.");
}
@@ -129,8 +137,7 @@ export function notifyStudioDictationUnavailable(
// Both engines need a secure context (localhost or HTTPS).
if (typeof window !== "undefined" && !window.isSecureContext) {
toast.error("Voice typing needs a secure connection.", {
- description:
- "Open Unsloth at http://127.0.0.1 (localhost) or over HTTPS to dictate.",
+ description: insecureDictationGuidance(currentDictationEntryMode()),
});
return;
}
diff --git a/studio/frontend/src/features/chat/adapters/studio-model-dictation-adapter.ts b/studio/frontend/src/features/chat/adapters/studio-model-dictation-adapter.ts
index 2e0841fe76f..bf4c7e95242 100644
--- a/studio/frontend/src/features/chat/adapters/studio-model-dictation-adapter.ts
+++ b/studio/frontend/src/features/chat/adapters/studio-model-dictation-adapter.ts
@@ -17,6 +17,7 @@ import {
} from "@/features/settings/stores/voice-settings-store";
import type { DictationAdapter } from "@assistant-ui/react";
import { toast } from "sonner";
+import { withAbort } from "../../hub/lib/abort-signals";
import { encryptProviderApiKey } from "../api/providers-api";
import { getExternalProviderApiKey } from "../external-providers";
import { useExternalProvidersStore } from "../stores/external-providers-store";
@@ -115,6 +116,7 @@ export async function transcribeAudioBlob(
model?: string;
language?: string;
engine?: SttEngine;
+ device?: SttDevice;
providerId?: string;
signal?: AbortSignal;
} = {},
@@ -180,7 +182,7 @@ export async function transcribeAudioBlob(
const engine = options.engine ?? sttEngineFor(model);
const params = new URLSearchParams({ model, fast: "true", engine });
if (language) params.set("language", language);
- params.set("device", settings.sttDevice);
+ params.set("device", options.device ?? settings.sttDevice);
const response = await authFetch(
`/api/inference/audio/transcribe/raw?${params.toString()}`,
{
@@ -256,14 +258,17 @@ function queueSttLifecycle(operation: () => Promise<void>): Promise<void> {
export async function fetchSttStatus(
refreshKey?: number,
model?: string,
+ signal?: AbortSignal,
): Promise<SttStatus> {
const params = new URLSearchParams();
if (refreshKey !== undefined) params.set("refresh", String(refreshKey));
if (model) params.set("model", model);
const query = params.toString();
- const response = await authFetch(
+ const request = authFetch(
`/api/inference/audio/stt/status${query ? `?${query}` : ""}`,
+ { signal },
);
+ const response = await withAbort(request, signal);
if (!response.ok) throw new Error(`HTTP ${response.status}`);
return (await response.json()) as SttStatus;
}
diff --git a/studio/frontend/src/features/chat/components/chat-audio-upload-mount.tsx b/studio/frontend/src/features/chat/components/chat-audio-upload-mount.tsx
new file mode 100644
index 00000000000..137e1d7ca5b
--- /dev/null
+++ b/studio/frontend/src/features/chat/components/chat-audio-upload-mount.tsx
@@ -0,0 +1,24 @@
+// 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 { lazy, Suspense } from "react";
+import type { useChatAudioUpload } from "../hooks/use-chat-audio-upload";
+
+const ChatAudioUpload = lazy(() =>
+ import("./chat-audio-upload").then((module) => ({
+ default: module.ChatAudioUpload,
+ })),
+);
+
+export function ChatAudioUploadMount({
+ audioUpload,
+}: {
+ audioUpload: ReturnType<typeof useChatAudioUpload>;
+}) {
+ if (!audioUpload.dialogOpen) return null;
+ return (
+ <Suspense fallback={null}>
+ <ChatAudioUpload audioUpload={audioUpload} />
+ </Suspense>
+ );
+}
diff --git a/studio/frontend/src/features/chat/components/chat-audio-upload.tsx b/studio/frontend/src/features/chat/components/chat-audio-upload.tsx
new file mode 100644
index 00000000000..5cd9b11d9e4
--- /dev/null
+++ b/studio/frontend/src/features/chat/components/chat-audio-upload.tsx
@@ -0,0 +1,232 @@
+// 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 { Button } from "@/components/ui/button";
+import {
+ Dialog,
+ DialogContent,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+} from "@/components/ui/dialog";
+import { Spinner } from "@/components/ui/spinner";
+import { sttModelName, useSettingsDialogStore } from "@/features/settings";
+import { useT } from "@/i18n";
+import { AUDIO_PICKER_ACCEPT } from "@/lib/audio-utils";
+import { useEffect, useMemo, useRef } from "react";
+import type { useChatAudioUpload } from "../hooks/use-chat-audio-upload";
+import { currentRecordingPickerPlatform } from "../utils/dictation-entry";
+
+export interface ChatAudioUploadProps {
+ audioUpload: ReturnType<typeof useChatAudioUpload>;
+}
+
+export function ChatAudioUpload({ audioUpload }: ChatAudioUploadProps) {
+ const t = useT();
+ const recordInputRef = useRef<HTMLInputElement>(null);
+ const chooseInputRef = useRef<HTMLInputElement>(null);
+ const platform = useMemo(() => currentRecordingPickerPlatform(), []);
+ const { dialogOpen, pickerCancelled } = audioUpload;
+ useEffect(() => {
+ if (!dialogOpen) return;
+ const cancelled = () => pickerCancelled();
+ const inputs = [recordInputRef.current, chooseInputRef.current];
+ for (const input of inputs) input?.addEventListener("cancel", cancelled);
+ return () => {
+ for (const input of inputs)
+ input?.removeEventListener("cancel", cancelled);
+ };
+ }, [dialogOpen, pickerCancelled]);
+ const languageName =
+ audioUpload.language === "auto"
+ ? t("settings.voice.dictation.audioUploadAutomatic")
+ : (new Intl.DisplayNames(undefined, { type: "language" }).of(
+ audioUpload.language,
+ ) ?? audioUpload.language);
+ const pickerDisabled =
+ audioUpload.readiness.state === "checking" ||
+ audioUpload.readiness.state === "downloading" ||
+ audioUpload.readiness.state === "unavailable";
+
+ const launch = (input: HTMLInputElement | null) => {
+ if (!input || !audioUpload.snapshotForPicker()) return;
+ input.click();
+ };
+
+ const acceptFile = (input: HTMLInputElement) => {
+ const file = input.files?.[0];
+ input.value = "";
+ if (file) audioUpload.selectFile(file);
+ };
+
+ const readinessText = (() => {
+ switch (audioUpload.readiness.state) {
+ case "ready":
+ return t("settings.voice.dictation.audioUploadModelReady");
+ case "missing":
+ return t("settings.voice.dictation.sttNotDownloaded");
+ case "downloading":
+ return audioUpload.readiness.progress === null
+ ? t("settings.voice.dictation.sttDownloadChecking")
+ : t("settings.voice.dictation.sttDownloading", {
+ progress: audioUpload.readiness.progress,
+ });
+ case "unavailable":
+ return t("settings.voice.dictation.sttUnavailable");
+ case "error":
+ return t("settings.voice.dictation.sttDownloadStatusFailed");
+ case "checking":
+ case "idle":
+ return t("settings.voice.dictation.sttChecking");
+ }
+ })();
+
+ return (
+ <Dialog
+ open={audioUpload.dialogOpen && !audioUpload.busy}
+ onOpenChange={(open) => {
+ if (!open) audioUpload.closeDialog();
+ }}
+ >
+ <DialogContent
+ showCloseButton={false}
+ className="max-sm:content-start sm:max-w-lg"
+ >
+ <DialogHeader>
+ <DialogTitle>
+ {t("settings.voice.dictation.audioUploadTitle")}
+ </DialogTitle>
+ <DialogDescription>
+ {t("settings.voice.dictation.audioUploadDescription")}
+ </DialogDescription>
+ </DialogHeader>
+
+ {platform === "ios" ? (
+ <p className="rounded-2xl bg-muted px-3 py-2 text-sm text-muted-foreground">
+ {t("settings.voice.dictation.audioUploadIphoneHint")}
+ </p>
+ ) : null}
+
+ <dl className="grid min-w-0 gap-3 rounded-2xl bg-muted/60 p-3 text-sm">
+ <div>
+ <dt className="text-muted-foreground">
+ {t("settings.voice.dictation.sttModelLabel")}
+ </dt>
+ <dd className="break-words font-medium">
+ <bdi>{sttModelName(audioUpload.model)}</bdi>
+ </dd>
+ </div>
+ <div>
+ <dt className="text-muted-foreground">
+ {t("settings.voice.dictation.languageLabel")}
+ </dt>
+ <dd className="break-words font-medium">
+ <bdi>{languageName}</bdi>
+ </dd>
+ </div>
+ <div className="flex items-center gap-2 text-muted-foreground">
+ {audioUpload.readiness.state === "checking" ||
+ audioUpload.readiness.state === "downloading" ? (
+ <Spinner className="size-3.5" />
+ ) : null}
+ <span>{readinessText}</span>
+ </div>
+ </dl>
+
+ <p className="text-sm text-muted-foreground">
+ {t("settings.voice.dictation.audioUploadServerModelNote")}
+ </p>
+
+ {audioUpload.failureMessage ? (
+ <div
+ className="rounded-2xl border border-destructive/30 bg-destructive/5 p-3 text-sm"
+ role="alert"
+ >
+ <p className="font-medium text-destructive">
+ {t("settings.voice.dictation.audioUploadRetryTitle", {
+ file: audioUpload.failedFileName ?? "",
+ })}
+ </p>
+ <p className="mt-1 break-words text-muted-foreground">
+ {audioUpload.failureMessage}
+ </p>
+ </div>
+ ) : null}
+
+ <input
+ ref={recordInputRef}
+ type="file"
+ accept="audio/*"
+ capture="user"
+ className="hidden"
+ onChange={(event) => acceptFile(event.currentTarget)}
+ />
+ <input
+ ref={chooseInputRef}
+ type="file"
+ accept={AUDIO_PICKER_ACCEPT}
+ className="hidden"
+ onChange={(event) => acceptFile(event.currentTarget)}
+ />
+
+ <Button
+ type="button"
+ variant="outline"
+ className="h-auto min-h-9 whitespace-normal py-2"
+ onClick={() => useSettingsDialogStore.getState().openDialog("voice")}
+ >
+ {t("settings.voice.dictation.sttOpenVoiceSettings")}
+ </Button>
+
+ <DialogFooter className="gap-2 sm:gap-2">
+ <Button
+ type="button"
+ variant="ghost"
+ onClick={audioUpload.closeDialog}
+ >
+ {t("common.cancel")}
+ </Button>
+ {audioUpload.readiness.state === "error" ? (
+ <Button
+ type="button"
+ variant="outline"
+ onClick={() => void audioUpload.refreshReadiness()}
+ >
+ {t("settings.voice.dictation.sttRetry")}
+ </Button>
+ ) : null}
+ {audioUpload.failureMessage ? (
+ <Button
+ type="button"
+ disabled={pickerDisabled}
+ onClick={audioUpload.retry}
+ >
+ {t("settings.voice.dictation.audioUploadRetry")}
+ </Button>
+ ) : (
+ <>
+ {platform === "android" ? (
+ <Button
+ type="button"
+ disabled={pickerDisabled}
+ onClick={() => launch(recordInputRef.current)}
+ >
+ {t("settings.voice.dictation.audioUploadRecord")}
+ </Button>
+ ) : null}
+ <Button
+ type="button"
+ variant={platform === "android" ? "outline" : "default"}
+ disabled={pickerDisabled}
+ onClick={() => launch(chooseInputRef.current)}
+ >
+ {t("settings.voice.dictation.audioUploadChooseFile")}
+ </Button>
+ </>
+ )}
+ </DialogFooter>
+ </DialogContent>
+ </Dialog>
+ );
+}
diff --git a/studio/frontend/src/features/chat/hooks/use-chat-audio-upload.ts b/studio/frontend/src/features/chat/hooks/use-chat-audio-upload.ts
new file mode 100644
index 00000000000..e95d151d9d9
--- /dev/null
+++ b/studio/frontend/src/features/chat/hooks/use-chat-audio-upload.ts
@@ -0,0 +1,498 @@
+// 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 {
+ AUTH_SESSION_CLEARED_EVENT,
+ AUTH_SESSION_STORED_EVENT,
+ getAuthSessionEpoch,
+} from "@/features/auth";
+import {
+ applyDictationDictionary,
+ recordRecentDictation,
+ requestSttDownload,
+ useSettingsDialogStore,
+ useVoiceSettingsStore,
+} from "@/features/settings";
+import { useT } from "@/i18n";
+import { accountTransitionPending } from "@/lib/account-transition";
+import { toast } from "@/lib/toast";
+import { classifiedAttachmentFile, isVideoFile } from "@/lib/video-utils";
+import {
+ useCallback,
+ useEffect,
+ useLayoutEffect,
+ useRef,
+ useState,
+} from "react";
+import {
+ type SttEngine,
+ SttModelNotDownloadedError,
+ fetchSttStatus,
+ sttEngineFor,
+ sttEngineStatusFor,
+ transcribeAudioBlob,
+} from "../adapters/studio-model-dictation-adapter";
+import { resolveDictationChatId } from "../adapters/studio-web-speech-dictation-adapter";
+import {
+ ChatAudioReadinessRefreshQueue,
+ type ChatAudioUploadFence,
+ MAX_AUDIO_SIZE_LABEL,
+ appendChatAudioTranscript,
+ chatAudioUploadFenceMatches,
+ chatAudioUploadFileError,
+ completeChatAudioUpload,
+} from "../utils/chat-audio-upload";
+
+export interface UseChatAudioUploadOptions {
+ owner: string;
+ chatId?: string | null;
+ disabled?: boolean;
+ readDraft: () => string;
+ writeDraft: (value: string) => void;
+ focusDraft?: () => void;
+}
+
+export type ChatAudioUploadReadiness =
+ | { state: "idle" | "checking" | "error" | "unavailable"; model: string }
+ | { state: "ready" | "missing"; model: string }
+ | { state: "downloading"; model: string; progress: number | null };
+
+interface ChatAudioUploadSnapshot extends ChatAudioUploadFence {
+ model: string;
+ engine: SttEngine;
+ language: string;
+ device: "auto" | "cpu";
+ chatId: string | undefined;
+}
+
+interface RetainedRecording {
+ file: File;
+ snapshot: ChatAudioUploadSnapshot;
+ error: string;
+}
+
+function readinessProgress(
+ done: number | null,
+ total: number | null,
+): number | null {
+ if (done === null || total === null || total <= 0) return null;
+ return Math.max(0, Math.min(100, Math.round((done / total) * 100)));
+}
+
+export function useChatAudioUpload({
+ owner,
+ chatId,
+ disabled = false,
+ readDraft,
+ writeDraft,
+ focusDraft,
+}: UseChatAudioUploadOptions) {
+ const t = useT();
+ const model = useVoiceSettingsStore((state) => state.sttModel);
+ const language = useVoiceSettingsStore((state) => state.dictationLanguage);
+ const device = useVoiceSettingsStore((state) => state.sttDevice);
+ const [dialogOpen, setDialogOpen] = useState(false);
+ const [busy, setBusy] = useState(false);
+ const [retained, setRetained] = useState<RetainedRecording | null>(null);
+ const [readiness, setReadiness] = useState<ChatAudioUploadReadiness>({
+ state: "idle",
+ model,
+ });
+ const generationRef = useRef(0);
+ const readinessRefreshQueueRef = useRef(new ChatAudioReadinessRefreshQueue());
+ const ownerRef = useRef(owner);
+ const controllerRef = useRef<AbortController | null>(null);
+ const activeFenceRef = useRef<ChatAudioUploadFence | null>(null);
+ const pickerSnapshotRef = useRef<ChatAudioUploadSnapshot | null>(null);
+ const retainedRef = useRef<RetainedRecording | null>(null);
+ const readDraftRef = useRef(readDraft);
+ const writeDraftRef = useRef(writeDraft);
+ const focusDraftRef = useRef(focusDraft);
+
+ const invalidateRefs = useCallback(() => {
+ generationRef.current += 1;
+ readinessRefreshQueueRef.current.invalidate();
+ pickerSnapshotRef.current = null;
+ activeFenceRef.current = null;
+ controllerRef.current?.abort();
+ controllerRef.current = null;
+ }, []);
+
+ const clearOperation = useCallback(() => {
+ invalidateRefs();
+ retainedRef.current = null;
+ setRetained(null);
+ setDialogOpen(false);
+ setBusy(false);
+ }, [invalidateRefs]);
+
+ useLayoutEffect(() => {
+ const ownerChanged = ownerRef.current !== owner;
+ ownerRef.current = owner;
+ readDraftRef.current = readDraft;
+ writeDraftRef.current = writeDraft;
+ focusDraftRef.current = focusDraft;
+ if (ownerChanged) clearOperation();
+ }, [clearOperation, focusDraft, owner, readDraft, writeDraft]);
+
+ useLayoutEffect(() => {
+ if (!disabled) return;
+ invalidateRefs();
+ const invalidatedGeneration = generationRef.current;
+ queueMicrotask(() => {
+ if (generationRef.current !== invalidatedGeneration) return;
+ retainedRef.current = null;
+ setRetained(null);
+ setDialogOpen(false);
+ setBusy(false);
+ });
+ }, [disabled, invalidateRefs]);
+
+ useEffect(() => {
+ if (typeof window === "undefined") return;
+ window.addEventListener(AUTH_SESSION_CLEARED_EVENT, clearOperation);
+ window.addEventListener(AUTH_SESSION_STORED_EVENT, clearOperation);
+ return () => {
+ window.removeEventListener(AUTH_SESSION_CLEARED_EVENT, clearOperation);
+ window.removeEventListener(AUTH_SESSION_STORED_EVENT, clearOperation);
+ };
+ }, [clearOperation]);
+
+ useEffect(() => clearOperation, [clearOperation]);
+
+ const targetSettings = useCallback(() => {
+ const retry = retainedRef.current;
+ return retry
+ ? {
+ model: retry.snapshot.model,
+ language: retry.snapshot.language,
+ device: retry.snapshot.device,
+ engine: retry.snapshot.engine,
+ }
+ : {
+ model: model.trim(),
+ language,
+ device,
+ engine: sttEngineFor(model.trim()),
+ };
+ }, [device, language, model]);
+
+ const refreshReadiness = useCallback(
+ async (silent = false) => {
+ const queue = readinessRefreshQueueRef.current;
+ const target = targetSettings();
+ const targetModel = target.model;
+ if (!silent) {
+ setReadiness({
+ state: targetModel ? "checking" : "error",
+ model: targetModel,
+ });
+ }
+ await queue.run(silent, async (attempt, signal) => {
+ const ownerAtStart = ownerRef.current;
+ const authAtStart = getAuthSessionEpoch();
+ if (!targetModel) {
+ setReadiness({ state: "error", model: targetModel });
+ return;
+ }
+ try {
+ const status = await fetchSttStatus(undefined, targetModel, signal);
+ if (
+ !queue.isCurrent(attempt) ||
+ ownerRef.current !== ownerAtStart ||
+ getAuthSessionEpoch() !== authAtStart ||
+ accountTransitionPending()
+ ) {
+ return;
+ }
+ const engineStatus = sttEngineStatusFor(
+ status,
+ targetModel,
+ target.engine,
+ );
+ if (!engineStatus?.available) {
+ setReadiness({ state: "unavailable", model: targetModel });
+ return;
+ }
+ if (
+ engineStatus.download.downloading &&
+ engineStatus.download.model === targetModel
+ ) {
+ setReadiness({
+ state: "downloading",
+ model: targetModel,
+ progress: readinessProgress(
+ engineStatus.download.bytes_done,
+ engineStatus.download.bytes_total,
+ ),
+ });
+ return;
+ }
+ setReadiness({
+ state: engineStatus.downloaded_models.includes(targetModel)
+ ? "ready"
+ : "missing",
+ model: targetModel,
+ });
+ } catch {
+ if (
+ queue.isCurrent(attempt) &&
+ ownerRef.current === ownerAtStart &&
+ getAuthSessionEpoch() === authAtStart
+ ) {
+ setReadiness({ state: "error", model: targetModel });
+ }
+ }
+ });
+ },
+ [targetSettings],
+ );
+
+ useEffect(() => {
+ if (!dialogOpen || disabled) return;
+ queueMicrotask(() => void refreshReadiness());
+ }, [dialogOpen, disabled, refreshReadiness]);
+
+ useEffect(() => {
+ if (
+ !dialogOpen ||
+ (readiness.state !== "missing" && readiness.state !== "downloading")
+ ) {
+ return;
+ }
+ const timer = window.setInterval(() => void refreshReadiness(true), 1500);
+ return () => window.clearInterval(timer);
+ }, [dialogOpen, readiness.state, refreshReadiness]);
+
+ const openDialog = useCallback(() => {
+ if (disabled || busy) return;
+ setDialogOpen(true);
+ }, [busy, disabled]);
+
+ const closeDialog = useCallback(() => clearOperation(), [clearOperation]);
+
+ const snapshotForPicker = useCallback((): ChatAudioUploadSnapshot | null => {
+ if (disabled || busy || accountTransitionPending()) return null;
+ const target = targetSettings();
+ if (!target.model) {
+ toast.error(t("settings.voice.dictation.audioUploadChooseModel"), {
+ action: {
+ label: t("settings.voice.dictation.sttOpenVoiceSettings"),
+ onClick: () => useSettingsDialogStore.getState().openDialog("voice"),
+ },
+ });
+ return null;
+ }
+ if (readiness.model !== target.model || readiness.state !== "ready") {
+ if (readiness.model === target.model && readiness.state === "missing") {
+ requestSttDownload(target.model);
+ } else if (readiness.state === "error") {
+ void refreshReadiness();
+ }
+ return null;
+ }
+ const generation = generationRef.current + 1;
+ generationRef.current = generation;
+ const snapshot: ChatAudioUploadSnapshot = {
+ generation,
+ owner: ownerRef.current,
+ authSessionEpoch: getAuthSessionEpoch(),
+ model: target.model,
+ engine: target.engine,
+ language: target.language,
+ device: target.device,
+ chatId: resolveDictationChatId(chatId),
+ };
+ pickerSnapshotRef.current = snapshot;
+ return snapshot;
+ }, [busy, chatId, disabled, readiness, refreshReadiness, t, targetSettings]);
+
+ const pickerCancelled = useCallback(() => {
+ pickerSnapshotRef.current = null;
+ }, []);
+
+ const runTranscription = useCallback(
+ async (file: File, source: ChatAudioUploadSnapshot) => {
+ if (disabled || controllerRef.current || accountTransitionPending())
+ return;
+ if (
+ !chatAudioUploadFenceMatches(source, {
+ generation: generationRef.current,
+ owner: ownerRef.current,
+ authSessionEpoch: getAuthSessionEpoch(),
+ })
+ ) {
+ clearOperation();
+ return;
+ }
+ const controller = new AbortController();
+ controllerRef.current = controller;
+ activeFenceRef.current = source;
+ setDialogOpen(false);
+ setBusy(true);
+ let failure: string | null = null;
+ try {
+ const completion = await completeChatAudioUpload({
+ started: source,
+ current: () => ({
+ generation: generationRef.current,
+ owner: ownerRef.current,
+ authSessionEpoch: getAuthSessionEpoch(),
+ }),
+ signal: controller.signal,
+ blocked: accountTransitionPending,
+ transcribe: () =>
+ transcribeAudioBlob(file, {
+ model: source.model,
+ engine: source.engine,
+ language: source.language,
+ device: source.device,
+ signal: controller.signal,
+ }),
+ commit: (transcript) => {
+ const corrected = applyDictationDictionary(transcript).trim();
+ if (!corrected) return;
+ writeDraftRef.current(
+ appendChatAudioTranscript(readDraftRef.current(), corrected),
+ );
+ recordRecentDictation(corrected, source.chatId);
+ },
+ });
+ if (completion === "committed") {
+ retainedRef.current = null;
+ setRetained(null);
+ requestAnimationFrame(() => focusDraftRef.current?.());
+ } else if (completion === "empty") {
+ failure = t("settings.voice.dictation.audioUploadNoSpeech");
+ } else {
+ clearOperation();
+ }
+ } catch (error) {
+ if (controller.signal.aborted) return;
+ if (error instanceof SttModelNotDownloadedError) {
+ setReadiness({ state: "missing", model: source.model });
+ requestSttDownload(source.model);
+ }
+ failure =
+ error instanceof Error && error.message
+ ? error.message
+ : t("settings.voice.dictation.audioUploadFailed");
+ } finally {
+ if (activeFenceRef.current === source) {
+ activeFenceRef.current = null;
+ controllerRef.current = null;
+ setBusy(false);
+ if (failure) {
+ const next = { file, snapshot: source, error: failure };
+ retainedRef.current = next;
+ setRetained(next);
+ setDialogOpen(true);
+ }
+ }
+ }
+ },
+ [clearOperation, disabled, t],
+ );
+
+ const selectFile = useCallback(
+ async (file: File) => {
+ const snapshot = pickerSnapshotRef.current;
+ pickerSnapshotRef.current = null;
+ if (!snapshot) return;
+ const validationError = chatAudioUploadFileError(file);
+ if (validationError) {
+ toast.error(
+ validationError === "empty"
+ ? t("settings.voice.dictation.audioUploadEmpty")
+ : t("settings.voice.dictation.audioUploadTooLarge", {
+ size: MAX_AUDIO_SIZE_LABEL,
+ }),
+ );
+ return;
+ }
+ const classifiedFile = await classifiedAttachmentFile(file);
+ if (
+ !chatAudioUploadFenceMatches(snapshot, {
+ generation: generationRef.current,
+ owner: ownerRef.current,
+ authSessionEpoch: getAuthSessionEpoch(),
+ }) ||
+ accountTransitionPending()
+ ) {
+ return;
+ }
+ if (isVideoFile(classifiedFile)) {
+ toast.error(t("settings.voice.dictation.audioUploadVideoUnsupported"));
+ return;
+ }
+ retainedRef.current = null;
+ setRetained(null);
+ void runTranscription(classifiedFile, snapshot);
+ },
+ [runTranscription, t],
+ );
+
+ const retry = useCallback(() => {
+ const failed = retainedRef.current;
+ if (!failed || disabled || busy || accountTransitionPending()) return;
+ if (
+ failed.snapshot.owner !== ownerRef.current ||
+ failed.snapshot.authSessionEpoch !== getAuthSessionEpoch()
+ ) {
+ clearOperation();
+ return;
+ }
+ if (
+ readiness.model !== failed.snapshot.model ||
+ readiness.state !== "ready"
+ ) {
+ if (
+ readiness.model === failed.snapshot.model &&
+ readiness.state === "missing"
+ ) {
+ requestSttDownload(failed.snapshot.model);
+ } else {
+ void refreshReadiness();
+ }
+ return;
+ }
+ const generation = generationRef.current + 1;
+ generationRef.current = generation;
+ const snapshot = { ...failed.snapshot, generation };
+ retainedRef.current = null;
+ setRetained(null);
+ void runTranscription(failed.file, snapshot);
+ }, [
+ busy,
+ clearOperation,
+ disabled,
+ readiness,
+ refreshReadiness,
+ runTranscription,
+ ]);
+
+ const display = retained?.snapshot ?? {
+ model: model.trim(),
+ language,
+ device,
+ };
+
+ return {
+ model: display.model,
+ language: display.language,
+ device: display.device,
+ busy,
+ dialogOpen,
+ readiness,
+ failedFileName: retained?.file.name ?? null,
+ failureMessage: retained?.error ?? null,
+ openDialog,
+ closeDialog,
+ snapshotForPicker,
+ pickerCancelled,
+ selectFile,
+ retry,
+ refreshReadiness,
+ cancel: clearOperation,
+ };
+}
diff --git a/studio/frontend/src/features/chat/index.ts b/studio/frontend/src/features/chat/index.ts
index beb4b662689..b3cc4dd3f47 100644
--- a/studio/frontend/src/features/chat/index.ts
+++ b/studio/frontend/src/features/chat/index.ts
@@ -343,10 +343,7 @@ export {
removeChatThreadTombstones,
} from "./utils/chat-thread-tombstones";
export { emitChatAttachmentDeleted } from "./utils/chat-attachment-events";
-export {
- forkCountFor,
- subscribeForkCounts,
-} from "./utils/fork-count-store";
+export { forkCountFor, subscribeForkCounts } from "./utils/fork-count-store";
export { resolveReasoningGroupDuration } from "./utils/reasoning-duration";
export {
reasoningFollowsPreference,
@@ -464,6 +461,9 @@ export {
releaseTtsAudioUrl,
} from "./adapters/studio-speech-synthesis-adapter";
export { ChatSkillsDialog } from "./components/chat-skills-dialog";
+export { ChatAudioUploadMount } from "./components/chat-audio-upload-mount";
+export { useChatAudioUpload } from "./hooks/use-chat-audio-upload";
+export { currentDictationEntryMode } from "./utils/dictation-entry";
export {
SKILL_MENTION_PATTERN,
listSkills,
diff --git a/studio/frontend/src/features/chat/shared-composer.tsx b/studio/frontend/src/features/chat/shared-composer.tsx
index fea9295f214..c9d2b8de303 100644
--- a/studio/frontend/src/features/chat/shared-composer.tsx
+++ b/studio/frontend/src/features/chat/shared-composer.tsx
@@ -19,8 +19,8 @@ import {
composerSubmitIntent,
composerShortcutLabels,
} from "./utils/composer-preferences";
-import { useT } from "@/i18n";
import { Button } from "@/components/ui/button";
+import { Spinner } from "@/components/ui/spinner";
import { BulbIcon } from "@/lib/bulb-icon";
import { MicIcon } from "@/lib/mic-icon";
import { Tick02Icon } from "@/lib/tick-icon";
@@ -121,7 +121,10 @@ import { reasoningCapsFromLoad } from "./lib/apply-inference-status-to-store";
import { KnowledgeBaseComposerButton } from "@/features/rag/components/knowledge-base-composer-button";
import { NewProjectDialog } from "./components/new-project-dialog";
import { ChatSkillsDialog } from "./components/chat-skills-dialog";
+import { ChatAudioUploadMount } from "./components/chat-audio-upload-mount";
import { useChatProjects } from "./hooks/use-chat-projects";
+import { useChatAudioUpload } from "./hooks/use-chat-audio-upload";
+import { currentDictationEntryMode } from "./utils/dictation-entry";
import { confirmRemoteCodeIfNeeded } from "@/features/security";
import {
DEFAULT_MAX_SEQ_LENGTH,
@@ -165,6 +168,7 @@ import { compareModelDisplayName } from "./lib/external-model-label";
import { useExternalProvidersStore } from "./stores/external-providers-store";
import { useComposerPillFit } from "@/hooks/use-composer-pill-fit";
import { useIsMobile } from "@/hooks/use-mobile";
+import { useT } from "@/i18n";
import {
PLUS_MENU_ORDER,
type PlusMenuItemId,
@@ -209,6 +213,7 @@ import {
useCallback,
useContext,
useEffect,
+ useId,
useRef,
useState,
useSyncExternalStore,
@@ -595,6 +600,15 @@ export function SharedComposer({
const textRef = useRef(text);
const pendingImagesRef = useRef(pendingImages);
const pendingAudioRef = useRef(pendingAudio);
+ const setCurrentText = useCallback(
+ (value: string | ((previous: string) => string)) => {
+ const next =
+ typeof value === "function" ? value(textRef.current) : value;
+ textRef.current = next;
+ setText(next);
+ },
+ [],
+ );
useEffect(() => {
textRef.current = text;
pendingImagesRef.current = pendingImages;
@@ -676,7 +690,7 @@ export function SharedComposer({
const skillMentions = useTextareaSkillMentions({
text,
- setText,
+ setText: setCurrentText,
inputRef: textareaRef,
composingRef,
enabled: supportsTools,
@@ -907,9 +921,36 @@ export function SharedComposer({
const {
isDictating,
isFinalizing: isDictationFinalizing,
- start: startDictation,
+ start: startDictationSession,
stop: stopDictation,
- } = useDictation(setText);
+ } = useDictation(setCurrentText);
+ const chatActive = useChatActive();
+ const compareUploadInstanceId = useId();
+ const audioUploadOwner = `${compareUploadInstanceId}:${model1?.id ?? ""}:${model2?.id ?? ""}`;
+ const readAudioUploadDraft = useCallback(() => textRef.current, []);
+ const writeAudioUploadDraft = useCallback(
+ (value: string) => setCurrentText(value),
+ [setCurrentText],
+ );
+ const focusAudioUploadDraft = useCallback(() => {
+ textareaRef.current?.focus({ preventScroll: true });
+ }, []);
+ const audioUpload = useChatAudioUpload({
+ owner: audioUploadOwner,
+ chatId: null,
+ disabled: isDictating || !chatActive,
+ readDraft: readAudioUploadDraft,
+ writeDraft: writeAudioUploadDraft,
+ focusDraft: focusAudioUploadDraft,
+ });
+ const startDictation = useCallback(() => {
+ if (audioUpload.busy || !chatActive) return;
+ if (currentDictationEntryMode() === "recording-file") {
+ audioUpload.openDialog();
+ return;
+ }
+ startDictationSession();
+ }, [audioUpload, chatActive, startDictationSession]);
useEffect(() => {
const id = setInterval(() => {
@@ -944,7 +985,7 @@ export function SharedComposer({
toast(`Prompt ${nextIndex + 1} / ${queueRef.current.length}`, {
description: next.length > 80 ? next.slice(0, 80) + "…" : next,
});
- setText(next);
+ setCurrentText(next);
setTimeout(() => { sendRef.current?.(); }, 100);
}
@@ -1229,7 +1270,7 @@ export function SharedComposer({
});
};
const clearSubmittedDraft = () => {
- setText("");
+ setCurrentText("");
setPendingImages([]);
setPendingAudio(null);
clearPendingAudioStore();
@@ -1303,6 +1344,7 @@ export function SharedComposer({
keepChangedDraft();
return;
}
+ audioUpload.cancel();
clearSubmittedDraft();
// Set when an accepted transformers install unloaded the active model server-side; a later
// failure must then clear the stale checkpoint.
@@ -1942,6 +1984,7 @@ export function SharedComposer({
reservations.push(token);
}
}
+ audioUpload.cancel();
clearSubmittedDraft();
for (const handle of handles) {
handle.append(content);
@@ -2000,7 +2043,6 @@ export function SharedComposer({
// Compare mode swaps this composer in for the single-chat one and only one is ever on screen, so the
// chords register in both. Both gate on the chat tab being visible: off-route the pane is hidden.
- const chatActive = useChatActive();
useShortcut(
"startDictation",
() => {
@@ -2107,7 +2149,7 @@ export function SharedComposer({
<DropdownMenuItem
key={p.id}
onSelect={() => {
- setText(p.text);
+ setCurrentText(p.text);
requestAnimationFrame(() => textareaRef.current?.focus());
}}
>
@@ -2244,12 +2286,13 @@ export function SharedComposer({
}}
>
<ChatSkillsDialog open={skillsOpen} onOpenChange={setSkillsOpen} />
+ <ChatAudioUploadMount audioUpload={audioUpload} />
<PromptStorageDialog
open={promptStorageOpen}
onOpenChange={setPromptStorageOpen}
onUse={(t) => {
- setText(t);
+ setCurrentText(t);
requestAnimationFrame(() => textareaRef.current?.focus());
}}
onRunList={(items) => {
@@ -2276,7 +2319,7 @@ export function SharedComposer({
toast(`Prompt 1 / ${filtered.length}`, {
description: filtered[0].length > 80 ? filtered[0].slice(0, 80) + "…" : filtered[0],
});
- setText(filtered[0]);
+ setCurrentText(filtered[0]);
setTimeout(() => { sendRef.current?.(); }, 100);
}}
/>
@@ -2341,7 +2384,7 @@ export function SharedComposer({
// must match the DOM at all times, else an unrelated parent re-render reconciles the textarea back
// to the stored value mid-composition, wiping the preedit (#5318).
setCompositionState(isNativeComposing(e.nativeEvent));
- setText(e.target.value);
+ setCurrentText(e.target.value);
skillMentions.update(
e.target.value,
e.target.selectionStart ?? e.target.value.length,
@@ -2363,7 +2406,7 @@ export function SharedComposer({
}}
onCompositionEnd={(e: CompositionEvent<HTMLTextAreaElement>) => {
setCompositionState(false);
- setText(e.currentTarget.value);
+ setCurrentText(e.currentTarget.value);
}}
onKeyDown={(event) => {
if (!skillMentions.onKeyDown(event)) onKeyDown(event);
@@ -2880,17 +2923,34 @@ export function SharedComposer({
{
<>
{!isDictating ? (
- <TooltipIconButton
- tooltip="Dictate"
- side="bottom"
- variant="ghost"
- size="icon"
- className="size-8 rounded-full text-muted-foreground"
- onClick={startDictation}
- aria-label="Dictate"
- >
- <MicIcon className="unsloth-dictate-icon size-4" />
- </TooltipIconButton>
+ audioUpload.busy ? (
+ <Button
+ type="button"
+ variant="ghost"
+ size="sm"
+ className="h-8 gap-1.5 rounded-full px-2 text-muted-foreground"
+ aria-label={t("settings.voice.dictation.audioUploadCancel")}
+ title={t("settings.voice.dictation.audioUploadCancel")}
+ onClick={audioUpload.cancel}
+ >
+ <Spinner className="size-3.5" />
+ <span>{t("settings.voice.dictation.audioUploadTranscribing")}</span>
+ <XIcon className="size-3" aria-hidden="true" />
+ </Button>
+ ) : (
+ <TooltipIconButton
+ tooltip="Dictate"
+ side="bottom"
+ variant="ghost"
+ size="icon"
+ className="size-8 rounded-full text-muted-foreground"
+ disabled={!chatActive}
+ onClick={startDictation}
+ aria-label="Dictate"
+ >
+ <MicIcon className="unsloth-dictate-icon size-4" />
+ </TooltipIconButton>
+ )
) : (
<TooltipIconButton
tooltip={
diff --git a/studio/frontend/src/features/chat/utils/chat-audio-upload.ts b/studio/frontend/src/features/chat/utils/chat-audio-upload.ts
new file mode 100644
index 00000000000..21b0cadc414
--- /dev/null
+++ b/studio/frontend/src/features/chat/utils/chat-audio-upload.ts
@@ -0,0 +1,144 @@
+// 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 {
+ MAX_AUDIO_SIZE_LABEL,
+ getAudioSizeError,
+} from "../../../lib/audio-utils.ts";
+import { pollSignal } from "../../hub/lib/abort-signals.ts";
+
+const CHAT_AUDIO_READINESS_TIMEOUT_MS = 10_000;
+
+export interface ChatAudioUploadFence {
+ generation: number;
+ owner: string;
+ authSessionEpoch: number;
+}
+
+export type ChatAudioReadinessRefreshResult =
+ | "ran"
+ | "skipped"
+ | "superseded";
+
+export class ChatAudioReadinessRefreshQueue {
+ private tail: Promise<void> | null = null;
+ private generation = 0;
+ private activeController: AbortController | null = null;
+ private readonly timeoutMs: number;
+
+ constructor(timeoutMs: number = CHAT_AUDIO_READINESS_TIMEOUT_MS) {
+ this.timeoutMs = timeoutMs;
+ }
+
+ invalidate(): void {
+ this.generation += 1;
+ this.activeController?.abort();
+ }
+
+ isCurrent(generation: number): boolean {
+ return this.generation === generation;
+ }
+
+ async run(
+ silent: boolean,
+ refresh: (generation: number, signal: AbortSignal) => Promise<void>,
+ ): Promise<ChatAudioReadinessRefreshResult> {
+ const preceding = this.tail;
+ if (silent && preceding) return "skipped";
+
+ const generation = silent ? null : this.generation + 1;
+ if (generation !== null) {
+ this.generation = generation;
+ this.activeController?.abort();
+ }
+
+ let release = () => {};
+ const turn = new Promise<void>((resolve) => {
+ release = resolve;
+ });
+ this.tail = turn;
+
+ if (preceding) await preceding;
+ try {
+ const activeGeneration = generation ?? this.generation + 1;
+ if (generation === null) this.generation = activeGeneration;
+ if (!this.isCurrent(activeGeneration)) return "superseded";
+ const controller = new AbortController();
+ const request = pollSignal(controller.signal, this.timeoutMs);
+ this.activeController = controller;
+ try {
+ await refresh(activeGeneration, request.signal);
+ return "ran";
+ } finally {
+ request.dispose();
+ if (this.activeController === controller) {
+ this.activeController = null;
+ }
+ }
+ } finally {
+ release();
+ if (this.tail === turn) this.tail = null;
+ }
+ }
+}
+
+export type ChatAudioUploadFileError = "empty" | "too-large";
+
+export function chatAudioUploadFileError(
+ file: Pick<File, "size">,
+): ChatAudioUploadFileError | null {
+ if (file.size === 0) return "empty";
+ return getAudioSizeError(file.size) ? "too-large" : null;
+}
+
+export { MAX_AUDIO_SIZE_LABEL };
+
+export function chatAudioUploadFenceMatches(
+ started: ChatAudioUploadFence,
+ current: ChatAudioUploadFence,
+): boolean {
+ return (
+ started.generation === current.generation &&
+ started.owner === current.owner &&
+ started.authSessionEpoch === current.authSessionEpoch
+ );
+}
+
+export function appendChatAudioTranscript(
+ draft: string,
+ transcript: string,
+): string {
+ const finalTranscript = transcript.trim();
+ if (!finalTranscript) return draft;
+ return draft ? `${draft.trimEnd()} ${finalTranscript}` : finalTranscript;
+}
+
+export type ChatAudioUploadCompletion = "committed" | "empty" | "stale";
+
+export async function completeChatAudioUpload({
+ started,
+ current,
+ signal,
+ blocked,
+ transcribe,
+ commit,
+}: {
+ started: ChatAudioUploadFence;
+ current: () => ChatAudioUploadFence;
+ signal: AbortSignal;
+ blocked: () => boolean;
+ transcribe: () => Promise<string>;
+ commit: (transcript: string) => void;
+}): Promise<ChatAudioUploadCompletion> {
+ const transcript = (await transcribe()).trim();
+ if (
+ signal.aborted ||
+ blocked() ||
+ !chatAudioUploadFenceMatches(started, current())
+ ) {
+ return "stale";
+ }
+ if (!transcript) return "empty";
+ commit(transcript);
+ return "committed";
+}
diff --git a/studio/frontend/src/features/chat/utils/dictation-entry.ts b/studio/frontend/src/features/chat/utils/dictation-entry.ts
new file mode 100644
index 00000000000..259c806c5c7
--- /dev/null
+++ b/studio/frontend/src/features/chat/utils/dictation-entry.ts
@@ -0,0 +1,79 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+export type DictationEntryMode = "live" | "recording-file";
+
+function isLoopbackHostname(hostname: string): boolean {
+ const normalized = hostname
+ .trim()
+ .toLowerCase()
+ .replace(/^\[|\]$/g, "");
+ return (
+ normalized === "localhost" ||
+ normalized.endsWith(".localhost") ||
+ normalized === "127.0.0.1" ||
+ normalized === "::1"
+ );
+}
+
+/** Only insecure remote HTTP (no page mic) falls back; else live path keeps its errors visible. */
+export function dictationEntryMode({
+ isSecureContext,
+ protocol,
+ hostname,
+}: {
+ isSecureContext: boolean;
+ protocol: string;
+ hostname: string;
+}): DictationEntryMode {
+ if (isSecureContext || protocol !== "http:" || isLoopbackHostname(hostname)) {
+ return "live";
+ }
+ return "recording-file";
+}
+
+export function currentDictationEntryMode(): DictationEntryMode {
+ if (typeof window === "undefined") return "live";
+ return dictationEntryMode({
+ isSecureContext: window.isSecureContext,
+ protocol: window.location.protocol,
+ hostname: window.location.hostname,
+ });
+}
+
+export function insecureDictationGuidance(mode: DictationEntryMode): string {
+ return mode === "recording-file"
+ ? "Press Dictate to choose a recording on this connection, or open this Studio server through HTTPS to dictate live."
+ : "Open Unsloth at http://127.0.0.1 (localhost) or over HTTPS to dictate.";
+}
+
+export type RecordingPickerPlatform = "android" | "ios" | "other";
+
+/** Presentation only; touch check because iPadOS can identify as Macintosh. */
+export function recordingPickerPlatform({
+ userAgent,
+ platform = "",
+ maxTouchPoints = 0,
+}: {
+ userAgent: string;
+ platform?: string;
+ maxTouchPoints?: number;
+}): RecordingPickerPlatform {
+ if (/android/i.test(userAgent)) return "android";
+ if (
+ /iphone|ipad|ipod/i.test(userAgent) ||
+ (/^mac/i.test(platform) && maxTouchPoints > 1)
+ ) {
+ return "ios";
+ }
+ return "other";
+}
+
+export function currentRecordingPickerPlatform(): RecordingPickerPlatform {
+ if (typeof navigator === "undefined") return "other";
+ return recordingPickerPlatform({
+ userAgent: navigator.userAgent,
+ platform: navigator.platform,
+ maxTouchPoints: navigator.maxTouchPoints,
+ });
+}
diff --git a/studio/frontend/src/features/settings/index.ts b/studio/frontend/src/features/settings/index.ts
index ca1b14d0562..413945499d5 100644
--- a/studio/frontend/src/features/settings/index.ts
+++ b/studio/frontend/src/features/settings/index.ts
@@ -95,7 +95,13 @@ export {
export type { ShortcutId } from "./lib/keyboard-shortcuts";
export { useSettingsDialogStore } from "./stores/settings-dialog-store";
export type { SettingsTab } from "./stores/settings-dialog-store";
+export { requestSttDownload } from "./stores/stt-download-prompt-store";
+export {
+ applyDictationDictionary,
+ recordRecentDictation,
+ sttModelName,
+ useVoiceSettingsStore,
+} from "./stores/voice-settings-store";
export type { Palette, ResolvedTheme, Theme } from "./stores/theme-store";
-export { useVoiceSettingsStore } from "./stores/voice-settings-store";
export { isMacPlatform } from "./lib/keyboard-shortcuts";
diff --git a/studio/frontend/src/i18n/locales/ar.ts b/studio/frontend/src/i18n/locales/ar.ts
index 360ca50a421..c34ff9ccdb0 100644
--- a/studio/frontend/src/i18n/locales/ar.ts
+++ b/studio/frontend/src/i18n/locales/ar.ts
@@ -613,6 +613,29 @@ export const ar = {
title: "الصوت",
description: "الميكروفون والإملاء وتحويل الكلام إلى نص والقراءة بصوت عالٍ",
dictation: {
+ audioUploadTitle: "الإملاء باستخدام تسجيل",
+ audioUploadDescription:
+ "يتطلب متصفحك اتصالاً آمنًا (HTTPS) للتسجيل مباشرةً في Studio. على اتصال HTTP هذا، استخدم مسجل هاتفك أو اختر تسجيلاً محفوظًا بدلاً من ذلك. سينسخ خادم Studio الصوت ويضيف النص إلى رسالتك.",
+ audioUploadChooseFile: "اختيار تسجيل",
+ audioUploadRecord: "تسجيل الصوت",
+ audioUploadIphoneHint:
+ "على iPhone، احفظ تسجيلاً من Voice Memos أو من تطبيق تسجيل آخر، ثم اختره هنا.",
+ audioUploadServerModelNote:
+ "يستخدم هذا التسجيل النموذج المحلي الموضّح أعلاه على خادم Studio، حتى إذا كان الإملاء العادي يستخدم محركًا مختلفًا.",
+ audioUploadAutomatic: "تلقائي",
+ audioUploadModelReady: "جاهز على خادم Studio هذا",
+ audioUploadRetryTitle: "تعذّر نسخ {file}",
+ audioUploadRetry: "إعادة محاولة النسخ",
+ audioUploadChooseModel:
+ "اختر نموذجًا محليًا للتعرّف على الكلام في إعدادات الصوت.",
+ audioUploadEmpty: "التسجيل المحدد فارغ.",
+ audioUploadTooLarge: "اختر تسجيلاً أصغر من {size}.",
+ audioUploadVideoUnsupported:
+ "اختر تسجيلاً صوتياً، وليس مقطع فيديو.",
+ audioUploadNoSpeech: "لم يسمع النموذج أي كلام في ذلك التسجيل.",
+ audioUploadFailed: "تعذّر نسخ التسجيل.",
+ audioUploadCancel: "إلغاء النسخ",
+ audioUploadTranscribing: "جارٍ نسخ الصوت…",
sectionTitle: "الإملاء",
engineLabel: "محرك الإملاء",
engineBrowser: "المتصفح",
diff --git a/studio/frontend/src/i18n/locales/de.ts b/studio/frontend/src/i18n/locales/de.ts
index 4e92ac2cae3..65e36c5c77b 100644
--- a/studio/frontend/src/i18n/locales/de.ts
+++ b/studio/frontend/src/i18n/locales/de.ts
@@ -617,6 +617,29 @@ export const de = {
title: "Sprachfunktionen",
description: "Mikrofon, Diktat, Spracherkennung und Vorlesen",
dictation: {
+ audioUploadTitle: "Mit einer Aufnahme diktieren",
+ audioUploadDescription:
+ "Ihr Browser benötigt eine sichere Verbindung (HTTPS), um direkt in Studio aufzunehmen. Verwenden Sie bei dieser HTTP-Verbindung stattdessen den Rekorder Ihres Smartphones oder wählen Sie eine gespeicherte Aufnahme aus. Ihr Studio-Server transkribiert das Audio und fügt den Text Ihrer Nachricht hinzu.",
+ audioUploadChooseFile: "Aufnahme auswählen",
+ audioUploadRecord: "Audio aufnehmen",
+ audioUploadIphoneHint:
+ "Speichern Sie auf dem iPhone eine Aufnahme aus Sprachmemos oder einer anderen Rekorder-App und wählen Sie sie dann hier aus.",
+ audioUploadServerModelNote:
+ "Diese Aufnahme verwendet das oben angezeigte lokale Modell auf dem Studio-Server, auch wenn das normale Diktat eine andere Engine verwendet.",
+ audioUploadAutomatic: "Automatisch",
+ audioUploadModelReady: "Auf diesem Studio-Server bereit",
+ audioUploadRetryTitle: "{file} konnte nicht transkribiert werden",
+ audioUploadRetry: "Transkription wiederholen",
+ audioUploadChooseModel:
+ "Wählen Sie in den Spracheinstellungen ein lokales Spracherkennungsmodell aus.",
+ audioUploadEmpty: "Die ausgewählte Aufnahme ist leer.",
+ audioUploadTooLarge: "Wählen Sie eine Aufnahme, die kleiner als {size} ist.",
+ audioUploadVideoUnsupported:
+ "Wählen Sie eine Audioaufnahme und kein Video aus.",
+ audioUploadNoSpeech: "Das Modell hat in dieser Aufnahme keine Sprache erkannt.",
+ audioUploadFailed: "Die Aufnahme konnte nicht transkribiert werden.",
+ audioUploadCancel: "Transkription abbrechen",
+ audioUploadTranscribing: "Audio wird transkribiert…",
sectionTitle: "Diktat",
engineLabel: "Diktat-Engine",
engineBrowser: "Browser",
diff --git a/studio/frontend/src/i18n/locales/en.ts b/studio/frontend/src/i18n/locales/en.ts
index 7a912999d4b..245d21de060 100644
--- a/studio/frontend/src/i18n/locales/en.ts
+++ b/studio/frontend/src/i18n/locales/en.ts
@@ -620,6 +620,29 @@ export const en = {
title: "Voice",
description: "Microphone, dictation, speech-to-text and read-aloud",
dictation: {
+ audioUploadTitle: "Dictate with a recording",
+ audioUploadDescription:
+ "Your browser requires a secure connection (HTTPS) to record directly in Studio. On this HTTP connection, use your phone's recorder or choose a saved recording instead. Your Studio server will transcribe the audio and add the text to your message.",
+ audioUploadChooseFile: "Choose recording",
+ audioUploadRecord: "Record audio",
+ audioUploadIphoneHint:
+ "On iPhone, save a recording from Voice Memos or another recorder app, then choose it here.",
+ audioUploadServerModelNote:
+ "This recording uses the local model shown above on the Studio server, even if normal dictation uses a different engine.",
+ audioUploadAutomatic: "Automatic",
+ audioUploadModelReady: "Ready on this Studio server",
+ audioUploadRetryTitle: "Could not transcribe {file}",
+ audioUploadRetry: "Retry transcription",
+ audioUploadChooseModel:
+ "Choose a local speech-recognition model in Voice settings.",
+ audioUploadEmpty: "The selected recording is empty.",
+ audioUploadTooLarge: "Choose a recording smaller than {size}.",
+ audioUploadVideoUnsupported:
+ "Choose an audio recording, not a video.",
+ audioUploadNoSpeech: "The model heard no speech in that recording.",
+ audioUploadFailed: "The recording could not be transcribed.",
+ audioUploadCancel: "Cancel transcription",
+ audioUploadTranscribing: "Transcribing audio…",
sectionTitle: "Dictation",
engineLabel: "Dictation engine",
engineBrowser: "Browser",
diff --git a/studio/frontend/src/i18n/locales/es.ts b/studio/frontend/src/i18n/locales/es.ts
index 03621944775..a2057669052 100644
--- a/studio/frontend/src/i18n/locales/es.ts
+++ b/studio/frontend/src/i18n/locales/es.ts
@@ -616,6 +616,29 @@ export const es = {
title: "Voz",
description: "Micrófono, dictado, voz a texto y lectura en voz alta",
dictation: {
+ audioUploadTitle: "Dictar con una grabación",
+ audioUploadDescription:
+ "Tu navegador necesita una conexión segura (HTTPS) para grabar directamente en Studio. Con esta conexión HTTP, usa la grabadora de tu teléfono o elige una grabación guardada. Tu servidor de Studio transcribirá el audio y añadirá el texto a tu mensaje.",
+ audioUploadChooseFile: "Elegir grabación",
+ audioUploadRecord: "Grabar audio",
+ audioUploadIphoneHint:
+ "En iPhone, guarda una grabación de Notas de Voz o de otra aplicación de grabación y elígela aquí.",
+ audioUploadServerModelNote:
+ "Esta grabación usa el modelo local que se muestra arriba en el servidor de Studio, aunque el dictado normal use un motor diferente.",
+ audioUploadAutomatic: "Automático",
+ audioUploadModelReady: "Listo en este servidor de Studio",
+ audioUploadRetryTitle: "No se pudo transcribir {file}",
+ audioUploadRetry: "Reintentar la transcripción",
+ audioUploadChooseModel:
+ "Elige un modelo local de reconocimiento de voz en los ajustes de Voz.",
+ audioUploadEmpty: "La grabación seleccionada está vacía.",
+ audioUploadTooLarge: "Elige una grabación de menos de {size}.",
+ audioUploadVideoUnsupported:
+ "Elige una grabación de audio, no un vídeo.",
+ audioUploadNoSpeech: "El modelo no detectó voz en esa grabación.",
+ audioUploadFailed: "No se pudo transcribir la grabación.",
+ audioUploadCancel: "Cancelar transcripción",
+ audioUploadTranscribing: "Transcribiendo audio…",
sectionTitle: "Dictado",
engineLabel: "Motor de dictado",
engineBrowser: "Navegador",
diff --git a/studio/frontend/src/i18n/locales/fr.ts b/studio/frontend/src/i18n/locales/fr.ts
index eef77293bc8..f84af6f9a67 100644
--- a/studio/frontend/src/i18n/locales/fr.ts
+++ b/studio/frontend/src/i18n/locales/fr.ts
@@ -617,6 +617,29 @@ export const fr = {
title: "Voix",
description: "Microphone, dictée, reconnaissance vocale et lecture à voix haute",
dictation: {
+ audioUploadTitle: "Dicter avec un enregistrement",
+ audioUploadDescription:
+ "Votre navigateur nécessite une connexion sécurisée (HTTPS) pour enregistrer directement dans Studio. Avec cette connexion HTTP, utilisez plutôt l'enregistreur de votre téléphone ou choisissez un enregistrement sauvegardé. Votre serveur Studio transcrira l'audio et ajoutera le texte à votre message.",
+ audioUploadChooseFile: "Choisir un enregistrement",
+ audioUploadRecord: "Enregistrer l'audio",
+ audioUploadIphoneHint:
+ "Sur iPhone, sauvegardez un enregistrement depuis Dictaphone ou une autre application d'enregistrement, puis choisissez-le ici.",
+ audioUploadServerModelNote:
+ "Cet enregistrement utilise le modèle local indiqué ci-dessus sur le serveur Studio, même si la dictée habituelle utilise un autre moteur.",
+ audioUploadAutomatic: "Automatique",
+ audioUploadModelReady: "Prêt sur ce serveur Studio",
+ audioUploadRetryTitle: "Impossible de transcrire {file}",
+ audioUploadRetry: "Réessayer la transcription",
+ audioUploadChooseModel:
+ "Choisissez un modèle local de reconnaissance vocale dans les paramètres Voix.",
+ audioUploadEmpty: "L'enregistrement sélectionné est vide.",
+ audioUploadTooLarge: "Choisissez un enregistrement de moins de {size}.",
+ audioUploadVideoUnsupported:
+ "Choisissez un enregistrement audio, pas une vidéo.",
+ audioUploadNoSpeech: "Le modèle n'a détecté aucune parole dans cet enregistrement.",
+ audioUploadFailed: "Impossible de transcrire l'enregistrement.",
+ audioUploadCancel: "Annuler la transcription",
+ audioUploadTranscribing: "Transcription audio en cours…",
sectionTitle: "Dictée",
engineLabel: "Moteur de dictée",
engineBrowser: "Navigateur",
diff --git a/studio/frontend/src/i18n/locales/hi.ts b/studio/frontend/src/i18n/locales/hi.ts
index bf1ad5097e1..e23272c892e 100644
--- a/studio/frontend/src/i18n/locales/hi.ts
+++ b/studio/frontend/src/i18n/locales/hi.ts
@@ -616,6 +616,29 @@ export const hi = {
title: "आवाज़",
description: "माइक्रोफ़ोन, डिक्टेशन, स्पीच-टू-टेक्स्ट और ज़ोर से पढ़ना",
dictation: {
+ audioUploadTitle: "रिकॉर्डिंग से डिक्टेशन करें",
+ audioUploadDescription:
+ "आपके ब्राउज़र को Studio में सीधे रिकॉर्ड करने के लिए सुरक्षित कनेक्शन (HTTPS) चाहिए। इस HTTP कनेक्शन पर, इसके बजाय अपने फ़ोन का रिकॉर्डर इस्तेमाल करें या सेव की गई रिकॉर्डिंग चुनें। आपका Studio सर्वर ऑडियो को टेक्स्ट में बदलेगा और उसे आपके संदेश में जोड़ देगा।",
+ audioUploadChooseFile: "रिकॉर्डिंग चुनें",
+ audioUploadRecord: "ऑडियो रिकॉर्ड करें",
+ audioUploadIphoneHint:
+ "iPhone पर, Voice Memos या किसी अन्य रिकॉर्डर ऐप से रिकॉर्डिंग सेव करें, फिर उसे यहाँ चुनें।",
+ audioUploadServerModelNote:
+ "यह रिकॉर्डिंग Studio सर्वर पर ऊपर दिखाए गए स्थानीय मॉडल का उपयोग करती है, भले ही सामान्य डिक्टेशन किसी दूसरे इंजन का उपयोग करता हो।",
+ audioUploadAutomatic: "स्वचालित",
+ audioUploadModelReady: "इस Studio सर्वर पर तैयार",
+ audioUploadRetryTitle: "{file} को टेक्स्ट में नहीं बदला जा सका",
+ audioUploadRetry: "ट्रांसक्रिप्शन फिर से आज़माएँ",
+ audioUploadChooseModel:
+ "आवाज़ की सेटिंग में कोई स्थानीय स्पीच-रिकग्निशन मॉडल चुनें।",
+ audioUploadEmpty: "चुनी गई रिकॉर्डिंग खाली है।",
+ audioUploadTooLarge: "{size} से छोटी रिकॉर्डिंग चुनें।",
+ audioUploadVideoUnsupported:
+ "वीडियो के बजाय कोई ऑडियो रिकॉर्डिंग चुनें।",
+ audioUploadNoSpeech: "मॉडल को उस रिकॉर्डिंग में कोई आवाज़ सुनाई नहीं दी।",
+ audioUploadFailed: "रिकॉर्डिंग को टेक्स्ट में नहीं बदला जा सका।",
+ audioUploadCancel: "ट्रांसक्रिप्शन रद्द करें",
+ audioUploadTranscribing: "ऑडियो को टेक्स्ट में बदला जा रहा है…",
sectionTitle: "डिक्टेशन",
engineLabel: "डिक्टेशन इंजन",
engineBrowser: "ब्राउज़र",
diff --git a/studio/frontend/src/i18n/locales/it.ts b/studio/frontend/src/i18n/locales/it.ts
index fc0aeb66ed2..b196087ec19 100644
--- a/studio/frontend/src/i18n/locales/it.ts
+++ b/studio/frontend/src/i18n/locales/it.ts
@@ -582,6 +582,29 @@ export const it = {
description:
"Microfono, dettatura, riconoscimento vocale e lettura ad alta voce",
dictation: {
+ audioUploadTitle: "Detta con una registrazione",
+ audioUploadDescription:
+ "Il browser richiede una connessione sicura (HTTPS) per registrare direttamente in Studio. Con questa connessione HTTP, usa invece il registratore del telefono o scegli una registrazione salvata. Il server Studio trascriverà l'audio e aggiungerà il testo al messaggio.",
+ audioUploadChooseFile: "Scegli registrazione",
+ audioUploadRecord: "Registra audio",
+ audioUploadIphoneHint:
+ "Su iPhone, salva una registrazione da Memo Vocali o da un'altra app di registrazione, quindi sceglila qui.",
+ audioUploadServerModelNote:
+ "Questa registrazione usa il modello locale mostrato sopra sul server Studio, anche se la dettatura normale usa un motore diverso.",
+ audioUploadAutomatic: "Automatico",
+ audioUploadModelReady: "Pronto su questo server Studio",
+ audioUploadRetryTitle: "Impossibile trascrivere {file}",
+ audioUploadRetry: "Riprova la trascrizione",
+ audioUploadChooseModel:
+ "Scegli un modello locale di riconoscimento vocale nelle impostazioni Voce.",
+ audioUploadEmpty: "La registrazione selezionata è vuota.",
+ audioUploadTooLarge: "Scegli una registrazione più piccola di {size}.",
+ audioUploadVideoUnsupported:
+ "Scegli una registrazione audio, non un video.",
+ audioUploadNoSpeech: "Il modello non ha rilevato alcuna voce nella registrazione.",
+ audioUploadFailed: "Non è stato possibile trascrivere la registrazione.",
+ audioUploadCancel: "Annulla trascrizione",
+ audioUploadTranscribing: "Trascrizione audio in corso…",
sectionTitle: "Dettatura",
engineLabel: "Motore di dettatura",
engineBrowser: "Browser",
diff --git a/studio/frontend/src/i18n/locales/ja.ts b/studio/frontend/src/i18n/locales/ja.ts
index c89d16209d8..312ea7d6cdc 100644
--- a/studio/frontend/src/i18n/locales/ja.ts
+++ b/studio/frontend/src/i18n/locales/ja.ts
@@ -615,6 +615,29 @@ export const ja = {
title: "音声",
description: "マイク、音声入力、音声認識、読み上げ",
dictation: {
+ audioUploadTitle: "録音を使って音声入力",
+ audioUploadDescription:
+ "Studio で直接録音するには、ブラウザーでセキュア接続 (HTTPS) が必要です。この HTTP 接続では、代わりにスマートフォンのレコーダーを使うか、保存済みの録音を選択してください。Studio サーバーが音声を文字起こしし、テキストをメッセージに追加します。",
+ audioUploadChooseFile: "録音を選択",
+ audioUploadRecord: "音声を録音",
+ audioUploadIphoneHint:
+ "iPhone では、Voice Memos または別の録音アプリで録音を保存してから、ここで選択してください。",
+ audioUploadServerModelNote:
+ "この録音では、通常の音声入力に別のエンジンを使用している場合でも、Studio サーバー上で上に表示されているローカルモデルを使用します。",
+ audioUploadAutomatic: "自動",
+ audioUploadModelReady: "この Studio サーバーで使用可能",
+ audioUploadRetryTitle: "{file} を文字起こしできませんでした",
+ audioUploadRetry: "文字起こしを再試行",
+ audioUploadChooseModel:
+ "音声設定でローカル音声認識モデルを選択してください。",
+ audioUploadEmpty: "選択した録音は空です。",
+ audioUploadTooLarge: "{size} 未満の録音を選択してください。",
+ audioUploadVideoUnsupported:
+ "動画ではなく、音声録音を選択してください。",
+ audioUploadNoSpeech: "この録音から音声を検出できませんでした。",
+ audioUploadFailed: "録音を文字起こしできませんでした。",
+ audioUploadCancel: "文字起こしをキャンセル",
+ audioUploadTranscribing: "音声を文字起こし中…",
sectionTitle: "音声入力",
engineLabel: "音声入力エンジン",
engineBrowser: "ブラウザー",
diff --git a/studio/frontend/src/i18n/locales/ko.ts b/studio/frontend/src/i18n/locales/ko.ts
index 9bdba5b53ef..d344615c1d9 100644
--- a/studio/frontend/src/i18n/locales/ko.ts
+++ b/studio/frontend/src/i18n/locales/ko.ts
@@ -613,6 +613,29 @@ export const ko = {
title: "음성",
description: "마이크, 받아쓰기, 음성 인식 및 소리 내어 읽기",
dictation: {
+ audioUploadTitle: "녹음으로 받아쓰기",
+ audioUploadDescription:
+ "Studio에서 직접 녹음하려면 브라우저에 보안 연결(HTTPS)이 필요합니다. 이 HTTP 연결에서는 휴대폰 녹음기를 사용하거나 저장된 녹음을 선택하세요. Studio 서버가 오디오를 텍스트로 변환하여 메시지에 추가합니다.",
+ audioUploadChooseFile: "녹음 선택",
+ audioUploadRecord: "오디오 녹음",
+ audioUploadIphoneHint:
+ "iPhone에서는 Voice Memos 또는 다른 녹음 앱에서 녹음을 저장한 다음 여기에서 선택하세요.",
+ audioUploadServerModelNote:
+ "일반 받아쓰기가 다른 엔진을 사용하더라도 이 녹음에는 Studio 서버에서 위에 표시된 로컬 모델이 사용됩니다.",
+ audioUploadAutomatic: "자동",
+ audioUploadModelReady: "이 Studio 서버에서 준비됨",
+ audioUploadRetryTitle: "{file}을(를) 전사하지 못했습니다",
+ audioUploadRetry: "전사 다시 시도",
+ audioUploadChooseModel:
+ "음성 설정에서 로컬 음성 인식 모델을 선택하세요.",
+ audioUploadEmpty: "선택한 녹음이 비어 있습니다.",
+ audioUploadTooLarge: "{size}보다 작은 녹음을 선택하세요.",
+ audioUploadVideoUnsupported:
+ "동영상이 아닌 오디오 녹음을 선택하세요.",
+ audioUploadNoSpeech: "모델이 해당 녹음에서 음성을 감지하지 못했습니다.",
+ audioUploadFailed: "녹음을 전사하지 못했습니다.",
+ audioUploadCancel: "전사 취소",
+ audioUploadTranscribing: "오디오 전사 중…",
sectionTitle: "받아쓰기",
engineLabel: "받아쓰기 엔진",
engineBrowser: "브라우저",
diff --git a/studio/frontend/src/i18n/locales/pt-br.ts b/studio/frontend/src/i18n/locales/pt-br.ts
index bccc95d166e..4dff21112d5 100644
--- a/studio/frontend/src/i18n/locales/pt-br.ts
+++ b/studio/frontend/src/i18n/locales/pt-br.ts
@@ -615,6 +615,29 @@ export const ptBR = {
title: "Voz",
description: "Microfone, ditado, conversão de fala em texto e leitura em voz alta",
dictation: {
+ audioUploadTitle: "Ditar com uma gravação",
+ audioUploadDescription:
+ "Seu navegador exige uma conexão segura (HTTPS) para gravar diretamente no Studio. Nesta conexão HTTP, use o gravador do celular ou escolha uma gravação salva. Seu servidor do Studio transcreverá o áudio e adicionará o texto à sua mensagem.",
+ audioUploadChooseFile: "Escolher gravação",
+ audioUploadRecord: "Gravar áudio",
+ audioUploadIphoneHint:
+ "No iPhone, salve uma gravação do Gravador ou de outro aplicativo de gravação e escolha-a aqui.",
+ audioUploadServerModelNote:
+ "Esta gravação usa o modelo local mostrado acima no servidor do Studio, mesmo que o ditado normal use outro mecanismo.",
+ audioUploadAutomatic: "Automático",
+ audioUploadModelReady: "Pronto neste servidor do Studio",
+ audioUploadRetryTitle: "Não foi possível transcrever {file}",
+ audioUploadRetry: "Tentar transcrever novamente",
+ audioUploadChooseModel:
+ "Escolha um modelo local de reconhecimento de fala nas configurações de Voz.",
+ audioUploadEmpty: "A gravação selecionada está vazia.",
+ audioUploadTooLarge: "Escolha uma gravação menor que {size}.",
+ audioUploadVideoUnsupported:
+ "Escolha uma gravação de áudio, não um vídeo.",
+ audioUploadNoSpeech: "O modelo não detectou fala nessa gravação.",
+ audioUploadFailed: "Não foi possível transcrever a gravação.",
+ audioUploadCancel: "Cancelar transcrição",
+ audioUploadTranscribing: "Transcrevendo áudio…",
sectionTitle: "Ditado",
engineLabel: "Mecanismo de ditado",
engineBrowser: "Navegador",
diff --git a/studio/frontend/src/i18n/locales/ru.ts b/studio/frontend/src/i18n/locales/ru.ts
index 3858f087a10..12ddbe049d1 100644
--- a/studio/frontend/src/i18n/locales/ru.ts
+++ b/studio/frontend/src/i18n/locales/ru.ts
@@ -615,6 +615,28 @@ export const ru = {
title: "Голос",
description: "Микрофон, диктовка, распознавание речи и чтение вслух",
dictation: {
+ audioUploadTitle: "Диктовка с помощью записи",
+ audioUploadDescription:
+ "Для записи непосредственно в Studio браузеру требуется безопасное подключение (HTTPS). При этом HTTP-подключении используйте диктофон на телефоне или выберите сохранённую запись. Сервер Studio расшифрует аудио и добавит текст в ваше сообщение.",
+ audioUploadChooseFile: "Выбрать запись",
+ audioUploadRecord: "Записать аудио",
+ audioUploadIphoneHint:
+ "На iPhone сохраните запись из приложения «Диктофон» (Voice Memos) или другого приложения для записи, а затем выберите её здесь.",
+ audioUploadServerModelNote:
+ "Для этой записи используется указанная выше локальная модель на сервере Studio, даже если обычная диктовка использует другой движок.",
+ audioUploadAutomatic: "Автоматически",
+ audioUploadModelReady: "Готово на этом сервере Studio",
+ audioUploadRetryTitle: "Не удалось расшифровать {file}",
+ audioUploadRetry: "Повторить расшифровку",
+ audioUploadChooseModel:
+ "Выберите локальную модель распознавания речи в настройках голоса.",
+ audioUploadEmpty: "Выбранная запись пуста.",
+ audioUploadTooLarge: "Выберите запись размером менее {size}.",
+ audioUploadVideoUnsupported: "Выберите аудиозапись, а не видео.",
+ audioUploadNoSpeech: "Модель не обнаружила речь в этой записи.",
+ audioUploadFailed: "Не удалось расшифровать запись.",
+ audioUploadCancel: "Отменить расшифровку",
+ audioUploadTranscribing: "Расшифровка аудио…",
sectionTitle: "Диктовка",
engineLabel: "Движок диктовки",
engineBrowser: "Браузер",
diff --git a/studio/frontend/src/i18n/locales/zh-CN.ts b/studio/frontend/src/i18n/locales/zh-CN.ts
index 5c469c46dbf..94f0aee6202 100644
--- a/studio/frontend/src/i18n/locales/zh-CN.ts
+++ b/studio/frontend/src/i18n/locales/zh-CN.ts
@@ -611,6 +611,28 @@ export const zhCN = {
title: "语音",
description: "麦克风、语音输入、语音转文字和朗读",
dictation: {
+ audioUploadTitle: "使用录音进行语音输入",
+ audioUploadDescription:
+ "浏览器需要安全连接 (HTTPS) 才能直接在 Studio 中录音。在当前 HTTP 连接下,请改用手机录音工具,或选择已保存的录音。Studio 服务器会转写音频,并将文字添加到您的消息中。",
+ audioUploadChooseFile: "选择录音",
+ audioUploadRecord: "录制音频",
+ audioUploadIphoneHint:
+ "在 iPhone 上,请通过“语音备忘录”(Voice Memos) 或其他录音应用保存录音,然后在此处选择。",
+ audioUploadServerModelNote:
+ "此录音会使用 Studio 服务器上方显示的本地模型,即使普通语音输入使用的是其他引擎。",
+ audioUploadAutomatic: "自动",
+ audioUploadModelReady: "已在此 Studio 服务器上就绪",
+ audioUploadRetryTitle: "无法转写 {file}",
+ audioUploadRetry: "重试转写",
+ audioUploadChooseModel:
+ "请在“语音”设置中选择本地语音识别模型。",
+ audioUploadEmpty: "所选录音为空。",
+ audioUploadTooLarge: "请选择小于 {size} 的录音。",
+ audioUploadVideoUnsupported: "请选择录音文件,而不是视频。",
+ audioUploadNoSpeech: "模型未在该录音中检测到语音。",
+ audioUploadFailed: "无法转写该录音。",
+ audioUploadCancel: "取消转录",
+ audioUploadTranscribing: "正在转录音频…",
sectionTitle: "语音输入",
engineLabel: "语音输入引擎",
engineBrowser: "浏览器",
diff --git a/studio/frontend/tests/chat-audio-upload.test.ts b/studio/frontend/tests/chat-audio-upload.test.ts
new file mode 100644
index 00000000000..188fd103d43
--- /dev/null
+++ b/studio/frontend/tests/chat-audio-upload.test.ts
@@ -0,0 +1,521 @@
+// 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 { readSrc } from "./helpers/kit.ts";
+
+import {
+ ChatAudioReadinessRefreshQueue,
+ appendChatAudioTranscript,
+ chatAudioUploadFenceMatches,
+ chatAudioUploadFileError,
+ completeChatAudioUpload,
+} from "../src/features/chat/utils/chat-audio-upload.ts";
+
+const hookSource = readSrc("features/chat/hooks/use-chat-audio-upload.ts");
+const sharedComposerSource = readSrc("features/chat/shared-composer.tsx");
+const threadSource = readSrc("components/assistant-ui/thread.tsx");
+const dialogSource = readSrc("features/chat/components/chat-audio-upload.tsx");
+const modelAdapterSource = readSrc(
+ "features/chat/adapters/studio-model-dictation-adapter.ts",
+);
+
+test("audio upload rejects empty and oversized files", () => {
+ assert.equal(chatAudioUploadFileError({ size: 0 }), "empty");
+ assert.equal(chatAudioUploadFileError({ size: 25 * 1024 * 1024 }), null);
+ assert.equal(
+ chatAudioUploadFileError({ size: 25 * 1024 * 1024 + 1 }),
+ "too-large",
+ );
+});
+
+test("audio upload classifies ambiguous 3GP files and refuses video", () => {
+ const selectFileStart = hookSource.indexOf("const selectFile = useCallback(");
+ const selectFile = hookSource.slice(
+ selectFileStart,
+ hookSource.indexOf(" const retry = useCallback", selectFileStart),
+ );
+ const classified = selectFile.indexOf(
+ "const classifiedFile = await classifiedAttachmentFile(file);",
+ );
+ const refused = selectFile.indexOf("if (isVideoFile(classifiedFile))");
+ const transcribed = selectFile.indexOf(
+ "void runTranscription(classifiedFile, snapshot);",
+ );
+ assert.ok(classified >= 0 && classified < refused && refused < transcribed);
+ assert.match(selectFile, /audioUploadVideoUnsupported[\s\S]*?return;/);
+ const staleClassification = selectFile.slice(classified, refused);
+ assert.match(staleClassification, /chatAudioUploadFenceMatches/);
+ assert.doesNotMatch(staleClassification, /clearOperation\(\)/);
+});
+
+test("audio upload accepts only its current owner, generation and auth session", () => {
+ const started = { generation: 3, owner: "thread-a", authSessionEpoch: 7 };
+ assert.equal(chatAudioUploadFenceMatches(started, { ...started }), true);
+ assert.equal(
+ chatAudioUploadFenceMatches(started, { ...started, generation: 4 }),
+ false,
+ );
+ assert.equal(
+ chatAudioUploadFenceMatches(started, { ...started, owner: "thread-b" }),
+ false,
+ );
+ assert.equal(
+ chatAudioUploadFenceMatches(started, { ...started, authSessionEpoch: 8 }),
+ false,
+ );
+});
+
+test("audio transcript appends once without replacing a draft typed meanwhile", () => {
+ assert.equal(appendChatAudioTranscript("", " hello "), "hello");
+ assert.equal(appendChatAudioTranscript("draft", " hello "), "draft hello");
+ assert.equal(appendChatAudioTranscript("draft ", "hello"), "draft hello");
+ assert.equal(appendChatAudioTranscript("draft", " "), "draft");
+});
+
+test("an ignored abort cannot commit a late transcript", async () => {
+ let resolveTranscript: (value: string) => void = () => {};
+ const transcript = new Promise<string>((resolve) => {
+ resolveTranscript = resolve;
+ });
+ const controller = new AbortController();
+ const fence = { generation: 1, owner: "thread-a", authSessionEpoch: 2 };
+ let commits = 0;
+ const completion = completeChatAudioUpload({
+ started: fence,
+ current: () => fence,
+ signal: controller.signal,
+ blocked: () => false,
+ transcribe: () => transcript,
+ commit: () => {
+ commits += 1;
+ },
+ });
+ controller.abort();
+ resolveTranscript("late words");
+ assert.equal(await completion, "stale");
+ assert.equal(commits, 0);
+});
+
+test("an owner change before transcription resolves cannot commit", async () => {
+ let resolveTranscript: (value: string) => void = () => {};
+ const transcript = new Promise<string>((resolve) => {
+ resolveTranscript = resolve;
+ });
+ const started = { generation: 1, owner: "thread-a", authSessionEpoch: 2 };
+ let current = started;
+ let commits = 0;
+ const completion = completeChatAudioUpload({
+ started,
+ current: () => current,
+ signal: new AbortController().signal,
+ blocked: () => false,
+ transcribe: () => transcript,
+ commit: () => {
+ commits += 1;
+ },
+ });
+ current = { ...started, owner: "thread-b" };
+ resolveTranscript("late words");
+ assert.equal(await completion, "stale");
+ assert.equal(commits, 0);
+});
+
+test("an A to B to A generation change still rejects the first result", async () => {
+ const started = { generation: 1, owner: "thread-a", authSessionEpoch: 2 };
+ let current = started;
+ let commits = 0;
+ const completion = completeChatAudioUpload({
+ started,
+ current: () => current,
+ signal: new AbortController().signal,
+ blocked: () => false,
+ transcribe: async () => {
+ current = { ...started, generation: 3 };
+ return "late words";
+ },
+ commit: () => {
+ commits += 1;
+ },
+ });
+ assert.equal(await completion, "stale");
+ assert.equal(commits, 0);
+});
+
+test("a current result commits exactly once and empty audio commits nothing", async () => {
+ const fence = { generation: 1, owner: "thread-a", authSessionEpoch: 2 };
+ const committed: string[] = [];
+ const run = (value: string) =>
+ completeChatAudioUpload({
+ started: fence,
+ current: () => fence,
+ signal: new AbortController().signal,
+ blocked: () => false,
+ transcribe: async () => value,
+ commit: (result) => committed.push(result),
+ });
+ assert.equal(await run(" hello "), "committed");
+ assert.deepEqual(committed, ["hello"]);
+ assert.equal(await run(" "), "empty");
+ assert.deepEqual(committed, ["hello"]);
+});
+
+test("the main composer has one Dictate entry and does not gate it on draft text", () => {
+ const callStart = threadSource.indexOf("<ComposerRightControls");
+ assert.notEqual(callStart, -1);
+ const call = threadSource.slice(
+ callStart,
+ threadSource.indexOf("/>", callStart),
+ );
+ assert.match(call, /dictationDisabled=\{dictationEntryDisabled\}/);
+ assert.match(threadSource, /const dictationEntryDisabled = !chatActive;/);
+ const entryGate = threadSource.slice(
+ threadSource.indexOf("const dictationEntryDisabled"),
+ threadSource.indexOf(
+ "const audioUpload",
+ threadSource.indexOf("const dictationEntryDisabled"),
+ ),
+ );
+ assert.doesNotMatch(
+ entryGate,
+ /disabled|isComposing|hasPendingAttachments|hasSendableContent/,
+ );
+ const controlsStart = threadSource.indexOf("const ComposerRightControls:");
+ const controls = threadSource.slice(controlsStart);
+ assert.equal((controls.match(/tooltip="Dictate"/g) ?? []).length, 1);
+ assert.doesNotMatch(controls, /Upload01Icon|DialogTrigger/);
+});
+
+test("a later disabled transition cancels the active upload", () => {
+ assert.match(
+ hookSource,
+ /useLayoutEffect\(\(\) => \{\s*if \(!disabled\) return;\s*invalidateRefs\(\);[\s\S]*?queueMicrotask\([\s\S]*?setBusy\(false\);/,
+ );
+});
+
+test("owner changes fence stale completions before passive effects", () => {
+ assert.match(
+ hookSource,
+ /useLayoutEffect\(\(\) => \{\s*const ownerChanged = ownerRef\.current !== owner;[\s\S]*?if \(ownerChanged\) clearOperation\(\);/,
+ );
+});
+
+test("an open dialog observes a missing model becoming ready", () => {
+ assert.match(
+ hookSource,
+ /const refreshReadiness = useCallback\(\s*async \(silent = false\) => \{[\s\S]*?if \(!silent\) \{[\s\S]*?state: targetModel \? "checking" : "error"/,
+ );
+ const pollStart = hookSource.indexOf(
+ '(readiness.state !== "missing" && readiness.state !== "downloading")',
+ );
+ assert.notEqual(pollStart, -1);
+ const poll = hookSource.slice(
+ pollStart,
+ hookSource.indexOf(" }, [dialogOpen", pollStart),
+ );
+ assert.match(
+ poll,
+ /setInterval\(\(\) => void refreshReadiness\(true\), 1500\)/,
+ );
+});
+
+test("readiness refreshes stay serialized across invalidation and reopen", async () => {
+ const queue = new ChatAudioReadinessRefreshQueue();
+ let resolveFirst = () => {};
+ let resolveSecond = () => {};
+ const firstGate = new Promise<void>((resolve) => {
+ resolveFirst = resolve;
+ });
+ const secondGate = new Promise<void>((resolve) => {
+ resolveSecond = resolve;
+ });
+ let calls = 0;
+ let firstGeneration = -1;
+ const committed: string[] = [];
+
+ const first = queue.run(false, async (generation) => {
+ firstGeneration = generation;
+ calls += 1;
+ await firstGate;
+ if (queue.isCurrent(generation)) committed.push("first");
+ });
+ assert.equal(calls, 1);
+ assert.equal(await queue.run(true, async () => {}), "skipped");
+
+ queue.invalidate();
+ const reopened = queue.run(false, async (generation) => {
+ calls += 1;
+ await secondGate;
+ if (queue.isCurrent(generation)) committed.push("reopened");
+ });
+ assert.equal(calls, 1);
+ assert.equal(queue.isCurrent(firstGeneration), false);
+ assert.equal(await queue.run(true, async () => {}), "skipped");
+
+ resolveFirst();
+ assert.equal(await first, "ran");
+ assert.deepEqual(committed, []);
+ await Promise.resolve();
+ assert.equal(calls, 2);
+ assert.equal(await queue.run(true, async () => {}), "skipped");
+
+ resolveSecond();
+ assert.equal(await reopened, "ran");
+ assert.equal(calls, 2);
+ assert.deepEqual(committed, ["reopened"]);
+});
+
+test("a newer explicit readiness refresh supersedes one still in flight", async () => {
+ const queue = new ChatAudioReadinessRefreshQueue();
+ let release = () => {};
+ const gate = new Promise<void>((resolve) => {
+ release = resolve;
+ });
+ let firstGeneration = -1;
+ const committed: string[] = [];
+
+ const first = queue.run(false, async (generation) => {
+ firstGeneration = generation;
+ await gate;
+ if (queue.isCurrent(generation)) committed.push("old-model");
+ });
+ const replacement = queue.run(false, async (generation) => {
+ if (queue.isCurrent(generation)) committed.push("new-model");
+ });
+
+ assert.equal(queue.isCurrent(firstGeneration), false);
+ release();
+ assert.equal(await first, "ran");
+ assert.equal(await replacement, "ran");
+ assert.deepEqual(committed, ["new-model"]);
+});
+
+test("invalidation aborts a hung refresh before a reopened one starts", async () => {
+ const queue = new ChatAudioReadinessRefreshQueue();
+ let calls = 0;
+ let firstAborted = false;
+
+ const first = queue.run(false, async (_generation, signal) => {
+ calls += 1;
+ await new Promise<void>((resolve) => {
+ signal.addEventListener(
+ "abort",
+ () => {
+ firstAborted = true;
+ resolve();
+ },
+ { once: true },
+ );
+ });
+ });
+ queue.invalidate();
+ const reopened = queue.run(false, async () => {
+ calls += 1;
+ });
+
+ assert.equal(await first, "ran");
+ assert.equal(firstAborted, true);
+ assert.equal(await reopened, "ran");
+ assert.equal(calls, 2);
+});
+
+test("readiness status passes the queue's abort signal to authFetch", () => {
+ assert.match(
+ modelAdapterSource,
+ /export async function fetchSttStatus\([\s\S]*?signal\?: AbortSignal[\s\S]*?authFetch\([\s\S]*?\{ signal \},[\s\S]*?withAbort\(request, signal\)/,
+ );
+ assert.match(
+ hookSource,
+ /queue\.run\(silent, async \(attempt, signal\)[\s\S]*?fetchSttStatus\(undefined, targetModel, signal\)/,
+ );
+});
+
+test("a hung readiness refresh times out and releases the queue", async () => {
+ const queue = new ChatAudioReadinessRefreshQueue(5);
+ let timedOut = false;
+
+ const first = queue.run(false, async (_generation, signal) => {
+ await new Promise<void>((resolve) => {
+ signal.addEventListener(
+ "abort",
+ () => {
+ timedOut = true;
+ resolve();
+ },
+ { once: true },
+ );
+ });
+ });
+
+ let guard: ReturnType<typeof setTimeout> | undefined;
+ try {
+ assert.equal(
+ await Promise.race([
+ first,
+ new Promise<never>((_resolve, reject) => {
+ guard = setTimeout(
+ () => reject(new Error("readiness timeout did not fire")),
+ 250,
+ );
+ }),
+ ]),
+ "ran",
+ );
+ } finally {
+ if (guard !== undefined) clearTimeout(guard);
+ }
+ assert.equal(timedOut, true);
+ assert.equal(await queue.run(false, async () => {}), "ran");
+});
+
+test("Main cancels an upload only after a queue or normal send is accepted", () => {
+ const queueStart = threadSource.indexOf(
+ "const startHydratedPromptQueue = useCallback",
+ );
+ const queue = threadSource.slice(
+ queueStart,
+ threadSource.indexOf("const queuePastedTextPrompt", queueStart),
+ );
+ const accepted = queue.indexOf("cancelAudioUpload();");
+ assert.ok(accepted > queue.indexOf(".then((target) =>"));
+ assert.ok(accepted < queue.indexOf("startPromptQueue(", accepted));
+ assert.ok(queue.indexOf("onAborted?.()", accepted) > accepted);
+
+ const sendStart = threadSource.indexOf(
+ "const sendReservedComposer = useCallback",
+ );
+ const send = threadSource.slice(
+ sendStart,
+ threadSource.indexOf("const interceptSend", sendStart),
+ );
+ const refused = send.indexOf("if (!reservationToken)");
+ const cancel = send.indexOf("cancelAudioUpload();", refused);
+ assert.ok(refused >= 0 && cancel > refused);
+ assert.ok(cancel < send.indexOf("aui.composer().send();", cancel));
+
+ const submitStart = threadSource.indexOf("const handleSubmit = useCallback");
+ const submit = threadSource.slice(
+ submitStart,
+ threadSource.indexOf("const stopQueue", submitStart),
+ );
+ assert.equal(submit.includes("audioUpload.cancel();"), false);
+});
+
+test("Compare routes button and shortcut through the same Dictate entry", () => {
+ const wrapperStart = sharedComposerSource.indexOf(
+ "const startDictation = useCallback",
+ );
+ assert.notEqual(wrapperStart, -1);
+ const wrapper = sharedComposerSource.slice(
+ wrapperStart,
+ sharedComposerSource.indexOf(" }, [", wrapperStart),
+ );
+ assert.match(wrapper, /if \(audioUpload\.busy \|\| !chatActive\) return;/);
+ assert.match(wrapper, /currentDictationEntryMode\(\) === "recording-file"/);
+ assert.match(wrapper, /audioUpload\.openDialog\(\)/);
+ assert.match(wrapper, /startDictationSession\(\)/);
+ assert.match(sharedComposerSource, /onClick=\{startDictation\}/);
+
+ const shortcutStart = sharedComposerSource.indexOf(
+ 'useShortcut(\n "startDictation"',
+ );
+ const shortcut = sharedComposerSource.slice(
+ shortcutStart,
+ sharedComposerSource.indexOf("\n );", shortcutStart),
+ );
+ assert.match(shortcut, /startDictation\(\)/);
+});
+
+test("Compare keeps its current draft synchronous for transcript appends", () => {
+ assert.match(
+ sharedComposerSource,
+ /const setCurrentText = useCallback\([\s\S]*?typeof value === "function" \? value\(textRef\.current\) : value;[\s\S]*?textRef\.current = next;\s*setText\(next\);/,
+ );
+ assert.match(
+ sharedComposerSource,
+ /const writeAudioUploadDraft = useCallback\(\s*\(value: string\) => setCurrentText\(value\)/,
+ );
+ assert.match(
+ sharedComposerSource,
+ /onChange=\{\(e\) => \{[\s\S]*?setCurrentText\(e\.target\.value\);/,
+ );
+});
+
+test("the controlled dialog has separate Android recorder and saved-file inputs", () => {
+ assert.doesNotMatch(dialogSource, /DialogTrigger|Upload01Icon/);
+ assert.match(dialogSource, /accept="audio\/\*"\s*capture="user"/);
+ assert.match(dialogSource, /accept=\{AUDIO_PICKER_ACCEPT\}/);
+ const chooseInput = dialogSource.slice(
+ dialogSource.indexOf("ref={chooseInputRef}"),
+ );
+ assert.doesNotMatch(
+ chooseInput.slice(0, chooseInput.indexOf("/>")),
+ /capture=/,
+ );
+ assert.match(dialogSource, /platform === "android"/);
+ assert.match(dialogSource, /platform === "ios"/);
+});
+
+test("picker launch snapshots before the native input opens", () => {
+ assert.match(
+ dialogSource,
+ /if \(!input \|\| !audioUpload\.snapshotForPicker\(\)\) return;\s*input\.click\(\);/,
+ );
+ assert.match(
+ hookSource,
+ /pickerSnapshotRef\.current = snapshot;\s*return snapshot;/,
+ );
+ assert.match(
+ hookSource,
+ /const snapshot = pickerSnapshotRef\.current;\s*pickerSnapshotRef\.current = null;/,
+ );
+});
+
+test("Compare queue setup preserves an upload until a queued send is accepted", () => {
+ const advanceQueueStart = sharedComposerSource.indexOf(
+ "function advanceQueue() {",
+ );
+ const advanceQueue = sharedComposerSource.slice(
+ advanceQueueStart,
+ sharedComposerSource.indexOf("\n }", advanceQueueStart) + 4,
+ );
+ assert.match(
+ advanceQueue,
+ /setCurrentText\(next\);\s*setTimeout\(\(\) => \{ sendRef\.current\?\.\(\); \}, 100\);/,
+ );
+ assert.doesNotMatch(advanceQueue, /audioUpload\.cancel\(\)/);
+
+ const runListStart = sharedComposerSource.indexOf("onRunList={(items) => {");
+ const runList = sharedComposerSource.slice(
+ runListStart,
+ sharedComposerSource.indexOf(" }}", runListStart),
+ );
+ const incompleteModelGuard = runList.indexOf(
+ "if (hasCompareHandles && !isGeneralizedCompare)",
+ );
+ const replace = runList.indexOf("setCurrentText(filtered[0])");
+ assert.ok(incompleteModelGuard >= 0 && incompleteModelGuard < replace);
+ assert.doesNotMatch(runList, /audioUpload\.cancel\(\)/);
+});
+
+test("Compare cancels uploads only after a send reaches an accepted effect", () => {
+ const sendStart = sharedComposerSource.indexOf("async function send() {");
+ const send = sharedComposerSource.slice(
+ sendStart,
+ sharedComposerSource.indexOf("sendRef.current = send", sendStart),
+ );
+ const cancellations = [...send.matchAll(/audioUpload\.cancel\(\)/g)];
+ assert.equal(cancellations.length, 2);
+ for (const cancellation of cancellations) {
+ assert.match(
+ send.slice(cancellation.index, cancellation.index + 100),
+ /audioUpload\.cancel\(\);\s*clearSubmittedDraft\(\);/,
+ );
+ }
+ assert.doesNotMatch(
+ send.slice(0, send.indexOf("if (isGeneralizedCompare)")),
+ /audioUpload\.cancel\(\)/,
+ );
+});
diff --git a/studio/frontend/tests/composer-steering.test.ts b/studio/frontend/tests/composer-steering.test.ts
index 3d001ad8701..c36de12048c 100644
--- a/studio/frontend/tests/composer-steering.test.ts
+++ b/studio/frontend/tests/composer-steering.test.ts
@@ -486,7 +486,8 @@ async function targetForSelection(
function hydratedFactory(
w: ReturnType<typeof world>,
target: Target,
- hydrate = () => Promise.resolve(target),
+ hydrate: () => Promise<Target | null> = () => Promise.resolve(target),
+ cancelAudioUpload = () => undefined,
) {
const pending = new Map();
const deps = {
@@ -504,6 +505,7 @@ function hydratedFactory(
shouldAbortPendingQueueForModelBoundary,
shouldAbortPendingQueueForSettingsChange,
createPromptQueueTarget: hydrate,
+ cancelAudioUpload,
startPromptQueue: w.startPromptQueue,
toast: { error: (message: string) => assert.fail(message) },
};
@@ -523,6 +525,51 @@ function hydratedFactory(
) => boolean;
}
+test("an accepted hydrated queue cancels the composer audio upload", async () => {
+ const w = world();
+ const target = makeTarget("chat");
+ let cancellations = 0;
+ const accept = hydratedFactory(
+ w,
+ target,
+ () => Promise.resolve(target),
+ () => {
+ cancellations += 1;
+ },
+ );
+ assert.equal(accept(["queued draft"], true), true);
+ await Promise.resolve();
+ assert.equal(cancellations, 1);
+});
+
+test("an aborted hydrated queue leaves the composer audio upload active", async () => {
+ const w = world();
+ let cancellations = 0;
+ let aborted = 0;
+ const accept = hydratedFactory(
+ w,
+ makeTarget("chat"),
+ () => Promise.resolve(null),
+ () => {
+ cancellations += 1;
+ },
+ );
+ assert.equal(
+ accept(
+ ["queued draft"],
+ true,
+ undefined,
+ () => {
+ aborted += 1;
+ },
+ ),
+ true,
+ );
+ await Promise.resolve();
+ assert.equal(aborted, 1);
+ assert.equal(cancellations, 0);
+});
+
for (const firstBehavior of ["queue", "steer"] as const) {
for (const latestBehavior of ["queue", "steer"] as const) {
for (const latestWait of [true, false]) {
diff --git a/studio/frontend/tests/composer-submit-path.test.ts b/studio/frontend/tests/composer-submit-path.test.ts
index 997bbd15e84..0ed05feef83 100644
--- a/studio/frontend/tests/composer-submit-path.test.ts
+++ b/studio/frontend/tests/composer-submit-path.test.ts
@@ -43,6 +43,12 @@ const releaseCallback = lift(
.getText(source)
.includes("const behavior = pendingFollowUpBehaviorRef.current;"),
);
+const reservedSendCallback = lift(
+ (n) =>
+ ts.isArrowFunction(n) &&
+ n.getText(source).startsWith("(...alsoGuard: string[]) =>") &&
+ n.getText(source).includes("reservePreStreamRun"),
+);
function createCallback(
code: string,
deps: Record<string, unknown>,
@@ -145,12 +151,59 @@ for (const active of ["runtime", "pre-stream", "queue", "idle"]) {
release(); // An old render cannot release a cancelled/consumed send twice.
assert.deepEqual(
calls,
- active === "idle" ? ["clear", "send"] : [[active !== "queue", "steer"]],
+ active === "idle"
+ ? ["clear", "send"]
+ : [[active !== "queue", "steer"]],
);
assert.equal(pendingSendRef.current, false);
});
}
+test("main cancels an audio upload only after a normal send reservation succeeds", () => {
+ const run = (reservationToken: symbol | null) => {
+ const calls: string[] = [];
+ const deps = {
+ aui: {
+ threads: () => ({ __internal_getAssistantRuntime: () => undefined }),
+ composer: () => ({
+ getState: () => ({ text: "Draft" }),
+ send: () => calls.push("send"),
+ }),
+ },
+ reservePreStreamRun: () => reservationToken,
+ preStreamThreadIds: ["chat"],
+ parseExternalModelId: () => null,
+ useChatRuntimeStore: {
+ getState: () => ({
+ params: { checkpoint: "local/model" },
+ incognito: false,
+ activeGgufVariant: null,
+ }),
+ },
+ preStreamRunReservationRef: { current: null },
+ toast: { error: () => calls.push("refused") },
+ cancelAudioUpload: () => calls.push("cancel-upload"),
+ claimThreadCreation: () => calls.push("claim"),
+ projectScope: null,
+ armJustSent: () => calls.push("arm"),
+ releasePreStreamRunReservation: () => true,
+ notifyPromptQueueRunFailed: () => undefined,
+ referenceThreadId: "chat",
+ };
+ const send = createCallback(reservedSendCallback, deps);
+ send();
+ return calls;
+ };
+
+ assert.deepEqual(run(null), ["refused"]);
+ assert.deepEqual(run(Symbol("reservation")), [
+ "cancel-upload",
+ "claim",
+ "send",
+ "arm",
+ ]);
+});
+
test("main, edit and comparison composers use the setting and expose settings access", () => {
assert.match(text, /submitMode="none"/);
assert.match(
diff --git a/studio/frontend/tests/dictation-entry.test.ts b/studio/frontend/tests/dictation-entry.test.ts
new file mode 100644
index 00000000000..ba38a72f8fa
--- /dev/null
+++ b/studio/frontend/tests/dictation-entry.test.ts
@@ -0,0 +1,98 @@
+// 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 {
+ dictationEntryMode,
+ insecureDictationGuidance,
+ recordingPickerPlatform,
+} from "../src/features/chat/utils/dictation-entry.ts";
+
+test("ordinary HTTP LAN uses the recording-file fallback", () => {
+ assert.equal(
+ dictationEntryMode({
+ isSecureContext: false,
+ protocol: "http:",
+ hostname: "192.168.1.42",
+ }),
+ "recording-file",
+ );
+});
+
+test("HTTPS and loopback stay on live dictation", () => {
+ assert.equal(
+ dictationEntryMode({
+ isSecureContext: true,
+ protocol: "https:",
+ hostname: "studio.example.com",
+ }),
+ "live",
+ );
+ for (const hostname of [
+ "localhost",
+ "studio.localhost",
+ "127.0.0.1",
+ "::1",
+ "[::1]",
+ ]) {
+ assert.equal(
+ dictationEntryMode({
+ isSecureContext: false,
+ protocol: "http:",
+ hostname,
+ }),
+ "live",
+ );
+ }
+});
+
+test("a non-HTTP insecure context keeps the existing live error path", () => {
+ assert.equal(
+ dictationEntryMode({
+ isSecureContext: false,
+ protocol: "https:",
+ hostname: "studio.example.com",
+ }),
+ "live",
+ );
+ assert.equal(
+ insecureDictationGuidance("live"),
+ "Open Unsloth at http://127.0.0.1 (localhost) or over HTTPS to dictate.",
+ );
+ assert.doesNotMatch(insecureDictationGuidance("live"), /choose a recording/i);
+});
+
+test("only the HTTP fallback advertises the recording picker", () => {
+ assert.match(
+ insecureDictationGuidance("recording-file"),
+ /Press Dictate to choose a recording on this connection/,
+ );
+});
+
+test("only Android receives the recorder action", () => {
+ assert.equal(
+ recordingPickerPlatform({
+ userAgent: "Mozilla/5.0 (Linux; Android 15; Pixel 9)",
+ }),
+ "android",
+ );
+ assert.equal(
+ recordingPickerPlatform({
+ userAgent: "Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X)",
+ }),
+ "ios",
+ );
+ assert.equal(
+ recordingPickerPlatform({
+ userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",
+ platform: "MacIntel",
+ maxTouchPoints: 5,
+ }),
+ "ios",
+ );
+ assert.equal(
+ recordingPickerPlatform({ userAgent: "Mozilla/5.0 (Windows NT 10.0)" }),
+ "other",
+ );
+});
diff --git a/studio/frontend/tests/voice-stt-device-control.test.ts b/studio/frontend/tests/voice-stt-device-control.test.ts
index 7d89f969dc3..eb650d5cb75 100644
--- a/studio/frontend/tests/voice-stt-device-control.test.ts
+++ b/studio/frontend/tests/voice-stt-device-control.test.ts
@@ -34,5 +34,8 @@ test("the unload API still takes the engine and model that scoping needs", () =>
test("the device preference travels with every load and transcribe", () => {
// A load that omits it reads as "no opinion", so the setting never applies.
assert.match(adapter, /device: resolvedDevice/);
- assert.match(adapter, /params\.set\("device", settings\.sttDevice\)/);
+ assert.match(
+ adapter,
+ /params\.set\("device", options\.device \?\? settings\.sttDevice\)/,
+ );
});