Skip to content

refactor(voice): restructure voice subsystem and add tts chunking - #704

Merged
haiphucnguyen merged 3 commits into
mainfrom
feature/voice-improvements
Sep 7, 2026
Merged

haiphucnguyen merged 3 commits into
mainfrom
feature/voice-improvements

Conversation

@haiphucnguyen

Copy link
Copy Markdown
Collaborator
  • 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.
Copilot AI lite review requested due to automatic review settings September 7, 2026 00:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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 in VoicePlaybackController.
  • 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)) }
Comment on lines +203 to 205
Snapshot.withMutableSnapshot {
if (playingMessageId == messageId) playingMessageId = null
}
*/
package io.askimo.ui.voice

import io.askimo.core.config.VoiceConfig

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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 its rememberCoroutineScope() is cancelled), the catch blocks won’t run and loadingMessageId / playingMessageId can remain stuck, leaving the UI in a perpetual loading/playing state. Clear the state in finally when this job is still the active playbackJob.
            } 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

Comment on lines +47 to +51
if (sentence.length > maxChars) {
flush()
sentence.chunked(maxChars).forEach { chunks += it }
continue
}
Comment on lines +122 to +133
@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) }
}
}

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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 finally without clearing loadingMessageId/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 active playbackJob.
            } 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

Comment on lines +21 to +24
* 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.
Comment on lines +154 to +160
@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)

@haiphucnguyen
haiphucnguyen merged commit 8bda50e into main Sep 7, 2026
11 checks passed
@haiphucnguyen
haiphucnguyen deleted the feature/voice-improvements branch September 7, 2026 01:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants