mirror of
https://github.com/livekit/agents.git
synced 2026-12-06 04:18:36 +00:00
56 lines
1.9 KiB
Python
56 lines
1.9 KiB
Python
"""Automatic streaming adaptation preserves a repeated recognizer's health identity."""
|
|
|
|
import pytest
|
|
|
|
from livekit import rtc
|
|
from livekit.agents import NOT_GIVEN, APIError, LanguageCode, NotGivenOr, stt
|
|
from livekit.agents.types import DEFAULT_API_CONNECT_OPTIONS, APIConnectOptions
|
|
|
|
from .fake_vad import FakeVAD
|
|
|
|
pytestmark = pytest.mark.unit
|
|
|
|
|
|
class BatchStt(stt.STT):
|
|
def __init__(self, *, fail: bool) -> None:
|
|
super().__init__(capabilities=stt.STTCapabilities(streaming=False, interim_results=False))
|
|
self.fail = fail
|
|
|
|
async def _recognize_impl(
|
|
self,
|
|
buffer: list[rtc.AudioFrame] | rtc.AudioFrame,
|
|
*,
|
|
language: NotGivenOr[str] = NOT_GIVEN,
|
|
conn_options: APIConnectOptions = DEFAULT_API_CONNECT_OPTIONS,
|
|
) -> stt.SpeechEvent:
|
|
if self.fail:
|
|
raise APIError("recognizer failed", retryable=True)
|
|
return stt.SpeechEvent(
|
|
type=stt.SpeechEventType.FINAL_TRANSCRIPT,
|
|
alternatives=[stt.SpeechData(language=LanguageCode("en"), text="Recovered.")],
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("batch", [False, True])
|
|
async def test_repeated_batch_recognizers_share_one_health_transition_after_adaptation(
|
|
batch: bool,
|
|
) -> None:
|
|
primary, backup = BatchStt(fail=True), BatchStt(fail=False)
|
|
adapter = stt.FallbackAdapter(
|
|
[primary, primary, backup], vad=FakeVAD(), max_retry_per_stt=0, retry_interval=0
|
|
)
|
|
unavailable: list[stt.STT] = []
|
|
|
|
@adapter.on("stt_availability_changed")
|
|
def changed(event: stt.AvailabilityChangedEvent) -> None:
|
|
if not event.available:
|
|
unavailable.append(event.stt)
|
|
|
|
try:
|
|
frame = rtc.AudioFrame.create(16_000, 1, 160)
|
|
result = await adapter.recognize([frame] if batch else frame)
|
|
assert result.alternatives[0].text == "Recovered."
|
|
finally:
|
|
await adapter.aclose()
|
|
assert len(unavailable) == 1, "automatic wrappers must share a repeated backend's health"
|