Skip to content

Conversation

@doublefx
Copy link

@doublefx doublefx commented Jan 14, 2026

Summary

Fixes multiple critical bugs in the TTS system that caused incomplete speech playback and mangled text output.

Fixes #901

Problems Fixed

1. cleanText() Regex Bug (Primary Issue)

The regex pattern was fundamentally broken, matching from any opening tag to any closing tag and destroying content in between.

Before:

/<[a-zA-Z][a-zA-Z0-9]*>.*?<\/[a-zA-Z][a-zA-Z0-9]*>/gs
// Result: <p>Text</p><pre>Code</pre> → "</pre>" (everything between <p> and </pre> removed!)

After: Replaced with DOMParser for proper HTML parsing

2. Streaming Race Condition

The for-loop would exit after speaking available chunks. If the finished=true update arrived while speech was running (polling interval: 25ms), it would be ignored and remaining chunks never spoken.

Fix: Replaced for-loop with continuous while-loop that checks for new chunks until stream finishes.

3. Audio Cancellation Bug

Both speakWithBrowser() and speakWithKokoro() called stopAudio() even when waitForPrevious=true, canceling speech immediately after waiting for it.

Fix: Only call stopAudio() when NOT waiting for previous speech.

4. Replay Button Issue

The speak button extracted plain text instead of HTML, preventing cleanText() from finding <pre> and <code> tags.

Fix: Pass HTML to cleanText() so code blocks are properly replaced with placeholder.

Testing

  • ✅ Streaming TTS speaks all chunks as they arrive
  • ✅ Replay button correctly handles code blocks ("See code attached..." instead of reading code)
  • ✅ Works with both browser TTS and Kokoro TTS

Files Changed

  • webui/components/chat/speech/speech-store.js
    • Refactored cleanText() with DOMParser
    • Replaced for-loop with while-loop in speakStream()
    • Fixed stopAudio() logic in speakWithBrowser() and speakWithKokoro()
  • webui/components/messages/action-buttons/simple-action-buttons.js
    • Fixed speak button to extract HTML instead of plain text

Frederic Thomas and others added 2 commits January 14, 2026 13:57
The cleanText() function was using broken regex patterns that resulted in
mangled HTML fragments being returned instead of clean text. This caused
TTS to only speak the last segment of responses.

Changes:
- Replace broken regex HTML parsing with DOMParser (browser native)
- Properly extract text content while preserving all readable text
- Replace <pre> and <code> tags with placeholder before text extraction
- Add fallback for edge cases where DOMParser might fail

Before: cleanText output was '</pre> distribution in version.</p>'
After: cleanText output is 'Linux Distribution...See code attached...version.'

Fixes agent0ai#901

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
- Replace for-loop with while-loop in speakStream() to handle fast streaming
  Without this, if finished=true arrives while speech is running, remaining
  chunks are lost forever due to the guard at line 208 rejecting updates.
  The while-loop continues checking for new chunks until stream finishes.

- Fix audio cancellation bug in speakWithBrowser() and speakWithKokoro()
  Both functions were calling stopAudio() even when waitForPrevious=true,
  canceling the speech immediately after waiting for it to finish.
  Now only calls stopAudio() when NOT waiting for previous speech.

- Fix replay button to pass HTML instead of plain text
  The speak button in simple-action-buttons.js was extracting innerText,
  preventing cleanText() from finding <pre> and <code> tags to replace.
  Now passes HTML so code blocks are properly replaced with placeholder.

Fixes: agent0ai#901

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@doublefx doublefx changed the title Add browser TTS voice selector with language filtering Fix TTS cleanText bugs causing incomplete speech playback Jan 14, 2026
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.

1 participant