Skip to content

Commit 2f320c9

Browse files
committed
Update agent text in UI after we have full streamed text
1 parent 405f36d commit 2f320c9

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

coffee_ws/src/coffee_voice_agent_ui/coffee_voice_agent_ui/widgets/conversation_widget.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def __init__(self):
4242
self.turn_count = 0
4343
self.response_times = deque(maxlen=20) # Last 20 response times
4444
self.auto_scroll = True
45+
self.last_agent_message_index = None # Track last agent message for updates
4546

4647
self._setup_ui()
4748

@@ -207,9 +208,13 @@ def update_agent_state(self, status: AgentStatus):
207208
self.current_phase = new_phase
208209
self.phase_label.setText(f"Phase: {new_phase}")
209210

210-
# Handle agent speech
211+
# Handle agent speech - both starting and completion
211212
if status.speech_status == "speaking" and status.speech_text:
213+
# Agent started speaking - add new message with preview text
212214
self._add_agent_message(status.speech_text, status.emotion)
215+
elif status.speech_status == "idle" and status.speech_text and self.last_agent_message_index is not None:
216+
# Agent finished speaking - update last message with full text
217+
self._update_last_agent_message(status.speech_text)
213218

214219
def add_user_speech(self, speech_text: str):
215220
"""Add user speech to conversation"""
@@ -240,10 +245,53 @@ def _add_agent_message(self, text: str, emotion: str = ""):
240245
self._add_conversation_item("🤖 AGENT", f"{emotion_prefix}{text}", "#28a745")
241246
self.last_agent_message_time = datetime.now()
242247

248+
# Track this as the last agent message for potential updates
249+
self.last_agent_message_index = len(self.conversation_items) - 1
250+
243251
# Start timeout tracking
244252
self.timeout_progress.setVisible(True)
245253
self.timeout_progress.setValue(0)
246254

255+
def _update_last_agent_message(self, full_text: str):
256+
"""Update the last agent message with the full text"""
257+
if self.last_agent_message_index is not None and self.last_agent_message_index < len(self.conversation_items):
258+
# Get the last agent message item
259+
timestamp, role, old_text, color = self.conversation_items[self.last_agent_message_index]
260+
261+
# Extract emotion prefix from old text if present
262+
emotion_prefix = ""
263+
if old_text.startswith("[") and "]" in old_text:
264+
end_bracket = old_text.find("]")
265+
emotion_prefix = old_text[:end_bracket + 2] # Include space after ]
266+
267+
# Create updated text with same emotion prefix
268+
updated_text = f"{emotion_prefix}{full_text}"
269+
270+
# Update the item in the deque
271+
self.conversation_items[self.last_agent_message_index] = (timestamp, role, updated_text, color)
272+
273+
# Rebuild entire conversation display to show update
274+
self._refresh_conversation_display()
275+
276+
def _refresh_conversation_display(self):
277+
"""Refresh the entire conversation display to show updates"""
278+
# Clear current display
279+
self.conversation_text.clear()
280+
281+
# Re-add all conversation items
282+
for timestamp, role, text, color in self.conversation_items:
283+
time_str = timestamp.strftime("%H:%M:%S")
284+
formatted_text = f"<span style='color: {color}; font-weight: bold;'>[{time_str}] {role}:</span> {text}<br>"
285+
286+
cursor = self.conversation_text.textCursor()
287+
cursor.movePosition(QTextCursor.End)
288+
cursor.insertHtml(formatted_text)
289+
290+
# Auto-scroll if enabled
291+
if self.auto_scroll:
292+
scrollbar = self.conversation_text.verticalScrollBar()
293+
scrollbar.setValue(scrollbar.maximum())
294+
247295
def _add_conversation_item(self, role: str, text: str, color: str):
248296
"""Add an item to the conversation display"""
249297
timestamp = datetime.now()
@@ -281,6 +329,7 @@ def _clear_conversation(self):
281329
self.turn_count = 0
282330
self.turns_label.setText("Turns: 0")
283331
self.response_times.clear()
332+
self.last_agent_message_index = None # Reset agent message tracking
284333
self._update_response_metrics()
285334

286335
def _update_dynamic_elements(self):

0 commit comments

Comments
 (0)