@@ -57,6 +57,7 @@ def __init__(self, agent=None):
5757
5858 # Goodbye coordination flag
5959 self .goodbye_pending = False # Flag to coordinate goodbye handling between user and assistant handlers
60+ self .end_after_current_speech = False # Flag to end conversation when agent stops speaking
6061
6162 # Conversation timing tracking for admin message injection
6263 self .conversation_start_time = None
@@ -133,6 +134,7 @@ async def _enter_new_state(self):
133134 # Reset conversation timing and VIP status
134135 self .conversation_start_time = None
135136 self .is_vip_session = False
137+ self .end_after_current_speech = False
136138
137139 # Resume wake word detection when returning to dormant
138140 if self .agent :
@@ -345,12 +347,16 @@ async def handle_conversation_item():
345347 self .goodbye_pending = False
346348 self .ending_conversation = True
347349
348- # End conversation after a brief delay to let LLM response complete
349- async def delayed_end_conversation ():
350- await asyncio .sleep (2 ) # Brief delay for response completion
351- await self .end_conversation ()
350+ # Set flag to end conversation when TTS completes
351+ self .end_after_current_speech = True
352+ return # Skip normal timer logic since we're ending
353+
354+ # Check if this is a system-initiated ending
355+ if self .ending_conversation :
356+ logger .info ("🔍 DEBUG: System-initiated ending - will end after TTS completes" )
352357
353- asyncio .create_task (delayed_end_conversation ())
358+ # Set flag to end conversation when TTS completes
359+ self .end_after_current_speech = True
354360 return # Skip normal timer logic since we're ending
355361
356362 # Only start new timer if we're not ending the conversation
@@ -404,6 +410,12 @@ async def handle_state_change():
404410 elif event .old_state == "speaking" and event .new_state != "speaking" :
405411 logger .info ("🔍 DEBUG: Agent stopped speaking - sending agent status" )
406412 await self ._send_agent_status (current_behavioral_mode , "idle" )
413+
414+ # Check if we should end conversation after TTS completion
415+ if self .end_after_current_speech :
416+ logger .info ("🔍 DEBUG: TTS completed - ending conversation as requested" )
417+ self .end_after_current_speech = False
418+ asyncio .create_task (self .end_conversation ())
407419 except Exception as e :
408420 logger .error (f"Error handling agent state change status events: { e } " )
409421
0 commit comments