HEL-899: custom voice via reference audio on the unified API — reuse .12 panel zero-shot registry (server-side proxy, cg- namespaced IDs never collide with the 11 builtin voices, panel stays the only voices.json writer) + CosyVoice fresh-synthesis preview (unknown voice pre-checked: no silent default fallback) + persisted selection/manifest/usage journal in instance data space with atomic writes and full rollback; session-open captures the effective voice (selection if valid else worker-file value else default, D1 domain untouched when user never chose) pinned per call, mid-call changes land at next call at the earliest; explicit allow_asr gates FunASR transcription, preview/ASR journaled, preview writes no chat/memory/receipt rows; zero new deps (raw-bytes upload endpoint), zero real calls in tests: 18 new cases (formats/size/duration refusals, collision+concurrency, panel down/reject/lying, tts down, rollback, restart persistence, call pinning, identity/shared-state untouched) + offline chain script 12/12, suite 370; req governance v1.12: CG-010 boundary updated to custom-voice-first per user comment 01a17de1 (verbatim evidence + changes + manifest), checks pass vs origin/main and base 8dc84f8 with handoff-HEL-899
Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
+35
-13
@@ -15,6 +15,7 @@ from typing import Literal
|
||||
from uuid import UUID
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
import httpx
|
||||
import yaml
|
||||
from anyio import CancelScope
|
||||
from fastapi import FastAPI, HTTPException, Query, Request
|
||||
@@ -25,6 +26,7 @@ from pydantic import BaseModel, ConfigDict, Field, StrictInt, field_validator
|
||||
|
||||
from ..config import CharacterConfig, ModelsConfig
|
||||
from ..contracts import LLMMessage
|
||||
from ..contracts.text import safe_text
|
||||
from ..core.bazi import (
|
||||
GAN_ELEM,
|
||||
Derived,
|
||||
@@ -48,7 +50,13 @@ from ..memory.semantic import build_semantic
|
||||
from ..memory.store import Store
|
||||
from ..think.scenes import Scene
|
||||
from ..think.wiring import build_router_from_config
|
||||
from ..voice import VoiceSessionService, build_voice_router, load_voice_config
|
||||
from ..voice import (
|
||||
CustomVoiceConfig,
|
||||
CustomVoiceService,
|
||||
VoiceSessionService,
|
||||
build_voice_router,
|
||||
load_voice_config,
|
||||
)
|
||||
|
||||
HOURS = [
|
||||
"子时",
|
||||
@@ -67,17 +75,6 @@ HOURS = [
|
||||
ERROR = "她这会儿没接上话,稍后再试试。你写的话还在。"
|
||||
|
||||
|
||||
def safe_text(value: str, maximum: int, label: str, required: bool = False) -> str:
|
||||
value = value.strip()
|
||||
if (
|
||||
(required and not value)
|
||||
or len(value) > maximum
|
||||
or any(ord(c) < 32 or ord(c) == 127 or c in "<>" for c in value)
|
||||
):
|
||||
raise ValueError(f"{label}请填写{1 if required else 0}–{maximum}字,不含控制字符或尖括号。")
|
||||
return value
|
||||
|
||||
|
||||
class StrictBody(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid", strict=True)
|
||||
|
||||
@@ -182,6 +179,7 @@ def create_app(
|
||||
semantic_mode: str | None = None,
|
||||
semantic_config: dict | None = None,
|
||||
voice_service=None,
|
||||
custom_voice_service=None,
|
||||
) -> FastAPI:
|
||||
"""Dependencies can be injected for isolated fault/cancellation tests, never via browser flags."""
|
||||
db_path = db_path or os.environ.get(
|
||||
@@ -291,6 +289,8 @@ def create_app(
|
||||
await provider.aclose()
|
||||
if getattr(app.state, "voice_service", None):
|
||||
await app.state.voice_service.aclose()
|
||||
if getattr(app.state, "custom_voice_service", None):
|
||||
await app.state.custom_voice_service.aclose()
|
||||
store.close()
|
||||
finally:
|
||||
app.state.lifespan_active = False
|
||||
@@ -315,8 +315,30 @@ def create_app(
|
||||
Path(db_path).stem + "-voice-sessions.json"),
|
||||
) if voice_config is not None else None
|
||||
)
|
||||
# Custom voice service (HEL-899) rides the same config: same instance
|
||||
# data space (selection/registry/journal beside the DB), dedicated
|
||||
# HTTP client because the .12 voice panel is https with a self-signed
|
||||
# cert the API must not fail to trust server-to-server.
|
||||
if voice_config is not None:
|
||||
in_memory = str(db_path) == ":memory:"
|
||||
custom_voice_service = CustomVoiceService(
|
||||
config=CustomVoiceConfig(
|
||||
panel_url=voice_config.panel_url,
|
||||
tts_url=voice_config.tts_url,
|
||||
registry_file=voice_config.registry_file,
|
||||
worker_state_file=voice_config.worker_state_file,
|
||||
),
|
||||
http=httpx.AsyncClient(verify=False, timeout=httpx.Timeout(30, read=120)),
|
||||
selection_path=None if in_memory else Path(db_path).with_name(
|
||||
Path(db_path).stem + "-voice-selection.json"),
|
||||
registry_path=None if in_memory else Path(db_path).with_name(
|
||||
Path(db_path).stem + "-voice-custom.json"),
|
||||
journal_path=None if in_memory else Path(db_path).with_name(
|
||||
Path(db_path).stem + "-voice-usage.jsonl"),
|
||||
)
|
||||
app.state.voice_service = voice_service
|
||||
app.include_router(build_voice_router(voice_service))
|
||||
app.state.custom_voice_service = custom_voice_service
|
||||
app.include_router(build_voice_router(voice_service, custom_voice_service))
|
||||
|
||||
@app.exception_handler(RequestValidationError)
|
||||
async def invalid(_request, exc):
|
||||
|
||||
Reference in New Issue
Block a user