From b7688ae8eb81ac6b311c3daaafdc3dbec7775ebf Mon Sep 17 00:00:00 2001 From: Long Chen Date: Sun, 12 Jan 2025 17:59:52 +0800 Subject: [PATCH] fix nested speech done future (#1355) --- livekit-agents/livekit/agents/pipeline/pipeline_agent.py | 2 ++ livekit-agents/livekit/agents/pipeline/speech_handle.py | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/livekit-agents/livekit/agents/pipeline/pipeline_agent.py b/livekit-agents/livekit/agents/pipeline/pipeline_agent.py index 921406374..e67af5b9f 100644 --- a/livekit-agents/livekit/agents/pipeline/pipeline_agent.py +++ b/livekit-agents/livekit/agents/pipeline/pipeline_agent.py @@ -870,6 +870,7 @@ def _commit_user_question_if_needed() -> None: }, ) + @utils.log_exceptions(logger=logger) async def _execute_function_calls() -> None: nonlocal interrupted, collected_text @@ -995,6 +996,7 @@ async def _execute_function_calls() -> None: speech_handle._set_done() return + speech_handle._nested_speech_done_fut = asyncio.Future[None]() fnc_task = asyncio.create_task(_execute_function_calls()) while not speech_handle.nested_speech_done: nesting_changed = asyncio.create_task( diff --git a/livekit-agents/livekit/agents/pipeline/speech_handle.py b/livekit-agents/livekit/agents/pipeline/speech_handle.py index cd1f39dec..93a73df9f 100644 --- a/livekit-agents/livekit/agents/pipeline/speech_handle.py +++ b/livekit-agents/livekit/agents/pipeline/speech_handle.py @@ -46,7 +46,7 @@ def __init__( self._nested_speech_handles: list[SpeechHandle] = [] self._nested_speech_changed = asyncio.Event() - self._nested_speech_done_fut = asyncio.Future[None]() + self._nested_speech_done_fut: asyncio.Future[None] | None = None @staticmethod def create_assistant_reply( @@ -227,9 +227,12 @@ def nested_speech_changed(self) -> asyncio.Event: @property def nested_speech_done(self) -> bool: - return self._nested_speech_done_fut.done() + # True if not started or done + return ( + self._nested_speech_done_fut is None or self._nested_speech_done_fut.done() + ) def mark_nested_speech_done(self) -> None: - if self._nested_speech_done_fut.done(): + if self._nested_speech_done_fut is None or self._nested_speech_done_fut.done(): return self._nested_speech_done_fut.set_result(None)