Merge pull request #745 from pipecat-ai/mb/user-idle
Only run the UserIdleProcessor while pipeline is running
This commit is contained in:
@@ -5,11 +5,12 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
from typing import Awaitable, Callable
|
from typing import Awaitable, Callable
|
||||||
|
|
||||||
from pipecat.frames.frames import (
|
from pipecat.frames.frames import (
|
||||||
BotSpeakingFrame,
|
BotSpeakingFrame,
|
||||||
|
CancelFrame,
|
||||||
|
EndFrame,
|
||||||
Frame,
|
Frame,
|
||||||
UserStartedSpeakingFrame,
|
UserStartedSpeakingFrame,
|
||||||
UserStoppedSpeakingFrame,
|
UserStoppedSpeakingFrame,
|
||||||
@@ -32,20 +33,25 @@ class UserIdleProcessor(FrameProcessor):
|
|||||||
**kwargs,
|
**kwargs,
|
||||||
):
|
):
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
self._callback = callback
|
self._callback = callback
|
||||||
self._timeout = timeout
|
self._timeout = timeout
|
||||||
|
|
||||||
self._interrupted = False
|
self._interrupted = False
|
||||||
|
|
||||||
self._create_idle_task()
|
self._create_idle_task()
|
||||||
|
|
||||||
|
async def _stop(self):
|
||||||
|
self._idle_task.cancel()
|
||||||
|
await self._idle_task
|
||||||
|
|
||||||
async def process_frame(self, frame: Frame, direction: FrameDirection):
|
async def process_frame(self, frame: Frame, direction: FrameDirection):
|
||||||
await super().process_frame(frame, direction)
|
await super().process_frame(frame, direction)
|
||||||
|
|
||||||
|
# Check for end frames before processing
|
||||||
|
if isinstance(frame, (EndFrame, CancelFrame)):
|
||||||
|
await self._stop()
|
||||||
|
|
||||||
await self.push_frame(frame, direction)
|
await self.push_frame(frame, direction)
|
||||||
|
|
||||||
# We shouldn't call the idle callback if the user or the bot are speaking.
|
# We shouldn't call the idle callback if the user or the bot are speaking
|
||||||
if isinstance(frame, UserStartedSpeakingFrame):
|
if isinstance(frame, UserStartedSpeakingFrame):
|
||||||
self._interrupted = True
|
self._interrupted = True
|
||||||
self._idle_event.set()
|
self._idle_event.set()
|
||||||
@@ -56,8 +62,7 @@ class UserIdleProcessor(FrameProcessor):
|
|||||||
self._idle_event.set()
|
self._idle_event.set()
|
||||||
|
|
||||||
async def cleanup(self):
|
async def cleanup(self):
|
||||||
self._idle_task.cancel()
|
await self._stop()
|
||||||
await self._idle_task
|
|
||||||
|
|
||||||
def _create_idle_task(self):
|
def _create_idle_task(self):
|
||||||
self._idle_event = asyncio.Event()
|
self._idle_event = asyncio.Event()
|
||||||
|
|||||||
Reference in New Issue
Block a user