mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
feat(stt): add energy-based VAD with silence gate and auto-stop (#249)
* loop: stt-warmup completed after 6 iterations
* refactor: extract STT startup helpers and simplify audio recorder
* feat(stt): add energy-based VAD with silence gate and auto-stop
- New VoiceActivityDetector class: adaptive noise floor, per-frame RMS
classification, trailing-silence endpointing, configurable thresholds
- AudioWorkletProcessor computes RMS in existing flush loop and posts
{samples, rms} instead of a bare Int16Array
- AudioRecorder integrates VAD via handleVadFrame() in both worklet and
ScriptProcessor paths; auto-calls stop() on trailing silence; silence
gate in processRecording() fires onNoSpeech instead of sending empty
audio to the transcription API (fixes Whisper hallucination bug)
- useSTT wires setOnNoSpeech to reset isProcessing/isRecording cleanly
without surfacing an error
- computeRms() exported helper for ScriptProcessor fallback path
* fix(stt): clear processing state when stopping a silent recording
stopRecording() called recorder.stop() before setIsProcessing(true). For
a silent take the recorder synchronously fires onNoSpeech (which sets
processing false), so the subsequent setIsProcessing(true) won and the
voice overlay was stuck in the processing state forever.
Set processing before stop() so the recorder callback (onNoSpeech or
onDataAvailable) has the final say on the processing flag.
* fix(stt): always emit a terminal signal so the voice overlay never sticks
Two remaining stuck paths after the silent-stop fix:
- processRecording() early-returned without firing onNoSpeech when a take
had no captured audio (e.g. a quick start/stop under one worklet flush,
totalSamples === 0). Now any non-emitting path (empty audio or no
detected speech) fires onNoSpeech, while a genuine abort still returns
silently.
- stopRecording() set isProcessing true even when the recorder was no
longer recording (double-stop or post auto-stop), where stop() skips
processRecording entirely and nothing clears the flag. Guard the
external path on recorder state before entering processing.
* loop: mediarecorder-stt completed after 6 iterations
* refactor(stt): extract shared helpers and cleanup
* refactor(stt): add separate arm/disarm thresholds for voice swipe
* fix(stt): prevent default touch behavior on voice gesture handlers
* feat(stt): add audio-level speech detection before emitting data
This commit is contained in:
1 parent
ab624a0970
commit
2b2cb6d23d
13 files changed
+1625
-549
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { sttApi } from './stt'
|
||||
import { FetchError } from './fetchWrapper'
|
||||
|
||||
describe('WAV extension selection logic', () => {
|
||||
const originalFetch = global.fetch
|
||||
@@ -69,6 +70,18 @@ describe('WAV extension selection logic', () => {
|
||||
expect(audioFile.name).toBe('recording.m4a')
|
||||
})
|
||||
|
||||
it('should return webm for audio/webm;codecs=opus blob', async () => {
|
||||
const blob = new Blob([], { type: 'audio/webm;codecs=opus' })
|
||||
mockFetch.mockResolvedValueOnce(createMockResponse(true, { text: 'test' }))
|
||||
|
||||
await sttApi.transcribe(blob, 'test-user')
|
||||
|
||||
const callArgs = mockFetch.mock.calls[0]
|
||||
const formData = callArgs[1]?.body as FormData
|
||||
const audioFile = formData.get('audio') as File
|
||||
expect(audioFile.name).toBe('recording.webm')
|
||||
})
|
||||
|
||||
it('should default to wav for unknown types', async () => {
|
||||
const blob = new Blob([], { type: 'audio/unknown' })
|
||||
mockFetch.mockResolvedValueOnce(createMockResponse(true, { text: 'test' }))
|
||||
@@ -123,3 +136,85 @@ describe('WAV extension selection logic', () => {
|
||||
expect(formData.get('audio')).toBeInstanceOf(File)
|
||||
})
|
||||
})
|
||||
|
||||
describe('sttApi.transcribe cancellation and timeout', () => {
|
||||
const originalFetch = global.fetch
|
||||
|
||||
beforeEach(() => {
|
||||
vi.stubGlobal('fetch', vi.fn())
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
global.fetch = originalFetch
|
||||
})
|
||||
|
||||
it('throws 499 CANCELED when caller aborts', async () => {
|
||||
const blob = new Blob([], { type: 'audio/webm;codecs=opus' })
|
||||
const abortController = new AbortController()
|
||||
|
||||
const mockFetch = global.fetch as ReturnType<typeof vi.fn>
|
||||
mockFetch.mockImplementation((_url: string, options: RequestInit) => {
|
||||
const signal = options.signal as AbortSignal
|
||||
return new Promise((_resolve, reject) => {
|
||||
if (signal.aborted) {
|
||||
const err = new Error('The operation was aborted')
|
||||
err.name = 'AbortError'
|
||||
reject(err)
|
||||
return
|
||||
}
|
||||
signal.addEventListener('abort', () => {
|
||||
const err = new Error('The operation was aborted')
|
||||
err.name = 'AbortError'
|
||||
reject(err)
|
||||
}, { once: true })
|
||||
})
|
||||
})
|
||||
|
||||
const promise = sttApi.transcribe(blob, 'test-user', abortController.signal)
|
||||
|
||||
abortController.abort()
|
||||
|
||||
await expect(promise).rejects.toThrow(FetchError)
|
||||
await expect(promise).rejects.toMatchObject({
|
||||
statusCode: 499,
|
||||
code: 'CANCELED',
|
||||
})
|
||||
})
|
||||
|
||||
it('throws 408 TIMEOUT when transcription times out', async () => {
|
||||
vi.useFakeTimers()
|
||||
|
||||
const blob = new Blob([], { type: 'audio/webm;codecs=opus' })
|
||||
|
||||
const mockFetch = global.fetch as ReturnType<typeof vi.fn>
|
||||
mockFetch.mockImplementation((_url: string, options: RequestInit) => {
|
||||
const signal = options.signal as AbortSignal
|
||||
return new Promise((_resolve, reject) => {
|
||||
if (signal.aborted) {
|
||||
const err = new Error('The operation was aborted')
|
||||
err.name = 'AbortError'
|
||||
reject(err)
|
||||
return
|
||||
}
|
||||
signal.addEventListener('abort', () => {
|
||||
const err = new Error('The operation was aborted')
|
||||
err.name = 'AbortError'
|
||||
reject(err)
|
||||
}, { once: true })
|
||||
})
|
||||
})
|
||||
|
||||
const promise = sttApi.transcribe(blob, 'test-user')
|
||||
|
||||
vi.advanceTimersByTime(60000)
|
||||
|
||||
await expect(promise).rejects.toThrow(FetchError)
|
||||
await expect(promise).rejects.toMatchObject({
|
||||
statusCode: 408,
|
||||
code: 'TIMEOUT',
|
||||
})
|
||||
|
||||
vi.useRealTimers()
|
||||
})
|
||||
})
|
||||
+17
-3
@@ -54,18 +54,27 @@ export const sttApi = {
|
||||
urlObj.searchParams.set('userId', userId)
|
||||
|
||||
const controller = new AbortController()
|
||||
const timeoutId = setTimeout(() => controller.abort(), 60000)
|
||||
let timeoutFired = false
|
||||
const timeoutId = setTimeout(() => {
|
||||
timeoutFired = true
|
||||
controller.abort()
|
||||
}, 60000)
|
||||
|
||||
const abortSignal = signal || controller.signal
|
||||
if (signal?.aborted) {
|
||||
controller.abort()
|
||||
}
|
||||
const onAbort = () => controller.abort()
|
||||
signal?.addEventListener('abort', onAbort, { once: true })
|
||||
|
||||
try {
|
||||
const response = await fetch(urlObj.toString(), {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
signal: abortSignal,
|
||||
signal: controller.signal,
|
||||
})
|
||||
|
||||
clearTimeout(timeoutId)
|
||||
signal?.removeEventListener('abort', onAbort)
|
||||
|
||||
if (!response.ok) {
|
||||
const data = await response.json().catch(() => ({ error: 'Transcription failed' }))
|
||||
@@ -75,7 +84,12 @@ export const sttApi = {
|
||||
return response.json()
|
||||
} catch (error) {
|
||||
clearTimeout(timeoutId)
|
||||
signal?.removeEventListener('abort', onAbort)
|
||||
|
||||
if (error instanceof Error && error.name === 'AbortError') {
|
||||
if (signal?.aborted && !timeoutFired) {
|
||||
throw new FetchError('Transcription canceled', 499, 'CANCELED')
|
||||
}
|
||||
throw new FetchError('Transcription timeout', 408, 'TIMEOUT')
|
||||
}
|
||||
throw error
|
||||
|
||||
Reference in new issue
Block a user