fix(voice): stabilize v2 streaming topic handling

This commit is contained in:
Yera All
2026-04-12 21:59:32 +05:00
parent 7f4c80add1
commit 989bf9a0c1
6 changed files with 254 additions and 8 deletions
+39
View File
@@ -1497,6 +1497,45 @@ def test_voice_postprocess_reply_rewrites_false_lookup_promise():
assert "филиал" in reply_text.lower() or "адрес" in reply_text.lower()
def test_voice_postprocess_reply_rewrites_midcall_greeting_with_followup_question():
reply_text = voice_module._voice_postprocess_reply_text(
language="ru",
transcript_text="Здравствуйте, мне надо узнать...",
transcript_window=[
SimpleNamespace(speaker="assistant", text=voice_module._voice_greeting("ru"), sequence_no=1, source_type="voice_policy", barge_in_interrupted=False, created_at=utc_now_iso()),
SimpleNamespace(speaker="caller", text="Здравствуйте, мне надо узнать...", sequence_no=2, source_type="voice_asr", barge_in_interrupted=False, created_at=utc_now_iso()),
],
reply_text="Здравствуйте, Ернур! О чем именно вы хотите узнать?",
kb_results=[],
needs_handoff=False,
)
normalized = reply_text.lower()
assert "здравствуйте" not in normalized
assert "о чем именно" not in normalized
def test_voice_postprocess_reply_reuses_active_topic_after_frustration_turn():
reply_text = voice_module._voice_postprocess_reply_text(
language="ru",
transcript_text="Сколько раз повторять тебе?",
transcript_window=[
SimpleNamespace(speaker="caller", text="Мне надо узнать график работы.", sequence_no=1, source_type="voice_asr", barge_in_interrupted=False, created_at=utc_now_iso()),
SimpleNamespace(speaker="caller", text="В городе Алма-Ата.", sequence_no=2, source_type="voice_asr", barge_in_interrupted=False, created_at=utc_now_iso()),
SimpleNamespace(speaker="caller", text="Меня интересует филиал в Ауэзовском районе.", sequence_no=3, source_type="voice_asr", barge_in_interrupted=False, created_at=utc_now_iso()),
SimpleNamespace(speaker="caller", text="Сколько раз повторять тебе?", sequence_no=4, source_type="voice_asr", barge_in_interrupted=False, created_at=utc_now_iso()),
],
reply_text="Пожалуйста, уточните, какая услуга вас интересует, чтобы я мог подсказать тариф.",
kb_results=[],
needs_handoff=False,
)
normalized = reply_text.lower()
assert "тариф" not in normalized
assert "услуг" not in normalized
assert "филиал" in normalized or "адрес" in normalized
def test_turn_voice_session_early_plan_does_not_persist_partial_turns(monkeypatch):
monkeypatch.setenv("AI_VOICE_POLICY_MODE", "v2_streaming_duplex")
+99
View File
@@ -624,6 +624,7 @@ def test_media_runtime_voice_v2_plays_short_ack_before_main_reply():
frame_ms=20,
frame_bytes=320,
)
actor.finalized_caller_turn_count = 1
await runtime._process_utterance(actor, pcm_frame, False)
asyncio.run(_scenario())
@@ -842,6 +843,7 @@ def test_media_runtime_voice_v2_emits_generic_ack_before_full_asr_without_partia
frame_ms=20,
frame_bytes=320,
)
actor.finalized_caller_turn_count = 1
await runtime._process_utterance(actor, pcm_frame * 45, False)
asyncio.run(_scenario())
@@ -940,6 +942,7 @@ def test_media_runtime_voice_v2_emits_ack_for_short_utterance_after_reduced_thre
frame_ms=20,
frame_bytes=320,
)
actor.finalized_caller_turn_count = 1
await runtime._process_utterance(actor, pcm_frame * 40, False)
asyncio.run(_scenario())
@@ -949,6 +952,100 @@ def test_media_runtime_voice_v2_emits_ack_for_short_utterance_after_reduced_thre
assert speak_events[0][1] < timings["full_finished"]
def test_media_runtime_voice_v2_skips_blind_ack_on_first_turn_without_partial_signal():
speak_events: list[tuple[str, float]] = []
class _FastASRProvider(ASRProvider):
name = "fast-asr"
def transcribe(self, audio_bytes: bytes, *, language_hint: str | None = None) -> ASRTranscription:
assert audio_bytes
time.sleep(0.08)
return ASRTranscription(text="hello there", language=language_hint or "ru", confidence=0.9)
runtime = AudioSocketMediaRuntime(
enabled=True,
host="127.0.0.1",
port=0,
frame_ms=20,
idle_timeout_seconds=2.0,
registration_wait_timeout_seconds=0.5,
min_speech_ms=40,
trailing_silence_ms=40,
max_turn_ms=2000,
asr_provider=_FastASRProvider(),
tts_provider=_StubTTSProvider(),
load_registration_by_media_uuid=lambda value: None,
mark_media_connected=lambda session_id, value: None,
mark_media_ended=lambda session_id, reason: None,
touch_media_frame=lambda session_id: None,
set_state=lambda session_id, state, handoff_reason, metadata: None,
get_pending_greeting=lambda session_id: None,
mark_reply_delivered=lambda session_id, text, is_greeting: None,
plan_reply=lambda session_id, text, metadata, kind: None,
process_turn=lambda session_id, transcript_text, language, barge_in, metadata: VoiceAITurnDecisionOut(
language=language or "ru",
intent="clarification",
reply_text="Подскажите подробнее, пожалуйста.",
confidence=0.9,
needs_handoff=False,
handoff_reason=None,
case_action="keep_open",
kb_refs=[],
summary_text="reply ready",
model="stub-voice",
latency_ms=1,
status="active",
),
request_handoff=lambda session_id, customer_request_text, decision: None,
handle_media_error=lambda session_id, message, metadata: None,
)
async def _fake_speak_text(
current_actor,
text: str,
*,
is_greeting: bool,
style_hints: dict[str, object] | None = None,
) -> None:
del current_actor, is_greeting, style_hints
speak_events.append((text, time.monotonic()))
await asyncio.sleep(0)
runtime._speak_text = _fake_speak_text # type: ignore[method-assign]
pcm_frame = (1000).to_bytes(2, "little", signed=True) * 160
async def _scenario() -> None:
actor = MediaActor(
registration=MediaRegistration(
voice_session_id="avs_media_runtime_v2_first_turn",
call_id="call_media_runtime_v2_first_turn",
interaction_id="int_media_runtime_v2_first_turn",
ai_session_id="ais_media_runtime_v2_first_turn",
language="ru",
media_uuid=str(uuid.uuid4()),
queue_code="voice_lab_ai",
queue_id="que_voice_lab_ai",
agent_profile="voice_support",
voice_v2_enabled=True,
voice_v2_ack_mode="immediate_short",
voice_v2_streaming_tts=True,
voice_v2_partial_asr=False,
),
reader=asyncio.StreamReader(),
writer=None, # type: ignore[arg-type]
vad=EnergyVAD(frame_ms=20, min_speech_ms=40, trailing_silence_ms=40, max_turn_ms=2000),
frame_ms=20,
frame_bytes=320,
)
await runtime._process_utterance(actor, pcm_frame * 40, False)
asyncio.run(_scenario())
assert len(speak_events) == 1
assert speak_events[0][0] == "Подскажите подробнее, пожалуйста."
def test_media_runtime_ignores_low_signal_utterance_without_ack_or_turn():
planned: list[tuple[str, str, str, dict | None]] = []
delivered: list[tuple[str, str, bool]] = []
@@ -1120,6 +1217,7 @@ def test_media_runtime_voice_v2_inserts_small_gap_between_ack_and_main_reply():
frame_ms=20,
frame_bytes=320,
)
actor.finalized_caller_turn_count = 1
await runtime._process_utterance(actor, pcm_frame * 40, False)
asyncio.run(_scenario())
@@ -1226,6 +1324,7 @@ def test_media_runtime_voice_v2_emotive_ack_uses_ru_variants_and_style_hints_onl
frame_ms=20,
frame_bytes=320,
)
actor.finalized_caller_turn_count = 1
pcm_frame = (1000).to_bytes(2, "little", signed=True) * 160
await runtime._process_utterance(actor, pcm_frame, False)
@@ -133,6 +133,49 @@ def test_streaming_asr_sidecar_stream_lifecycle(monkeypatch):
assert len(engine.calls) >= 3
def test_streaming_asr_sidecar_partial_uses_recent_audio_tail(monkeypatch):
engine = _reset_sidecar(monkeypatch)
monkeypatch.setattr(sidecar_module, "_partial_max_audio_ms", lambda: 1800)
client = TestClient(sidecar_module.app)
open_response = client.post(
"/internal/asr/streams",
headers=_admin_headers(),
json={
"session_id": "avs_sidecar_tail",
"language_hint": "ru",
"sample_rate_hz": 8000,
"encoding": "pcm_s16le",
},
)
assert open_response.status_code == 200
stream_id = open_response.json()["stream_id"]
long_chunk = _pcm_chunk(3200)
push_response = client.post(
f"/internal/asr/streams/{stream_id}/chunks",
headers=_admin_headers(),
json={
"pcm_b64": base64.b64encode(long_chunk).decode("ascii"),
"sample_rate_hz": 8000,
"encoding": "pcm_s16le",
},
)
assert push_response.status_code == 200
partial_response = client.get(
f"/internal/asr/streams/{stream_id}/partial",
headers=_admin_headers(),
)
assert partial_response.status_code == 200
assert engine.calls
partial_len, sample_rate_hz, language_hint = engine.calls[-1]
assert sample_rate_hz == 8000
assert language_hint == "ru"
assert partial_len < len(long_chunk)
assert partial_len <= int(1.8 * 8000 * 2)
def test_streaming_asr_sidecar_rejects_unsupported_language(monkeypatch):
_reset_sidecar(monkeypatch, supported_languages={"ru"})
client = TestClient(sidecar_module.app)