|
8 | 8 | import unittest |
9 | 9 |
|
10 | 10 | from pipecat.frames.frames import ( |
| 11 | + BotStartedSpeakingFrame, |
11 | 12 | TranscriptionFrame, |
12 | 13 | UserStartedSpeakingFrame, |
13 | 14 | UserStoppedSpeakingFrame, |
14 | 15 | VADUserStartedSpeakingFrame, |
15 | 16 | VADUserStoppedSpeakingFrame, |
16 | 17 | ) |
| 18 | +from pipecat.turns.user_start.min_words_user_turn_start_strategy import ( |
| 19 | + MinWordsUserTurnStartStrategy, |
| 20 | +) |
17 | 21 | from pipecat.turns.user_turn_controller import UserTurnController |
18 | 22 | from pipecat.turns.user_turn_strategies import ExternalUserTurnStrategies, UserTurnStrategies |
19 | 23 | from pipecat.utils.asyncio.task_manager import TaskManager, TaskManagerParams |
@@ -58,6 +62,46 @@ async def on_user_turn_stopped(controller, strategy, params): |
58 | 62 | self.assertTrue(should_start) |
59 | 63 | self.assertTrue(should_stop) |
60 | 64 |
|
| 65 | + async def test_user_turn_start_reset(self): |
| 66 | + controller = UserTurnController( |
| 67 | + user_turn_strategies=UserTurnStrategies( |
| 68 | + start=[MinWordsUserTurnStartStrategy(min_words=3)] |
| 69 | + ), |
| 70 | + user_turn_stop_timeout=USER_TURN_STOP_TIMEOUT, |
| 71 | + ) |
| 72 | + |
| 73 | + await controller.setup(self.task_manager) |
| 74 | + |
| 75 | + should_start = 0 |
| 76 | + |
| 77 | + @controller.event_handler("on_user_turn_started") |
| 78 | + async def on_user_turn_started(controller, strategy, params): |
| 79 | + nonlocal should_start |
| 80 | + should_start += 1 |
| 81 | + |
| 82 | + await controller.process_frame(BotStartedSpeakingFrame()) |
| 83 | + await controller.process_frame(TranscriptionFrame(text="One", user_id="cat", timestamp="")) |
| 84 | + self.assertEqual(should_start, 0) |
| 85 | + |
| 86 | + await controller.process_frame( |
| 87 | + TranscriptionFrame(text=" two three!", user_id="cat", timestamp="") |
| 88 | + ) |
| 89 | + self.assertEqual(should_start, 1) |
| 90 | + |
| 91 | + # Trigger user stop turn so we can trigger user start turn again. |
| 92 | + await asyncio.sleep(USER_TURN_STOP_TIMEOUT + 0.1) |
| 93 | + |
| 94 | + await controller.process_frame(BotStartedSpeakingFrame()) |
| 95 | + await controller.process_frame( |
| 96 | + TranscriptionFrame(text="Hello", user_id="cat", timestamp="") |
| 97 | + ) |
| 98 | + self.assertEqual(should_start, 1) |
| 99 | + |
| 100 | + await controller.process_frame( |
| 101 | + TranscriptionFrame(text=" there friend!", user_id="cat", timestamp="") |
| 102 | + ) |
| 103 | + self.assertEqual(should_start, 2) |
| 104 | + |
61 | 105 | async def test_user_turn_stop_timeout_no_transcription(self): |
62 | 106 | controller = UserTurnController( |
63 | 107 | user_turn_strategies=UserTurnStrategies(), |
|
0 commit comments