Skip to content

Commit 32f5a5b

Browse files
committed
Fix TTS audio not playing back when the conversation times out
1 parent c03c283 commit 32f5a5b

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

coffee_ws/src/coffee_voice_agent/scripts/state/state_manager.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

coffee_ws/src/coffee_voice_agent/scripts/tools/coffee_tools.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,6 @@ async def manage_conversation_time_impl(
149149
# Signal the state manager to end conversation gracefully
150150
if _agent_instance and hasattr(_agent_instance, 'state_manager'):
151151
_agent_instance.state_manager.ending_conversation = True
152-
# Schedule conversation end after a brief delay
153-
asyncio.create_task(_delayed_conversation_end())
154152
result = f"Conversation ending initiated: {reason}"
155153

156154
elif action == "extend":
@@ -246,11 +244,4 @@ async def check_user_status_impl(
246244
logger.info(f"Standard user: {user_identifier}")
247245

248246
await send_tool_event("check_user_status", "completed", [user_identifier], result)
249-
return result
250-
251-
252-
async def _delayed_conversation_end():
253-
"""Helper function to end conversation after a brief delay"""
254-
await asyncio.sleep(2) # Allow current response to complete
255-
if _agent_instance and hasattr(_agent_instance, 'state_manager'):
256-
await _agent_instance.state_manager.end_conversation()
247+
return result

0 commit comments

Comments
 (0)