fix(voice): race slow streaming asr finalize
This commit is contained in:
@@ -607,8 +607,12 @@ def _voice_reply_with_name(language: str, reply_text: str, name: str | None) ->
|
||||
short_name = _voice_short_name(name)
|
||||
if not short_name:
|
||||
return reply_text
|
||||
reply_text = _voice_strip_reply_greeting_prefix(reply_text, name=short_name)
|
||||
prefix = _voice_disclosure_prefix(language)
|
||||
normalized_short = _voice_text_key(short_name)
|
||||
first_words = _voice_text_key(" ".join(str(reply_text or "").split()[:8])).split()
|
||||
if normalized_short in first_words:
|
||||
return reply_text
|
||||
if reply_text.startswith(prefix):
|
||||
rest = reply_text[len(prefix) :].lstrip()
|
||||
if _voice_text_key(rest).startswith(normalized_short):
|
||||
@@ -1344,6 +1348,28 @@ def _voice_is_midcall_greeting_reply(text: str | None) -> bool:
|
||||
)
|
||||
|
||||
|
||||
def _voice_strip_reply_greeting_prefix(reply_text: str | None, *, name: str | None = None) -> str:
|
||||
text = str(reply_text or "").strip()
|
||||
if not text:
|
||||
return text
|
||||
greeting_pattern = (
|
||||
r"^\s*(?:здравствуйте|здравствуй|привет(?:ствую)?|"
|
||||
r"добрый\s+(?:день|вечер|утро)|сәлеметсіз\s+бе|сәлем)\b[\s,!.:-]*"
|
||||
)
|
||||
stripped = re.sub(greeting_pattern, "", text, count=1, flags=re.IGNORECASE).strip()
|
||||
if name and stripped != text:
|
||||
short_name = _voice_short_name(name)
|
||||
if short_name:
|
||||
stripped = re.sub(
|
||||
rf"^\s*{re.escape(short_name)}\b[\s,!.:-]*",
|
||||
"",
|
||||
stripped,
|
||||
count=1,
|
||||
flags=re.IGNORECASE,
|
||||
).strip()
|
||||
return stripped or text
|
||||
|
||||
|
||||
def _voice_contains_false_lookup_promise(text: str | None) -> bool:
|
||||
normalized = _voice_text_key(text)
|
||||
if not normalized:
|
||||
@@ -1388,15 +1414,16 @@ def _voice_postprocess_reply_text(
|
||||
prior_caller_texts = _voice_caller_context_before_current(caller_texts, transcript_text)
|
||||
active_topic_texts = _voice_service_context_texts(prior_caller_texts or caller_texts)
|
||||
active_topic_prompt = _voice_topic_prompt(language, active_topic_texts) if active_topic_texts else None
|
||||
has_assistant_context = any(segment.speaker == "assistant" for segment in transcript_window)
|
||||
if _voice_is_hearing_check_caller_text(transcript_text):
|
||||
return _voice_hearing_check_reply(language, prior_caller_texts or caller_texts)
|
||||
if _voice_contains_false_lookup_promise(normalized_reply) and not kb_results:
|
||||
return active_topic_prompt or _voice_topic_prompt(language, caller_texts) or _voice_generic_prompt(language)
|
||||
if _voice_is_midcall_greeting_reply(normalized_reply) and any(
|
||||
segment.speaker == "assistant" for segment in transcript_window
|
||||
):
|
||||
if _voice_is_midcall_greeting_reply(normalized_reply) and has_assistant_context:
|
||||
context_texts = prior_caller_texts if _voice_is_low_signal_caller_text(transcript_text) else caller_texts
|
||||
return _voice_topic_prompt(language, context_texts) or _voice_generic_prompt(language)
|
||||
if has_assistant_context or _voice_has_service_topic(transcript_text):
|
||||
normalized_reply = _voice_strip_reply_greeting_prefix(normalized_reply)
|
||||
if (
|
||||
active_topic_prompt
|
||||
and not _voice_has_service_topic(transcript_text)
|
||||
|
||||
@@ -40,6 +40,16 @@ from services.shared.models import VoiceAITurnDecisionOut
|
||||
logger = logging.getLogger("uvicorn.error")
|
||||
|
||||
|
||||
def _env_float(name: str, default: float) -> float:
|
||||
raw = str(os.getenv(name, "")).strip()
|
||||
if not raw:
|
||||
return default
|
||||
try:
|
||||
return float(raw)
|
||||
except ValueError:
|
||||
return default
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class MediaRegistration:
|
||||
voice_session_id: str
|
||||
@@ -204,6 +214,10 @@ class AudioSocketMediaRuntime:
|
||||
self._streaming_asr_push_queue_max_frames = 250
|
||||
self._streaming_asr_push_batch_max_bytes = self._frame_bytes * 8
|
||||
self._streaming_asr_push_drain_timeout_seconds = 1.5
|
||||
self._streaming_finalize_race_grace_seconds = max(
|
||||
0.0,
|
||||
_env_float("AI_VOICE_V2_STREAMING_FINALIZE_RACE_GRACE_SECONDS", 0.35),
|
||||
)
|
||||
self._thinking_continuation_grace_seconds = 0.45
|
||||
self._thinking_continuation_max_bytes = int(1800 * 16)
|
||||
self._partial_first_final_enabled = (
|
||||
@@ -1056,6 +1070,68 @@ class AudioSocketMediaRuntime:
|
||||
finally:
|
||||
await asyncio.to_thread(self._streaming_asr_provider.close_stream, stream_id)
|
||||
|
||||
def _observe_late_streaming_finalize(self, actor: MediaActor, task: asyncio.Task) -> None:
|
||||
def _done(done_task: asyncio.Task) -> None:
|
||||
try:
|
||||
done_task.result()
|
||||
except asyncio.CancelledError:
|
||||
return
|
||||
except StreamingASRUnavailable as exc:
|
||||
logger.warning(
|
||||
"audiosocket.streaming_asr_late_finalize_failed session_id=%s error=%s",
|
||||
actor.registration.voice_session_id,
|
||||
str(exc)[:500],
|
||||
)
|
||||
self._mark_streaming_asr_backoff(actor)
|
||||
except Exception as exc:
|
||||
logger.warning(
|
||||
"audiosocket.streaming_asr_late_finalize_error session_id=%s error=%s",
|
||||
actor.registration.voice_session_id,
|
||||
str(exc)[:500],
|
||||
)
|
||||
|
||||
task.add_done_callback(_done)
|
||||
|
||||
async def _batch_transcribe_turn(
|
||||
self,
|
||||
actor: MediaActor,
|
||||
*,
|
||||
pcm_bytes: bytes,
|
||||
) -> ASRTranscription:
|
||||
wav_bytes = pcm16le_to_wav_bytes(pcm_bytes, sample_rate_hz=8000)
|
||||
return await asyncio.to_thread(
|
||||
self._asr_provider.transcribe,
|
||||
wav_bytes,
|
||||
language_hint=actor.registration.language,
|
||||
)
|
||||
|
||||
async def _finalize_transcription_after_streaming_failure(
|
||||
self,
|
||||
actor: MediaActor,
|
||||
*,
|
||||
pcm_bytes: bytes,
|
||||
partial_transcript: str,
|
||||
error: Exception,
|
||||
) -> tuple[ASRTranscription, str]:
|
||||
logger.warning(
|
||||
"audiosocket.streaming_asr_finalize_failed session_id=%s error=%s",
|
||||
actor.registration.voice_session_id,
|
||||
str(error)[:500],
|
||||
)
|
||||
self._mark_streaming_asr_backoff(actor)
|
||||
partial_first_text = str(partial_transcript or actor.stable_partial_transcript or actor.partial_transcript or "").strip()
|
||||
if self._partial_first_final_enabled and partial_first_text and not self._is_low_signal_transcript(partial_first_text):
|
||||
return (
|
||||
ASRTranscription(
|
||||
text=partial_first_text,
|
||||
language=actor.registration.language,
|
||||
confidence=None,
|
||||
),
|
||||
"streaming_partial_after_finalize_failure",
|
||||
)
|
||||
transcription = await self._batch_transcribe_turn(actor, pcm_bytes=pcm_bytes)
|
||||
return transcription, "batch_fallback_after_streaming_failure"
|
||||
|
||||
async def _finalize_turn_transcription(
|
||||
self,
|
||||
actor: MediaActor,
|
||||
@@ -1066,45 +1142,91 @@ class AudioSocketMediaRuntime:
|
||||
) -> tuple[ASRTranscription, str]:
|
||||
if detached_stream is not None and detached_stream[0]:
|
||||
stream_id, push_task, push_queue, streaming_failed = detached_stream
|
||||
try:
|
||||
transcription = await self._finalize_detached_streaming_transcription(
|
||||
streaming_task = asyncio.create_task(
|
||||
self._finalize_detached_streaming_transcription(
|
||||
actor,
|
||||
stream_id=stream_id,
|
||||
push_task=push_task,
|
||||
push_queue=push_queue,
|
||||
streaming_failed=streaming_failed,
|
||||
)
|
||||
return transcription, "streaming_final"
|
||||
except StreamingASRUnavailable as exc:
|
||||
logger.warning(
|
||||
"audiosocket.streaming_asr_finalize_failed session_id=%s error=%s",
|
||||
actor.registration.voice_session_id,
|
||||
str(exc)[:500],
|
||||
)
|
||||
if self._streaming_finalize_race_grace_seconds > 0:
|
||||
done, _pending = await asyncio.wait(
|
||||
{streaming_task},
|
||||
timeout=self._streaming_finalize_race_grace_seconds,
|
||||
)
|
||||
self._mark_streaming_asr_backoff(actor)
|
||||
partial_first_text = str(partial_transcript or actor.stable_partial_transcript or actor.partial_transcript or "").strip()
|
||||
if self._partial_first_final_enabled and partial_first_text and not self._is_low_signal_transcript(partial_first_text):
|
||||
return (
|
||||
ASRTranscription(
|
||||
text=partial_first_text,
|
||||
language=actor.registration.language,
|
||||
confidence=None,
|
||||
),
|
||||
"streaming_partial_after_finalize_failure",
|
||||
if streaming_task in done:
|
||||
try:
|
||||
return streaming_task.result(), "streaming_final"
|
||||
except StreamingASRUnavailable as exc:
|
||||
return await self._finalize_transcription_after_streaming_failure(
|
||||
actor,
|
||||
pcm_bytes=pcm_bytes,
|
||||
partial_transcript=partial_transcript,
|
||||
error=exc,
|
||||
)
|
||||
partial_first_text = str(partial_transcript or actor.stable_partial_transcript or actor.partial_transcript or "").strip()
|
||||
if self._partial_first_final_enabled and partial_first_text and not self._is_low_signal_transcript(partial_first_text):
|
||||
self._observe_late_streaming_finalize(actor, streaming_task)
|
||||
return (
|
||||
ASRTranscription(
|
||||
text=partial_first_text,
|
||||
language=actor.registration.language,
|
||||
confidence=None,
|
||||
),
|
||||
"streaming_partial_before_slow_finalize",
|
||||
)
|
||||
batch_task = asyncio.create_task(
|
||||
self._batch_transcribe_turn(actor, pcm_bytes=pcm_bytes),
|
||||
name=f"voice-batch-asr-race-{actor.registration.voice_session_id}",
|
||||
)
|
||||
done, _pending = await asyncio.wait(
|
||||
{streaming_task, batch_task},
|
||||
return_when=asyncio.FIRST_COMPLETED,
|
||||
)
|
||||
if streaming_task in done:
|
||||
try:
|
||||
return streaming_task.result(), "streaming_final"
|
||||
except StreamingASRUnavailable as exc:
|
||||
logger.warning(
|
||||
"audiosocket.streaming_asr_finalize_failed session_id=%s error=%s",
|
||||
actor.registration.voice_session_id,
|
||||
str(exc)[:500],
|
||||
)
|
||||
wav_bytes = pcm16le_to_wav_bytes(pcm_bytes, sample_rate_hz=8000)
|
||||
transcription = await asyncio.to_thread(
|
||||
self._asr_provider.transcribe,
|
||||
wav_bytes,
|
||||
language_hint=actor.registration.language,
|
||||
)
|
||||
return transcription, "batch_fallback_after_streaming_failure"
|
||||
wav_bytes = pcm16le_to_wav_bytes(pcm_bytes, sample_rate_hz=8000)
|
||||
transcription = await asyncio.to_thread(
|
||||
self._asr_provider.transcribe,
|
||||
wav_bytes,
|
||||
language_hint=actor.registration.language,
|
||||
)
|
||||
self._mark_streaming_asr_backoff(actor)
|
||||
partial_first_text = str(
|
||||
partial_transcript or actor.stable_partial_transcript or actor.partial_transcript or ""
|
||||
).strip()
|
||||
if (
|
||||
self._partial_first_final_enabled
|
||||
and partial_first_text
|
||||
and not self._is_low_signal_transcript(partial_first_text)
|
||||
):
|
||||
return (
|
||||
ASRTranscription(
|
||||
text=partial_first_text,
|
||||
language=actor.registration.language,
|
||||
confidence=None,
|
||||
),
|
||||
"streaming_partial_after_finalize_failure",
|
||||
)
|
||||
return await batch_task, "batch_fallback_after_streaming_failure"
|
||||
try:
|
||||
transcription = batch_task.result()
|
||||
except Exception:
|
||||
try:
|
||||
return await streaming_task, "streaming_final"
|
||||
except StreamingASRUnavailable as exc:
|
||||
return await self._finalize_transcription_after_streaming_failure(
|
||||
actor,
|
||||
pcm_bytes=pcm_bytes,
|
||||
partial_transcript=partial_transcript,
|
||||
error=exc,
|
||||
)
|
||||
self._observe_late_streaming_finalize(actor, streaming_task)
|
||||
return transcription, "batch_race_before_streaming_finalize"
|
||||
transcription = await self._batch_transcribe_turn(actor, pcm_bytes=pcm_bytes)
|
||||
return transcription, "batch"
|
||||
|
||||
async def _record_reply_status(
|
||||
|
||||
Reference in New Issue
Block a user