diff --git a/apps/web/src/components/settings/MicrophoneTest.tsx b/apps/web/src/components/settings/MicrophoneTest.tsx index 23f1b29e86062..d3597e2dea670 100644 --- a/apps/web/src/components/settings/MicrophoneTest.tsx +++ b/apps/web/src/components/settings/MicrophoneTest.tsx @@ -3,6 +3,7 @@ import { CheckIcon, MicIcon, PauseIcon, PlayIcon, RotateCcwIcon, XIcon } from "l import { useEffect, useRef, useState } from "react"; import { useStableCallback } from "~/hooks/useStableCallback"; +import workletUrl from "../../speech/pcmWorklet.ts?worker&url"; import { VoiceInputErrorPill } from "../chat/VoiceInputErrorPill"; import { VoiceInputPill, VoiceWaveform } from "../chat/VoiceInputPill"; import { Button } from "../ui/button"; @@ -15,8 +16,9 @@ type Capture = { stream?: MediaStream; recorder?: MediaRecorder; context?: AudioContext; + worklet?: AudioWorkletNode; timer?: ReturnType; - meter?: ReturnType; + elapsedTimer?: ReturnType; startedAt?: number; }; @@ -39,7 +41,9 @@ export function MicrophoneTest({ microphoneId }: { microphoneId: string }) { if (!current) return; capture.current = null; clearTimeout(current.timer); - clearInterval(current.meter); + clearInterval(current.elapsedTimer); + current.worklet?.disconnect(); + current.worklet?.port.close(); if (current.recorder && current.recorder.state !== "inactive") current.recorder.stop(); current.stream?.getTracks().forEach((track) => track.stop()); void current.context?.close().catch(() => undefined); @@ -66,7 +70,11 @@ export function MicrophoneTest({ microphoneId }: { microphoneId: string }) { setLevel(0); setElapsed(0); setPhase("starting"); - if (!navigator.mediaDevices?.getUserMedia || typeof MediaRecorder === "undefined") { + if ( + !navigator.mediaDevices?.getUserMedia || + typeof MediaRecorder === "undefined" || + typeof AudioWorkletNode === "undefined" + ) { setError("Microphone testing is unavailable in this browser. Use HTTPS or the desktop app."); setPhase("idle"); return; @@ -104,13 +112,14 @@ export function MicrophoneTest({ microphoneId }: { microphoneId: string }) { setRecording({ url: recordingUrl.current, duration }); setPhase("ready"); }); - recorder.addEventListener("error", () => { + const onRecordingError = () => { if (capture.current !== current) return; releaseCapture(); setLevel(0); setError("Could not record microphone audio. Try again."); setPhase("idle"); - }); + }; + recorder.addEventListener("error", onRecordingError); for (const track of stream.getAudioTracks()) { track.addEventListener( "ended", @@ -124,23 +133,28 @@ export function MicrophoneTest({ microphoneId }: { microphoneId: string }) { } const context = new AudioContext(); current.context = context; - const analyser = context.createAnalyser(); - analyser.fftSize = 256; - context.createMediaStreamSource(stream).connect(analyser); + await context.audioWorklet.addModule(workletUrl); + if (capture.current !== current) return; + const worklet = new AudioWorkletNode(context, "t3-pcm-capture", { + channelCount: 1, + processorOptions: { meterOnly: true }, + }); + current.worklet = worklet; + worklet.port.onmessage = ({ data }: MessageEvent) => { + if (capture.current === current && typeof data === "number") setLevel(data); + }; + worklet.onprocessorerror = onRecordingError; + context.createMediaStreamSource(stream).connect(worklet); + worklet.connect(context.destination); await context.resume(); if (capture.current !== current) return; - const samples = new Float32Array(analyser.fftSize); const startedAt = performance.now(); current.startedAt = startedAt; - current.meter = setInterval(() => { + current.elapsedTimer = setInterval(() => { setElapsed(Math.min(LOOPBACK_SECONDS, Math.floor((performance.now() - startedAt) / 1_000))); - analyser.getFloatTimeDomainData(samples); - const rms = Math.sqrt( - samples.reduce((sum, value) => sum + value * value, 0) / samples.length, - ); - setLevel(Math.min(1, rms * 5)); }, 100); recorder.start(); + worklet.port.postMessage("start", []); setPhase("recording"); current.timer = setTimeout(() => { if (recorder.state !== "inactive") recorder.stop(); diff --git a/apps/web/src/components/settings/VoiceSettingsPanel.browser.test.tsx b/apps/web/src/components/settings/VoiceSettingsPanel.browser.test.tsx index 59a46ec99570a..551b091d7a009 100644 --- a/apps/web/src/components/settings/VoiceSettingsPanel.browser.test.tsx +++ b/apps/web/src/components/settings/VoiceSettingsPanel.browser.test.tsx @@ -1,10 +1,17 @@ import { PrimaryConnectionTarget } from "@t3tools/client-runtime/connection"; -import { EnvironmentId, type EnvironmentSpeechModel } from "@t3tools/contracts"; +import { + EnvironmentId, + type EnvironmentSpeechModel, + type EnvironmentSpeechStatus, +} from "@t3tools/contracts"; import { expect, it, vi } from "vite-plus/test"; import { userEvent } from "vite-plus/test/browser"; import { render } from "../../../test/render"; import { __setClientSettingsForTests, getClientSettings } from "../../hooks/useSettings"; -import { useVoiceTranscriptionDestination } from "../../speech/useEnvironmentSpeechInput"; +import { + useEnvironmentSpeechInput, + useVoiceTranscriptionDestination, +} from "../../speech/useEnvironmentSpeechInput"; import { chooseAudioDestination, chooseTranscriptCleanup } from "../../speech/voiceConsent"; import { toastManager } from "../ui/toast"; import { SettingsScopeProvider } from "./SettingsScopeContext"; @@ -13,6 +20,111 @@ import { VoiceSettingsPanel } from "./VoiceSettingsPanel"; vi.mock(import("../../speech/useEnvironmentSpeechInput"), { spy: true }); +it("preserves the model test transcript when its model is filtered out", async () => { + const target = new PrimaryConnectionTarget({ + environmentId: EnvironmentId.make("voice-filter"), + label: "Test environment", + httpBaseUrl: window.location.origin, + wsBaseUrl: "ws://localhost", + }); + const prepared = { ...target, target, socketUrl: target.wsBaseUrl, httpAuthorization: null }; + const model: EnvironmentSpeechModel = { + id: "tiny", + name: "Tiny", + description: "", + size: 100, + languages: ["en"], + accuracy: 1, + speed: 1, + recommended: false, + supportsStreaming: false, + supportsTranslation: false, + supportsLanguageDetection: false, + active: true, + state: "installed", + }; + const status = { + supported: true, + state: "ready", + modelId: model.id, + model: model.name, + size: model.size, + supportsStreaming: false, + supportsTranslation: false, + acceleration: "cpu", + modelUnloadTimeout: "never", + gpuDevices: [], + } satisfies EnvironmentSpeechStatus; + const fetch = globalThis.fetch; + using _network = vi.spyOn(globalThis, "fetch").mockImplementation((input, init) => { + const url = new URL(input instanceof Request ? input.url : input, window.location.href); + if (url.pathname === "/api/voice/status") return Promise.resolve(Response.json(status)); + if (url.pathname === "/api/voice/models") + return Promise.resolve(Response.json({ models: [model] })); + return fetch(input, init); + }); + const transcript = "Keep this transcript."; + await vi.mocked(useVoiceTranscriptionDestination).withImplementation( + () => ({ environmentId: target.environmentId, label: target.label, grantId: "test", prepared }), + async () => { + await vi.mocked(useEnvironmentSpeechInput).withImplementation( + (input) => ({ + available: true, + status, + transcriptionEnvironmentId: target.environmentId, + transcriptionEnvironmentLabel: target.label, + setup: { + open: false, + step: 0, + model: null, + downloading: false, + error: null, + setOpen: () => {}, + download: async () => {}, + cancelDownload: async () => {}, + startRecording: async () => {}, + }, + state: { phase: "idle", error: null, errorAction: null }, + preview: null, + level: 0, + preparing: false, + blocksSubmission: false, + freezesEditor: false, + start: async () => { + input.commitDraft(transcript, { start: transcript.length, end: transcript.length }); + }, + submitAfterDictation: () => {}, + stop: async () => {}, + cancel: () => {}, + skipPostProcessing: () => {}, + }), + async () => { + await using screen = await render( + {}}> + + , + ); + const testModel = screen.getByRole("button", { name: "Test model", exact: true }); + await expect.element(testModel).toBeVisible(); + await userEvent.click(testModel); + const result = screen.getByRole("status", { name: "Test transcript" }); + await expect.element(result).toHaveTextContent(transcript); + + const search = screen.getByRole("textbox", { name: "Search transcription models" }); + await userEvent.fill(search, "no matching model"); + await expect.element(screen.getByText("No matching models.")).toBeVisible(); + await userEvent.clear(search); + await vi.waitFor(() => + expect( + screen.container.querySelector('[aria-label="Test transcript"]')?.textContent, + ).toBe(transcript), + ); + }, + ); + }, + ); +}); + it("recovers from a failed download progress poll without notifying, but reports a failed initial load", async () => { const target = new PrimaryConnectionTarget({ environmentId: EnvironmentId.make("voice-progress"), diff --git a/apps/web/src/components/settings/VoiceSettingsPanel.tsx b/apps/web/src/components/settings/VoiceSettingsPanel.tsx index 75823d74f2977..cf99502a74682 100644 --- a/apps/web/src/components/settings/VoiceSettingsPanel.tsx +++ b/apps/web/src/components/settings/VoiceSettingsPanel.tsx @@ -34,7 +34,7 @@ import { Trash2Icon, XIcon, } from "lucide-react"; -import { useEffect, useRef, useState } from "react"; +import { useEffect, useRef, useState, type ReactNode } from "react"; import { useStableCallback } from "~/hooks/useStableCallback"; import { listBrowserMicrophones, type BrowserMicrophone } from "../../speech/browserMicrophones"; @@ -57,7 +57,7 @@ import { useEnvironmentSpeechInput, useVoiceTranscriptionDestination, } from "../../speech/useEnvironmentSpeechInput"; -import { ComposerSpeechButton, ComposerSpeechRecordingPill } from "../chat/ComposerSpeechButton"; +import { ComposerSpeechRecordingPill } from "../chat/ComposerSpeechButton"; import { ProviderModelPicker } from "../chat/ProviderModelPicker"; import { TraitsPicker } from "../chat/TraitsPicker"; import { VoiceInputSetup } from "../chat/VoiceInputSetup"; @@ -75,6 +75,7 @@ import { SettingResetButton, SettingsPageContainer, SettingsRow, + SettingsSearchTarget, SettingsSection, } from "./settingsLayout"; import { searchableSetting } from "./settingsSearch"; @@ -113,7 +114,7 @@ const modelSortOrder = (model: EnvironmentSpeechModel) => { return model.recommended ? 3 : 4; }; -function ModelCard(props: { +type ModelCardProps = { readonly model: EnvironmentSpeechModel; readonly busy: boolean; readonly canCancel: boolean; @@ -121,12 +122,16 @@ function ModelCard(props: { readonly onSelect: () => void; readonly onCancel: () => void; readonly onDelete: () => void; -}) { + readonly testControls?: ReactNode; + readonly children?: ReactNode; +}; + +function ModelCard(props: ModelCardProps) { const { model } = props; const downloading = model.state === "downloading" || model.state === "verifying"; const progress = model.downloaded === undefined ? 0 : (model.downloaded / model.size) * 100; return ( -
+
{model.name} @@ -180,7 +185,8 @@ function ModelCard(props: {
) : null}
-
+
+ {props.testControls} {downloading ? (
+ {props.children}
); } @@ -485,6 +492,9 @@ function EnvironmentModelsSection(props: { readonly prepared: PreparedConnection; readonly status: EnvironmentSpeechStatus | null; readonly onStatus: (connection: PreparedConnection, status: EnvironmentSpeechStatus) => void; + readonly testEnvironmentId: EnvironmentId; + readonly projectId: ProjectId | null; + readonly projectName: string | null; }) { const { prepared, status, onStatus } = props; const canManage = useEnvironmentScope(prepared.environmentId, AuthSettingsWriteScope); @@ -586,6 +596,11 @@ function EnvironmentModelsSection(props: { (!query || (model.name + " " + model.description).toLocaleLowerCase().includes(query)), ) .toSorted((left, right) => modelSortOrder(left) - modelSortOrder(right)); + const activeModel = models.find((model) => model.active); + const renderedModels = + activeModel && !sortedModels.includes(activeModel) + ? [activeModel, ...sortedModels] + : sortedModels; return ( @@ -670,7 +685,7 @@ function EnvironmentModelsSection(props: { +
No matching models.

) : null}
- {sortedModels.map((model) => ( - { + const card: ModelCardProps = { + model: model.id === requestedDownload && model.state === "downloadable" ? { ...model, state: "downloading" } - : model - } - busy={controlsDisabled} - canCancel={canManage && !busy} - onDownload={() => + : model, + busy: controlsDisabled, + canCancel: canManage && !busy, + onDownload: () => void run( "Could not download the model", () => runtime.runPromise(downloadEnvironmentSpeechModel(prepared, model.id)), model.id, - ) - } - onSelect={() => + ), + onSelect: () => void run("Could not select the model", () => runtime.runPromise(selectEnvironmentSpeechModel(prepared, model.id)), - ) - } - onCancel={() => + ), + onCancel: () => void run("Could not cancel the download", () => runtime.runPromise(cancelEnvironmentSpeechModelDownload(prepared, model.id)), - ) - } - onDelete={() => + ), + onDelete: () => void run("Could not delete the model", () => runtime.runPromise(removeEnvironmentSpeechModel(prepared, model.id)), - ) - } - /> - ))} + ), + }; + return model.active ? ( +
) : null} @@ -748,12 +769,15 @@ function EnvironmentModelsSection(props: { * never the cleanup provider: this checks the model, the loopback test checks the mic. */ function TranscriptionTest(props: { + readonly card: ModelCardProps; + readonly hidden: boolean; readonly environmentId: EnvironmentId; readonly projectId: ProjectId | null; readonly projectName: string | null; - readonly modelName: string | null; }) { - const [draft, setDraft] = useState(""); + const [transcript, setTranscript] = useState(null); + const draft = transcript ?? ""; + const [showTranscript, setShowTranscript] = useState(false); const draftRef = useRef(draft); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- publishes the committed draft to the asynchronous transcription controller useEffect(() => { @@ -770,54 +794,85 @@ function TranscriptionTest(props: { text: draftRef.current, selection: { start: draftRef.current.length, end: draftRef.current.length }, }), - commitDraft: (text) => setDraft(text), + commitDraft: (text) => setTranscript(text), }); return ( - - void speech.start()} - onStop={() => void speech.stop()} - onCancel={() => speech.cancel()} - onSkipPostProcessing={() => speech.skipPostProcessing()} - /> - {speech.available ? ( -
- } - > - {speech.preview ? ( -

- {speech.preview.committed} - {speech.preview.tentative} -

- ) : null} -