Simplify the TranscriptProcessor _emit_aggregated_text logic

This commit is contained in:
Mark Backman
2025-03-17 16:30:46 -04:00
parent acd0660f66
commit 6885d07e88
2 changed files with 50 additions and 284 deletions

View File

@@ -90,52 +90,62 @@ class AssistantTranscriptProcessor(BaseTranscriptProcessor):
self._aggregation_start_time: Optional[str] = None
async def _emit_aggregated_text(self):
"""Emit aggregated text as a transcript message.
"""Aggregates and emits text fragments as a transcript message.
This method intelligently joins text fragments to create natural spacing,
handling both word-by-word and pre-spaced text fragments appropriately.
This method uses a heuristic to automatically detect whether text fragments
use pre-spacing (spaces at the beginning of fragments) or not, and applies
the appropriate joining strategy. It handles fragments from different TTS
services with different formatting patterns.
The implementation handles two common patterns from TTS services:
Examples:
Pre-spaced fragments (concatenated):
```
TTSTextFrame: ["Hello"]
TTSTextFrame: [" there"]
TTSTextFrame: ["!"]
TTSTextFrame: [" How"]
TTSTextFrame: ["'s"]
TTSTextFrame: [" it"]
TTSTextFrame: [" going"]
TTSTextFrame: ["?"]
```
Result: "Hello there! How's it going?"
1. Word-by-word fragments without spacing:
```
TTSTextFrame: ['Hello.']
TTSTextFrame: ['How']
TTSTextFrame: ['can']
TTSTextFrame: ['I']
TTSTextFrame: ['assist']
TTSTextFrame: ['you']
TTSTextFrame: ['today?']
```
Result: "Hello. How can I assist you today?"
2. Pre-spaced fragments:
```
TTSTextFrame: ['Hello']
TTSTextFrame: [' there']
TTSTextFrame: ['!']
TTSTextFrame: [' How']
TTSTextFrame: ["'s"]
TTSTextFrame: [' it']
TTSTextFrame: [' going']
TTSTextFrame: ['?']
```
Result: "Hello there! How's it going?"
Word-by-word fragments (joined with spaces):
```
TTSTextFrame: ["Hello"]
TTSTextFrame: ["there!"]
TTSTextFrame: ["How"]
TTSTextFrame: ["is"]
TTSTextFrame: ["it"]
TTSTextFrame: ["going?"]
```
Result: "Hello there! How is it going?"
"""
if self._current_text_parts and self._aggregation_start_time:
# Build content with intelligent spacing
content = ""
for i, part in enumerate(self._current_text_parts):
# Add a space only when the current part doesn't start with
# whitespace or punctuation/special characters
if i > 0 and not part.startswith((" ", ".", ",", "!", "?", ";", ":", "'", '"')):
content += " "
content += part
# Heuristic to detect pre-spaced fragments
uses_prespacing = False
if len(self._current_text_parts) > 1:
# Check if any fragment after the first one starts with whitespace
has_spaced_parts = any(
part and part[0].isspace() for part in self._current_text_parts[1:]
)
if has_spaced_parts:
uses_prespacing = True
# Apply appropriate joining method
if uses_prespacing:
# Pre-spaced fragments - just concatenate
content = "".join(self._current_text_parts)
else:
# Word-by-word fragments - join with spaces
content = " ".join(self._current_text_parts)
# Clean up any excessive whitespace
content = content.strip()
if content:
logger.debug(f"Emitting aggregated assistant message: {content}")
logger.trace(f"Emitting aggregated assistant message: {content}")
message = TranscriptionMessage(
role="assistant",
content=content,
@@ -143,7 +153,7 @@ class AssistantTranscriptProcessor(BaseTranscriptProcessor):
)
await self._emit_update([message])
else:
logger.debug("No content to emit after stripping whitespace")
logger.trace("No content to emit after stripping whitespace")
# Reset aggregation state
self._current_text_parts = []