-
Notifications
You must be signed in to change notification settings - Fork 3.4k
fix(voice): make STT the user_state source when turn_detection is stt #6641
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
Open
biztex
wants to merge
3
commits into
livekit:main
Choose a base branch
from
biztex:fix/stt-turn-detection-user-state
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+276
−10
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| """Regression tests for #5580: with STT-driven turn detection, VAD must not drive user_state. | ||
|
|
||
| When ``turn_detection="stt"``, both VAD and STT used to write ``user_state``. | ||
| In noisy environments VAD flips it to "speaking" on background noise even when | ||
| the STT hears nothing, breaking everything keyed on user state (away timeouts, | ||
| filler triggering) — and the only workaround, ``vad=None``, also gave up | ||
| VAD-based interruption sensing. STT is now the authoritative ``user_state`` | ||
| source in that mode, while VAD keeps its interruption/endpointing roles. | ||
| """ | ||
|
|
||
| import time | ||
|
|
||
| import pytest | ||
|
|
||
| from livekit.agents import vad | ||
| from livekit.agents.voice.agent_activity import AgentActivity | ||
|
|
||
| from .fake_session import FakeActions, create_session | ||
| from .test_agent_session import MyAgent, _close_test_session | ||
|
|
||
| pytestmark = [pytest.mark.unit, pytest.mark.virtual_time, pytest.mark.no_concurrent] | ||
|
|
||
|
|
||
| def _vad_event(type_: vad.VADEventType) -> vad.VADEvent: | ||
| return vad.VADEvent( | ||
| type=type_, | ||
| samples_index=0, | ||
| timestamp=time.time(), | ||
| speech_duration=0.5, | ||
| silence_duration=0.0, | ||
| ) | ||
|
|
||
|
|
||
| def _make_activity(turn_detection: str | None) -> AgentActivity: | ||
| session = create_session(FakeActions(), turn_handling={"turn_detection": turn_detection}) | ||
| return AgentActivity(MyAgent(), session) | ||
|
|
||
|
|
||
| class TestSttDrivenUserState: | ||
| async def test_vad_noise_does_not_flip_user_state_in_stt_mode(self) -> None: | ||
| activity = _make_activity("stt") | ||
| session = activity._session | ||
| try: | ||
| assert session.user_state == "listening" | ||
|
|
||
| # background noise: VAD fires but the STT hears no speech | ||
| activity.on_start_of_speech(_vad_event(vad.VADEventType.START_OF_SPEECH), time.time()) | ||
| assert session.user_state == "listening" | ||
|
|
||
| activity.on_end_of_speech(_vad_event(vad.VADEventType.END_OF_SPEECH)) | ||
| assert session.user_state == "listening" | ||
| finally: | ||
| await _close_test_session(session) | ||
|
|
||
| async def test_stt_speech_drives_user_state_in_stt_mode(self) -> None: | ||
| activity = _make_activity("stt") | ||
| session = activity._session | ||
| try: | ||
| # STT-sourced hook calls pass ev=None | ||
| activity.on_start_of_speech(None, time.time()) | ||
| assert session.user_state == "speaking" | ||
|
|
||
| activity.on_end_of_speech(None) | ||
| assert session.user_state == "listening" | ||
| finally: | ||
| await _close_test_session(session) | ||
|
|
||
| async def test_vad_drives_user_state_in_default_mode(self) -> None: | ||
| activity = _make_activity(None) | ||
| session = activity._session | ||
| try: | ||
| activity.on_start_of_speech(_vad_event(vad.VADEventType.START_OF_SPEECH), time.time()) | ||
| assert session.user_state == "speaking" | ||
|
|
||
| activity.on_end_of_speech(_vad_event(vad.VADEventType.END_OF_SPEECH)) | ||
| assert session.user_state == "listening" | ||
| finally: | ||
| await _close_test_session(session) | ||
|
|
||
| async def test_vad_end_recovers_speaking_written_by_non_stt_source(self) -> None: | ||
| # "speaking" can be entered by writers the STT will never clear | ||
| # (claim_user_turn re-derivation, a turn_detection switch mid-speech); | ||
| # a VAD end-of-speech must recover the state instead of leaving it | ||
| # stuck at "speaking" with no STT end-of-speech ever coming | ||
| activity = _make_activity("stt") | ||
| session = activity._session | ||
| try: | ||
| session._update_user_state("speaking", last_speaking_time=time.time()) | ||
| assert session.user_state == "speaking" | ||
|
|
||
| activity.on_end_of_speech(_vad_event(vad.VADEventType.END_OF_SPEECH)) | ||
| assert session.user_state == "listening" | ||
| finally: | ||
| await _close_test_session(session) | ||
|
|
||
| async def test_vad_end_does_not_clear_stt_authored_speaking(self) -> None: | ||
| # the VAD usually endpoints before the STT: its end-of-speech must not | ||
| # cut short a "speaking" state the STT opened and will close itself | ||
| activity = _make_activity("stt") | ||
| session = activity._session | ||
| try: | ||
| activity.on_start_of_speech(None, time.time()) | ||
| assert session.user_state == "speaking" | ||
|
|
||
| activity.on_end_of_speech(_vad_event(vad.VADEventType.END_OF_SPEECH)) | ||
| assert session.user_state == "speaking" | ||
|
|
||
| activity.on_end_of_speech(None) | ||
| assert session.user_state == "listening" | ||
| finally: | ||
| await _close_test_session(session) | ||
|
|
||
| async def test_claimed_turn_with_vad_noise_recovers(self) -> None: | ||
| # background noise trips the VAD during a programmatic (text) turn: | ||
| # the release re-derives "speaking" from the VAD-driven silence event, | ||
| # and only the later VAD end-of-speech can clear it (the STT heard | ||
| # nothing, so no STT end-of-speech will ever arrive) | ||
| activity = _make_activity("stt") | ||
| session = activity._session | ||
| session._activity = activity | ||
| try: | ||
| async with session._claim_user_turn(): | ||
| activity.on_start_of_speech( | ||
| _vad_event(vad.VADEventType.START_OF_SPEECH), time.time() | ||
| ) | ||
| assert session.user_state == "speaking" | ||
|
|
||
| activity.on_end_of_speech(_vad_event(vad.VADEventType.END_OF_SPEECH)) | ||
| assert session.user_state == "listening" | ||
| finally: | ||
| await _close_test_session(session) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.