Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions changelog/3455.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fixed an issue where user turn start strategies were not being reset after a user turn started, causing incorrect strategy behavior.
4 changes: 4 additions & 0 deletions src/pipecat/turns/user_turn_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ async def _trigger_user_turn_start(
self._user_turn = True
self._user_turn_stop_timeout_event.set()

# Reset all user turn start strategies to start fresh.
for s in self._user_turn_strategies.start or []:
await s.reset()

await self._call_event_handler("on_user_turn_started", strategy, params)

async def _trigger_user_turn_stop(
Expand Down
44 changes: 44 additions & 0 deletions tests/test_user_turn_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
import unittest

from pipecat.frames.frames import (
BotStartedSpeakingFrame,
TranscriptionFrame,
UserStartedSpeakingFrame,
UserStoppedSpeakingFrame,
VADUserStartedSpeakingFrame,
VADUserStoppedSpeakingFrame,
)
from pipecat.turns.user_start.min_words_user_turn_start_strategy import (
MinWordsUserTurnStartStrategy,
)
from pipecat.turns.user_turn_controller import UserTurnController
from pipecat.turns.user_turn_strategies import ExternalUserTurnStrategies, UserTurnStrategies
from pipecat.utils.asyncio.task_manager import TaskManager, TaskManagerParams
Expand Down Expand Up @@ -58,6 +62,46 @@ async def on_user_turn_stopped(controller, strategy, params):
self.assertTrue(should_start)
self.assertTrue(should_stop)

async def test_user_turn_start_reset(self):
controller = UserTurnController(
user_turn_strategies=UserTurnStrategies(
start=[MinWordsUserTurnStartStrategy(min_words=3)]
),
user_turn_stop_timeout=USER_TURN_STOP_TIMEOUT,
)

await controller.setup(self.task_manager)

should_start = 0

@controller.event_handler("on_user_turn_started")
async def on_user_turn_started(controller, strategy, params):
nonlocal should_start
should_start += 1

await controller.process_frame(BotStartedSpeakingFrame())
await controller.process_frame(TranscriptionFrame(text="One", user_id="cat", timestamp=""))
self.assertEqual(should_start, 0)

await controller.process_frame(
TranscriptionFrame(text=" two three!", user_id="cat", timestamp="")
)
self.assertEqual(should_start, 1)

# Trigger user stop turn so we can trigger user start turn again.
await asyncio.sleep(USER_TURN_STOP_TIMEOUT + 0.1)

await controller.process_frame(BotStartedSpeakingFrame())
await controller.process_frame(
TranscriptionFrame(text="Hello", user_id="cat", timestamp="")
)
self.assertEqual(should_start, 1)

await controller.process_frame(
TranscriptionFrame(text=" there friend!", user_id="cat", timestamp="")
)
self.assertEqual(should_start, 2)

async def test_user_turn_stop_timeout_no_transcription(self):
controller = UserTurnController(
user_turn_strategies=UserTurnStrategies(),
Expand Down