Files
Théo Monnom b2c0fb5b0c Unify provider streams and harden voice lifecycle persistence
Migrate provider WebSocket handling to shared typed protocols, pooling, pacing, and idle timers. Preserve SmallestAI continuations with response-based silence completion and isolated segments.

Keep connection acquisition, reconnect controls, stream cleanup, and host wrapper ownership safe across cancellation. Consolidate provider interfaces and typed MCP contracts.

Normalize pending speech boundaries and preserve conversation identity, ordering, late results, and acknowledged checkpoints across overlapping voice runs. Extend deterministic regressions, storage observations, and lifecycle documentation.

Validation: rebuilt extension; 2900 Python tests passed; formatting, lint, and type checks passed. Rust, binding, codegen, and deterministic simulation checks are recorded in the audit and lifecycle verification records.
2026-11-14 16:10:47 -08:00

187 lines
6.0 KiB
Python

# Copyright 2025 LiveKit, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
from typing import Annotated
from urllib.parse import urlparse
from pydantic import BaseModel, BeforeValidator, JsonValue, ValidationError
from livekit.agents import APIStatusError
from livekit.agents.types import ErrorKind
from .models import (
CUSTOM_VOICE_PREFIX,
MAX_SPEED,
MAX_TEXT_CHARACTERS,
MIN_SPEED,
SUPPORTED_LANGUAGES,
SUPPORTED_MODELS,
SUPPORTED_SAMPLE_RATES,
TTS_STREAM_PATH,
TTS_WEBSOCKET_PATH,
)
# A WebSocket error frame's code; any other code refused the request.
WS_ERROR_KINDS: dict[str, ErrorKind] = {
"rate_limit_exceeded": "rate_limit",
"concurrency_limit_exceeded": "rate_limit",
"tts_workers_busy": "server",
"tts_workers_unconfigured": "server",
"tts_workers_unavailable": "server",
"internal_error": "server",
}
class _ErrorDetail(BaseModel):
code: str | int | None = None
def _error_detail(value: object) -> object:
# only an object carries a code: any other shape is no error detail
return value if isinstance(value, dict) else None
class ErrorResponse(BaseModel):
error: Annotated[_ErrorDetail | None, BeforeValidator(_error_detail)] = None
def normalize_voice(voice: str) -> str:
"""Normalize a preset voice name or custom ``vc_`` voice ID."""
if not voice.strip():
raise ValueError("voice must be a non-empty string")
normalized = voice.strip()
if normalized == CUSTOM_VOICE_PREFIX:
raise ValueError("custom voice IDs must include a value after 'vc_'")
return normalized
def normalize_base_url(base_url: str, *, allow_insecure_base_url: bool = False) -> str:
"""Normalize and validate a public API base URL."""
normalized = base_url.rstrip("/")
parsed = urlparse(normalized)
if parsed.scheme != "http":
return normalized
host = parsed.hostname or ""
if allow_insecure_base_url or host in {"localhost", "127.0.0.1", "::1"}:
return normalized
raise ValueError(
"base_url must use HTTPS unless it points to localhost. "
"Pass allow_insecure_base_url=True only for trusted development networks."
)
def websocket_url(base_url: str) -> str:
"""Build the Vakyam TTS WebSocket URL from an HTTP(S) or WS(S) base URL."""
root = base_url.rstrip("/")
if root.startswith("https://"):
root = "wss://" + root[len("https://") :]
elif root.startswith("http://"):
root = "ws://" + root[len("http://") :]
elif not root.startswith(("ws://", "wss://")):
root = "wss://" + root
if root.endswith(TTS_WEBSOCKET_PATH):
return root
return root + TTS_WEBSOCKET_PATH
def http_stream_url(base_url: str) -> str:
"""Build the Vakyam HTTP streaming TTS URL."""
return base_url.rstrip("/") + TTS_STREAM_PATH
def validate_tts_options(
*,
model: str,
language: str,
sample_rate: int,
speed: float,
voice: str,
) -> None:
"""Validate constructor / update_options values."""
if model not in SUPPORTED_MODELS:
valid = ", ".join(sorted(SUPPORTED_MODELS))
raise ValueError(f"model '{model}' is not supported. Valid values are: {valid}.")
if language not in SUPPORTED_LANGUAGES:
valid = ", ".join(sorted(SUPPORTED_LANGUAGES))
raise ValueError(f"language '{language}' is not supported. Valid values are: {valid}.")
if sample_rate not in SUPPORTED_SAMPLE_RATES:
valid = ", ".join(str(v) for v in sorted(SUPPORTED_SAMPLE_RATES))
raise ValueError(
f"sample_rate '{sample_rate}' is not supported. Valid values are: {valid}."
)
if not MIN_SPEED <= speed <= MAX_SPEED:
raise ValueError(f"speed must be between {MIN_SPEED} and {MAX_SPEED}")
normalize_voice(voice)
def validate_text(text: str) -> None:
"""Validate utterance text for a single synthesis request."""
if not text:
raise ValueError("text is required")
character_count = len(text)
if character_count > MAX_TEXT_CHARACTERS:
raise ValueError(
f"Input text is {character_count} characters. Maximum allowed is "
f"{MAX_TEXT_CHARACTERS} Unicode characters."
)
def speech_payload(
*,
text: str,
model: str,
voice: str,
language: str,
sample_rate: int,
speed: float,
output_format: str = "pcm",
) -> dict[str, JsonValue]:
"""JSON body for HTTP generate/stream requests."""
validate_text(text)
validate_tts_options(
model=model, language=language, sample_rate=sample_rate, speed=speed, voice=voice
)
return {
"text": text,
"model_id": model,
"voice": normalize_voice(voice),
"language": language,
"output_format": output_format,
"sample_rate": sample_rate,
"speed": speed,
}
def raise_http_error(status: int, body: str) -> None:
"""Raise ``APIStatusError`` from a Vakyam HTTP error envelope."""
try:
parsed = ErrorResponse.model_validate_json(body)
except ValidationError:
parsed = None
code = parsed.error.code if parsed is not None and parsed.error is not None else None
error_code = str(code) if code is not None else None
message = f"Vakyam TTS request failed with status {status}"
safe_body: dict[str, int | str] = {"status_code": status}
if error_code is not None:
message += f" (error code: {error_code})"
safe_body["error_code"] = error_code
raise APIStatusError(message, status_code=status, body=safe_body)