Merge pull request #282 from livesRan/fix/citation-link-mapping-pr

修复搜索场景下 citation 标签偶发未替换问题(FINISHED 后继续收集引用元数据)
This commit is contained in:
CJACK. authored and GitHub committed 2026-04-22 23:31:32 +08:00
commit 3a79b07d33
2 files changed
+26 -1

No files matched your search

+8 -1
View File
@@ -29,6 +29,7 @@ func CollectStream(resp *http.Response, thinkingEnabled bool, closeBody bool) Co
text := strings.Builder{}
thinking := strings.Builder{}
contentFilter := false
stopped := false
collector := newCitationLinkCollector()
currentType := "text"
if thinkingEnabled {
@@ -38,6 +39,9 @@ func CollectStream(resp *http.Response, thinkingEnabled bool, closeBody bool) Co
if chunk, done, parsed := ParseDeepSeekSSELine(line); parsed && !done {
collector.ingestChunk(chunk)
}
if stopped {
return true
}
result := ParseDeepSeekContentLine(line, thinkingEnabled, currentType)
currentType = result.NextType
if !result.Parsed {
@@ -47,7 +51,10 @@ func CollectStream(resp *http.Response, thinkingEnabled bool, closeBody bool) Co
if result.ContentFilter {
contentFilter = true
}
return false
// Keep scanning to collect late-arriving citation metadata lines
// that can appear after response/status=FINISHED.
stopped = true
return true
}
for _, p := range result.Parts {
if p.Type == "thinking" {
+18
View File
@@ -185,6 +185,24 @@ func TestCollectStreamExtractsCitationLinksWithRepeatedURLsAndNilIndices(t *test
}
}
func TestCollectStreamCollectsCitationLinksAfterFinished(t *testing.T) {
resp := makeHTTPResponse(
"data: {\"p\":\"response/content\",\"v\":\"结论[citation:1]\"}\n" +
"data: {\"p\":\"response/status\",\"v\":\"FINISHED\"}\n" +
"data: {\"p\":\"response/fragments/-1/results\",\"v\":[{\"url\":\"https://example.com/a\",\"cite_index\":1}]}\n" +
"data: {\"p\":\"response/content\",\"v\":\"should-not-append\"}\n" +
"data: [DONE]\n",
)
result := CollectStream(resp, false, false)
if result.Text != "结论[citation:1]" {
t.Fatalf("expected text to freeze after finished, got %q", result.Text)
}
if got := result.CitationLinks[1]; got != "https://example.com/a" {
t.Fatalf("expected citation 1 link, got %q", got)
}
}
func TestCollectStreamMultipleThinkingChunks(t *testing.T) {
resp := makeHTTPResponse(
"data: {\"p\":\"response/thinking_content\",\"v\":\"part1\"}\n" +