@@ -42,6 +42,10 @@ def __init__(self, agent=None):
4242 self .current_emotion = "waiting" # Track current emotional state
4343 self .emotion_history = [] # Log emotional journey
4444 self .ending_conversation = False # Flag to prevent timer conflicts during goodbye
45+
46+ # Text tracking for TTS events
47+ self .current_speech_preview = "" # Preview text for "started" events
48+ self .current_speech_full_text = "" # Accumulated full text for "finished" events
4549 self .virtual_request_queue = [] # Queue for virtual coffee requests
4650 self .announcing_virtual_request = False # Flag to prevent conflicts during announcements
4751 self .recent_greetings = [] # Track recent greetings to avoid repetition
@@ -264,11 +268,15 @@ async def handle_state_change():
264268 if event .new_state == "speaking" :
265269 logger .info ("🔍 DEBUG: Agent started speaking - sending TTS started event" )
266270 current_emotion = self .current_emotion
267- await self ._send_tts_event ("started" , "Agent Response" , current_emotion , "session" )
271+ # Use preview text for started event
272+ text_to_send = self .current_speech_preview or "Agent Response"
273+ await self ._send_tts_event ("started" , text_to_send , current_emotion , "session" )
268274 elif event .old_state == "speaking" and event .new_state != "speaking" :
269275 logger .info ("🔍 DEBUG: Agent stopped speaking - sending TTS finished event" )
270276 current_emotion = self .current_emotion
271- await self ._send_tts_event ("finished" , "Agent Response" , current_emotion , "session" )
277+ # Use full accumulated text for finished event
278+ text_to_send = self .current_speech_full_text or "Agent Response"
279+ await self ._send_tts_event ("finished" , text_to_send , current_emotion , "session" )
272280 except Exception as e :
273281 logger .error (f"Error handling agent state change TTS events: { e } " )
274282
@@ -502,6 +510,10 @@ async def say_with_emotion(self, text: str, emotion: str = None):
502510 logger .info (f"🔍 DEBUG: say_with_emotion emotion: { emotion } " )
503511
504512 if self .session :
513+ # Store text for TTS events
514+ self .current_speech_preview = text [:50 ] + "..." if len (text ) > 50 else text
515+ self .current_speech_full_text = text
516+
505517 # Send TTS_STARTED event - COMMENTED OUT to prevent duplicates (using agent_state_changed instead)
506518 # logger.info("🔍 DEBUG: About to send TTS_STARTED event")
507519 # await self._send_tts_event("started", text, emotion or self.current_emotion, "manual")
0 commit comments