Repository navigation
refactor(voice): restructure voice subsystem and add tts chunking - #704
Conversation
haiphucnguyen
commented
Sep 7, 2026
- Replace monolithic VoiceServices with a modular registry and factory-based architecture.
- Implement TtsTextChunker to support splitting long text for reliable audio playback.
- Update VoicePlaybackController to integrate new services and handle sequential audio streaming.
- Add unit tests for TtsTextChunker logic.
- Replace monolithic VoiceServices with a modular registry and factory-based architecture. - Implement TtsTextChunker to support splitting long text for reliable audio playback. - Update VoicePlaybackController to integrate new services and handle sequential audio streaming. - Add unit tests for TtsTextChunker logic. BREAKING CHANGE: The VoiceServices class has been removed. Please migrate to VoiceServiceRegistry and utilize the new factory interfaces for speech and text services.
There was a problem hiding this comment.
🟡 Changes recommended
There are correctness/robustness issues in new chunking and playback state management that should be addressed before merging.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR refactors the desktop shared voice subsystem by splitting the previous monolithic voice service definitions into focused files, introducing a TTS text chunking utility to respect provider input limits, and updating message voice playback to synthesize/play long texts sequentially.
Changes:
- Extracted voice types (services, factories, registry, exception, audio format) into dedicated Kotlin files.
- Added
chunkTextForTts(+MAX_TTS_CHARS) and integrated chunked synthesis/playback inVoicePlaybackController. - Added unit tests for TTS chunking behavior.
File summaries
| File | Description |
|---|---|
| desktop-shared/src/test/kotlin/io/askimo/ui/voice/TtsTextChunkerTest.kt | Adds unit coverage for sentence-based chunking, hard-splitting, and whitespace normalization. |
| desktop-shared/src/main/kotlin/io/askimo/ui/voice/VoiceServices.kt | Removes the previously monolithic voice definitions file (replaced by new focused files). |
| desktop-shared/src/main/kotlin/io/askimo/ui/voice/VoiceServiceRegistry.kt | Introduces a dedicated registry for resolving configured STT/TTS implementations. |
| desktop-shared/src/main/kotlin/io/askimo/ui/voice/VoiceServiceException.kt | Extracts VoiceServiceException into its own file. |
| desktop-shared/src/main/kotlin/io/askimo/ui/voice/VoiceAudioFormat.kt | Extracts VoiceAudioFormat into its own file. |
| desktop-shared/src/main/kotlin/io/askimo/ui/voice/TtsTextChunker.kt | Implements text chunking with a max character threshold for TTS provider limits. |
| desktop-shared/src/main/kotlin/io/askimo/ui/voice/TextToSpeechService.kt | Extracts TextToSpeechService into its own file and documents chunking usage. |
| desktop-shared/src/main/kotlin/io/askimo/ui/voice/TextToSpeechFactory.kt | Extracts TextToSpeechFactory into its own file. |
| desktop-shared/src/main/kotlin/io/askimo/ui/voice/SpeechToTextService.kt | Extracts SpeechToTextService into its own file. |
| desktop-shared/src/main/kotlin/io/askimo/ui/voice/SpeechToTextFactory.kt | Extracts SpeechToTextFactory into its own file. |
| desktop-shared/src/main/kotlin/io/askimo/ui/chat/MessageComponents.kt | Updates voice playback to chunk long texts and play synthesized audio sequentially; adds stoppable playback job tracking. |
Review details
- Files reviewed: 11/11 changed files
- Comments generated: 3
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| } | ||
| flush() | ||
|
|
||
| return chunks.ifEmpty { listOf(text.take(maxChars)) } |
| Snapshot.withMutableSnapshot { | ||
| if (playingMessageId == messageId) playingMessageId = null | ||
| } |
| */ | ||
| package io.askimo.ui.voice | ||
|
|
||
| import io.askimo.core.config.VoiceConfig |
There was a problem hiding this comment.
🟡 Changes recommended
The new chunking/playback flow has confirmed edge-case bugs (UTF-16 surrogate splitting in hard-split and stale UI state on coroutine cancellation) that should be addressed before approval.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
Suppressed comments (1)
desktop-shared/src/main/kotlin/io/askimo/ui/chat/MessageComponents.kt:222
- If the coroutine is cancelled by the provided
scope(e.g., the composable leaves composition and itsrememberCoroutineScope()is cancelled), thecatchblocks won’t run andloadingMessageId/playingMessageIdcan remain stuck, leaving the UI in a perpetual loading/playing state. Clear the state infinallywhen this job is still the activeplaybackJob.
} finally {
// Clear once this run ends (success, caught error, or cancellation) so
// `playbackJob` doesn't linger on a finished job. Guarded by identity since a
// cancelled job's `finally` can run after a newer toggle() already replaced it.
if (playbackJob === thisJob) playbackJob = null
}
- Files reviewed: 11/11 changed files
- Comments generated: 2
- Review effort level: Lite
| if (sentence.length > maxChars) { | ||
| flush() | ||
| sentence.chunked(maxChars).forEach { chunks += it } | ||
| continue | ||
| } |
| @Test | ||
| fun `never truncates text even in the defensive empty-chunks fallback path`() { | ||
| // Every non-empty text yields at least one non-empty sentence token, so the internal | ||
| // "no chunks produced" fallback is unreachable for any text this function actually | ||
| // accepts — but it must stay lossless (return the full text, not a truncated prefix) | ||
| // rather than silently dropping content, which would produce incomplete TTS audio. | ||
| val text = "x".repeat(10_000) | ||
| val chunks = chunkTextForTts(text, maxChars = 4000) | ||
| assertEquals(text, chunks.joinToString("")) | ||
| chunks.forEach { assertTrue(it.length <= 4000) } | ||
| } | ||
| } |
There was a problem hiding this comment.
🟡 Changes recommended
There is a confirmed cancellation-state bug risk in VoicePlaybackController that can leave UI playback/loading state stuck when the coroutine is canceled outside stopAll().
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
Suppressed comments (2)
Previously missed (1) — in code that hasn't changed since the last review.
desktop-shared/src/test/kotlin/io/askimo/ui/voice/TtsTextChunkerTest.kt:16
- This class-level KDoc says chunking always "reconstruct[s] the original content when rejoined", but later tests explicitly assert inter-sentence whitespace normalization during chunking (newlines/tabs collapsed). Update the KDoc to match the behavior being tested.
desktop-shared/src/main/kotlin/io/askimo/ui/chat/MessageComponents.kt:222
- In the cancellation path (e.g., the passed CoroutineScope is canceled while synthesizing/awaiting playback), this coroutine jumps to
finallywithout clearingloadingMessageId/playingMessageId, which can leave the UI stuck in a loading/playing state. Consider clearing those state vars when (and only when) this job is still the activeplaybackJob.
} finally {
// Clear once this run ends (success, caught error, or cancellation) so
// `playbackJob` doesn't linger on a finished job. Guarded by identity since a
// cancelled job's `finally` can run after a newer toggle() already replaced it.
if (playbackJob === thisJob) playbackJob = null
}
- Files reviewed: 11/11 changed files
- Comments generated: 2
- Review effort level: Lite
| * Splits preferentially on sentence boundaries (after `.`, `!`, `?` followed by whitespace); a | ||
| * single sentence longer than [maxChars] is hard-split via [chunkPreservingSurrogatePairs] (safe | ||
| * for emoji/supplementary Unicode) as a last resort. Returns `listOf(text)` unchanged when it | ||
| * already fits in one chunk. |
| @Test | ||
| fun `hard-split never separates a surrogate pair, even at the exact chunk boundary`() { | ||
| // Each 🎉 is one codepoint but 2 UTF-16 chars — with maxChars = 2, a naive chunked(2) | ||
| // would place the boundary right between the two halves of the second emoji's pair. | ||
| val text = "🎉🎉🎉🎉🎉" // 5 codepoints, 10 UTF-16 chars, no sentence punctuation | ||
| val chunks = chunkTextForTts(text, maxChars = 2) | ||
|
|