fix: preserve assistant message in chat history when TTS is interrupted
Move chatHistory update into try-finally in speakAndDrain so the response is committed even when the user cancels mid-speech. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
64e7a42f9a
commit
7194acbf02
1 changed files with 12 additions and 9 deletions
|
|
@ -208,18 +208,21 @@ class AssistantManager(
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun speakAndDrain(firstContent: String, speechQueue: Channel<String>) {
|
private suspend fun speakAndDrain(firstContent: String, speechQueue: Channel<String>) {
|
||||||
|
val chunks = mutableListOf(firstContent)
|
||||||
|
try {
|
||||||
_state.update { it.copy(phase = VoicePhase.SPEAKING, responseText = firstContent) }
|
_state.update { it.copy(phase = VoicePhase.SPEAKING, responseText = firstContent) }
|
||||||
ttsWrapper.speak(firstContent)
|
ttsWrapper.speak(firstContent)
|
||||||
val chunks = mutableListOf(firstContent)
|
|
||||||
while (true) {
|
while (true) {
|
||||||
val next = speechQueue.tryReceive().getOrNull() ?: break
|
val next = speechQueue.tryReceive().getOrNull() ?: break
|
||||||
|
chunks.add(next)
|
||||||
_state.update { it.copy(responseText = next) }
|
_state.update { it.copy(responseText = next) }
|
||||||
ttsWrapper.speak(next)
|
ttsWrapper.speak(next)
|
||||||
chunks.add(next)
|
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
val fullResponse = chunks.joinToString("\n")
|
val fullResponse = chunks.joinToString("\n")
|
||||||
_state.update { it.copy(chatHistory = it.chatHistory + ChatTurn("assistant", fullResponse)) }
|
_state.update { it.copy(chatHistory = it.chatHistory + ChatTurn("assistant", fullResponse)) }
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private suspend fun drainSpeechQueue(speechQueue: Channel<String>) {
|
private suspend fun drainSpeechQueue(speechQueue: Channel<String>) {
|
||||||
val first = speechQueue.tryReceive().getOrNull() ?: return
|
val first = speechQueue.tryReceive().getOrNull() ?: return
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue