From 15e9eb3639f104fa04eab7bb26e4a1400ac72c18 Mon Sep 17 00:00:00 2001 From: songguoliang <957057673@qq.com> Date: Tue, 28 Apr 2026 16:42:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=20reference=20=E5=BC=95?= =?UTF-8?q?=E7=94=A8=E6=A0=87=E7=AD=BE=E8=BD=AC=E9=93=BE=E6=8E=A5=EF=BC=8C?= =?UTF-8?q?=E5=B9=B6=E5=85=BC=E5=AE=B9=200=20=E5=9F=BA=E5=BA=8F=E5=8F=B7?= =?UTF-8?q?=E6=98=A0=E5=B0=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../httpapi/openai/citation_links_test.go | 28 +++++++++++++++++++ .../httpapi/openai/shared/citation_links.go | 15 ++++++---- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/internal/httpapi/openai/citation_links_test.go b/internal/httpapi/openai/citation_links_test.go index 1cdaf90..a7f10d0 100644 --- a/internal/httpapi/openai/citation_links_test.go +++ b/internal/httpapi/openai/citation_links_test.go @@ -26,3 +26,31 @@ func TestReplaceCitationMarkersWithLinksKeepsUnknownIndex(t *testing.T) { t.Fatalf("expected %q, got %q", want, got) } } + +func TestReplaceCitationMarkersWithLinksSupportsReferenceMarker(t *testing.T) { + raw := "新闻摘要[reference:1],详情[reference:2]。" + links := map[int]string{ + 1: "https://example.com/r1", + 2: "https://example.com/r2", + } + + got := replaceCitationMarkersWithLinks(raw, links) + want := "新闻摘要[1](https://example.com/r1),详情[2](https://example.com/r2)。" + if got != want { + t.Fatalf("expected %q, got %q", want, got) + } +} + +func TestReplaceCitationMarkersWithLinksSupportsReferenceZeroBased(t *testing.T) { + raw := "来源[reference:0] 与 [reference:1]。" + links := map[int]string{ + 1: "https://example.com/first", + 2: "https://example.com/second", + } + + got := replaceCitationMarkersWithLinks(raw, links) + want := "来源[0](https://example.com/first) 与 [1](https://example.com/second)。" + if got != want { + t.Fatalf("expected %q, got %q", want, got) + } +} diff --git a/internal/httpapi/openai/shared/citation_links.go b/internal/httpapi/openai/shared/citation_links.go index 60d7408..9b2b77f 100644 --- a/internal/httpapi/openai/shared/citation_links.go +++ b/internal/httpapi/openai/shared/citation_links.go @@ -7,22 +7,27 @@ import ( "strings" ) -var citationMarkerPattern = regexp.MustCompile(`(?i)\[citation:\s*(\d+)\]`) +var citationMarkerPattern = regexp.MustCompile(`(?i)\[(citation|reference):\s*(\d+)\]`) func ReplaceCitationMarkersWithLinks(text string, links map[int]string) string { if strings.TrimSpace(text) == "" || len(links) == 0 { return text } + zeroBased := strings.Contains(strings.ToLower(text), "[reference:0]") return citationMarkerPattern.ReplaceAllStringFunc(text, func(match string) string { sub := citationMarkerPattern.FindStringSubmatch(match) - if len(sub) < 2 { + if len(sub) < 3 { return match } - idx, err := strconv.Atoi(strings.TrimSpace(sub[1])) - if err != nil || idx <= 0 { + idx, err := strconv.Atoi(strings.TrimSpace(sub[2])) + if err != nil || idx < 0 { return match } - url := strings.TrimSpace(links[idx]) + lookupIdx := idx + if zeroBased { + lookupIdx = idx + 1 + } + url := strings.TrimSpace(links[lookupIdx]) if url == "" { return match }