Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(chat): cap TTS text at 500 characters instead of 500 bytes (#20344)

Marcel Jiron · MarMar Labsee8ed2120398
diff --git a/internal/service/chat_pipeline.go b/internal/service/chat_pipeline.go
index dfc54532f3..80e3cb4e59 100644
--- a/internal/service/chat_pipeline.go
+++ b/internal/service/chat_pipeline.go
@@ -2525,29 +2525,67 @@ func parseFileDicts(files interface{}) ([]map[string]interface{}, bool) {
}
// cleanTTSText sanitizes text for TTS synthesis.
-// Mirrors dialog_service.py:1404-1423.
func cleanTTSText(text string) string {
if text == "" {
return ""
}
+ // Drop invalid UTF-8 before applying the character limit.
+ text = strings.ToValidUTF8(text, "")
// Strip control chars.
controlRe := regexp.MustCompile(`[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F]`)
text = controlRe.ReplaceAllString(text, "")
// Strip emojis.
emojiRe := regexp.MustCompile("[\U0001f600-\U0001f64f\U0001f300-\U0001f5ff\U0001f680-\U0001f6ff\U0001f1e0-\U0001f1ff\U00002700-\U000027bf\U0001f900-\U0001f9ff\U0001fa70-\U0001faff\U0001fad0-\U0001faff]+")
text = emojiRe.ReplaceAllString(text, "")
+ text = stripTTSTags(text)
// Collapse whitespace.
wsRe := regexp.MustCompile(`\s+`)
text = wsRe.ReplaceAllString(text, " ")
text = strings.TrimSpace(text)
- if len(text) > 500 {
- text = text[:500]
+ if r := []rune(text); len(r) > 500 {
+ text = string(r[:500])
}
return text
}
+// stripTTSTags removes paired markup and standalone reasoning/void tags.
+// Unmatched tags stay literal so compact comparisons such as a<b>c survive.
+func stripTTSTags(text string) string {
+ // Quoted attributes may contain > without ending the tag.
+ tagRe := regexp.MustCompile(`<(/?)([A-Za-z][A-Za-z0-9:-]*)(?:\s+(?:[^<>"']|"[^"]*"|'[^']*')*)?\s*/?>`)
+ matches := tagRe.FindAllStringSubmatchIndex(text, -1)
+ strip := make([]bool, len(matches))
+ open := make(map[string][]int)
+ for i, match := range matches {
+ name := strings.ToLower(text[match[4]:match[5]])
+ switch name {
+ case "think", "area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source", "track", "wbr":
+ strip[i] = true
+ default:
+ if strings.HasSuffix(text[match[0]:match[1]], "/>") {
+ strip[i] = true
+ } else if match[2] == match[3] {
+ open[name] = append(open[name], i)
+ } else if stack := open[name]; len(stack) > 0 {
+ strip[stack[len(stack)-1]] = true
+ strip[i] = true
+ open[name] = stack[:len(stack)-1]
+ }
+ }
+ }
+ var result strings.Builder
+ last := 0
+ for i, match := range matches {
+ if strip[i] {
+ result.WriteString(text[last:match[0]])
+ last = match[1]
+ }
+ }
+ result.WriteString(text[last:])
+ return result.String()
+}
+
// synthesizeTTS calls the TTS model to convert text to audio.
-// Mirrors dialog_service.py:1426-1432.
func (s *ChatPipelineService) synthesizeTTS(ctx context.Context, ttsModel *modelModule.TTSModel, text string) interface{} {
if ttsModel == nil || text == "" {
return nil
diff --git a/internal/service/chat_pipeline_test.go b/internal/service/chat_pipeline_test.go
index bf619c4d4d..d0c7ec568f 100644
--- a/internal/service/chat_pipeline_test.go
+++ b/internal/service/chat_pipeline_test.go
@@ -2773,3 +2773,101 @@ func TestGetLLMModelConfigReadsToolSupportPerResolution(t *testing.T) {
t.Error("a flipped is_tools flag was not seen by the next resolution")
}
}
+
+// ttsSpyDriver records the text a TTS driver is asked to synthesize.
+type ttsSpyDriver struct {
+ modelModule.ModelDriver
+ input string
+ called bool
+}
+
+func (d *ttsSpyDriver) AudioSpeech(_ context.Context, _ *string, audioContent *string, _ *modelModule.APIConfig, _ *modelModule.TTSConfig, _ *common.ModelUsage) (*modelModule.TTSResponse, error) {
+ d.input = *audioContent
+ d.called = true
+ return &modelModule.TTSResponse{Audio: []byte("audio")}, nil
+}
+
+// TestSynthesizeTTS_CutsAtFiveHundredCharacters pins the cap to 500 characters,
+// so a CJK answer keeps its length and never ends in half a rune.
+func TestSynthesizeTTS_CutsAtFiveHundredCharacters(t *testing.T) {
+ spy := &ttsSpyDriver{}
+ name := "tts"
+ ttsModel := modelModule.NewTTSModel(spy, &name, nil)
+ s := &ChatPipelineService{}
+ if audio := s.synthesizeTTS(t.Context(), ttsModel, strings.Repeat("知", 600)); audio == nil {
+ t.Fatal("expected audio")
+ }
+ if want := strings.Repeat("知", 500); spy.input != want {
+ t.Errorf("TTS input is %d bytes / %d runes, want 500 whole characters", len(spy.input), len([]rune(spy.input)))
+ }
+}
+
+func TestCleanTTSText(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ text string
+ want string
+ }{
+ {name: "empty"},
+ {name: "ASCII below limit", text: strings.Repeat("a", 499), want: strings.Repeat("a", 499)},
+ {name: "ASCII at limit", text: strings.Repeat("a", 500), want: strings.Repeat("a", 500)},
+ {name: "ASCII over limit", text: strings.Repeat("a", 501), want: strings.Repeat("a", 500)},
+ {name: "CJK below limit", text: strings.Repeat("知", 499), want: strings.Repeat("知", 499)},
+ {name: "CJK at limit", text: strings.Repeat("知", 500), want: strings.Repeat("知", 500)},
+ {name: "CJK over limit", text: strings.Repeat("知", 501), want: strings.Repeat("知", 500)},
+ {name: "mixed rune widths", text: strings.Repeat("aé知𐐀", 126), want: strings.Repeat("aé知𐐀", 125)},
+ {name: "control emoji and whitespace", text: "\x00\t你好🙂\nworld\x7f ", want: "你好 world"},
+ {name: "tags", text: "<think>reason</think> <b>answer</b>", want: "reason answer"},
+ {name: "comparisons", text: "2 < 3 and 4 > 1; a < b and c > d", want: "2 < 3 and 4 > 1; a < b and c > d"},
+ {name: "compact comparisons", text: "a<b>c; x<value>y", want: "a<b>c; x<value>y"},
+ {name: "unclosed tag", text: "<B>answer", want: "<B>answer"},
+ {name: "unmatched closing tag", text: "a</b>c", want: "a</b>c"},
+ {name: "nested tags", text: "<B><i>answer</i></b>", want: "answer"},
+ {name: "unmatched outer tag", text: "<b>unclosed <b>paired</b>", want: "<b>unclosed paired"},
+ {name: "encoded comparisons", text: "a&lt;b&gt;c", want: "a&lt;b&gt;c"},
+ {name: "incomplete tag", text: `answer <b title="unfinished`, want: `answer <b title="unfinished`},
+ {name: "quoted attributes", text: `<b title="a > b">answer</b> <span title='c > d'>text</span><br/>`, want: "answer text"},
+ {name: "trailing reasoning tag", text: "答案</think>", want: "答案"},
+ {name: "invalid UTF-8", text: "\xff知\xc3é\xe7\x9f", want: "知é"},
+ {name: "valid replacement character", text: "知\ufffdé", want: "知\ufffdé"},
+ {name: "cleanup before limit", text: "<think>" + strings.Repeat("知", 499) + "</think>\xff界🙂\x00余", want: strings.Repeat("知", 499) + "界"},
+ {name: "cleanup to empty", text: "<think></think>🙂\xff\x00 \t\n"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := cleanTTSText(tc.text); got != tc.want {
+ t.Errorf("cleanTTSText() = %q, want %q", got, tc.want)
+ }
+ })
+ }
+}
+
+func TestSynthesizeTTS_CleansTextBeforeCallingDriver(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ text string
+ want string
+ }{
+ {name: "mixed answer", text: "\xff<think>思考</think> \x00hello🙂", want: "思考 hello"},
+ {name: "compact comparison", text: "a<b>c", want: "a<b>c"},
+ {name: "empty"},
+ {name: "cleanup to empty", text: "<think></think>🙂\xff\x00 \t\n"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ spy := &ttsSpyDriver{}
+ name := "tts"
+ ttsModel := modelModule.NewTTSModel(spy, &name, nil)
+ s := &ChatPipelineService{}
+ audio := s.synthesizeTTS(t.Context(), ttsModel, tc.text)
+ wantCall := tc.want != ""
+ if spy.called != wantCall {
+ t.Errorf("TTS driver called = %v, want %v", spy.called, wantCall)
+ }
+ if spy.input != tc.want {
+ t.Errorf("TTS input = %q, want %q", spy.input, tc.want)
+ }
+ if (audio != nil) != wantCall {
+ t.Errorf("audio = %v, want audio present = %v", audio, wantCall)
+ }
+ })
+ }
+}