diff --git a/src/main/speech/stt-service.ts b/src/main/speech/stt-service.ts index 6aa14d9e396..201e30451d1 100644 --- a/src/main/speech/stt-service.ts +++ b/src/main/speech/stt-service.ts @@ -16,7 +16,7 @@ import { teardownIdleSttEngine } from './stt-session-stop' -export { START_DICTATION_TIMEOUT_MS } from './stt-session-timeouts' +export { START_DICTATION_TIMEOUT_MS } from '../../shared/dictation-finish-deadlines' export type SttEvent = | { type: 'ready' } diff --git a/src/main/speech/stt-session-start.ts b/src/main/speech/stt-session-start.ts index 841396bf72a..293d30e0350 100644 --- a/src/main/speech/stt-session-start.ts +++ b/src/main/speech/stt-session-start.ts @@ -26,7 +26,7 @@ import { initializeSttWorker, waitForSttWorkerReady } from './stt-worker-startup' -import { START_DICTATION_TIMEOUT_MS } from './stt-session-timeouts' +import { START_DICTATION_TIMEOUT_MS } from '../../shared/dictation-finish-deadlines' import { STT_WORKER_EXITED_ERROR } from './stt-worker-process' export async function startSttDictation( diff --git a/src/main/speech/stt-session-timeouts.ts b/src/main/speech/stt-session-timeouts.ts deleted file mode 100644 index 1e9b06f7e10..00000000000 --- a/src/main/speech/stt-session-timeouts.ts +++ /dev/null @@ -1 +0,0 @@ -export const START_DICTATION_TIMEOUT_MS = 60_000 diff --git a/src/main/speech/stt-worker.ts b/src/main/speech/stt-worker.ts index 6922c958303..dd1fa95debf 100644 --- a/src/main/speech/stt-worker.ts +++ b/src/main/speech/stt-worker.ts @@ -9,6 +9,8 @@ import type { SttWorkerRequest } from './stt-worker-process' type AudioPortInput = { port: MessagePortMain onEnded: (() => void) | null + /** 'end' arrived; its frames may still wait behind a decode. */ + ending: boolean } const parentPort = connectSttWorkerParentPort() @@ -46,8 +48,7 @@ function attachAudioPort(streamId: string, port: MessagePortMain | undefined): v if (!port) { return } - const input: AudioPortInput = { port, onEnded: null } - // The renderer closes its end after 'end'; a close alone means it went away. + const input: AudioPortInput = { port, onEnded: null, ending: false } const end = (): void => { if (audioPorts.get(streamId) !== input) { return @@ -59,6 +60,7 @@ function attachAudioPort(streamId: string, port: MessagePortMain | undefined): v port.on('message', ({ data }) => { // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: only the desktop preload holds the other end, and it sends SpeechAudioPortMessage. const msg = data as SpeechAudioPortMessage + input.ending ||= msg.type === 'end' enqueue(() => { // A discard or end ran first; a late frame must not reopen the stream. if (audioPorts.get(streamId) !== input) { @@ -69,7 +71,13 @@ function attachAudioPort(streamId: string, port: MessagePortMain | undefined): v : handleFeed({ type: 'feed', streamId, samples: msg.samples, sampleRate: msg.sampleRate }) }) }) - port.on('close', end) + // The renderer closes its end right after 'end', so that close must leave the queued frames to decode. + // A close alone means it canceled or went away. + port.on('close', () => { + if (!input.ending) { + end() + } + }) audioPorts.set(streamId, input) port.start() } diff --git a/src/renderer/src/hooks/use-audio-capture.ts b/src/renderer/src/hooks/use-audio-capture.ts index b98211188d4..f6b965ff8c3 100644 --- a/src/renderer/src/hooks/use-audio-capture.ts +++ b/src/renderer/src/hooks/use-audio-capture.ts @@ -8,6 +8,7 @@ import { } from '@/components/dictation/dictation-capture-worklet-protocol' import { DICTATION_SAMPLE_RATE } from '../../../shared/rpc-contract/speech-params' import { DICTATION_METER_WINDOW_MS } from '../../../shared/dictation-audio-meter' +import { START_DICTATION_TIMEOUT_MS } from '../../../shared/dictation-finish-deadlines' import { useAudioCaptureMeter, type DictationMeterPublisher } from './use-audio-capture-meter' import { useAudioCaptureStopTail, type StopCaptureOptions } from './use-audio-capture-stop-tail' @@ -35,7 +36,8 @@ export type StartAudioCaptureResult = { fellBackToDefaultMicrophone: boolean } -const MAX_BUFFERED_AUDIO_SECONDS = 30 +// Holds everything said while the model loads, for as long as the host waits for it to load. +const MAX_BUFFERED_AUDIO_SECONDS = START_DICTATION_TIMEOUT_MS / 1000 const MAX_BUFFERED_AUDIO_BYTES = 8 * 1024 * 1024 const CAPTURE_WORKLET_OPTIONS: DictationCaptureWorkletOptions = { outputSampleRate: DICTATION_SAMPLE_RATE, diff --git a/src/shared/dictation-finish-deadlines.ts b/src/shared/dictation-finish-deadlines.ts index e355e364d1b..c5f32f00fe6 100644 --- a/src/shared/dictation-finish-deadlines.ts +++ b/src/shared/dictation-finish-deadlines.ts @@ -7,6 +7,8 @@ const CLOUD_RESPONSE_ALLOWANCE_MS = 30_000 const UPLOAD_BYTES_PER_MS = 125 export const LOCAL_DICTATION_STOP_TIMEOUT_MS = 60_000 +// How long an on-device model may take to load before its dictation fails. +export const START_DICTATION_TIMEOUT_MS = 60_000 // The host's answer still has to cross the network after its own deadline. const PAIRED_CLIENT_REPLY_MARGIN_MS = 15_000