Skip to content

Enterprise agent platform for building, deploying, and operating AI teammates.

refactor(speech): move streaming speech sessions into shared runners (#3834)

Alex Liu25ffac2bf35d
diff --git a/src/xagent/web/api/speech.py b/src/xagent/web/api/speech.py
index 8e31f203a..10a378823 100644
--- a/src/xagent/web/api/speech.py
+++ b/src/xagent/web/api/speech.py
@@ -1,51 +1,34 @@
-"""Authenticated browser transports for model-layer streaming speech."""
+"""Authenticated browser transports for model-layer streaming speech.
+
+These routes own login authentication and resolve the caller's own model
+configuration; the streaming sessions themselves run in
+:mod:`xagent.web.services.speech_streams`.
+"""
from __future__ import annotations
-import asyncio
-import json
-import logging
-from collections.abc import AsyncIterator, Coroutine
-from contextlib import aclosing, suppress
-from dataclasses import asdict
from types import SimpleNamespace
from typing import Any, Literal
-from fastapi import (
- APIRouter,
- Depends,
- HTTPException,
- Query,
- WebSocket,
- WebSocketDisconnect,
-)
-from fastapi.responses import JSONResponse, StreamingResponse
-from pydantic import ValidationError
+from fastapi import APIRouter, Depends, HTTPException, Query, WebSocket
+from fastapi.responses import StreamingResponse
from sqlalchemy.orm import Session
-from starlette.types import Message, Receive, Scope, Send
-from starlette.websockets import WebSocketState
from ...core.model.asr.adapter import get_asr_model_instance
-from ...core.model.asr.base import ASRStreamSession, BaseASR
from ...core.model.tts.adapter import get_tts_model_instance
-from ...core.model.tts.base import BaseTTS, TTSChunk
-from ...core.utils.security import redact_sensitive_text
from ..auth_dependencies import get_current_user
from ..models.database import get_db, get_session_local, release_db_connection_if_clean
from ..models.user import User
from ..schemas.speech import ASRStreamStart, TTSStreamRequest
-from ..services.db_runtime import (
- cancel_and_drain_async_task,
- drain_async_task_cancellation_safe,
- run_db_io_cancellation_safe,
-)
+from ..services.db_runtime import run_db_io_cancellation_safe
from ..services.model_service import _passes_model_visibility_hook
-from ..services.release_hold import WritersRetired, release_hold
-from ..tracking.standalone_usage import (
- STANDALONE_USAGE_REFUSED,
- StandaloneUsageRefused,
- check_standalone_gate_async,
- usage_scope,
+from ..services.speech_streams import (
+ SpeechStreamPlan,
+ _close_asr_socket,
+ _close_model,
+ _log_provider_error,
+ run_asr_stream,
+ tts_stream_response,
)
from .model import _resolve_accessible_model
from .websocket_auth import (
@@ -55,26 +38,8 @@ from .websocket_auth import (
revalidate_websocket_session,
)
-logger = logging.getLogger(__name__)
speech_router = APIRouter(prefix="/api/models/speech", tags=["speech"])
-# Wire limits keep this short-interaction transport bounded. Audio fragments
-# are sent directly to the provider; no recording or unbounded queue is kept.
-_AUDIO_FRAME_BYTES = 64 * 1024
-_CONTROL_FRAME_BYTES = 4096
-_IO_TIMEOUT = 30.0
-_SESSION_SECONDS = 600.0
-# Terminal frames are best effort, including on a transport under backpressure.
-_TERMINAL_TIMEOUT = 1.0
-
-
-class _ASRSessionTimeout(TimeoutError):
- """A local transport deadline, rather than a provider failure."""
-
-
-class _ASRWriteTimeout(_ASRSessionTimeout):
- """The outbound transport stalled; do not send another data frame."""
-
def _load_speech_config(
user_id: int, model_id: str, ability: Literal["asr", "tts"]
@@ -107,135 +72,6 @@ def _load_speech_config(
)
-async def _race(*operations: Coroutine[Any, Any, None]) -> None:
- """Stop and drain both halves when either finishes, fails, or disconnects."""
- tasks = [asyncio.create_task(operation) for operation in operations]
- try:
- done, _ = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
- for task in done:
- await task
- finally:
-
- async def cleanup() -> None:
- for task in tasks:
- await cancel_and_drain_async_task(task)
-
- await drain_async_task_cancellation_safe(asyncio.create_task(cleanup()))
-
-
-def _log_provider_error(
- model: BaseASR | BaseTTS | SimpleNamespace | None, error: Exception
-) -> None:
- detail = str(error) or type(error).__name__
- key = getattr(model, "api_key", None)
- if key:
- detail = detail.replace(key, "[REDACTED]")
- logger.warning("Speech stream failed: %s", redact_sensitive_text(detail))
-
-
-async def _close_model(model: BaseASR | BaseTTS) -> None:
- async def close() -> None:
- method = getattr(model, "aclose", None)
- if callable(method):
- await method()
-
- await drain_async_task_cancellation_safe(asyncio.create_task(close()))
-
-
-async def _receive_asr_message(websocket: WebSocket) -> Message:
- try:
- return await asyncio.wait_for(websocket.receive(), _IO_TIMEOUT)
- except TimeoutError:
- raise _ASRSessionTimeout from None
-
-
-async def _close_asr_socket(
- websocket: WebSocket, code: int = 1000, reason: str = ""
-) -> None:
- if websocket.application_state in {
- WebSocketState.CONNECTING,
- WebSocketState.CONNECTED,
- }:
- with suppress(WebSocketDisconnect, RuntimeError, OSError, TimeoutError):
- await asyncio.wait_for(
- websocket.close(code=code, reason=reason), _TERMINAL_TIMEOUT
- )
-
-
-async def _send_asr_error(
- websocket: WebSocket,
- code: str,
- message: str,
- close_code: int,
- close_reason: str | None = None,
-) -> None:
- if websocket.application_state is WebSocketState.CONNECTED:
- with suppress(WebSocketDisconnect, RuntimeError, OSError, TimeoutError):
- await asyncio.wait_for(
- websocket.send_json(
- {"type": "error", "code": code, "message": message}
- ),
- _TERMINAL_TIMEOUT,
- )
- await _close_asr_socket(
- websocket, close_code, message if close_reason is None else close_reason
- )
-
-
-async def _receive_asr_audio(
- websocket: WebSocket, ready: asyncio.Future[ASRStreamSession], start: ASRStreamStart
-) -> None:
- audio_bytes = 0
- width = 2 if start.format == "pcm" else 1
- while True:
- message = await _receive_asr_message(websocket)
- if message["type"] == "websocket.disconnect":
- raise WebSocketDisconnect(message.get("code", 1000))
- audio = message.get("bytes")
- if audio is not None:
- if len(audio) > _AUDIO_FRAME_BYTES:
- raise ValueError("Audio frame exceeds 64 KiB")
- if len(audio) % width:
- raise ValueError("PCM fragments must contain complete 16-bit samples")
- audio_bytes += len(audio)
- if audio_bytes > start.sample_rate * width * _SESSION_SECONDS:
- raise ValueError("ASR audio duration limit reached")
- stream = await asyncio.shield(ready)
- await stream.send_audio(audio)
- else:
- text = message.get("text") or ""
- if len(text.encode("utf-8")) > _CONTROL_FRAME_BYTES:
- raise ValueError("Control frame exceeds 4 KiB")
- command = json.loads(text)
- if command == {"type": "close"}:
- return
- if command != {"type": "commit"}:
- raise ValueError("Expected binary audio, commit, or close")
- stream = await asyncio.shield(ready)
- await stream.commit()
-
-
-async def _relay_asr_events(
- websocket: WebSocket,
- model: BaseASR,
- ready: asyncio.Future[ASRStreamSession],
- start: ASRStreamStart,
-) -> None:
- options = start.model_dump(exclude={"type", "model_id"}, exclude_none=True)
- try:
- stream = await model.open_stream(**options)
- except ValueError as exc:
- _log_provider_error(model, exc)
- raise HTTPException(status_code=400, detail="ASR model unavailable") from None
- async with stream:
- ready.set_result(stream)
- async for event in stream:
- try:
- await asyncio.wait_for(websocket.send_json(asdict(event)), _IO_TIMEOUT)
- except TimeoutError:
- raise _ASRWriteTimeout from None
-
-
@speech_router.websocket("/asr/stream")
@revalidate_websocket_session
async def stream_asr(
@@ -250,216 +86,23 @@ async def stream_asr(
await _close_asr_socket(websocket, 4001, "Authentication required")
return
await accept_authenticated_websocket(websocket)
- # Transcription meters usage when it ends, so a session is a socket write
- # the host's retire waits for (and winds down when the fence goes up). A
- # retired process cannot write, so the client reconnects to the new one;
- # the close frame needs the handshake accepted first.
- session_task = asyncio.current_task()
- assert session_task is not None
- try:
- write_token = release_hold.acquire_socket_write(session_task.cancel)
- except WritersRetired:
- await _close_asr_socket(websocket, 1012, "Service restart")
- return
- model: BaseASR | None = None
- config: SimpleNamespace | None = None
- model_closed = False
- try:
- message = await _receive_asr_message(websocket)
- if message["type"] == "websocket.disconnect":
- return
- text = message.get("text") or ""
- if len(text.encode("utf-8")) > _CONTROL_FRAME_BYTES:
- raise ValueError("Control frame exceeds 4 KiB")
+
+ async def prepare(text: str) -> SpeechStreamPlan[ASRStreamStart]:
start = ASRStreamStart.model_validate_json(text)
if not await get_authenticated_user(websocket, token, revalidate=True):
- return
+ # The session is gone or already closed; nothing more to send.
+ raise _WebSocketAuthenticationTerminated()
config = await run_db_io_cancellation_safe(
lambda: _load_speech_config(user.id, start.model_id, "asr")
)
- try:
- model = get_asr_model_instance(config)
- except ValueError as exc:
- _log_provider_error(config, exc)
- raise HTTPException(
- status_code=400, detail="Invalid ASR model configuration"
- ) from None
- if not (await model.get_streaming_capabilities()).get("streaming"):
- raise HTTPException(
- status_code=400, detail="Selected ASR model does not support live audio"
- )
- async with usage_scope(
- user.id,
+ return SpeechStreamPlan(
+ user_id=user.id,
+ config=config,
+ options=start,
usage_source="speech_transcribe",
- model_ids=(config.model_id,),
- ):
- try:
- deadline = asyncio.timeout(_SESSION_SECONDS)
- try:
- async with deadline:
- ready: asyncio.Future[ASRStreamSession] = (
- asyncio.get_running_loop().create_future()
- )
- await _race(
- _receive_asr_audio(websocket, ready, start),
- _relay_asr_events(websocket, model, ready, start),
- )
- except TimeoutError:
- if not deadline.expired():
- raise
- raise _ASRSessionTimeout from None
- finally:
- # Close before the opening scope delivers ASR input duration.
- model_closed = True
- await _close_model(model)
- except (WebSocketDisconnect, _WebSocketAuthenticationTerminated):
- return
- except asyncio.CancelledError:
- if not release_hold.socket_writes_fenced:
- raise
- # The retire fence wound this session down: tell the client to
- # reconnect to the successor instead of leaking the cancellation. The
- # usage scope has already delivered what was metered (on its own
- # tracked worker), and the token is held until the finally below.
- if session_task.uncancel() > 0:
- # A foreign cancel (shutdown, the revalidate wrapper) landed with
- # the fence cancel: it must not be swallowed.
- raise
- await _close_asr_socket(websocket, 1012, "Service restart")
- except _ASRWriteTimeout:
- await _close_asr_socket(websocket, 1000, "Speech session timed out")
- except _ASRSessionTimeout:
- await _send_asr_error(
- websocket, "session_timeout", "Speech session timed out", 1000
- )
- except (ValidationError, ValueError) as exc:
- await _send_asr_error(
- websocket, "invalid_request", "Invalid speech stream request", 1008
)
- logger.debug("Invalid ASR stream request: %s", type(exc).__name__)
- except StandaloneUsageRefused as exc:
- # The gate's message can outgrow a close frame's 123-byte reason.
- await _send_asr_error(
- websocket,
- STANDALONE_USAGE_REFUSED,
- exc.message,
- 1008,
- close_reason="Usage limit reached",
- )
- except HTTPException as exc:
- await _send_asr_error(websocket, "model_unavailable", exc.detail, 1008)
- except Exception as exc:
- _log_provider_error(model or config, exc)
- await _send_asr_error(websocket, "stream_failed", "Speech stream failed", 1011)
- finally:
- try:
- if model is not None and not model_closed:
- await _close_model(model)
- await _close_asr_socket(websocket)
- finally:
- write_token.release()
-
-
-class _TTSAudioResponse(StreamingResponse):
- """Own first-audio negotiation, disconnect cancellation, and metering."""
-
- def __init__(self, model: BaseTTS, user_id: int, request: TTSStreamRequest) -> None:
- super().__init__(content=())
- self.model = model
- self.user_id = user_id
- self.options = request.model_dump(exclude={"model_id"}, exclude_none=True)
- async def _audio(
- self, first: TTSChunk, chunks: AsyncIterator[TTSChunk]
- ) -> AsyncIterator[bytes]:
- yield first.audio
- while True:
- try:
- chunk = await asyncio.wait_for(anext(chunks), _IO_TIMEOUT)
- except StopAsyncIteration:
- return
- yield chunk.audio
-
- async def _serve(self, scope: Scope, receive: Receive, send: Send) -> None:
- try:
- async with aclosing(self.model.synthesize_stream(**self.options)) as chunks:
- try:
- first = await asyncio.wait_for(anext(chunks), _IO_TIMEOUT)
- except Exception as exc:
- _log_provider_error(self.model, exc)
- status = (
- 400
- if isinstance(exc, ValueError)
- else 504
- if isinstance(exc, TimeoutError)
- else 502
- )
- await JSONResponse(
- {"detail": "Speech synthesis failed"}, status_code=status
- )(scope, receive, send)
- return
- self.media_type = {
- "mp3": "audio/mpeg",
- "wav": "audio/wav",
- "pcm": "audio/pcm",
- }.get(first.format, "application/octet-stream")
- headers = {
- "Cache-Control": "no-store",
- "X-Accel-Buffering": "no",
- "X-Audio-Format": first.format,
- }
- if first.sample_rate is not None:
- headers["X-Audio-Sample-Rate"] = str(first.sample_rate)
- self.init_headers(headers)
- self.body_iterator = self._audio(first, chunks)
- await self.stream_response(send)
- finally:
- await _close_model(self.model)
-
- async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
- # Meter on the request owner, outside the cancellable stream/disconnect
- # race. An EOF-triggered disconnect must not detach the quota hook.
- try:
- async with usage_scope(
- self.user_id,
- usage_source="speech_synthesize",
- # Stamped by get_tts_model_instance from the model row.
- model_ids=(getattr(self.model, "model_id", ""),),
- ):
- await self._respond(scope, receive, send)
- except StandaloneUsageRefused as exc:
- # Only entering the scope raises this: _respond turns every failure
- # into a response of its own. Nothing was sent or opened yet.
- await _close_model(self.model)
- await JSONResponse({"detail": exc.detail}, status_code=exc.status_code)(
- scope, receive, send
- )
-
- async def _respond(self, scope: Scope, receive: Receive, send: Send) -> None:
- started = False
-
- async def send_response(message: Message) -> None:
- nonlocal started
- if message["type"] == "http.response.start":
- started = True
- await send(message)
-
- try:
- async with asyncio.timeout(_SESSION_SECONDS):
- await _race(
- self._serve(scope, receive, send_response),
- self.listen_for_disconnect(receive),
- )
- except Exception as exc:
- _log_provider_error(self.model, exc)
- if started:
- # Preserve the truncated-response signal without exposing SDK details
- # through an ASGI exception traceback.
- raise RuntimeError("Speech synthesis stream interrupted") from None
- await JSONResponse(
- {"detail": "Speech synthesis failed"},
- status_code=504 if isinstance(exc, TimeoutError) else 502,
- )(scope, receive, send)
+ await run_asr_stream(websocket, prepare=prepare)
@speech_router.post("/tts/stream", response_class=StreamingResponse)
@@ -473,36 +116,14 @@ async def stream_tts(
config = await run_db_io_cancellation_safe(
lambda: _load_speech_config(user_id, request.model_id, "tts")
)
- # The metering scope opens once the response is being sent, too late for a
- # status code; refuse here, before the provider is reached. The scope
- # checks again, for a gate that turns in between.
- await check_standalone_gate_async(
- user_id, "speech_synthesize", model_ids=(config.model_id,)
- )
- try:
- model = get_tts_model_instance(config)
- except ValueError as exc:
- _log_provider_error(config, exc)
- raise HTTPException(
- status_code=400, detail="Invalid TTS model configuration"
- ) from exc
- try:
- capabilities = await model.get_streaming_capabilities()
- except BaseException as exc:
- await _close_model(model)
- if isinstance(exc, Exception):
- _log_provider_error(model, exc)
- raise HTTPException(
- status_code=504 if isinstance(exc, TimeoutError) else 502,
- detail="Speech capabilities unavailable",
- ) from None
- raise
- if not capabilities.get("streaming"):
- await _close_model(model)
- raise HTTPException(
- status_code=400, detail="Selected TTS model does not support streaming"
+ return await tts_stream_response(
+ plan=SpeechStreamPlan(
+ user_id=user_id,
+ config=config,
+ options=request,
+ usage_source="speech_synthesize",
)
- return _TTSAudioResponse(model, user_id, request)
+ )
@speech_router.get("/capabilities")
diff --git a/src/xagent/web/services/speech_streams.py b/src/xagent/web/services/speech_streams.py
new file mode 100644
index 000000000..cf7621c71
--- /dev/null
+++ b/src/xagent/web/services/speech_streams.py
@@ -0,0 +1,490 @@
+"""Streaming speech sessions shared by the speech transport routes.
+
+A route owns authentication and the WebSocket handshake; everything after
+that lives here. :func:`run_asr_stream` takes over an accepted transcription
+socket and :func:`tts_stream_response` takes over a synthesis request whose
+model configuration is already loaded. Both assume the caller has already
+authenticated the request and chosen who pays: they never check credentials
+themselves, and they bill :attr:`SpeechStreamPlan.user_id`.
+"""
+
+from __future__ import annotations
+
+import asyncio
+import json
+import logging
+from collections.abc import AsyncIterator, Awaitable, Callable, Coroutine
+from contextlib import aclosing, suppress
+from dataclasses import asdict, dataclass
+from types import SimpleNamespace
+from typing import Any, Generic, TypeVar
+
+from fastapi import HTTPException, WebSocket, WebSocketDisconnect
+from fastapi.responses import JSONResponse, StreamingResponse
+from pydantic import ValidationError
+from starlette.types import Message, Receive, Scope, Send
+from starlette.websockets import WebSocketState
+
+from ...core.model.asr.adapter import get_asr_model_instance
+from ...core.model.asr.base import ASRStreamSession, BaseASR
+from ...core.model.tts.adapter import get_tts_model_instance
+from ...core.model.tts.base import BaseTTS, TTSChunk
+from ...core.utils.security import redact_sensitive_text
+from ..api.websocket_auth import _WebSocketAuthenticationTerminated
+from ..schemas.speech import ASRStreamStart, TTSStreamRequest
+from ..tracking.standalone_usage import (
+ STANDALONE_USAGE_REFUSED,
+ StandaloneUsageRefused,
+ UsageSource,
+ check_standalone_gate_async,
+ usage_scope,
+)
+from .db_runtime import cancel_and_drain_async_task, drain_async_task_cancellation_safe
+from .release_hold import WritersRetired, release_hold
+
+logger = logging.getLogger(__name__)
+
+# Wire limits keep this short-interaction transport bounded. Audio fragments
+# are sent directly to the provider; no recording or unbounded queue is kept.
+_AUDIO_FRAME_BYTES = 64 * 1024
+_CONTROL_FRAME_BYTES = 4096
+_IO_TIMEOUT = 30.0
+_SESSION_SECONDS = 600.0
+# Terminal frames are best effort, including on a transport under backpressure.
+_TERMINAL_TIMEOUT = 1.0
+
+_OptionsT = TypeVar("_OptionsT", ASRStreamStart, TTSStreamRequest)
+
+
+@dataclass(frozen=True)
+class SpeechStreamPlan(Generic[_OptionsT]):
+ """What a route resolved for one speech session before the provider runs.
+
+ ``user_id`` is the account that pays: the gate is asked for it and usage is
+ metered on it. ``config`` is the loaded model configuration the provider
+ instance is built from. ``options`` is the validated client request: the
+ ASR start frame, or the TTS request whose text and output options are
+ synthesized.
+ """
+
+ user_id: int
+ config: SimpleNamespace
+ options: _OptionsT
+ usage_source: UsageSource
+
+
+class _ASRSessionTimeout(TimeoutError):
+ """A local transport deadline, rather than a provider failure."""
+
+
+class _ASRWriteTimeout(_ASRSessionTimeout):
+ """The outbound transport stalled; do not send another data frame."""
+
+
+async def _race(*operations: Coroutine[Any, Any, None]) -> None:
+ """Stop and drain both halves when either finishes, fails, or disconnects."""
+ tasks = [asyncio.create_task(operation) for operation in operations]
+ try:
+ done, _ = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
+ for task in done:
+ await task
+ finally:
+
+ async def cleanup() -> None:
+ for task in tasks:
+ await cancel_and_drain_async_task(task)
+
+ await drain_async_task_cancellation_safe(asyncio.create_task(cleanup()))
+
+
+def _log_provider_error(
+ model: BaseASR | BaseTTS | SimpleNamespace | None, error: Exception
+) -> None:
+ detail = str(error) or type(error).__name__
+ key = getattr(model, "api_key", None)
+ if key:
+ detail = detail.replace(key, "[REDACTED]")
+ logger.warning("Speech stream failed: %s", redact_sensitive_text(detail))
+
+
+async def _close_model(model: BaseASR | BaseTTS) -> None:
+ async def close() -> None:
+ method = getattr(model, "aclose", None)
+ if callable(method):
+ await method()
+
+ await drain_async_task_cancellation_safe(asyncio.create_task(close()))
+
+
+async def _receive_asr_message(websocket: WebSocket) -> Message:
+ try:
+ return await asyncio.wait_for(websocket.receive(), _IO_TIMEOUT)
+ except TimeoutError:
+ raise _ASRSessionTimeout from None
+
+
+async def _close_asr_socket(
+ websocket: WebSocket, code: int = 1000, reason: str = ""
+) -> None:
+ if websocket.application_state in {
+ WebSocketState.CONNECTING,
+ WebSocketState.CONNECTED,
+ }:
+ with suppress(WebSocketDisconnect, RuntimeError, OSError, TimeoutError):
+ await asyncio.wait_for(
+ websocket.close(code=code, reason=reason), _TERMINAL_TIMEOUT
+ )
+
+
+async def _send_asr_error(
+ websocket: WebSocket,
+ code: str,
+ message: str,
+ close_code: int,
+ close_reason: str | None = None,
+) -> None:
+ if websocket.application_state is WebSocketState.CONNECTED:
+ with suppress(WebSocketDisconnect, RuntimeError, OSError, TimeoutError):
+ await asyncio.wait_for(
+ websocket.send_json(
+ {"type": "error", "code": code, "message": message}
+ ),
+ _TERMINAL_TIMEOUT,
+ )
+ await _close_asr_socket(
+ websocket, close_code, message if close_reason is None else close_reason
+ )
+
+
+async def _receive_asr_audio(
+ websocket: WebSocket, ready: asyncio.Future[ASRStreamSession], start: ASRStreamStart
+) -> None:
+ audio_bytes = 0
+ width = 2 if start.format == "pcm" else 1
+ while True:
+ message = await _receive_asr_message(websocket)
+ if message["type"] == "websocket.disconnect":
+ raise WebSocketDisconnect(message.get("code", 1000))
+ audio = message.get("bytes")
+ if audio is not None:
+ if len(audio) > _AUDIO_FRAME_BYTES:
+ raise ValueError("Audio frame exceeds 64 KiB")
+ if len(audio) % width:
+ raise ValueError("PCM fragments must contain complete 16-bit samples")
+ audio_bytes += len(audio)
+ if audio_bytes > start.sample_rate * width * _SESSION_SECONDS:
+ raise ValueError("ASR audio duration limit reached")
+ stream = await asyncio.shield(ready)
+ await stream.send_audio(audio)
+ else:
+ text = message.get("text") or ""
+ if len(text.encode("utf-8")) > _CONTROL_FRAME_BYTES:
+ raise ValueError("Control frame exceeds 4 KiB")
+ command = json.loads(text)
+ if command == {"type": "close"}:
+ return
+ if command != {"type": "commit"}:
+ raise ValueError("Expected binary audio, commit, or close")
+ stream = await asyncio.shield(ready)
+ await stream.commit()
+
+
+async def _relay_asr_events(
+ websocket: WebSocket,
+ model: BaseASR,
+ ready: asyncio.Future[ASRStreamSession],
+ start: ASRStreamStart,
+) -> None:
+ options = start.model_dump(exclude={"type", "model_id"}, exclude_none=True)
+ try:
+ stream = await model.open_stream(**options)
+ except ValueError as exc:
+ _log_provider_error(model, exc)
+ raise HTTPException(status_code=400, detail="ASR model unavailable") from None
+ async with stream:
+ ready.set_result(stream)
+ async for event in stream:
+ try:
+ await asyncio.wait_for(websocket.send_json(asdict(event)), _IO_TIMEOUT)
+ except TimeoutError:
+ raise _ASRWriteTimeout from None
+
+
+async def run_asr_stream(
+ websocket: WebSocket,
+ *,
+ prepare: Callable[[str], Awaitable[SpeechStreamPlan[ASRStreamStart]]],
+) -> None:
+ """Serve one live transcription session on an accepted, authenticated socket.
+
+ The caller must have authenticated the client and accepted the handshake.
+ From there this owns the whole session: the release-hold write token, the
+ first frame (within ``_IO_TIMEOUT``), the provider instance, metering, the
+ ``_SESSION_SECONDS`` limit, every error frame and close code, and closing
+ the socket. ``prepare`` receives the raw first text frame and returns the
+ plan to run; it may raise ``ValidationError`` or ``ValueError`` (invalid
+ request), ``HTTPException`` (model unavailable), or
+ ``_WebSocketAuthenticationTerminated`` (the client was already answered),
+ which are reported exactly as the same failures inside the session are.
+ """
+ # Transcription meters usage when it ends, so a session is a socket write
+ # the host's retire waits for (and winds down when the fence goes up). A
+ # retired process cannot write, so the client reconnects to the new one;
+ # the close frame needs the handshake accepted first.
+ session_task = asyncio.current_task()
+ assert session_task is not None
+ try:
+ write_token = release_hold.acquire_socket_write(session_task.cancel)
+ except WritersRetired:
+ await _close_asr_socket(websocket, 1012, "Service restart")
+ return
+ model: BaseASR | None = None
+ config: SimpleNamespace | None = None
+ model_closed = False
+ try:
+ message = await _receive_asr_message(websocket)
+ if message["type"] == "websocket.disconnect":
+ return
+ text = message.get("text") or ""
+ if len(text.encode("utf-8")) > _CONTROL_FRAME_BYTES:
+ raise ValueError("Control frame exceeds 4 KiB")
+ plan = await prepare(text)
+ config = plan.config
+ start = plan.options
+ try:
+ model = get_asr_model_instance(config)
+ except ValueError as exc:
+ _log_provider_error(config, exc)
+ raise HTTPException(
+ status_code=400, detail="Invalid ASR model configuration"
+ ) from None
+ if not (await model.get_streaming_capabilities()).get("streaming"):
+ raise HTTPException(
+ status_code=400, detail="Selected ASR model does not support live audio"
+ )
+ async with usage_scope(
+ plan.user_id,
+ usage_source=plan.usage_source,
+ model_ids=(config.model_id,),
+ ):
+ try:
+ deadline = asyncio.timeout(_SESSION_SECONDS)
+ try:
+ async with deadline:
+ ready: asyncio.Future[ASRStreamSession] = (
+ asyncio.get_running_loop().create_future()
+ )
+ await _race(
+ _receive_asr_audio(websocket, ready, start),
+ _relay_asr_events(websocket, model, ready, start),
+ )
+ except TimeoutError:
+ if not deadline.expired():
+ raise
+ raise _ASRSessionTimeout from None
+ finally:
+ # Close before the opening scope delivers ASR input duration.
+ model_closed = True
+ await _close_model(model)
+ except (WebSocketDisconnect, _WebSocketAuthenticationTerminated):
+ return
+ except asyncio.CancelledError:
+ if not release_hold.socket_writes_fenced:
+ raise
+ # The retire fence wound this session down: tell the client to
+ # reconnect to the successor instead of leaking the cancellation. The
+ # usage scope has already delivered what was metered (on its own
+ # tracked worker), and the token is held until the finally below.
+ if session_task.uncancel() > 0:
+ # A foreign cancel (shutdown, the revalidate wrapper) landed with
+ # the fence cancel: it must not be swallowed.
+ raise
+ await _close_asr_socket(websocket, 1012, "Service restart")
+ except _ASRWriteTimeout:
+ await _close_asr_socket(websocket, 1000, "Speech session timed out")
+ except _ASRSessionTimeout:
+ await _send_asr_error(
+ websocket, "session_timeout", "Speech session timed out", 1000
+ )
+ except (ValidationError, ValueError) as exc:
+ await _send_asr_error(
+ websocket, "invalid_request", "Invalid speech stream request", 1008
+ )
+ logger.debug("Invalid ASR stream request: %s", type(exc).__name__)
+ except StandaloneUsageRefused as exc:
+ # The gate's message can outgrow a close frame's 123-byte reason.
+ await _send_asr_error(
+ websocket,
+ STANDALONE_USAGE_REFUSED,
+ exc.message,
+ 1008,
+ close_reason="Usage limit reached",
+ )
+ except HTTPException as exc:
+ await _send_asr_error(websocket, "model_unavailable", exc.detail, 1008)
+ except Exception as exc:
+ _log_provider_error(model or config, exc)
+ await _send_asr_error(websocket, "stream_failed", "Speech stream failed", 1011)
+ finally:
+ try:
+ if model is not None and not model_closed:
+ await _close_model(model)
+ await _close_asr_socket(websocket)
+ finally:
+ write_token.release()
+
+
+class _TTSAudioResponse(StreamingResponse):
+ """Own first-audio negotiation, disconnect cancellation, and metering."""
+
+ def __init__(
+ self,
+ model: BaseTTS,
+ user_id: int,
+ request: TTSStreamRequest,
+ usage_source: UsageSource = "speech_synthesize",
+ ) -> None:
+ super().__init__(content=())
+ self.model = model
+ self.user_id = user_id
+ # Metered under the same source the gate was asked about.
+ self.usage_source: UsageSource = usage_source
+ self.options = request.model_dump(exclude={"model_id"}, exclude_none=True)
+
+ async def _audio(
+ self, first: TTSChunk, chunks: AsyncIterator[TTSChunk]
+ ) -> AsyncIterator[bytes]:
+ yield first.audio
+ while True:
+ try:
+ chunk = await asyncio.wait_for(anext(chunks), _IO_TIMEOUT)
+ except StopAsyncIteration:
+ return
+ yield chunk.audio
+
+ async def _serve(self, scope: Scope, receive: Receive, send: Send) -> None:
+ try:
+ async with aclosing(self.model.synthesize_stream(**self.options)) as chunks:
+ try:
+ first = await asyncio.wait_for(anext(chunks), _IO_TIMEOUT)
+ except Exception as exc:
+ _log_provider_error(self.model, exc)
+ status = (
+ 400
+ if isinstance(exc, ValueError)
+ else 504
+ if isinstance(exc, TimeoutError)
+ else 502
+ )
+ await JSONResponse(
+ {"detail": "Speech synthesis failed"}, status_code=status
+ )(scope, receive, send)
+ return
+ self.media_type = {
+ "mp3": "audio/mpeg",
+ "wav": "audio/wav",
+ "pcm": "audio/pcm",
+ }.get(first.format, "application/octet-stream")
+ headers = {
+ "Cache-Control": "no-store",
+ "X-Accel-Buffering": "no",
+ "X-Audio-Format": first.format,
+ }
+ if first.sample_rate is not None:
+ headers["X-Audio-Sample-Rate"] = str(first.sample_rate)
+ self.init_headers(headers)
+ self.body_iterator = self._audio(first, chunks)
+ await self.stream_response(send)
+ finally:
+ await _close_model(self.model)
+
+ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
+ # Meter on the request owner, outside the cancellable stream/disconnect
+ # race. An EOF-triggered disconnect must not detach the quota hook.
+ try:
+ async with usage_scope(
+ self.user_id,
+ usage_source=self.usage_source,
+ # Stamped by get_tts_model_instance from the model row.
+ model_ids=(getattr(self.model, "model_id", ""),),
+ ):
+ await self._respond(scope, receive, send)
+ except StandaloneUsageRefused as exc:
+ # Only entering the scope raises this: _respond turns every failure
+ # into a response of its own. Nothing was sent or opened yet.
+ await _close_model(self.model)
+ await JSONResponse({"detail": exc.detail}, status_code=exc.status_code)(
+ scope, receive, send
+ )
+
+ async def _respond(self, scope: Scope, receive: Receive, send: Send) -> None:
+ started = False
+
+ async def send_response(message: Message) -> None:
+ nonlocal started
+ if message["type"] == "http.response.start":
+ started = True
+ await send(message)
+
+ try:
+ async with asyncio.timeout(_SESSION_SECONDS):
+ await _race(
+ self._serve(scope, receive, send_response),
+ self.listen_for_disconnect(receive),
+ )
+ except Exception as exc:
+ _log_provider_error(self.model, exc)
+ if started:
+ # Preserve the truncated-response signal without exposing SDK details
+ # through an ASGI exception traceback.
+ raise RuntimeError("Speech synthesis stream interrupted") from None
+ await JSONResponse(
+ {"detail": "Speech synthesis failed"},
+ status_code=504 if isinstance(exc, TimeoutError) else 502,
+ )(scope, receive, send)
+
+
+async def tts_stream_response(
+ *, plan: SpeechStreamPlan[TTSStreamRequest]
+) -> StreamingResponse:
+ """Build the streaming response for one synthesis request.
+
+ The caller must have authenticated the request and loaded ``plan.config``.
+ In order, this asks the usage gate (``StandaloneUsageRefused``, HTTP 402),
+ builds the provider instance, checks that it streams, and returns a
+ response that meters ``plan.user_id`` while it is sent. Failures before
+ the response exists raise ``HTTPException``; the instance is closed on
+ every such path.
+ """
+ # The metering scope opens once the response is being sent, too late for a
+ # status code; refuse here, before the provider is reached. The scope
+ # checks again, for a gate that turns in between.
+ await check_standalone_gate_async(
+ plan.user_id, plan.usage_source, model_ids=(plan.config.model_id,)
+ )
+ try:
+ model = get_tts_model_instance(plan.config)
+ except ValueError as exc:
+ _log_provider_error(plan.config, exc)
+ raise HTTPException(
+ status_code=400, detail="Invalid TTS model configuration"
+ ) from exc
+ try:
+ capabilities = await model.get_streaming_capabilities()
+ except BaseException as exc:
+ await _close_model(model)
+ if isinstance(exc, Exception):
+ _log_provider_error(model, exc)
+ raise HTTPException(
+ status_code=504 if isinstance(exc, TimeoutError) else 502,
+ detail="Speech capabilities unavailable",
+ ) from None
+ raise
+ if not capabilities.get("streaming"):
+ await _close_model(model)
+ raise HTTPException(
+ status_code=400, detail="Selected TTS model does not support streaming"
+ )
+ return _TTSAudioResponse(
+ model, plan.user_id, plan.options, usage_source=plan.usage_source
+ )
diff --git a/tests/web/api/test_speech_stream_runners.py b/tests/web/api/test_speech_stream_runners.py
new file mode 100644
index 000000000..1900f7fe0
--- /dev/null
+++ b/tests/web/api/test_speech_stream_runners.py
@@ -0,0 +1,216 @@
+"""The login ASR route's failure responses, as served by the shared runner."""
+
+# Pytest fixture imports are intentionally shadowed by test parameters.
+# ruff: noqa: F811
+
+from __future__ import annotations
+
+import asyncio
+from types import SimpleNamespace
+from unittest.mock import Mock
+
+import pytest
+from fastapi import HTTPException
+from starlette.websockets import WebSocketDisconnect, WebSocketState
+
+from tests.web.api.test_speech_streaming import ( # noqa: F401
+ ASR_PATH,
+ FakeASR,
+ api,
+)
+from xagent.core.model.chat.token_context import MediaCallType
+from xagent.core.model.media_metering import record_provider_usage
+from xagent.web.api import speech, websocket_auth
+from xagent.web.schemas.speech import TTSStreamRequest
+from xagent.web.services import quota_hooks, speech_streams
+from xagent.web.services.release_hold import release_hold
+
+START = {"type": "start", "model_id": "live"}
+
+
+def _serve(api, start):
+ """Return the error frame (or None) and the close of one login session."""
+ with api.client.websocket_connect(ASR_PATH, subprotocols=api.protocols) as ws:
+ if start is not None:
+ ws.send_json(start)
+ frame = None
+ with pytest.raises(WebSocketDisconnect) as closed:
+ frame = ws.receive_json()
+ ws.receive_json()
+ return frame, (closed.value.code, closed.value.reason)
+
+
[email protected]("case", ["no_start_frame", "missing_model"])
+def test_login_asr_failure_responses_unchanged(api, monkeypatch, case):
+ if case == "no_start_frame":
+ monkeypatch.setattr(speech_streams, "_IO_TIMEOUT", 0.1)
+ start, message, code = None, "Speech session timed out", "session_timeout"
+ close = (1000, "Speech session timed out")
+ else:
+ start = {"type": "start", "model_id": "missing"}
+ message, code = "Model not found", "model_unavailable"
+ close = (1008, "Model not found")
+ frame, closed = _serve(api, start)
+ assert frame == {"type": "error", "code": code, "message": message}
+ assert closed == close
+ api.asr_factory.assert_not_called()
+ assert not api.usage
+
+
+class _FencedSocket:
+ """Records what the session writes while it waits for the first frame."""
+
+ def __init__(self) -> None:
+ self.application_state = WebSocketState.CONNECTING
+ self.sent: list[object] = []
+ self.closes: list[tuple[int, str]] = []
+ self.waiting = asyncio.Event()
+
+ async def receive(self):
+ self.waiting.set()
+ await asyncio.Event().wait()
+
+ async def send_json(self, data) -> None:
+ self.sent.append(data)
+
+ async def close(self, code: int = 1000, reason: str = "") -> None:
+ self.closes.append((code, reason))
+ self.application_state = WebSocketState.DISCONNECTED
+
+
+async def test_login_asr_fence_while_waiting_for_start_closes_1012(monkeypatch):
+ socket = _FencedSocket()
+
+ async def authenticated(*_args, **_kwargs):
+ return SimpleNamespace(id=1)
+
+ async def accept(websocket) -> None:
+ websocket.application_state = WebSocketState.CONNECTED
+
+ monkeypatch.setattr(speech, "get_authenticated_user", authenticated)
+ monkeypatch.setattr(speech, "accept_authenticated_websocket", accept)
+ handler = asyncio.create_task(speech.stream_asr.__wrapped__(socket, token=None))
+ try:
+ await asyncio.wait_for(socket.waiting.wait(), 2)
+ release_hold.mark_writers_retired()
+ await asyncio.wait_for(handler, 2)
+ assert not handler.cancelled()
+ assert socket.sent == []
+ assert socket.closes == [(1012, "Service restart")]
+ assert await release_hold.drain_socket_writes(1.0)
+ finally:
+ if not handler.done():
+ handler.cancel()
+ release_hold.reset_writers_retired()
+
+
+ "raised", ["validation", "value", "http", "authentication_terminated"]
+)
+def test_login_asr_prepare_failures_keep_their_responses(api, monkeypatch, raised):
+ start = START
+ if raised == "validation":
+ start = {"type": "start", "model_id": "live", "sample_rate": 123}
+ elif raised == "value":
+ monkeypatch.setattr(
+ speech, "_load_speech_config", Mock(side_effect=ValueError("bad"))
+ )
+ elif raised == "http":
+ monkeypatch.setattr(
+ speech,
+ "_load_speech_config",
+ Mock(side_effect=HTTPException(400, "Incompatible speech model")),
+ )
+ else:
+ authenticate = speech.get_authenticated_user
+
+ async def terminated_on_revalidate(websocket, token=None, *, revalidate=False):
+ if revalidate:
+ raise websocket_auth._WebSocketAuthenticationTerminated()
+ return await authenticate(websocket, token)
+
+ monkeypatch.setattr(speech, "get_authenticated_user", terminated_on_revalidate)
+ frame, closed = _serve(api, start)
+ expected = {
+ "validation": (
+ {
+ "type": "error",
+ "code": "invalid_request",
+ "message": "Invalid speech stream request",
+ },
+ (1008, "Invalid speech stream request"),
+ ),
+ "value": (
+ {
+ "type": "error",
+ "code": "invalid_request",
+ "message": "Invalid speech stream request",
+ },
+ (1008, "Invalid speech stream request"),
+ ),
+ "http": (
+ {
+ "type": "error",
+ "code": "model_unavailable",
+ "message": "Incompatible speech model",
+ },
+ (1008, "Incompatible speech model"),
+ ),
+ # Authentication already answered the client: no frame of our own.
+ "authentication_terminated": (None, (1000, "")),
+ }[raised]
+ assert (frame, closed) == expected
+ # The release-hold write token is returned on every one of these exits.
+ assert release_hold.socket_write_count == 0
+ api.asr_factory.assert_not_called()
+ assert not api.usage
+
+
+class _ClosingMeteredASR(FakeASR):
+ """Records its ASR duration when the model is closed, as providers do."""
+
+ async def aclose(self):
+ await super().aclose()
+ record_provider_usage(self, MediaCallType.ASR, 7.5)
+
+
+def test_asr_model_is_closed_inside_the_usage_scope(api):
+ model = _ClosingMeteredASR()
+ api.asr_factory.return_value = model
+ with api.client.websocket_connect(ASR_PATH, subprotocols=api.protocols) as ws:
+ ws.send_json(START)
+ assert ws.receive_json()["type"] == "session_started"
+ ws.send_json({"type": "close"})
+ with pytest.raises(WebSocketDisconnect) as closed:
+ ws.receive_json()
+ assert closed.value.code == 1000
+ assert model.close_count == 1
+ assert len(api.usage) == 1
+ user_id, details, source = api.usage[0]
+ assert (user_id, source) == (1, "speech_transcribe")
+ assert [row["quantity"] for row in details] == [7.5]
+
+
+async def test_tts_gate_and_metering_share_the_plan_usage_source(api, monkeypatch):
+ gated = []
+
+ def gate(user_id, source):
+ gated.append((user_id, source))
+
+ monkeypatch.setattr(quota_hooks, "_standalone_gate_hook", gate)
+ plan = speech_streams.SpeechStreamPlan(
+ user_id=1,
+ config=SimpleNamespace(model_id="voice"),
+ options=TTSStreamRequest(model_id="voice", text="Hello"),
+ usage_source="telegram_voice",
+ )
+ response = await speech_streams.tts_stream_response(plan=plan)
+ incoming, messages = asyncio.Queue(), []
+
+ async def send(message):
+ messages.append(message)
+
+ await asyncio.wait_for(response({"type": "http"}, incoming.get, send), 2)
+ assert messages[0]["status"] == 200
+ assert gated and all(source == "telegram_voice" for _, source in gated)
+ assert [(row[0], row[2]) for row in api.usage] == [(1, "telegram_voice")]
diff --git a/tests/web/api/test_speech_streaming.py b/tests/web/api/test_speech_streaming.py
index 03be2f0f6..5c6be8dfa 100644
--- a/tests/web/api/test_speech_streaming.py
+++ b/tests/web/api/test_speech_streaming.py
@@ -28,7 +28,7 @@ from xagent.web.models import database
from xagent.web.models.model import Model
from xagent.web.models.user import User, UserModel
from xagent.web.schemas.speech import TTSStreamRequest
-from xagent.web.services import model_service, quota_hooks
+from xagent.web.services import model_service, quota_hooks, speech_streams
ASR_PATH = "/api/models/speech/asr/stream"
TTS_PATH = "/api/models/speech/tts/stream"
@@ -192,6 +192,8 @@ def api(monkeypatch, tmp_path):
asr_factory, tts_factory = Mock(return_value=asr), Mock(return_value=tts)
monkeypatch.setattr(speech, "get_asr_model_instance", asr_factory)
monkeypatch.setattr(speech, "get_tts_model_instance", tts_factory)
+ monkeypatch.setattr(speech_streams, "get_asr_model_instance", asr_factory)
+ monkeypatch.setattr(speech_streams, "get_tts_model_instance", tts_factory)
usage = []
monkeypatch.setattr(
quota_hooks, "_standalone_usage_hook", lambda *args: usage.append(args)
@@ -365,12 +367,12 @@ def test_asr_frame_size_checks_precede_json_parsing(api, monkeypatch, stage):
parse = Mock(wraps=speech.ASRStreamStart.model_validate_json)
monkeypatch.setattr(speech.ASRStreamStart, "model_validate_json", parse)
else:
- parse = Mock(wraps=speech.json.loads)
+ parse = Mock(wraps=speech_streams.json.loads)
with api.client.websocket_connect(ASR_PATH, subprotocols=api.protocols) as ws:
if stage == "control":
ws.send_json({"type": "start", "model_id": "live"})
assert ws.receive_json()["type"] == "session_started"
- monkeypatch.setattr(speech.json, "loads", parse)
+ monkeypatch.setattr(speech_streams.json, "loads", parse)
command = (
{"type": "start", "model_id": "live"}
if stage == "start"
@@ -434,7 +436,7 @@ def test_asr_failure_before_model_creation_is_logged_safely(
@pytest.mark.parametrize("size", [64 * 1024 + 1, 32002])
def test_asr_rejects_oversized_audio_and_closes_the_session(api, monkeypatch, size):
if size == 32002:
- monkeypatch.setattr(speech, "_SESSION_SECONDS", 1)
+ monkeypatch.setattr(speech_streams, "_SESSION_SECONDS", 1)
with api.client.websocket_connect(ASR_PATH, subprotocols=api.protocols) as ws:
ws.send_json({"type": "start", "model_id": "live"})
ws.receive_json()
@@ -494,9 +496,9 @@ def test_asr_transport_timeouts_are_distinct_from_provider_failure(
api, monkeypatch, caplog, deadline
):
if deadline in {"initial", "idle"}:
- monkeypatch.setattr(speech, "_IO_TIMEOUT", 0.1)
+ monkeypatch.setattr(speech_streams, "_IO_TIMEOUT", 0.1)
elif deadline == "session":
- monkeypatch.setattr(speech, "_SESSION_SECONDS", 0.1)
+ monkeypatch.setattr(speech_streams, "_SESSION_SECONDS", 0.1)
else:
api.asr.send_error = TimeoutError("synthetic-provider-secret")
with api.client.websocket_connect(ASR_PATH, subprotocols=api.protocols) as ws:
@@ -598,8 +600,8 @@ async def test_blocked_asr_terminal_writes_settle_handler_and_watcher(
):
release, blocked_send = asyncio.Event(), asyncio.Event()
messages = []
- monkeypatch.setattr(speech, "_IO_TIMEOUT", 0.03)
- monkeypatch.setattr(speech, "_TERMINAL_TIMEOUT", 0.03, raising=False)
+ monkeypatch.setattr(speech_streams, "_IO_TIMEOUT", 0.03)
+ monkeypatch.setattr(speech_streams, "_TERMINAL_TIMEOUT", 0.03, raising=False)
monkeypatch.setattr(
speech,
"get_authenticated_user",
@@ -709,7 +711,7 @@ async def test_repeated_cancellation_drains_both_stream_halves():
await release.wait()
closed[index].set()
- task = asyncio.create_task(speech._race(operation(0), operation(1)))
+ task = asyncio.create_task(speech_streams._race(operation(0), operation(1)))
try:
await asyncio.wait_for(asyncio.gather(*(event.wait() for event in started)), 2)
task.cancel()
@@ -847,7 +849,7 @@ def test_invalid_tts_output_options_are_local_400_errors(api, options):
def test_tts_first_audio_timeout_cancels_the_provider_and_closes_its_client(
api, monkeypatch
):
- monkeypatch.setattr(speech, "_IO_TIMEOUT", 0.01)
+ monkeypatch.setattr(speech_streams, "_IO_TIMEOUT", 0.01)
api.tts.block_first = True
api.tts.gate = asyncio.Event()
response = api.client.post(
@@ -865,7 +867,7 @@ async def test_tts_sends_first_audio_before_completion_and_cancels_on_disconnect
model = api.tts
model.gate = asyncio.Event()
model.block_first = before_first
- response = speech._TTSAudioResponse(
+ response = speech_streams._TTSAudioResponse(
model, 1, TTSStreamRequest(model_id="voice", text="Hello")
)
incoming = asyncio.Queue()
@@ -903,7 +905,7 @@ async def test_disconnect_during_tts_client_close_waits_for_cleanup_before_meter
closed.set()
api.tts.aclose = close
- response = speech._TTSAudioResponse(
+ response = speech_streams._TTSAudioResponse(
api.tts, 1, TTSStreamRequest(model_id="voice", text="Hello")
)
incoming = asyncio.Queue()
@@ -939,7 +941,7 @@ async def test_disconnect_after_audio_waits_for_usage_delivery(api, monkeypatch)
delivered.set()
monkeypatch.setattr(quota_hooks, "_standalone_usage_hook", delayed_usage)
- response = speech._TTSAudioResponse(
+ response = speech_streams._TTSAudioResponse(
api.tts, 1, TTSStreamRequest(model_id="voice", text="Hello")
)
incoming, messages = asyncio.Queue(), []
@@ -980,11 +982,11 @@ async def test_tts_late_failure_keeps_truncation_without_leaking_provider_detail
else:
api.tts.gate = asyncio.Event()
monkeypatch.setattr(
- speech,
+ speech_streams,
"_IO_TIMEOUT" if failure == "chunk_timeout" else "_SESSION_SECONDS",
0.02,
)
- response = speech._TTSAudioResponse(
+ response = speech_streams._TTSAudioResponse(
api.tts, 1, TTSStreamRequest(model_id="voice", text="Hello")
)
messages, incoming = [], asyncio.Queue()
@@ -1008,8 +1010,8 @@ async def test_tts_late_failure_keeps_truncation_without_leaking_provider_detail
async def test_tts_session_timeout_before_audio_is_a_504(api, monkeypatch):
api.tts.gate = asyncio.Event()
api.tts.block_first = True
- monkeypatch.setattr(speech, "_SESSION_SECONDS", 0.02)
- response = speech._TTSAudioResponse(
+ monkeypatch.setattr(speech_streams, "_SESSION_SECONDS", 0.02)
+ response = speech_streams._TTSAudioResponse(
api.tts, 1, TTSStreamRequest(model_id="voice", text="Hello")
)
messages, incoming = [], asyncio.Queue()
@@ -1035,7 +1037,7 @@ async def test_tts_completion_through_full_app_stack(api, monkeypatch, caplog, f
elif failure:
api.tts.gate = asyncio.Event()
monkeypatch.setattr(
- speech,
+ speech_streams,
"_IO_TIMEOUT" if failure == "chunk_timeout" else "_SESSION_SECONDS",
0.05,
)
diff --git a/tests/web/test_auth_dependencies.py b/tests/web/test_auth_dependencies.py
index 6301ba68c..5685cfcc5 100644
--- a/tests/web/test_auth_dependencies.py
+++ b/tests/web/test_auth_dependencies.py
@@ -763,22 +763,13 @@ def test_auth_consumer_topology_is_closed_across_source_tree() -> None:
"get_authenticated_user",
("WebSocketDisconnect", "_WebSocketAuthenticationTerminated", "Exception"),
),
+ # run_asr_stream handles what this call raises; that mapping is covered
+ # by the parametrized tests in tests/web/api/test_speech_stream_runners.py.
(
"web/api/speech.py",
- "stream_asr",
+ "stream_asr.prepare",
"get_authenticated_user",
- (
- "WebSocketDisconnect",
- "_WebSocketAuthenticationTerminated",
- "CancelledError",
- "_ASRWriteTimeout",
- "_ASRSessionTimeout",
- "ValidationError",
- "ValueError",
- "StandaloneUsageRefused",
- "HTTPException",
- "Exception",
- ),
+ (),
),
)
modules = {"xagent.web.auth_dependencies", "xagent.web.api.websocket_auth"}
diff --git a/tests/web/test_retire_writers.py b/tests/web/test_retire_writers.py
index 3b995bc29..8d82a16f0 100644
--- a/tests/web/test_retire_writers.py
+++ b/tests/web/test_retire_writers.py
@@ -404,6 +404,7 @@ async def test_a_wound_down_asr_session_closes_1012_and_releases_its_token(
monkeypatch,
):
from xagent.web.api import speech
+ from xagent.web.services import speech_streams
blocked = asyncio.Event()
@@ -419,7 +420,7 @@ async def test_a_wound_down_asr_session_closes_1012_and_releases_its_token(
monkeypatch.setattr(speech, "get_authenticated_user", authenticated)
monkeypatch.setattr(speech, "accept_authenticated_websocket", accept)
- monkeypatch.setattr(speech, "_receive_asr_message", receive)
+ monkeypatch.setattr(speech_streams, "_receive_asr_message", receive)
socket = _AsrSocket()
handler = asyncio.create_task(
@@ -1532,6 +1533,7 @@ def test_a_tracked_thread_is_counted_from_before_it_starts_until_it_returns():
async def test_a_foreign_cancel_beside_the_fence_cancel_is_not_swallowed(monkeypatch):
from xagent.web.api import speech
+ from xagent.web.services import speech_streams
blocked = asyncio.Event()
@@ -1547,7 +1549,7 @@ async def test_a_foreign_cancel_beside_the_fence_cancel_is_not_swallowed(monkeyp
monkeypatch.setattr(speech, "get_authenticated_user", authenticated)
monkeypatch.setattr(speech, "accept_authenticated_websocket", accept)
- monkeypatch.setattr(speech, "_receive_asr_message", receive)
+ monkeypatch.setattr(speech_streams, "_receive_asr_message", receive)
socket = _AsrSocket()
handler = asyncio.create_task(