From 574ddaceb3ca72eb2d18aae041e77123a2d130fe Mon Sep 17 00:00:00 2001 From: kuanjames Date: Sat, 28 Nov 2026 12:56:52 +0800 Subject: [PATCH] fix: fall back to configured expert voice when TTS discovery is empty OpenAI-compatible TTS providers expose no voice-listing endpoint, so voice discovery returns nothing and the audio overview pipeline fails with 'Configure distinct host and expert voices' even when the active model has a voice configured. The chat entry point cannot recover because it never sends per-request voices. Add an optional expert_voice to the TTS catalog model (settings UI field + runtime resolution) and let the audio overview fall back to the catalog's voice/expert_voice pair when discovery yields no choices, with an explicit distinctness check. --- .../capabilities/audio_overview/pipeline.py | 9 +- deeptutor/services/config/provider_runtime.py | 2 + deeptutor/services/voice/config.py | 4 + tests/capabilities/test_audio_overview.py | 127 ++++++++++++++++++ web/components/settings/VoiceModelFields.tsx | 20 +++ web/lib/model-catalog-types.ts | 3 + web/locales/en/app.json | 1 + web/locales/zh/app.json | 1 + 8 files changed, 165 insertions(+), 2 deletions(-) diff --git a/deeptutor/capabilities/audio_overview/pipeline.py b/deeptutor/capabilities/audio_overview/pipeline.py index c9e8d7301..2764c9211 100644 --- a/deeptutor/capabilities/audio_overview/pipeline.py +++ b/deeptutor/capabilities/audio_overview/pipeline.py @@ -423,9 +423,14 @@ class AudioOverviewPipeline: choices = voice_model_options(tts.provider_name, "tts", tts.model).get("voices", []) host_voice = host_voice or tts.voice expert_voice = expert_voice or next( - (str(item["id"]) for item in choices if item.get("id") != host_voice), None + (str(item["id"]) for item in choices if str(item["id"]) != host_voice), + None, ) - if not host_voice or not expert_voice: + # Providers without live voice discovery report no choices, so the + # catalog's own voices are the only way to pick a second speaker. + expert_voice = expert_voice or (tts.expert_voice or None) + host_voice = host_voice or (tts.expert_voice or None) + if not host_voice or not expert_voice or host_voice == expert_voice: raise AudioOverviewError( "Configure distinct host and expert voices for the active TTS model." ) diff --git a/deeptutor/services/config/provider_runtime.py b/deeptutor/services/config/provider_runtime.py index 6dba87e29..cb23c7f83 100644 --- a/deeptutor/services/config/provider_runtime.py +++ b/deeptutor/services/config/provider_runtime.py @@ -1383,6 +1383,7 @@ def resolve_tts_runtime_config( options = voice_model_options(provider, "tts", resolved_model) voice = _as_str((model or {}).get("voice")) + expert_voice = _as_str((model or {}).get("expert_voice")) response_format = _as_str((model or {}).get("response_format")) or options["formats"][0] raw_speed = (model or {}).get("speed") speed = _coerce_optional_float(raw_speed) @@ -1404,6 +1405,7 @@ def resolve_tts_runtime_config( api_version=_as_str((profile or {}).get("api_version")) or None, extra_headers=_to_headers((profile or {}).get("extra_headers")), voice=voice, + expert_voice=expert_voice, response_format=response_format, speed=speed, language=_as_str((model or {}).get("language")) or None, diff --git a/deeptutor/services/voice/config.py b/deeptutor/services/voice/config.py index c80322fab..c5e8adebc 100644 --- a/deeptutor/services/voice/config.py +++ b/deeptutor/services/voice/config.py @@ -58,6 +58,10 @@ class TTSConfig: api_version: str | None = None extra_headers: dict[str, str] = field(default_factory=dict) voice: str = "" + # Optional second configured voice. Providers without live voice discovery + # (anything OpenAI-compatible) still need two distinct voices for the two- + # speaker audio overview, so the catalog can state one explicitly. + expert_voice: str = "" response_format: str = "mp3" speed: float | None = None language: str | None = None diff --git a/tests/capabilities/test_audio_overview.py b/tests/capabilities/test_audio_overview.py index dc328cd90..30c6b0d9c 100644 --- a/tests/capabilities/test_audio_overview.py +++ b/tests/capabilities/test_audio_overview.py @@ -16,6 +16,8 @@ from deeptutor.capabilities.audio_overview.capability import AudioOverviewCapabi from deeptutor.capabilities.audio_overview.pipeline import ( AudioOverviewError, AudioOverviewPipeline, + AudioOverviewScript, + SourceCitation, _render_audio, normalize_rag_result, parse_script, @@ -243,6 +245,131 @@ async def test_pipeline_generates_two_voices_and_publishes_workspace_items( ] +def _two_speaker_pipeline(tmp_path: Path) -> AudioOverviewPipeline: + # No injected speech_func: exercises the runtime path that resolves + # voices from the catalog, like the real server does. + return AudioOverviewPipeline( + language="en", + system_prompt="Return JSON.", + instruction_prompt="Create the overview.", + workspace_output_dir=tmp_path, + ) + + +async def _silent_speech(text: str, **kwargs: Any) -> tuple[bytes, str]: + buffer = io.BytesIO() + with wave.open(buffer, "wb") as output: + output.setnchannels(1) + output.setsampwidth(2) + output.setframerate(24000) + output.writeframes(b"\x00\x00" * 2400) + return buffer.getvalue(), "audio/wav" + + +def _two_speaker_inputs() -> tuple[Any, Any]: + script = AudioOverviewScript( + title="Voices", + segments=[ + {"speaker": "host", "text": "Let's begin.", "source_ids": []}, + {"speaker": "expert", "text": "Backoff controls retries.", "source_ids": []}, + ], + ) + context = SimpleNamespace(citations=[]) + return script, context + + +@pytest.mark.asyncio +async def test_publish_falls_back_to_configured_voices_without_voice_discovery( + monkeypatch: pytest.MonkeyPatch, + tmp_path: Path, +) -> None: + """OpenAI-compatible providers report no voices, so the catalog decides.""" + + import deeptutor.services.config.provider_runtime as provider_runtime + import deeptutor.services.voice.options as voice_options + + monkeypatch.setattr( + provider_runtime, + "resolve_tts_runtime_config", + lambda: SimpleNamespace( + provider_name="openai", + model="some-tts", + voice="female", + expert_voice="male", + ), + ) + monkeypatch.setattr( + voice_options, + "voice_model_options", + lambda provider, service, model: {"voices": []}, + ) + + used_voices: list[str] = [] + + async def fake_speech(text: str, **kwargs: Any) -> tuple[bytes, str]: + used_voices.append(str(kwargs.get("voice"))) + return await _silent_speech(text, **kwargs) + + import deeptutor.services.voice as voice_module + + monkeypatch.setattr(voice_module, "synthesize_speech", fake_speech) + + script, context = _two_speaker_inputs() + pipeline = _two_speaker_pipeline(tmp_path) + + await pipeline.publish( + turn_id="turn-voices", + script=script, + context=context, + request_config=AudioOverviewRequestConfig(), + ) + + assert used_voices == ["female", "male"] + + +@pytest.mark.asyncio +async def test_publish_names_the_two_voice_requirement_when_nothing_is_configured( + monkeypatch: pytest.MonkeyPatch, + tmp_path: Path, +) -> None: + import deeptutor.services.config.provider_runtime as provider_runtime + import deeptutor.services.voice.options as voice_options + + monkeypatch.setattr( + provider_runtime, + "resolve_tts_runtime_config", + lambda: SimpleNamespace( + provider_name="openai", + model="some-tts", + voice="female", + expert_voice="", + ), + ) + monkeypatch.setattr( + voice_options, + "voice_model_options", + lambda provider, service, model: {"voices": []}, + ) + + async def unused_speech(text: str, **kwargs: Any) -> tuple[bytes, str]: + raise AssertionError("no audio should be requested without two voices") + + import deeptutor.services.voice as voice_module + + monkeypatch.setattr(voice_module, "synthesize_speech", unused_speech) + + script, context = _two_speaker_inputs() + pipeline = _two_speaker_pipeline(tmp_path) + + with pytest.raises(AudioOverviewError, match="distinct host and expert voices"): + await pipeline.publish( + turn_id="turn-voices", + script=script, + context=context, + request_config=AudioOverviewRequestConfig(), + ) + + @pytest.mark.asyncio async def test_capability_publishes_two_workspace_attachments() -> None: async def fake_retrieve(self, **kwargs: Any) -> Any: diff --git a/web/components/settings/VoiceModelFields.tsx b/web/components/settings/VoiceModelFields.tsx index 109c81771..72e3572ea 100644 --- a/web/components/settings/VoiceModelFields.tsx +++ b/web/components/settings/VoiceModelFields.tsx @@ -30,6 +30,7 @@ export function VoiceModelFields({ }) { const { t } = useTranslation() const speedId = useId() + const expertVoiceId = useId() const instructionId = useId() const timeoutId = useId() const languages = preset?.languages ?? [] @@ -73,6 +74,25 @@ export function VoiceModelFields({ /> )} + {service === 'tts' && ( +
+ + update('expert_voice', e.target.value)} + /> +

+ {t('Second speaker for audio overviews when your provider lists no voices.')} +

+
+ )} {(service === 'stt' || Boolean(preset?.languages.length)) && (