Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only run the UserIdleProcessor while pipeline is running #745

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions src/pipecat/processors/user_idle_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
#

import asyncio

from typing import Awaitable, Callable

from pipecat.frames.frames import (
BotSpeakingFrame,
EndFrame,
EndTaskFrame,
Frame,
UserStartedSpeakingFrame,
UserStoppedSpeakingFrame,
Expand All @@ -32,28 +33,34 @@ def __init__(
**kwargs,
):
super().__init__(**kwargs)

self._running = True
self._callback = callback
self._timeout = timeout

self._interrupted = False

self._create_idle_task()

async def process_frame(self, frame: Frame, direction: FrameDirection):
await super().process_frame(frame, direction)

# Check for end frames before processing
if isinstance(frame, (EndFrame, EndTaskFrame)):
self._running = False
self._idle_event.set() # Wake up idle task so it can exit
await self.cleanup()

await self.push_frame(frame, direction)

# We shouldn't call the idle callback if the user or the bot are speaking.
if isinstance(frame, UserStartedSpeakingFrame):
self._interrupted = True
self._idle_event.set()
elif isinstance(frame, UserStoppedSpeakingFrame):
self._interrupted = False
self._idle_event.set()
elif isinstance(frame, BotSpeakingFrame):
self._idle_event.set()
# Only process these frames if we're still running
if self._running:
# We shouldn't call the idle callback if the user or the bot are speaking.
if isinstance(frame, UserStartedSpeakingFrame):
self._interrupted = True
self._idle_event.set()
elif isinstance(frame, UserStoppedSpeakingFrame):
self._interrupted = False
self._idle_event.set()
elif isinstance(frame, BotSpeakingFrame):
self._idle_event.set()

async def cleanup(self):
self._idle_task.cancel()
Expand All @@ -64,7 +71,7 @@ def _create_idle_task(self):
self._idle_task = self.get_event_loop().create_task(self._idle_task_handler())

async def _idle_task_handler(self):
while True:
while self._running:
try:
await asyncio.wait_for(self._idle_event.wait(), timeout=self._timeout)
except asyncio.TimeoutError:
Expand Down
Loading