mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
* 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
99 lines
2.6 KiB
TypeScript
99 lines
2.6 KiB
TypeScript
import { API_BASE_URL } from '@/config'
|
|
import { fetchWrapper, FetchError } from './fetchWrapper'
|
|
|
|
export interface STTModelsResponse {
|
|
models: string[]
|
|
cached: boolean
|
|
}
|
|
|
|
export interface STTStatusResponse {
|
|
enabled: boolean
|
|
configured: boolean
|
|
provider: 'external' | 'builtin'
|
|
model: string
|
|
}
|
|
|
|
export interface STTTranscribeResponse {
|
|
text: string
|
|
}
|
|
|
|
export interface STTErrorResponse {
|
|
error: string
|
|
details?: string
|
|
}
|
|
|
|
export const sttApi = {
|
|
getModels: async (userId = 'default', forceRefresh = false): Promise<STTModelsResponse> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/stt/models`, {
|
|
params: { userId, ...(forceRefresh && { refresh: 'true' }) },
|
|
})
|
|
},
|
|
|
|
getStatus: async (userId = 'default'): Promise<STTStatusResponse> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/stt/status`, {
|
|
params: { userId },
|
|
})
|
|
},
|
|
|
|
transcribe: async (
|
|
audioBlob: Blob,
|
|
userId = 'default',
|
|
signal?: AbortSignal
|
|
): Promise<STTTranscribeResponse> => {
|
|
const formData = new FormData()
|
|
|
|
const type = audioBlob.type
|
|
const extension =
|
|
type.includes('wav') ? 'wav' :
|
|
type.includes('webm') ? 'webm' :
|
|
type.includes('ogg') ? 'ogg' :
|
|
type.includes('mp4') ? 'm4a' : 'wav'
|
|
formData.append('audio', audioBlob, `recording.${extension}`)
|
|
|
|
const urlObj = new URL(`${API_BASE_URL}/api/stt/transcribe`, window.location.origin)
|
|
urlObj.searchParams.set('userId', userId)
|
|
|
|
const controller = new AbortController()
|
|
let timeoutFired = false
|
|
const timeoutId = setTimeout(() => {
|
|
timeoutFired = true
|
|
controller.abort()
|
|
}, 60000)
|
|
|
|
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: controller.signal,
|
|
})
|
|
|
|
clearTimeout(timeoutId)
|
|
signal?.removeEventListener('abort', onAbort)
|
|
|
|
if (!response.ok) {
|
|
const data = await response.json().catch(() => ({ error: 'Transcription failed' }))
|
|
throw new FetchError(data.error || 'Transcription failed', response.status)
|
|
}
|
|
|
|
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
|
|
}
|
|
},
|
|
}
|