Skip to content

The agent that grows with you

refactor(tools): fold neutts into tts builtin dispatch table, trim dead re-exports, compact tts_tool

Teknium58659718bb7a
diff --git a/tools/tts_tool.py b/tools/tts_tool.py
index 2277bac125e..790c76ddfd2 100644
--- a/tools/tts_tool.py
+++ b/tools/tts_tool.py
@@ -1,44 +1,13 @@
#!/usr/bin/env python3
-"""
-Text-to-Speech Tool Module
-
-Built-in TTS providers:
-- Edge TTS (default, free, no API key): Microsoft Edge neural voices
-- ElevenLabs (premium): High-quality voices, needs ELEVENLABS_API_KEY
-- OpenAI TTS: Good quality, needs OPENAI_API_KEY
-- MiniMax TTS: High-quality with voice cloning, needs the selected region's key
-- Mistral (Voxtral TTS): Multilingual, native Opus, needs MISTRAL_API_KEY
-- Google Gemini TTS: Controllable, 30 prebuilt voices, needs GEMINI_API_KEY
-- xAI TTS: Grok voices, uses xAI Grok OAuth credentials or XAI_API_KEY
-- NeuTTS (local, free, no API key): On-device TTS via neutts
-- KittenTTS (local, free, no API key): On-device 25MB model
-- Piper (local, free, no API key): OHF-Voice/piper1-gpl neural VITS, 44 languages
-
-Custom command providers: any number of named ``type: command`` providers under
-``tts.providers.<name>`` in ``~/.hermes/config.yaml``; Hermes writes the text to
-a temp file and runs the shell template (see the Local Command section of
-``website/docs/user-guide/features/tts.md``).
-
-Output: Opus (.ogg) for voice-bubble platforms (Telegram etc.), MP3 elsewhere.
-Configuration lives under the ``tts:`` key; the user chooses provider/voice,
-the model just sends text.
-
-Module layout: this file owns config resolution, lazy SDK importers,
-built-in provider dispatch, output-path policy, the long-form tool entry point,
-the ``check_fn`` and the tool registration. Sibling modules: ``tts_command_provider``
-(``type: command`` providers), ``tts_tool_plugins`` (plugin-registered providers),
-``tts_tool_openai`` (OpenAI/DeepInfra + managed gateway), ``tts_tool_providers``
-(other cloud backends), ``tts_tool_local`` (on-device engines + model caches),
-``tts_tool_lifecycle`` (warm/release leases), ``tts_tool_delivery`` (caps /
-chunking / ffmpeg / packing), ``tts_tool_speaker`` (streaming speaker pipeline).
-Their names are re-imported here so ``tools.tts_tool.<name>`` keeps resolving
-and tests patching ``tools.tts_tool.<seam>`` still take effect (siblings
+"""Text-to-speech tool: config resolution, built-in provider dispatch, output policy, registration.
+
+Built-in providers: Edge (free default), ElevenLabs, OpenAI, DeepInfra, MiniMax, Mistral,
+Gemini, xAI, and the local NeuTTS / KittenTTS / Piper engines; plus any ``type: command``
+provider under ``tts.providers.<name>`` and plugin-registered providers. Output is Opus
+(.ogg) for voice-bubble platforms, MP3 elsewhere. Sibling ``tts_tool_*`` modules hold the
+backends/delivery/lifecycle; their names are re-imported here so ``tools.tts_tool.<name>``
+keeps resolving and tests patching ``tools.tts_tool.<seam>`` still take effect (siblings
resolve those seams through ``_origin()`` at call time).
-
-Usage:
- from tools.tts_tool import text_to_speech_tool, check_tts_requirements
-
- result = text_to_speech_tool(text="Hello world")
"""
import asyncio
@@ -57,12 +26,7 @@ logger = logging.getLogger(__name__)
def get_env_value(name, default=None):
- """Read env values through the live config module.
-
- Resolved at call time: tests monkeypatch/restore
- ``hermes_cli.config.get_env_value`` and must not leave TTS holding a stale
- function for the rest of the process.
- """
+ """Read env values through the live config module (resolved per call so test patches apply)."""
try:
from hermes_cli.config import get_env_value as _get_env_value
except ImportError:
@@ -72,12 +36,7 @@ def get_env_value(name, default=None):
def _resolve_provider_key(env_var: str, provider_id: str) -> str:
- """Resolve a TTS provider API key via the shared voice-key resolver.
-
- ``tools.tool_backend_helpers.resolve_provider_secret`` is the single owner
- of STT/TTS key resolution (config > env/.env > credential pool). Resolved
- at call time so tests that reload the helpers module see the live function.
- """
+ """Resolve a TTS provider API key via the shared voice-key resolver (config > env/.env > pool)."""
try:
from tools.tool_backend_helpers import resolve_provider_secret
except ImportError: # pragma: no cover — helpers are in-repo
@@ -129,18 +88,13 @@ from tools.tts_tool_delivery import ( # noqa: F401 — historical names re-expo
_wrap_pcm_as_wav,
)
from tools.tts_tool_providers import ( # noqa: F401 — historical names re-exported
- DEFAULT_ELEVENLABS_MODEL_ID,
- DEFAULT_ELEVENLABS_VOICE_ID,
DEFAULT_GEMINI_TTS_MODEL,
DEFAULT_GEMINI_TTS_VOICE,
DEFAULT_MINIMAX_BASE_URL,
DEFAULT_MINIMAX_CN_BASE_URL,
- TTS_RESPONSE_BODY_LIMIT_BYTES,
- _XAI_FIRST_SENTENCE_RE,
_XAI_INLINE_SPEECH_TAGS,
_XAI_WRAPPING_SPEECH_TAGS,
_apply_xai_auto_speech_tags,
- _elevenlabs_environment_kwargs,
_generate_edge_tts,
_generate_elevenlabs,
_generate_gemini_tts,
@@ -152,18 +106,14 @@ from tools.tts_tool_providers import ( # noqa: F401 — historical names re-exp
from tools.tts_tool_local import ( # noqa: F401 — historical names re-exported
DEFAULT_PIPER_VOICE,
_LOCAL_TTS_MODEL_CACHES,
- _TTS_MODEL_CACHE_MAX,
_generate_kittentts,
_generate_neutts,
_generate_piper_tts,
_kittentts_model_cache,
_piper_voice_cache,
_resolve_piper_voice_path,
- _tts_cache_get_or_load,
-)
-from tools.tts_tool_speaker import ( # noqa: F401 — historical names re-exported
- stream_tts_to_speaker,
)
+from tools.tts_tool_speaker import stream_tts_to_speaker # noqa: F401 — historical name re-exported
from tools.tts_text_normalize import _strip_markdown_for_tts # noqa: F401 — historical name re-exported
from tools.tts_tool_plugins import ( # noqa: F401 — historical names re-exported
_dispatch_to_plugin_provider,
@@ -172,9 +122,6 @@ from tools.tts_tool_plugins import ( # noqa: F401 — historical names re-expor
)
from tools.tts_tool_openai import ( # noqa: F401 — historical names re-exported
DEFAULT_OPENAI_BASE_URL,
- DEFAULT_OPENAI_MODEL,
- DEFAULT_OPENAI_VOICE,
- MANAGED_OPENAI_TTS_MODELS,
_generate_deepinfra_tts,
_generate_openai_tts,
_has_openai_audio_backend,
@@ -190,19 +137,18 @@ from tools.tts_tool_lifecycle import ( # noqa: F401 — historical names re-exp
warm_tts_provider,
)
+
# ---------------------------------------------------------------------------
-# Lazy imports -- providers are imported only when actually used to avoid
-# crashing in headless environments (SSH, Docker, WSL, no PortAudio).
+# Lazy SDK importers -- providers import only when used (headless boxes lack PortAudio etc.)
# ---------------------------------------------------------------------------
def _sdk_importer(module: str, attr: Optional[str] = None, feature: Optional[str] = None) -> Callable[[], Any]:
"""Lazy SDK importer: returns ``module`` (or ``module.attr``), raising ImportError when absent.
- ``feature`` names a ``tools.lazy_deps`` feature to best-effort install
- first (users who enabled a provider by editing config.yaml never ran the
- post-setup hook); any failure there falls through so the raw import still
- raises a clean ImportError. sounddevice additionally raises OSError when
- PortAudio is unavailable.
+ ``feature`` names a ``tools.lazy_deps`` feature to best-effort install first (users who
+ enabled a provider by editing config.yaml never ran the post-setup hook); any failure
+ there falls through so the raw import still raises a clean ImportError. sounddevice
+ additionally raises OSError when PortAudio is unavailable.
"""
def _import():
if feature:
@@ -226,6 +172,14 @@ _import_kittentts = _sdk_importer("kittentts", "KittenTTS")
_import_piper = _sdk_importer("piper", "PiperVoice") # piper-tts wheels embed espeak-ng
+def _importable(importer: Callable[[], Any]) -> bool:
+ try:
+ importer()
+ return True
+ except ImportError:
+ return False
+
+
def _package_installed(name: str) -> bool:
try:
return importlib.util.find_spec(name) is not None
@@ -245,9 +199,9 @@ def _check_piper_available() -> bool:
return _package_installed("piper")
-# ===========================================================================
-# Defaults
-# ===========================================================================
+# ---------------------------------------------------------------------------
+# Defaults / config
+# ---------------------------------------------------------------------------
DEFAULT_PROVIDER = "edge"
@@ -261,17 +215,14 @@ _DEFAULT_OUTPUT_DIR_AT_IMPORT = DEFAULT_OUTPUT_DIR
def _default_output_dir() -> str:
- """Return the active profile's audio output dir at call time.
+ """The active profile's audio output dir at call time.
- Long-lived multi-profile runtimes (dashboard, TUI/Desktop backend, cron)
- import this module once and later switch profiles via
- ``set_hermes_home_override()``; a frozen constant would keep writing into
- the launch profile's cache. ``DEFAULT_OUTPUT_DIR`` stays as a module
- attribute for tests/patchers and wins whenever it has been patched.
+ Long-lived multi-profile runtimes switch profiles after import, so a frozen constant
+ would keep writing into the launch profile's cache. ``DEFAULT_OUTPUT_DIR`` stays a
+ module attribute for tests/patchers and wins whenever it has been patched.
"""
- configured = DEFAULT_OUTPUT_DIR
- if configured != _DEFAULT_OUTPUT_DIR_AT_IMPORT:
- return configured
+ if DEFAULT_OUTPUT_DIR != _DEFAULT_OUTPUT_DIR_AT_IMPORT:
+ return DEFAULT_OUTPUT_DIR
return _get_default_output_dir()
@@ -279,15 +230,11 @@ def _default_output_dir() -> str:
MAX_TEXT_LENGTH = FALLBACK_MAX_TEXT_LENGTH
-# ===========================================================================
-# Config loader -- reads tts: section from ~/.hermes/config.yaml
-# ===========================================================================
def _load_tts_config() -> Dict[str, Any]:
"""Return the ``tts`` config section ({} when unavailable)."""
try:
from hermes_cli.config import load_config
- config = load_config()
- return config.get("tts") or {}
+ return load_config().get("tts") or {}
except ImportError:
logger.debug("hermes_cli.config not available, using default TTS config")
return {}
@@ -299,25 +246,18 @@ def _load_tts_config() -> Dict[str, Any]:
def _get_provider(tts_config: Dict[str, Any]) -> str:
"""The explicitly configured TTS provider, or the free default.
- Inference credentials do not imply consent to paid speech generation:
- cloud TTS is opt-in via ``tts.provider``. The managed selection
- (``tts.provider: nous``) is serviced by the OpenAI implementation, routed
- through the managed openai-audio gateway by
- ``_resolve_openai_audio_client_config``.
+ Inference credentials do not imply consent to paid speech generation: cloud TTS is
+ opt-in via ``tts.provider``. The managed selection (``nous``) is serviced by the
+ OpenAI implementation routed through the managed openai-audio gateway.
"""
provider = (tts_config.get("provider") or DEFAULT_PROVIDER).lower().strip()
- if provider == NOUS_MANAGED_PROVIDER:
- return "openai"
- return provider
+ return "openai" if provider == NOUS_MANAGED_PROVIDER else provider
-# Platforms whose native voice-bubble delivery requires Ogg/Opus audio
-# (MP3 renders as a broken attachment there).
+# Platforms whose native voice-bubble delivery requires Ogg/Opus (MP3 renders broken there).
OPUS_VOICE_PLATFORMS = frozenset({"telegram", "matrix", "feishu", "whatsapp", "signal"})
-
-# Built-ins that emit Opus natively when asked for .ogg (no ffmpeg needed).
+# Built-ins that emit Opus natively when asked for .ogg; the rest need ffmpeg for voice bubbles.
_NATIVE_OPUS_PROVIDERS = frozenset({"openai", "elevenlabs", "mistral", "gemini"})
-# Built-ins whose native output (MP3/WAV) needs ffmpeg for voice-bubble delivery.
_FFMPEG_OPUS_PROVIDERS = frozenset({"edge", "neutts", "minimax", "xai", "kittentts", "piper"})
@@ -325,32 +265,30 @@ def _has_any_command_tts_provider(tts_config: Optional[Dict[str, Any]] = None) -
"""Return True when any command-type TTS provider is configured."""
if tts_config is None:
tts_config = _load_tts_config()
- for _name, _cfg in _iter_command_providers(tts_config):
- return True
- return False
+ return any(True for _ in _iter_command_providers(tts_config))
-# ===========================================================================
+# ---------------------------------------------------------------------------
# Built-in provider dispatch
-# ===========================================================================
-# provider -> (importer-name or None, "package missing" error, log line,
-# generator-name). Names are looked up in module globals at call time so
-# tests that monkeypatch ``tools.tts_tool._import_x`` / ``_generate_x`` apply.
+# ---------------------------------------------------------------------------
+# provider -> (availability predicate or None, "package missing" error, log line, generator
+# name). Predicates and generator names resolve module globals at call time so tests that
+# monkeypatch ``tools.tts_tool._import_x`` / ``_check_x`` / ``_generate_x`` apply.
_BUILTIN_DISPATCH: Dict[str, tuple] = {
"elevenlabs": (
- "_import_elevenlabs",
+ lambda: _importable(_import_elevenlabs),
"ElevenLabs provider selected but 'elevenlabs' package not installed. Run: pip install elevenlabs",
"Generating speech with ElevenLabs...",
"_generate_elevenlabs",
),
"openai": (
- "_import_openai_client",
+ lambda: _importable(_import_openai_client),
"OpenAI provider selected but 'openai' package not installed.",
"Generating speech with OpenAI TTS...",
"_generate_openai_tts",
),
"deepinfra": (
- "_import_openai_client",
+ lambda: _importable(_import_openai_client),
"DeepInfra TTS uses the 'openai' SDK but it isn't installed.",
"Generating speech with DeepInfra TTS...",
"_generate_deepinfra_tts",
@@ -358,15 +296,22 @@ _BUILTIN_DISPATCH: Dict[str, tuple] = {
"minimax": (None, None, "Generating speech with MiniMax TTS...", "_generate_minimax_tts"),
"xai": (None, None, "Generating speech with xAI TTS...", "_generate_xai_tts"),
"mistral": (
- "_import_mistral_client",
+ lambda: _importable(_import_mistral_client),
"Mistral provider selected but 'mistralai' package not installed. "
"Run `hermes setup` to install Mistral support.",
"Generating speech with Mistral Voxtral TTS...",
"_generate_mistral_tts",
),
"gemini": (None, None, "Generating speech with Google Gemini TTS...", "_generate_gemini_tts"),
+ "neutts": (
+ lambda: _check_neutts_available(),
+ "NeuTTS provider selected but neutts is not installed. "
+ "Run hermes setup and choose NeuTTS, or install espeak-ng and run python -m pip install -U neutts[all].",
+ "Generating speech with NeuTTS (local)...",
+ "_generate_neutts",
+ ),
"kittentts": (
- "_import_kittentts",
+ lambda: _importable(_import_kittentts),
"KittenTTS provider selected but 'kittentts' package not installed. "
"Run 'hermes setup tts' and choose KittenTTS, or install manually: "
"pip install https://github.com/KittenML/KittenTTS/releases/download/0.8.1/kittentts-0.8.1-py3-none-any.whl",
@@ -374,7 +319,7 @@ _BUILTIN_DISPATCH: Dict[str, tuple] = {
"_generate_kittentts",
),
"piper": (
- "_import_piper",
+ lambda: _importable(_import_piper),
"Piper provider selected but 'piper-tts' package not installed. "
"Run 'hermes tools' and select Piper under TTS, or install manually: "
"pip install piper-tts",
@@ -382,10 +327,6 @@ _BUILTIN_DISPATCH: Dict[str, tuple] = {
"_generate_piper_tts",
),
}
-_NEUTTS_MISSING_ERROR = (
- "NeuTTS provider selected but neutts is not installed. "
- "Run hermes setup and choose NeuTTS, or install espeak-ng and run python -m pip install -U neutts[all]."
-)
def _error_json(message: str) -> str:
@@ -397,9 +338,7 @@ def _run_edge_tts(text: str, file_str: str, tts_config: Dict[str, Any]) -> None:
try:
import concurrent.futures
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:
- pool.submit(
- lambda: asyncio.run(_generate_edge_tts(text, file_str, tts_config))
- ).result(timeout=60)
+ pool.submit(lambda: asyncio.run(_generate_edge_tts(text, file_str, tts_config))).result(timeout=60)
except RuntimeError:
asyncio.run(_generate_edge_tts(text, file_str, tts_config))
@@ -407,20 +346,15 @@ def _run_edge_tts(text: str, file_str: str, tts_config: Dict[str, Any]) -> None:
def _select_builtin_engine(provider: str) -> tuple:
"""Check a built-in provider's SDK. Returns ``(engine, None)`` or ``(provider, error_json)``.
- Unknown names take the Edge default; when edge-tts is missing, NeuTTS is
- the local fallback (``engine`` then differs from ``provider``).
+ Unknown names take the Edge default; when edge-tts is missing, NeuTTS is the local
+ fallback (``engine`` then differs from ``provider``).
"""
entry = _BUILTIN_DISPATCH.get(provider)
if entry is not None:
- importer_name, missing_error = entry[0], entry[1]
- if importer_name is not None and not _importable(globals()[importer_name]):
+ available, missing_error = entry[0], entry[1]
+ if available is not None and not available():
return provider, _error_json(missing_error)
return provider, None
- if provider == "neutts":
- if not _check_neutts_available():
- return provider, _error_json(_NEUTTS_MISSING_ERROR)
- logger.info("Generating speech with NeuTTS (local)...")
- return provider, None
if _importable(_import_edge_tts):
return provider, None # Edge default; the reported provider stays as configured
if _check_neutts_available():
@@ -433,68 +367,56 @@ def _select_builtin_engine(provider: str) -> tuple:
def _synthesize_builtin(engine: str, text: str, file_str: str, tts_config: Dict[str, Any], instructions: Optional[str]) -> None:
- """Run the already-selected built-in *engine* (the caller logs the engine-selection line)."""
+ """Run the already-selected built-in *engine*."""
entry = _BUILTIN_DISPATCH.get(engine)
- if entry is not None:
- logger.info(entry[2])
- if engine == "openai":
- _generate_openai_tts(text, file_str, tts_config, instructions=instructions)
- else:
- globals()[entry[3]](text, file_str, tts_config)
- elif engine == "neutts":
- _generate_neutts(text, file_str, tts_config)
- else:
+ if entry is None:
logger.info("Generating speech with Edge TTS...")
_run_edge_tts(text, file_str, tts_config)
+ return
+ logger.info(entry[2])
+ if engine == "openai":
+ _generate_openai_tts(text, file_str, tts_config, instructions=instructions)
+ else:
+ globals()[entry[3]](text, file_str, tts_config)
def _finalize_voice_delivery(
- file_str: str,
- provider: str,
- command_provider_config: Optional[Dict[str, Any]],
- want_opus: bool,
+ file_str: str, provider: str, command_provider_config: Optional[Dict[str, Any]], want_opus: bool,
) -> tuple:
- """Decide voice-bubble eligibility and Opus-convert when needed.
+ """Decide voice-bubble eligibility and Opus-convert when needed; returns ``(path, voice_compatible)``.
- Command and plugin providers are documents by default and opt in via
- ``voice_compatible``; native-Opus built-ins are voice-compatible when the
- platform wants Opus and they wrote .ogg; MP3/WAV built-ins are converted
- with ffmpeg only when the platform needs Opus. Returns ``(path, voice_compatible)``.
+ Command and plugin providers are documents by default and opt in via ``voice_compatible``;
+ native-Opus built-ins are voice-compatible when the platform wants Opus and they wrote
+ .ogg; MP3/WAV built-ins are converted with ffmpeg only when the platform needs Opus.
"""
- voice_compatible = False
if command_provider_config is not None:
opted_in = _is_command_tts_voice_compatible(command_provider_config)
elif provider not in BUILTIN_TTS_PROVIDERS:
opted_in = _plugin_provider_is_voice_compatible(provider)
elif want_opus and provider in _FFMPEG_OPUS_PROVIDERS and not file_str.endswith(".ogg"):
opus_path = _convert_to_opus(file_str)
- if opus_path:
- return opus_path, True
- return file_str, False
+ return (opus_path, True) if opus_path else (file_str, False)
elif provider in _NATIVE_OPUS_PROVIDERS:
return file_str, want_opus and file_str.endswith(".ogg")
else:
return file_str, False
- if opted_in:
- if not file_str.endswith(".ogg"):
- opus_path = _convert_to_opus(file_str)
- if opus_path:
- file_str = opus_path
- voice_compatible = file_str.endswith(".ogg")
- return file_str, voice_compatible
+ if not opted_in:
+ return file_str, False
+ if not file_str.endswith(".ogg"):
+ file_str = _convert_to_opus(file_str) or file_str
+ return file_str, file_str.endswith(".ogg")
-# ===========================================================================
+# ---------------------------------------------------------------------------
# Main tool function
-# ===========================================================================
+# ---------------------------------------------------------------------------
def _apply_call_overrides(tts_config: Dict[str, Any], speed: Optional[float], provider: Optional[str]):
"""Apply per-call ``speed`` (clamped, on a shallow copy) and resolve the provider name."""
if speed is not None:
- clamped = max(0.25, min(4.0, float(speed)))
tts_config = dict(tts_config) # shallow copy to avoid mutating the cache
- tts_config["speed"] = clamped
+ tts_config["speed"] = max(0.25, min(4.0, float(speed)))
provider = provider.lower().strip() if provider else _get_provider(tts_config)
return tts_config, provider
@@ -507,18 +429,14 @@ def _session_platform() -> tuple:
def _resolve_output_base(
- output_path: Optional[str],
- provider: str,
- command_provider_config: Optional[Dict[str, Any]],
- want_opus: bool,
+ output_path: Optional[str], provider: str, command_provider_config: Optional[Dict[str, Any]], want_opus: bool,
) -> tuple:
"""Pick the output file. Returns ``(Path, None)`` or ``(None, error_json)``.
- A caller-supplied path is rejected on ``..`` traversal (bug or
- prompt-injection; an absolute path is fine) and on protected credential/
- system locations. Command providers get their configured extension.
- Default: ``<audio cache>/tts_<timestamp>.<ext>`` where ext is the command
- format, ``.ogg`` for native-Opus providers on Opus platforms, else ``.mp3``.
+ A caller-supplied path is rejected on ``..`` traversal (bug or prompt-injection; an
+ absolute path is fine) and on protected credential/system locations. Command providers
+ get their configured extension. Default: ``<audio cache>/tts_<timestamp>.<ext>`` where
+ ext is the command format, ``.ogg`` for native-Opus providers on Opus platforms, else ``.mp3``.
"""
if output_path:
from tools.path_security import has_traversal_component
@@ -543,10 +461,8 @@ def _resolve_output_base(
out_dir.mkdir(parents=True, exist_ok=True)
if command_provider_config is not None:
ext = _get_command_tts_output_format(command_provider_config)
- elif want_opus and provider in _NATIVE_OPUS_PROVIDERS:
- ext = "ogg"
else:
- ext = "mp3"
+ ext = "ogg" if want_opus and provider in _NATIVE_OPUS_PROVIDERS else "mp3"
file_path = out_dir / f"tts_{timestamp}.{ext}"
file_path.parent.mkdir(parents=True, exist_ok=True)
return file_path, None
@@ -571,12 +487,10 @@ def _text_to_speech_single(
) -> str:
"""Synthesize one provider-safe text chunk into *file_str* and return one final-encoded file.
- Text arrives already normalized and the output path already validated;
- :func:`text_to_speech_tool` owns long-form splitting, delivery packing and
- size enforcement. Command providers resolve BEFORE built-in dispatch;
- built-in names short-circuit so ``tts.providers.openai.command`` can't
- shadow OpenAI. Plugin providers fire only for names that are neither
- built-in nor command; a None return falls through to built-in dispatch
+ Text arrives already normalized and the output path already validated. Command providers
+ resolve BEFORE built-in dispatch; built-in names short-circuit so
+ ``tts.providers.openai.command`` can't shadow OpenAI. Plugin providers fire only for names
+ that are neither built-in nor command; a None return falls through to built-in dispatch
(unknown -> Edge default).
"""
try:
@@ -596,20 +510,14 @@ def _text_to_speech_single(
if not os.path.exists(file_str) or os.path.getsize(file_str) == 0:
return _error_json(f"TTS generation produced no output (provider: {provider})")
- # Sniff once for every provider: MP3/WAV bytes in a .ogg path render
- # as broken 0-second voice bubbles.
+ # Sniff once for every provider: MP3/WAV bytes in a .ogg path render as 0-second bubbles.
file_str = _repair_ogg_container(file_str)
- file_str, voice_compatible = _finalize_voice_delivery(
- file_str, provider, command_provider_config, want_opus,
- )
-
- file_size = os.path.getsize(file_str)
- logger.info("TTS audio saved: %s (%s bytes, provider: %s)", file_str, f"{file_size:,}", provider)
+ file_str, voice_compatible = _finalize_voice_delivery(file_str, provider, command_provider_config, want_opus)
+ logger.info("TTS audio saved: %s (%s bytes, provider: %s)", file_str, f"{os.path.getsize(file_str):,}", provider)
media_tag = f"MEDIA:{file_str}"
if voice_compatible:
media_tag = f"[[audio_as_voice]]\n{media_tag}"
-
return json.dumps({
"success": True,
"file_path": file_str,
@@ -617,7 +525,6 @@ def _text_to_speech_single(
"provider": provider,
"voice_compatible": voice_compatible,
}, ensure_ascii=False)
-
except ValueError as e:
return _tool_failure("TTS configuration error", provider, e)
except FileNotFoundError as e:
@@ -630,57 +537,32 @@ class _ChunkFailed(Exception):
"""One chunk's synthesis returned an error envelope; message is the final tool error text."""
-def _synthesize_chunks(
- chunks: List[str],
- base_path: Path,
- generated_artifacts: set,
- *,
- provider: str,
- tts_config: Dict[str, Any],
- command_provider_config: Optional[Dict[str, Any]],
- want_opus: bool,
- instructions: Optional[str],
-) -> tuple:
+def _synthesize_chunks(chunks: List[str], base_path: Path, generated_artifacts: set, **single_kwargs) -> tuple:
"""Synthesize every chunk into ``<base>.chunkNNN<ext>`` (or ``base`` alone) sequentially.
- Every path touched is added to *generated_artifacts* so the caller can
- sweep non-final files. Returns ``(encoded_paths, chunk_results)``; raises
- :class:`_ChunkFailed` when a chunk reports failure and ``RuntimeError``
- when it returns garbage or no audio.
+ Every path touched is added to *generated_artifacts* so the caller can sweep non-final
+ files. Returns ``(encoded_paths, chunk_results)``; raises :class:`_ChunkFailed` when a
+ chunk reports failure and ``RuntimeError`` when it returns garbage or no audio.
"""
+ provider = single_kwargs["provider"]
encoded_paths: List[str] = []
chunk_results: List[Dict[str, Any]] = []
for index, chunk in enumerate(chunks, start=1):
- if len(chunks) == 1:
- chunk_path = base_path
- else:
- chunk_path = base_path.with_name(
- f"{base_path.stem}.chunk{index:03d}{base_path.suffix}"
- )
+ chunk_path = base_path
+ if len(chunks) > 1:
+ chunk_path = base_path.with_name(f"{base_path.stem}.chunk{index:03d}{base_path.suffix}")
generated_artifacts.add(str(chunk_path))
- raw_result = _text_to_speech_single(
- chunk,
- str(chunk_path),
- provider=provider,
- tts_config=tts_config,
- command_provider_config=command_provider_config,
- want_opus=want_opus,
- instructions=instructions,
- )
+ raw_result = _text_to_speech_single(chunk, str(chunk_path), **single_kwargs)
try:
chunk_result = json.loads(raw_result)
except (json.JSONDecodeError, TypeError):
- raise RuntimeError(
- f"TTS chunk {index} returned invalid JSON: {str(raw_result)[:200]}"
- )
+ raise RuntimeError(f"TTS chunk {index} returned invalid JSON: {str(raw_result)[:200]}")
if not chunk_result.get("success"):
error_msg = chunk_result.get("error", "unknown error")
raise _ChunkFailed(f"TTS chunk {index} failed ({provider}): {error_msg}")
actual_path = str(chunk_result.get("file_path") or chunk_path)
if not os.path.isfile(actual_path) or os.path.getsize(actual_path) <= 0:
- raise RuntimeError(
- f"TTS chunk {index} produced no final audio: {actual_path}"
- )
+ raise RuntimeError(f"TTS chunk {index} produced no final audio: {actual_path}")
generated_artifacts.add(actual_path)
encoded_paths.append(actual_path)
chunk_results.append(chunk_result)
@@ -694,25 +576,13 @@ def text_to_speech_tool(
instructions: Optional[str] = None,
provider: Optional[str] = None,
) -> str:
- """Convert text to speech audio with long-form chunking.
-
- Text is normalized, split into provider-safe chunks, synthesized
- sequentially (each chunk final-encoded), then packed against the
- destination platform's upload limit. Multi-chunk voice output is
- re-encoded when combined; a failed combine keeps separate valid files;
- no over-limit artifact is returned. On messaging platforms the
- ``MEDIA:<path>`` tag is delivered as a native voice message.
-
- Args:
- text: Text to speak; longer input is split into ordered chunks, never
- silently truncated.
- output_path: Optional custom save path.
- speed: Optional playback speed multiplier (0.25-4.0).
- instructions: Optional voice-design guidance (tone, emotion, pacing).
- provider: Optional TTS provider override.
-
- Returns:
- str: JSON result with success, file_path, file_paths, and MEDIA tag.
+ """Convert text to speech audio with long-form chunking; returns the JSON result envelope.
+
+ Text is normalized, split into provider-safe chunks (never silently truncated),
+ synthesized sequentially (each chunk final-encoded), then packed against the destination
+ platform's upload limit. Multi-chunk voice output is re-encoded when combined; a failed
+ combine keeps separate valid files; no over-limit artifact is returned. ``speed`` is a
+ 0.25-4.0 multiplier; ``instructions`` is voice-design guidance for backends that take it.
"""
if not text or not text.strip():
return tool_error("Text is required", success=False)
@@ -727,7 +597,6 @@ def text_to_speech_tool(
return tool_error("Text is empty after TTS cleanup", success=False)
tts_config, provider = _apply_call_overrides(_load_tts_config(), speed, provider)
-
command_provider_config = _resolve_command_provider_config(provider, tts_config)
max_len = _resolve_max_text_length(provider, tts_config)
chunks = _split_text_for_tts(text, max_len)
@@ -736,15 +605,11 @@ def text_to_speech_tool(
if len(chunks) > 1:
logger.info(
"TTS text for provider %s split into %d chunks (input=%d chars, cap=%d)",
- provider,
- len(chunks),
- len(text),
- max_len,
+ provider, len(chunks), len(text), max_len,
)
platform, want_opus = _session_platform()
delivery_profile = _resolve_audio_delivery_profile(platform, tts_config)
-
base_path, error = _resolve_output_base(output_path, provider, command_provider_config, want_opus)
if error:
return error
@@ -760,25 +625,13 @@ def text_to_speech_tool(
want_opus=want_opus,
instructions=instructions,
)
-
- voice_compatible = bool(chunk_results) and all(
- bool(result.get("voice_compatible")) for result in chunk_results
- )
+ voice_compatible = bool(chunk_results) and all(bool(r.get("voice_compatible")) for r in chunk_results)
delivery_base = base_path.with_suffix(Path(encoded_paths[0]).suffix)
final_paths, combined_chunks = _build_audio_delivery_files(
- encoded_paths,
- str(delivery_base),
- delivery_profile,
- voice_compatible=voice_compatible,
+ encoded_paths, str(delivery_base), delivery_profile, voice_compatible=voice_compatible,
)
-
for path in final_paths:
- logger.info(
- "TTS audio saved: %s (%s bytes, provider: %s)",
- path,
- f"{os.path.getsize(path):,}",
- provider,
- )
+ logger.info("TTS audio saved: %s (%s bytes, provider: %s)", path, f"{os.path.getsize(path):,}", provider)
media_tag = "\n".join(f"MEDIA:{path}" for path in final_paths)
if voice_compatible:
media_tag = f"[[audio_as_voice]]\n{media_tag}"
@@ -808,21 +661,16 @@ def text_to_speech_tool(
finally:
final_absolute = {os.path.abspath(path) for path in final_paths}
for artifact in generated_artifacts:
- if os.path.abspath(artifact) in final_absolute:
- continue
- try:
- os.unlink(artifact)
- except OSError:
- pass
-
+ if os.path.abspath(artifact) not in final_absolute:
+ try:
+ os.unlink(artifact)
+ except OSError:
+ pass
-def _importable(importer: Callable[[], Any]) -> bool:
- try:
- importer()
- return True
- except ImportError:
- return False
+# ---------------------------------------------------------------------------
+# check_fn
+# ---------------------------------------------------------------------------
def _minimax_requirements() -> bool:
try:
@@ -841,8 +689,8 @@ def _xai_requirements() -> bool:
return False
-# Must mirror text_to_speech_tool dispatch: unrelated cloud credentials never
-# make the Edge default usable, and an explicit provider is checked on its own.
+# Must mirror text_to_speech_tool dispatch: unrelated cloud credentials never make the Edge
+# default usable, and an explicit provider is checked on its own.
_BUILTIN_REQUIREMENTS: Dict[str, Callable[[], bool]] = {
"edge": lambda: _importable(_import_edge_tts) or _check_neutts_available(),
"elevenlabs": lambda: _importable(_import_elevenlabs) and bool(_resolve_provider_key("ELEVENLABS_API_KEY", "elevenlabs")),
@@ -864,11 +712,9 @@ def check_tts_requirements() -> bool:
provider = _get_provider(tts_config)
if _resolve_command_provider_config(provider, tts_config) is not None:
return True
-
check = _BUILTIN_REQUIREMENTS.get(provider)
if check is not None:
return check()
-
return _plugin_provider_is_available(provider)