Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,39 @@ func TestEnvBackedStoreWritebackDoesNotBootstrapOnInvalidEnvJSON(t *testing.T) {
}
}

func TestEnvBackedStoreWritebackFallsBackToPersistedFileWhenEnvMalformed(t *testing.T) {
tmp, err := os.CreateTemp(t.TempDir(), "config-*.json")
if err != nil {
t.Fatalf("create temp config: %v", err)
}
path := tmp.Name()
_ = tmp.Close()

seed := `{"keys":["file-k"],"accounts":[{"email":"file@example.com","password":"p"}]}`
if err := os.WriteFile(path, []byte(seed), 0o644); err != nil {
t.Fatalf("write seed config: %v", err)
}

t.Setenv("DS2API_CONFIG_JSON", "{invalid-json")
t.Setenv("CONFIG_JSON", "")
t.Setenv("DS2API_CONFIG_PATH", path)
t.Setenv("DS2API_ENV_WRITEBACK", "1")

cfg, fromEnv, loadErr := loadConfig()
if loadErr == nil {
t.Fatalf("expected loadConfig error for invalid env json")
}
if fromEnv {
t.Fatalf("expected fromEnv=false when persisted config file fallback succeeds")
}
if len(cfg.Keys) != 1 || cfg.Keys[0] != "file-k" {
t.Fatalf("expected keys from persisted file, got %#v", cfg.Keys)
}
if len(cfg.Accounts) != 1 || cfg.Accounts[0].Email != "file@example.com" {
t.Fatalf("expected accounts from persisted file, got %#v", cfg.Accounts)
}
}

func TestRuntimeTokenRefreshIntervalHoursDefaultsToSix(t *testing.T) {
t.Setenv("DS2API_CONFIG_JSON", `{
"keys":["k1"],
Expand Down
11 changes: 11 additions & 0 deletions internal/config/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ func loadConfig() (Config, bool, error) {
if rawCfg != "" {
cfg, err := parseConfigString(rawCfg)
if err != nil {
if IsVercel() || !envWritebackEnabled() {
return cfg, true, err
}
content, fileErr := os.ReadFile(ConfigPath())
if fileErr == nil {
var fileCfg Config
if unmarshalErr := json.Unmarshal(content, &fileCfg); unmarshalErr == nil {
fileCfg.DropInvalidAccounts()
return fileCfg, false, err
}
}
return cfg, true, err
}
cfg.ClearAccountTokens()
Expand Down
26 changes: 25 additions & 1 deletion internal/sse/content_filter_leak.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ package sse

import "strings"

var leakedContentFilterSuffixPrefixes = []string{
"你好,这个问题我暂时无法回答",
"您好,这个问题我暂时无法回答",
",这个问题我暂时无法回答",
",这个问题我暂时无法回答",
}

func filterLeakedContentFilterParts(parts []ContentPart) []ContentPart {
if len(parts) == 0 {
return parts
Expand All @@ -22,9 +29,26 @@ func stripLeakedContentFilterSuffix(text string) string {
if text == "" {
return text
}
idx := strings.Index(strings.ToUpper(text), "CONTENT_FILTER")
upper := strings.ToUpper(text)
idx := strings.Index(upper, "CONTENT_FILTER")
if idx < 0 {
return text
}
suffix := strings.TrimSpace(text[idx+len("CONTENT_FILTER"):])
if !looksLikeLeakedContentFilterSuffix(suffix) {
return text
}
return strings.TrimRight(text[:idx], " \t\r\n")
}

func looksLikeLeakedContentFilterSuffix(suffix string) bool {
if suffix == "" {
return false
}
for _, p := range leakedContentFilterSuffixPrefixes {
if strings.HasPrefix(suffix, p) {
return true
}
}
return false
}
10 changes: 10 additions & 0 deletions internal/sse/line_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,13 @@ func TestParseDeepSeekContentLineDropsPureLeakedContentFilterChunk(t *testing.T)
t.Fatalf("expected empty parts, got %#v", res.Parts)
}
}

func TestParseDeepSeekContentLineKeepsLegitContentFilterMentions(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 != "字符串 CONTENT_FILTER 用于说明上游审核信号" {
t.Fatalf("unexpected parts for legit mention: %#v", res.Parts)
}
}
Loading