The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
feat: make the streaming TTS first-sentence threshold configurable (tts.streaming.min_len)
liuhao10249823ce4ff1ba
diff --git a/gateway/streaming_tts_consumer.py b/gateway/streaming_tts_consumer.pyindex f3d7ddd7660..930365f70a9 100644--- a/gateway/streaming_tts_consumer.py+++ b/gateway/streaming_tts_consumer.py@@ -34,7 +34,7 @@ class StreamingTTSConsumer:self._adapter, self._chat_id, self._loop, self._metadata = adapter, chat_id, loop, metadata# Resolved once; None => inactive, gateway falls back to whole-file TTS.self._streamer = resolve_streaming_provider(tts_config)- self._chunker = SentenceChunker()+ self._chunker = SentenceChunker.from_config(tts_config)self._audio_format = audio_format or AudioFormat() if self._streamer is None else (AudioFormat(**{f: int(getattr(self._streamer, f, getattr(AudioFormat, f)))for f in ("sample_rate", "channels", "sample_width")})diff --git a/hermes_cli/config_defaults.py b/hermes_cli/config_defaults.pyindex 5f0970c3936..d2509fa6d73 100644--- a/hermes_cli/config_defaults.py+++ b/hermes_cli/config_defaults.py@@ -1019,6 +1019,11 @@ DEFAULT_CONFIG = {# "edge" (free) | "elevenlabs" (premium) | "openai" | "xai" | "minimax" | "mistral" |# "gemini" | "deepinfra" | "neutts" (local) | "kittentts" (local) | "piper" (local)"provider": "edge",+ "streaming": {+ # Shortest first sentence (chars) spoken on its own by streaming TTS; shorter openers+ # ride with the next sentence. 20 suits English; CJK voice setups use ~6.+ "min_len": 20,+ },"edge": {# Popular: AriaNeural, JennyNeural, AndrewNeural, BrianNeural, SoniaNeural"voice": "en-US-AriaNeural",diff --git a/hermes_cli/web_routers/audio.py b/hermes_cli/web_routers/audio.pyindex 09a9a4c4699..ae7a787630b 100644--- a/hermes_cli/web_routers/audio.py+++ b/hermes_cli/web_routers/audio.py@@ -409,10 +409,10 @@ async def speak_stream_ws(ws: "WebSocket") -> None:cfg = _load_tts_config()streamer = resolve_streaming_provider(cfg)cap = _resolve_max_text_length(_get_provider(cfg), cfg) if streamer else 0- return streamer, cap+ return streamer, cap, cfgtry:- streamer, cap = await loop.run_in_executor(None, _resolve)+ streamer, cap, cfg = await loop.run_in_executor(None, _resolve)except Exception:_log.exception("speak-stream provider resolution failed")streamer, cap = None, 0@@ -441,7 +441,7 @@ async def speak_stream_ws(ws: "WebSocket") -> None:from tools.tts_streaming import SentenceChunkerfrom tools.tts_text_normalize import _strip_markdown_for_tts- chunker = SentenceChunker()+ chunker = SentenceChunker.from_config(cfg) # the requesting profile's tts.streaming.min_len# The session stays open for a whole agent turn and no text arrives# during tool execution, so without an idle flush a narration line withdiff --git a/tests/gateway/test_streaming_tts_consumer.py b/tests/gateway/test_streaming_tts_consumer.pyindex 95893da118b..d4d18311254 100644--- a/tests/gateway/test_streaming_tts_consumer.py+++ b/tests/gateway/test_streaming_tts_consumer.py@@ -463,6 +463,19 @@ class TestStreamerFormatAndLooping:tts_streaming.resolve_streaming_provider = original_resolveloop.close()+ def test_chunker_min_len_comes_from_tts_streaming_config(self):+ """The gateway consumer honours tts.streaming.min_len (#96927) instead of the class default."""+ import tools.tts_streaming as tts_streaming+ original_resolve = tts_streaming.resolve_streaming_provider+ tts_streaming.resolve_streaming_provider = lambda *_args, **_kwargs: None+ loop = asyncio.new_event_loop()+ try:+ consumer = StreamingTTSConsumer(FakeVoiceAdapter(), "chat1", {"streaming": {"min_len": 6}}, loop)+ assert consumer._chunker.min_len == 6+ finally:+ tts_streaming.resolve_streaming_provider = original_resolve+ loop.close()+class TestGatewayIntegrationSeam:"""The actual adapter seam is per-turn, not chat-only."""diff --git a/tests/tools/test_tts_streaming.py b/tests/tools/test_tts_streaming.pyindex 58fa2f5731d..3a4e5e35409 100644--- a/tests/tools/test_tts_streaming.py+++ b/tests/tools/test_tts_streaming.py@@ -44,6 +44,16 @@ class TestSentenceChunker:"A paragraph without punctuation\n\n"]+ def test_from_config_reads_streaming_min_len(self):+ """tts.streaming.min_len decides whether a short CJK opener is spoken alone (#96927);+ unset/invalid keep the English default of 20, 0 floors to 1."""+ assert ts.SentenceChunker.from_config({}).min_len == 20+ assert ts.SentenceChunker.from_config({"streaming": {"min_len": "abc"}}).min_len == 20+ assert ts.SentenceChunker.from_config({"streaming": {"min_len": 0}}).min_len == 1+ c = ts.SentenceChunker.from_config({"streaming": {"min_len": 6}})+ assert c.feed("记得,叫团团. ") == ["记得,叫团团. "]+ assert ts.SentenceChunker().feed("记得,叫团团. ") == []+# ── Interruption latch ───────────────────────────────────────────────────diff --git a/tools/tts_streaming.py b/tools/tts_streaming.pyindex 71bc4a2cee5..d66b6bd812f 100644--- a/tools/tts_streaming.py+++ b/tools/tts_streaming.py@@ -73,6 +73,18 @@ class SentenceChunker:self.min_len = min_lenself.buf = ""+ @classmethod+ def from_config(cls, tts_config: Dict) -> "SentenceChunker":+ """Chunker honouring ``tts.streaming.min_len``. 20 suits English; a CJK opener of 5–7+ characters is a whole clause, so voice setups lower it to speak the first sentence+ alone instead of buffering it behind the second. Floor 1: 0 would emit every boundary."""+ streaming = tts_config.get("streaming") if isinstance(tts_config, dict) else None+ raw = streaming.get("min_len", 20) if isinstance(streaming, dict) else 20+ try:+ return cls(min_len=max(1, int(raw)))+ except (TypeError, ValueError):+ return cls()+def feed(self, delta: str) -> List[str]:"""Absorb *delta*; return every complete sentence now ready to speak."""self.buf = _THINK_BLOCK_RE.sub("", self.buf + delta)diff --git a/tools/tts_tool_speaker.py b/tools/tts_tool_speaker.pyindex 2d05938a7ec..8cec027cc2b 100644--- a/tools/tts_tool_speaker.py+++ b/tools/tts_tool_speaker.py@@ -302,7 +302,7 @@ def stream_tts_to_speaker(stream_max_len = origin._resolve_max_text_length(provider or origin._get_provider(tts_config), tts_config)playback = _StreamerPlayback(streamer, stop_event)- chunker = SentenceChunker()+ chunker = SentenceChunker.from_config(tts_config)spoken_sentences: list[str] = [] # skip duplicate/near-duplicate sentences (LLM repetition)def _speak_sentence(sentence: str) -> None:diff --git a/website/docs/developer-guide/streaming-tts.md b/website/docs/developer-guide/streaming-tts.mdindex 2cd77224e18..c18df3ef871 100644--- a/website/docs/developer-guide/streaming-tts.md+++ b/website/docs/developer-guide/streaming-tts.md@@ -49,6 +49,7 @@ tts:provider: geministreaming:provider: gemini # or "auto"+ min_len: 20 # shortest first sentence (chars) spoken on its own; CJK setups use ~6gemini:model: gemini-2.5-flash-preview-ttsvoice: Korediff --git a/website/docs/user-guide/features/tts.md b/website/docs/user-guide/features/tts.mdindex ecff510d470..e49d7d5f621 100644--- a/website/docs/user-guide/features/tts.md+++ b/website/docs/user-guide/features/tts.md@@ -58,6 +58,8 @@ tts:base_url: "https://api.openai.com/v1" # Override for OpenAI-compatible TTS endpointsspeed: 1.0 # 0.25 - 4.0# language: "es" # Sent as lang_code — only for OpenAI-compatible endpoints that support it (e.g. Kokoro)+ streaming:+ min_len: 20 # Streaming TTS: shortest first sentence (chars) spoken on its own; CJK setups use ~6minimax:region: "global" # "global" or "cn"; see selection rules belowmodel: "speech-02-hd" # speech-02-hd (default), speech-02-turbo