fix(tts): pick earliest split for the first sentence, latest for the rest

_get_segment_text used rfind for every sentence and kept the smallest of the
last-occurrence positions across the punctuation set, an inconsistent
heuristic. Differentiate by position in the reply:

- First sentence: take the earliest punctuation boundary (find) so the first
  audio segment is produced as soon as possible — lower time-to-first-audio.
- Subsequent sentences: take the latest boundary (rfind) so segments are as
  large as possible — fewer TTS calls and smoother playback.

The tts_stop_request flush branch is unchanged.
This commit is contained in:
Brad Shannon 2026-06-17 17:57:49 -04:00
parent 071ed99a32
commit 5d6e71a641

View file

@ -494,10 +494,15 @@ class TTSProviderBase(ABC):
) )
for punct in punctuations_to_use: for punct in punctuations_to_use:
if self.is_first_sentence:
# 首句取最早的标点边界,尽快产出第一段音频,降低首音延迟
pos = current_text.find(punct)
if pos != -1 and (last_punct_pos == -1 or pos < last_punct_pos):
last_punct_pos = pos
else:
# 后续句取最晚的标点边界,合并为更大的分段,减少TTS调用次数
pos = current_text.rfind(punct) pos = current_text.rfind(punct)
if (pos != -1 and last_punct_pos == -1) or ( if pos != -1 and pos > last_punct_pos:
pos != -1 and pos < last_punct_pos
):
last_punct_pos = pos last_punct_pos = pos
if last_punct_pos != -1: if last_punct_pos != -1: