diff --git a/src/main/speech/model-manager.test.ts b/src/main/speech/model-manager.test.ts index ddcd490e104..d4942058d35 100644 --- a/src/main/speech/model-manager.test.ts +++ b/src/main/speech/model-manager.test.ts @@ -1,4 +1,4 @@ -import { mkdirSync, mkdtempSync, rmSync, truncateSync, writeFileSync } from 'node:fs' +import { existsSync, mkdirSync, mkdtempSync, rmSync, truncateSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' import { beforeEach, describe, expect, it, vi } from 'vitest' @@ -76,6 +76,58 @@ describe('ModelManager', () => { } }) + it('keeps the old files after a cancelled or failed update, so the update is offered again', async () => { + const dir = mkdtempSync(join(tmpdir(), 'orca-model-manager-')) + try { + const modelId = 'parakeet-tdt-0.6b-v2-int8' + const manager = new ModelManager(dir, openElectronSpeechDownloadRequest) + const modelDir = manager.getModelDir(modelId) + mkdirSync(modelDir, { recursive: true }) + for (const [name, size] of [ + ['encoder.int8.onnx', 652_184_296], + ['decoder.int8.onnx', 7_257_753], + ['joiner.int8.onnx', 1_739_080], + ['tokens.txt', 9_384] + ] as const) { + writeFileSync(join(modelDir, name), '') + truncateSync(join(modelDir, name), size) + } + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the test stubs the protected file download step. + const internals = manager as unknown as { + downloadModelFiles: ( + manifest: unknown, + stagingDir: string, + modelId: string, + isAborted: () => boolean, + signal: AbortSignal + ) => Promise + } + + internals.downloadModelFiles = (_manifest, _stagingDir, _modelId, _isAborted, signal) => + new Promise((_, reject) => { + signal.addEventListener('abort', () => reject(new Error('aborted'))) + }) + const cancelled = manager.downloadModel(modelId) + manager.cancelDownload(modelId) + await expect(cancelled).resolves.toBeUndefined() + expect((await manager.getModelState(modelId)).needsUpdate).toBe(true) + + const failed = new ModelManager(dir, openElectronSpeechDownloadRequest) + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: as above. + ;(failed as unknown as typeof internals).downloadModelFiles = async () => { + throw new Error('network down') + } + await expect(failed.downloadModel(modelId)).rejects.toThrow('network down') + expect(existsSync(`${modelDir}.partial`)).toBe(false) + const state = await new ModelManager(dir, openElectronSpeechDownloadRequest).getModelState( + modelId + ) + expect(state.needsUpdate).toBe(true) + } finally { + rmSync(dir, { recursive: true, force: true }) + } + }) + it('aborts an in-flight model download request when cancelled', async () => { const dir = mkdtempSync(join(tmpdir(), 'orca-model-manager-')) try { diff --git a/src/main/speech/model-manager.ts b/src/main/speech/model-manager.ts index 8e530065f6d..4e35bd631c8 100644 --- a/src/main/speech/model-manager.ts +++ b/src/main/speech/model-manager.ts @@ -184,6 +184,8 @@ export class ModelManager extends SpeechModelDownloadTransport { } } this.activeDownloads.set(modelId, handle) + // Until the swap starts, modelDir holds only the old files, which are how the update is found again. + let replacingModelDir = false try { await this.downloadModelFiles( @@ -198,6 +200,7 @@ export class ModelManager extends SpeechModelDownloadTransport { return } + replacingModelDir = true await rm(modelDir, { recursive: true, force: true }) await rename(stagingDir, modelDir) this.updateState(modelId, 'ready') @@ -209,7 +212,9 @@ export class ModelManager extends SpeechModelDownloadTransport { console.error('[speech] Model download failed:', modelId, err) this.updateState(modelId, 'error', undefined, String(err)) } - removeModelDownloadFiles(modelDir, stagingDir, legacyArchivePath) + if (replacingModelDir) { + removeModelDownloadFiles(modelDir, stagingDir, legacyArchivePath) + } if (!aborted) { // Settings awaits real failures; cancellation stays quiet. throw err diff --git a/src/renderer/src/components/dictation/DictationController.tsx b/src/renderer/src/components/dictation/DictationController.tsx index 9a69443b4a8..b10035c6acd 100644 --- a/src/renderer/src/components/dictation/DictationController.tsx +++ b/src/renderer/src/components/dictation/DictationController.tsx @@ -76,8 +76,8 @@ export function DictationController() { // A new recording gets a fresh pill (timer and all); a retry keeps the one it failed in. const [recordingKey, setRecordingKey] = useState(0) const isIdle = useCallback(() => sessionRef.current.phase === 'idle', []) - const { setupTarget, closeSetup, invalidatePendingStart, readyToRecord } = - useDictationSpeechModelSetup(isIdle, setPillAnchor) + const { setupTarget, closeSetup, hasPendingStart, invalidatePendingStart, readyToRecord } = + useDictationSpeechModelSetup(settings?.voice, isIdle, setPillAnchor) const queuedSend = useDictationQueuedSend(sessionRef, keptRef) const { clear: clearQueuedSend, release: releaseQueuedSend } = queuedSend @@ -357,12 +357,17 @@ export function DictationController() { }, [settings, dispatch, finishDictationSession, showFailure]) const toggleDictation = useCallback(() => { + // A second press while the model check runs takes the first one back. + if (hasPendingStart()) { + invalidatePendingStart() + return + } if (isDictationUnderway(sessionRef.current.phase)) { void stopDictation() } else { void startDictation() } - }, [startDictation, stopDictation]) + }, [startDictation, stopDictation, hasPendingStart, invalidatePendingStart]) useDictationControls(settings?.voice, dictationState, { start: () => void startDictation(), @@ -373,7 +378,9 @@ export function DictationController() { enterQueues: (event) => queuedSend.queueTerminalEnter(event.target), // The error pill leaves Esc to Settings and dialogs; only its ✕ discards the recording. escCancels: () => - isDictationUnderway(sessionRef.current.phase) || sessionRef.current.phase === 'stopping' + hasPendingStart() || + isDictationUnderway(sessionRef.current.phase) || + sessionRef.current.phase === 'stopping' }) const { isHolding } = useHoldDictationGesture({ diff --git a/src/renderer/src/components/dictation/dictation-speech-model-setup.test.tsx b/src/renderer/src/components/dictation/dictation-speech-model-setup.test.tsx index b0d5d96ba27..4e8cbacae51 100644 --- a/src/renderer/src/components/dictation/dictation-speech-model-setup.test.tsx +++ b/src/renderer/src/components/dictation/dictation-speech-model-setup.test.tsx @@ -123,4 +123,68 @@ describe('dictation shortcut and the speech model update', () => { expect(startCapture).toHaveBeenCalledOnce() expect(startDictation).toHaveBeenCalledWith('parakeet', {}, expect.any(String)) }) + + it('drops a press whose model check is still running when the press is taken back', async () => { + modelStates = [{ id: 'parakeet', status: 'ready' }] + let releaseStates = (): void => {} + getModelStates.mockImplementationOnce( + () => new Promise((resolve) => (releaseStates = () => resolve(modelStates))) + ) + await mountAndPress() + // A second toggle cancels instead of launching another check. + await act(async () => pressShortcut()) + await act(async () => releaseStates()) + + expect(getModelStates).toHaveBeenCalledOnce() + expect(startCapture).not.toHaveBeenCalled() + }) + + it('lets Escape cancel a press whose model check is still running', async () => { + modelStates = [{ id: 'parakeet', status: 'ready' }] + let releaseStates = (): void => {} + getModelStates.mockImplementationOnce( + () => new Promise((resolve) => (releaseStates = () => resolve(modelStates))) + ) + await mountAndPress() + await act(async () => { + document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true })) + }) + await act(async () => releaseStates()) + + expect(startCapture).not.toHaveBeenCalled() + }) + + it('never opens the mic when voice is turned off while the check runs', async () => { + modelStates = [{ id: 'parakeet', status: 'ready' }] + let releaseStates = (): void => {} + getModelStates.mockImplementationOnce( + () => new Promise((resolve) => (releaseStates = () => resolve(modelStates))) + ) + await mountAndPress() + await act(async () => root.render(null)) + await act(async () => releaseStates()) + + expect(startCapture).not.toHaveBeenCalled() + expect(startDictation).not.toHaveBeenCalled() + }) + + it('keeps the setup pill, not the mic, when pressed mid-download', async () => { + modelStates = [{ id: 'parakeet', status: 'downloading', progress: 0.4 }] + await mountAndPress() + await act(async () => pressShortcut()) + + expect(document.querySelector('[data-testid="dictation-setup-pill"]')).not.toBeNull() + expect(startCapture).not.toHaveBeenCalled() + }) + + it('offers the update again after a download was cancelled', async () => { + modelStates = [{ id: 'parakeet', status: 'not-downloaded', needsUpdate: true }] + await mountAndPress() + // The cancelled download's state lost its needsUpdate. + modelStates = [{ id: 'parakeet', status: 'not-downloaded' }] + await act(async () => pressShortcut()) + + expect(document.querySelector('[data-testid="dictation-setup-pill"]')).not.toBeNull() + expect(startCapture).not.toHaveBeenCalled() + }) }) diff --git a/src/renderer/src/components/dictation/speech-model-update-check.ts b/src/renderer/src/components/dictation/speech-model-update-check.ts index 8f11c721f80..b781c271abd 100644 --- a/src/renderer/src/components/dictation/speech-model-update-check.ts +++ b/src/renderer/src/components/dictation/speech-model-update-check.ts @@ -3,6 +3,7 @@ import { isPairedWebClientWindow } from '@/lib/desktop-window-chrome' import type { SpeechModelManifest, VoiceSettings } from '../../../../shared/speech-types' import { findSpeechModelUpdateTarget, + isSpeechModelReady, type SpeechModelUpdateTarget } from './speech-model-update-target' @@ -21,27 +22,56 @@ export function loadSpeechCatalog(): Promise { return catalogRequest } -// A model found up to date stays so: legacy or retired files never come back, so later presses skip the check. -const upToDateModelIds = new Set() +// Only a model seen ready stays so: legacy or retired files never come back, so later presses skip the check. +const readyModelIds = new Set() +// What the pill offered stays offered until the model is ready, so a cancelled or failed download can resume. +const offeredTargets = new Map() +// A press during a prefetch joins it instead of asking main again. +const inFlightChecks = new Map>() + +type CheckedVoice = Pick + +/** True when no check is needed before recording with this model. */ +export function isSpeechModelKnownReady(voice: CheckedVoice): boolean { + return !voice.enabled || readyModelIds.has(voice.sttModel) || isPairedWebClientWindow() +} /** The download the selected model needs before it can dictate, checked before any mic capture. */ -export async function checkSpeechModelUpdate( - voice: Pick +export function checkSpeechModelUpdate( + voice: CheckedVoice ): Promise { // Why skip paired web clients: the models live on the host, whose own window shows this. - if (!voice.enabled || upToDateModelIds.has(voice.sttModel) || isPairedWebClientWindow()) { - return null + if (isSpeechModelKnownReady(voice)) { + return Promise.resolve(null) } + const modelId = voice.sttModel + let check = inFlightChecks.get(modelId) + if (!check) { + check = runCheck(voice).finally(() => inFlightChecks.delete(modelId)) + inFlightChecks.set(modelId, check) + } + return check +} + +async function runCheck(voice: CheckedVoice): Promise { try { const [catalog, states] = await Promise.all([ loadSpeechCatalog(), window.api.speech.getModelStates() ]) useAppStore.getState().setModelStates(states) - const target = findSpeechModelUpdateTarget(voice, catalog, states) - // An empty catalog proves nothing, so it isn't remembered as up to date. - if (!target && catalog.length > 0) { - upToDateModelIds.add(voice.sttModel) + const selected = states.find((state) => state.id === voice.sttModel) + if (isSpeechModelReady(selected)) { + readyModelIds.add(voice.sttModel) + offeredTargets.delete(voice.sttModel) + return null + } + const target = + findSpeechModelUpdateTarget(voice, catalog, states) ?? + offeredTargets.get(voice.sttModel) ?? + null + if (target) { + offeredTargets.set(voice.sttModel, target) } return target } catch { @@ -52,5 +82,7 @@ export async function checkSpeechModelUpdate( export function resetSpeechModelUpdateCheckForTests(): void { catalogRequest = null - upToDateModelIds.clear() + readyModelIds.clear() + offeredTargets.clear() + inFlightChecks.clear() } diff --git a/src/renderer/src/components/dictation/speech-model-update-target.ts b/src/renderer/src/components/dictation/speech-model-update-target.ts index 4b948a07b7d..ec9982fa058 100644 --- a/src/renderer/src/components/dictation/speech-model-update-target.ts +++ b/src/renderer/src/components/dictation/speech-model-update-target.ts @@ -33,5 +33,18 @@ export function findSpeechModelUpdateTarget( const retired = catalog.find( (m) => m.replacedBy === model.id && stateOf(m.id)?.status === 'ready' ) - return retired ? { model, retired } : null + if (retired) { + return { model, retired } + } + // A download in flight has no needsUpdate, yet the model still can't dictate; the pill shows its progress. + return isSpeechModelDownloading(selected) ? { model } : null +} + +export function isSpeechModelDownloading(state: SpeechModelState | undefined): boolean { + return state?.status === 'downloading' || state?.status === 'extracting' +} + +/** Only a ready model with no pending update is known to dictate. */ +export function isSpeechModelReady(state: SpeechModelState | undefined): boolean { + return state?.status === 'ready' && !state.needsUpdate } diff --git a/src/renderer/src/components/dictation/use-dictation-speech-model-setup.ts b/src/renderer/src/components/dictation/use-dictation-speech-model-setup.ts index 6206e75287b..cdcf53ec4fc 100644 --- a/src/renderer/src/components/dictation/use-dictation-speech-model-setup.ts +++ b/src/renderer/src/components/dictation/use-dictation-speech-model-setup.ts @@ -1,17 +1,24 @@ -import { useCallback, useRef, useState } from 'react' +import { useCallback, useEffect, useRef, useState } from 'react' +import { useAppStore } from '@/store' import type { VoiceSettings } from '../../../../shared/speech-types' import { captureInsertionTarget } from './dictation-insertion-target' import { dictationPillAnchorFor, type DictationPillAnchor } from './dictation-pill-anchor' -import { checkSpeechModelUpdate } from './speech-model-update-check' +import { checkSpeechModelUpdate, isSpeechModelKnownReady } from './speech-model-update-check' import type { SpeechModelUpdateTarget } from './speech-model-update-target' +type CheckedVoice = Pick + +const PREFETCH_DELAY_MS = 1500 + /** The pill's offer of the download the selected model needs, made in place of recording. */ export function useDictationSpeechModelSetup( + voice: CheckedVoice | undefined, isIdle: () => boolean, setPillAnchor: (anchor: DictationPillAnchor | null) => void ) { const [setupTarget, setSetupTarget] = useState(null) - // A stop or cancel while the check runs means that press no longer wants a recording. + // The press whose model check is still running; a stop, cancel, toggle or teardown drops it. + const pendingStartRef = useRef(null) const startRequestRef = useRef(0) const closeSetup = useCallback(() => { @@ -21,18 +28,49 @@ export function useDictationSpeechModelSetup( } }, [isIdle, setPillAnchor]) + const hasPendingStart = useCallback(() => pendingStartRef.current !== null, []) + const invalidatePendingStart = useCallback(() => { startRequestRef.current += 1 + pendingStartRef.current = null }, []) + // An unmounted controller (voice turned off) must never open the mic from a check it started. + useEffect(() => invalidatePendingStart, [invalidatePendingStart]) + + // Warms the check off the startup path, so the first press records without waiting on main. + const enabled = voice?.enabled === true + const sttModel = voice?.sttModel + useEffect(() => { + if (!enabled || !sttModel || isSpeechModelKnownReady({ enabled, sttModel })) { + return + } + const timer = window.setTimeout( + () => void checkSpeechModelUpdate({ enabled, sttModel }), + PREFETCH_DELAY_MS + ) + return () => window.clearTimeout(timer) + }, [enabled, sttModel]) + /** Checked before the mic opens, so a model the engine can't load never records; true to go on. */ const readyToRecord = useCallback( - async (voice: Pick): Promise => { + async (requested: CheckedVoice): Promise => { + if (isSpeechModelKnownReady(requested)) { + setSetupTarget(null) + return true + } const request = ++startRequestRef.current - const target = await checkSpeechModelUpdate(voice) + pendingStartRef.current = request + const target = await checkSpeechModelUpdate(requested) if (request !== startRequestRef.current) { return false } + pendingStartRef.current = null + // Settings may have turned voice off or switched models while the check ran. + const current = useAppStore.getState().settings?.voice + if (!current?.enabled || current.sttModel !== requested.sttModel) { + return false + } if (target) { if (isIdle()) { setPillAnchor(dictationPillAnchorFor(captureInsertionTarget())) @@ -46,5 +84,5 @@ export function useDictationSpeechModelSetup( [isIdle, setPillAnchor] ) - return { setupTarget, closeSetup, invalidatePendingStart, readyToRecord } + return { setupTarget, closeSetup, hasPendingStart, invalidatePendingStart, readyToRecord } }