Skip to content

The agent that grows with you

fix: honour tts.streaming.min_len on the Desktop client-direct TTS path

teknium1744945b3ba43
diff --git a/apps/desktop/src/lib/voice-client-direct.test.ts b/apps/desktop/src/lib/voice-client-direct.test.ts
index 6109ef04421..a2446ac087f 100644
--- a/apps/desktop/src/lib/voice-client-direct.test.ts
+++ b/apps/desktop/src/lib/voice-client-direct.test.ts
@@ -334,4 +334,13 @@ describe('cutSentences', () => {
expect(sentences[0]).toContain('。')
expect(sentences).toHaveLength(2)
})
+
+ it('cuts a short CJK opener alone when the backend sends tts.streaming.min_len', () => {
+ const text = '记得,叫团团。 然后我们再说第二句话,这一句要长一些才行。 '
+
+ // Historical 24-char floor (older backend, no key): the opener rides with sentence two.
+ expect(cutSentences(text, false).sentences).toEqual(['记得,叫团团。 然后我们再说第二句话,这一句要长一些才行。'])
+ // tts.streaming.min_len = 6 (the CJK voice setup from #96927): spoken on its own.
+ expect(cutSentences(text, false, 6).sentences).toEqual(['记得,叫团团。', '然后我们再说第二句话,这一句要长一些才行。'])
+ })
})
diff --git a/apps/desktop/src/lib/voice-client-direct.ts b/apps/desktop/src/lib/voice-client-direct.ts
index ae13747783f..827ce99dbc2 100644
--- a/apps/desktop/src/lib/voice-client-direct.ts
+++ b/apps/desktop/src/lib/voice-client-direct.ts
@@ -38,6 +38,8 @@ export interface DirectTtsConfig {
model: null | string
voice: null | string
speed: null | number
+ /** tts.streaming.min_len — shortest first sentence (chars) cut on its own; absent on older backends. */
+ min_len?: null | number
}
interface RelayConfig {
@@ -341,7 +343,14 @@ export async function synthesizeSpeechClientDirect(tts: DirectTtsConfig, text: s
const SENTENCE_BOUNDARY_RE = /[.!?…。!?]+["'”’)\]]*\s+/g
const MIN_SENTENCE_CHARS = 24
-export function cutSentences(buffer: string, flush: boolean): { sentences: string[]; rest: string } {
+export function cutSentences(
+ buffer: string,
+ flush: boolean,
+ minSentenceChars?: null | number
+): { sentences: string[]; rest: string } {
+ // tts.streaming.min_len when the backend sends it (a 5–7 char CJK opener is a
+ // whole clause); the historical 24 for older backends without the key.
+ const minChars = minSentenceChars ?? MIN_SENTENCE_CHARS
const sentences: string[] = []
let rest = buffer
let start = 0
@@ -356,7 +365,7 @@ export function cutSentences(buffer: string, flush: boolean): { sentences: strin
// Too-short fragments ("e.g. ", "1. ") stay buffered so we don't fire a
// provider call per abbreviation — unless a later boundary extends them.
- if (candidate.length >= MIN_SENTENCE_CHARS) {
+ if (candidate.length >= minChars) {
sentences.push(candidate)
start = end
}
diff --git a/apps/desktop/src/lib/voice-playback.ts b/apps/desktop/src/lib/voice-playback.ts
index 0efb99c631f..8fc85065d9c 100644
--- a/apps/desktop/src/lib/voice-playback.ts
+++ b/apps/desktop/src/lib/voice-playback.ts
@@ -303,7 +303,7 @@ function openClientDirectSpeechSession(tts: DirectTtsConfig, options: VoicePlayb
}
const ingest = (flush: boolean) => {
- const cut = cutSentences(buffer, flush)
+ const cut = cutSentences(buffer, flush, tts.min_len)
buffer = cut.rest
if (cut.sentences.length > 0) {
diff --git a/tests/tools/test_voice_client_config.py b/tests/tools/test_voice_client_config.py
index ac9a0d46748..7f087f1dd11 100644
--- a/tests/tools/test_voice_client_config.py
+++ b/tests/tools/test_voice_client_config.py
@@ -146,6 +146,7 @@ def test_elevenlabs_tts_direct_carries_voice_and_model(voice_home, monkeypatch):
"tts": {
"provider": "elevenlabs",
"elevenlabs": {"voice_id": "voice123", "model_id": "eleven_turbo_v2"},
+ "streaming": {"min_len": 6},
},
})
monkeypatch.setenv("ELEVENLABS_API_KEY", "el_key")
@@ -155,6 +156,8 @@ def test_elevenlabs_tts_direct_carries_voice_and_model(voice_home, monkeypatch):
assert tts["voice"] == "voice123"
assert tts["model"] == "eleven_turbo_v2"
assert "elevenlabs.io" in tts["base_url"]
+ # Desktop client-direct playback cuts sentences with tts.streaming.min_len too (#96927).
+ assert tts["min_len"] == 6
def test_command_provider_relays(voice_home, monkeypatch):
diff --git a/tools/voice_client_config.py b/tools/voice_client_config.py
index 87b201dac85..109a80f8a9f 100644
--- a/tools/voice_client_config.py
+++ b/tools/voice_client_config.py
@@ -153,6 +153,10 @@ def _resolve_tts_client_config() -> Dict[str, Any]:
provider = tts._get_provider(tts_config)
if provider not in tts.BUILTIN_TTS_PROVIDERS:
return _relay("command/plugin provider")
+ # The desktop's client-direct sentence cutter honours the same tts.streaming.min_len as
+ # the gateway/CLI chunkers, so a short CJK opener is spoken alone on every surface.
+ from tools.tts_streaming import SentenceChunker
+ min_len = SentenceChunker.from_config(tts_config).min_len
if provider == "openai":
# Covers the direct-key, custom-base_url, and Nous-managed selections.
@@ -174,7 +178,8 @@ def _resolve_tts_client_config() -> Dict[str, Any]:
except (TypeError, ValueError):
speed = 1.0
return _direct(TTS_WIRE_OPENAI, "openai", base_url, api_key, model,
- voice=oai.get("voice") or tts_tool_openai.DEFAULT_OPENAI_VOICE, speed=speed)
+ voice=oai.get("voice") or tts_tool_openai.DEFAULT_OPENAI_VOICE, speed=speed,
+ min_len=min_len)
if provider == "elevenlabs":
api_key = tts._resolve_provider_key("ELEVENLABS_API_KEY", "elevenlabs")
if not api_key:
@@ -183,7 +188,8 @@ def _resolve_tts_client_config() -> Dict[str, Any]:
return _direct(TTS_WIRE_ELEVENLABS, "elevenlabs",
str(el.get("base_url") or "https://api.elevenlabs.io/v1").rstrip("/"),
api_key, el.get("model_id") or tts_tool_providers.DEFAULT_ELEVENLABS_MODEL_ID,
- voice=el.get("voice_id") or tts_tool_providers.DEFAULT_ELEVENLABS_VOICE_ID, speed=None)
+ voice=el.get("voice_id") or tts_tool_providers.DEFAULT_ELEVENLABS_VOICE_ID, speed=None,
+ min_len=min_len)
if provider == "deepinfra":
api_key = tts._resolve_provider_key("DEEPINFRA_API_KEY", "deepinfra")
if not api_key:
@@ -194,7 +200,7 @@ def _resolve_tts_client_config() -> Dict[str, Any]:
if not model:
return _relay("no deepinfra tts model")
return _direct(TTS_WIRE_OPENAI, "deepinfra", deepinfra_base_url(di), api_key, model,
- voice=di.get("voice") or "af_bella", speed=None)
+ voice=di.get("voice") or "af_bella", speed=None, min_len=min_len)
# edge / minimax / xai / mistral / gemini / neutts / kittentts / piper: server-host-only
# engines or wire shapes the desktop doesn't speak yet; the relay path serves them.
return _relay(f"provider {provider!r} has no client wire")