Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apps/desktop/src/app/chat/composer/hooks/use-mic-recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type BrowserAudioContext = typeof AudioContext

export interface MicRecorderOptions {
onLevel?: (level: number) => void
onSpeech?: () => void
onError?: (error: Error) => void
onSilence?: () => void
silenceLevel?: number
Expand Down Expand Up @@ -138,8 +139,12 @@ export function useMicRecorder(copy: MicRecorderErrorCopy): {

if (speechThreshold > 0 && options.onSilence && !silenceTriggeredRef.current) {
if (normalized >= speechThreshold) {
const wasSpeaking = heardSpeechRef.current
heardSpeechRef.current = true
silenceStartedAtRef.current = null
if (!wasSpeaking) {
options.onSpeech?.()
}
} else if (heardSpeechRef.current && silenceMs > 0) {
silenceStartedAtRef.current ??= now

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,19 @@ export function useVoiceConversation({
setStatus('speaking')

try {
// Keep a microphone capture alive while TTS is playing so the user can
// barge in. The first detected speech stops playback and hands the
// same recorder to the normal turn-closing path.
await handle.start({
silenceLevel: 0.075,
silenceMs: 1_250,
idleSilenceMs: 12_000,
onSpeech: () => {
stopVoicePlayback()
void handleTurn(true)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stopVoicePlayback() resolves playSpeechText(), so this handleTurn(true) races the enclosing speak() finally block, which unconditionally marks the conversation idle and re-arms listening. Track the interruption and skip that normal-completion re-arm path so this turn exclusively owns recorder stop/transcription.

},
onSilence: () => void handleTurn()
})
await playSpeechText(text, { source: 'voice-conversation' })
} catch (error) {
notifyError(error, voiceCopy.playbackFailed)
Expand All @@ -237,7 +250,7 @@ export function useVoiceConversation({
}
}
},
[voiceCopy.playbackFailed]
[handle, handleTurn, voiceCopy.playbackFailed]
)

const start = useCallback(async () => {
Expand Down