mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-05 00:45:29 +08:00
fix(sse): globally strip leaked CONTENT_FILTER suffix from output
This commit is contained in:
30
internal/sse/content_filter_leak.go
Normal file
30
internal/sse/content_filter_leak.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package sse
|
||||
|
||||
import "strings"
|
||||
|
||||
func filterLeakedContentFilterParts(parts []ContentPart) []ContentPart {
|
||||
if len(parts) == 0 {
|
||||
return parts
|
||||
}
|
||||
out := make([]ContentPart, 0, len(parts))
|
||||
for _, p := range parts {
|
||||
cleaned := stripLeakedContentFilterSuffix(p.Text)
|
||||
if strings.TrimSpace(cleaned) == "" {
|
||||
continue
|
||||
}
|
||||
p.Text = cleaned
|
||||
out = append(out, p)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func stripLeakedContentFilterSuffix(text string) string {
|
||||
if text == "" {
|
||||
return text
|
||||
}
|
||||
idx := strings.Index(strings.ToUpper(text), "CONTENT_FILTER")
|
||||
if idx < 0 {
|
||||
return text
|
||||
}
|
||||
return strings.TrimRight(text[:idx], " \t\r\n")
|
||||
}
|
||||
@@ -40,6 +40,7 @@ func ParseDeepSeekContentLine(raw []byte, thinkingEnabled bool, currentType stri
|
||||
}
|
||||
}
|
||||
parts, finished, nextType := ParseSSEChunkForContent(chunk, thinkingEnabled, currentType)
|
||||
parts = filterLeakedContentFilterParts(parts)
|
||||
return LineResult{
|
||||
Parsed: true,
|
||||
Stop: finished,
|
||||
|
||||
@@ -35,3 +35,23 @@ func TestParseDeepSeekContentLineContent(t *testing.T) {
|
||||
t.Fatalf("unexpected parts: %#v", res.Parts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseDeepSeekContentLineStripsLeakedContentFilterSuffix(t *testing.T) {
|
||||
res := ParseDeepSeekContentLine([]byte(`data: {"p":"response/content","v":"正常输出CONTENT_FILTER你好,这个问题我暂时无法回答"}`), false, "text")
|
||||
if !res.Parsed || res.Stop {
|
||||
t.Fatalf("expected parsed non-stop result: %#v", res)
|
||||
}
|
||||
if len(res.Parts) != 1 || res.Parts[0].Text != "正常输出" {
|
||||
t.Fatalf("unexpected parts after filter: %#v", res.Parts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseDeepSeekContentLineDropsPureLeakedContentFilterChunk(t *testing.T) {
|
||||
res := ParseDeepSeekContentLine([]byte(`data: {"p":"response/content","v":"CONTENT_FILTER你好,这个问题我暂时无法回答"}`), false, "text")
|
||||
if !res.Parsed || res.Stop {
|
||||
t.Fatalf("expected parsed non-stop result: %#v", res)
|
||||
}
|
||||
if len(res.Parts) != 0 {
|
||||
t.Fatalf("expected empty parts, got %#v", res.Parts)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user