The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
feat(voice): route CLI/TUI speak_text through the generic streaming dispatcher (#58930)
Teknium5422d296075f
diff --git a/hermes_cli/voice.py b/hermes_cli/voice.pyindex 33d40897fb3..2c2fa8b8a10 100644--- a/hermes_cli/voice.py+++ b/hermes_cli/voice.py@@ -775,6 +775,33 @@ def _continuous_on_silence() -> None:# ── TTS API ──────────────────────────────────────────────────────────+def _speak_text_streaming(text: str) -> bool:+ """Speak ``text`` via the generic streaming dispatcher; True on success.++ Bridges the one-shot ``speak_text`` contract onto the shared+ ``stream_tts_to_speaker`` pipeline (tools.tts_tool): the full reply is+ fed as a single delta + end-of-text sentinel, and we block until the+ pipeline's done event fires — same blocking semantics the sync path+ has, so callers (and the mic re-arm logic in ``speak_text``) see no+ behavioral difference beyond earlier first audio.++ Returns False when playback produced nothing (caller falls back to the+ whole-file sync path).+ """+ import queue as _queue+ import threading as _threading++ from tools.tts_tool import stream_tts_to_speaker++ text_queue: "_queue.Queue" = _queue.Queue()+ text_queue.put(text)+ text_queue.put(None) # end-of-text sentinel+ stop_event = _threading.Event()+ done_event = _threading.Event()+ stream_tts_to_speaker(text_queue, stop_event, done_event)+ return done_event.is_set()++def speak_text(text: str) -> None:"""Synthesize ``text`` with the configured TTS provider and play it.@@ -819,6 +846,22 @@ def speak_text(text: str) -> None:try:from tools.tts_tool import text_to_speech_tool+ # One dispatcher, zero parallel streaming implementations (#58930):+ # when the configured provider has a chunked streamer registered in+ # tools.tts_streaming, route the whole reply through the same+ # stream_tts_to_speaker pipeline the CLI voice mode uses — audio+ # starts on sentence one instead of after full synthesis. Falls+ # through to the legacy whole-file path when no streamer resolves.+ try:+ from tools.tts_streaming import resolve_streaming_provider+ from tools.tts_tool import _load_tts_config++ if resolve_streaming_provider(_load_tts_config()) is not None:+ if _speak_text_streaming(text):+ return+ except Exception as e:+ _debug(f"speak_text: streaming dispatch unavailable ({e}); using sync path")+# Shared cleaner (tools/tts_text_normalize): markdown, emoji,# <think> blocks, verifier footer, units, newline flattening.try:diff --git a/tests/hermes_cli/test_voice_wrapper.py b/tests/hermes_cli/test_voice_wrapper.pyindex a06a94735cb..2d996969d45 100644--- a/tests/hermes_cli/test_voice_wrapper.py+++ b/tests/hermes_cli/test_voice_wrapper.py@@ -785,3 +785,93 @@ class TestBeepsEnabledTruthyStrings:monkeypatch.setattr("hermes_cli.config.load_config", lambda: {"voice": {}})assert voice._beeps_enabled() is True++[email protected]_audio_playback+class TestSpeakTextStreamingDispatch:+ """speak_text routes through the generic streaming dispatcher (#58930)+ when a chunked streaming provider resolves — one dispatcher, zero+ parallel streaming implementations."""++ def test_streaming_provider_routes_through_dispatcher(self, monkeypatch):+ import hermes_cli.voice as voice+ import tools.tts_streaming as ts+ from tools import tts_tool++ streamed = []++ def fake_stream(text_queue, stop_event, done_event, *a, **k):+ while True:+ item = text_queue.get()+ if item is None:+ break+ streamed.append(item)+ done_event.set()++ monkeypatch.setattr(+ ts, "resolve_streaming_provider", lambda cfg, preferred=None: object()+ )+ monkeypatch.setattr(tts_tool, "stream_tts_to_speaker", fake_stream)++ synced = []+ monkeypatch.setattr(+ tts_tool, "text_to_speech_tool", lambda **kw: synced.append(kw) or "{}"+ )++ assert voice.speak_text("Hello streaming world") is None+ assert streamed == ["Hello streaming world"]+ assert synced == [], "sync whole-file path must be skipped when streaming"++ def test_no_streamer_falls_back_to_sync_path(self, monkeypatch):+ import hermes_cli.voice as voice+ import tools.tts_streaming as ts+ from tools import tts_tool++ monkeypatch.setattr(+ ts, "resolve_streaming_provider", lambda cfg, preferred=None: None+ )+ played = []+ returned_path = "/tmp/hermes_voice/fallback.mp3"+ monkeypatch.setattr(+ tts_tool,+ "text_to_speech_tool",+ lambda **_kw: f'{{"success": true, "file_path": "{returned_path}"}}',+ )+ monkeypatch.setattr(voice.os, "makedirs", lambda *a, **k: None)+ monkeypatch.setattr(voice.os.path, "isfile", lambda p: p == returned_path)+ monkeypatch.setattr(voice.os.path, "getsize", lambda _p: 1000)+ monkeypatch.setattr(voice.os, "unlink", lambda _p: None)+ monkeypatch.setattr(voice, "play_audio_file", lambda p: played.append(p))++ assert voice.speak_text("Hello sync world") is None+ assert played == [returned_path]++ def test_streaming_failure_falls_back_to_sync_path(self, monkeypatch):+ import hermes_cli.voice as voice+ import tools.tts_streaming as ts+ from tools import tts_tool++ monkeypatch.setattr(+ ts, "resolve_streaming_provider", lambda cfg, preferred=None: object()+ )++ def broken_stream(text_queue, stop_event, done_event, *a, **k):+ raise RuntimeError("audio device exploded")++ monkeypatch.setattr(tts_tool, "stream_tts_to_speaker", broken_stream)++ played = []+ returned_path = "/tmp/hermes_voice/recovered.mp3"+ monkeypatch.setattr(+ tts_tool,+ "text_to_speech_tool",+ lambda **_kw: f'{{"success": true, "file_path": "{returned_path}"}}',+ )+ monkeypatch.setattr(voice.os, "makedirs", lambda *a, **k: None)+ monkeypatch.setattr(voice.os.path, "isfile", lambda p: p == returned_path)+ monkeypatch.setattr(voice.os.path, "getsize", lambda _p: 1000)+ monkeypatch.setattr(voice.os, "unlink", lambda _p: None)+ monkeypatch.setattr(voice, "play_audio_file", lambda p: played.append(p))++ assert voice.speak_text("Recover me") is None+ assert played == [returned_path]