Skip to content

支持 reference 引用标签转链接,并兼容 0 基序号映射#343

Merged
CJackHwang merged 1 commit intoCJackHwang:devfrom
livesRan:fix-429Resend-pr
Apr 28, 2026
Merged

支持 reference 引用标签转链接,并兼容 0 基序号映射#343
CJackHwang merged 1 commit intoCJackHwang:devfrom
livesRan:fix-429Resend-pr

Conversation

@livesRan
Copy link
Copy Markdown
Contributor

💻 变更类型 | Change Type

  • ✨ feat
  • [✅ ] 🐛 fix
  • ♻️ refactor
  • 💄 style
  • 👷 build
  • ⚡️ perf
  • 📝 docs
  • 🔨 chore

🔀 变更说明 | Description of Change

背景
通过 API 返回的正文里出现 [reference:N] 标签,但旧逻辑只会替换 [citation:N],导致 reference 标签未被转成链接。

改动

正则由仅匹配 citation 扩展为同时匹配 citation 与 reference
引入 reference:0 场景下的序号偏移处理,保证链接映射正确
增加单元测试:
[reference:N] 标签替换
[reference:0] 的 0 基序号映射
保持原有未知索引不替换行为
验证

go test httpapi. 全部通过
本地接口验证,[reference:N] 标签可被替换为可点击链接
影响范围

仅影响 OpenAI 输出中的引用标签替换逻辑
不改变上游请求协议,不影响非引用文本内容

Copilot AI review requested due to automatic review settings April 28, 2026 08:46
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 28, 2026

@livesRan is attempting to deploy a commit to the cjack's projects Team on Vercel.

A member of the Team first needs to authorize it.

@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 28, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
ds2api Ready Ready Preview, Comment Apr 28, 2026 8:49am

@CJackHwang CJackHwang changed the base branch from main to dev April 28, 2026 08:49
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes OpenAI output citation handling by converting [reference:N] markers into markdown links and adding support for a zero-based [reference:0] indexing scheme.

Changes:

  • Extend the citation marker regex to match both citation and reference.
  • Add “zero-based reference” index shifting when [reference:0] is present.
  • Add unit tests for reference marker replacement and zero-based mapping behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
internal/httpapi/openai/shared/citation_links.go Expands marker parsing to include reference and introduces zero-based index shifting logic.
internal/httpapi/openai/citation_links_test.go Adds tests covering [reference:N] replacement and [reference:0] zero-based mapping.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +16 to +30
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])
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zeroBased is derived from the whole text and then applied to all markers. If the response contains both [citation:N] and [reference:0], citation lookups will be shifted by +1 and link to the wrong URL. Consider applying the +1 offset only when the matched marker type is reference (use sub[1]) and leaving citation indices unchanged.

Copilot uses AI. Check for mistakes.
Comment on lines 11 to +16

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]")
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The zeroBased detection uses a literal substring "[reference:0]", but the replacement regex allows whitespace (\s*). Inputs like [reference: 0] or [REFERENCE:0] would be matched/replaced, yet zeroBased would be false and the link index mapping would be wrong. Suggest detecting zero-based mode with a case-insensitive regex like (?i)\[reference:\s*0\] (or by scanning matches) so it stays consistent with the main pattern.

Suggested change
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]")
var zeroBasedReferencePattern = regexp.MustCompile(`(?i)\[reference:\s*0\]`)
func ReplaceCitationMarkersWithLinks(text string, links map[int]string) string {
if strings.TrimSpace(text) == "" || len(links) == 0 {
return text
}
zeroBased := zeroBasedReferencePattern.MatchString(text)

Copilot uses AI. Check for mistakes.
Comment on lines +22 to 24
idx, err := strconv.Atoi(strings.TrimSpace(sub[2]))
if err != nil || idx < 0 {
return match
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the index validation from idx <= 0 to idx < 0 means [citation:0] is now eligible for replacement if links[0] exists, which is a behavior change from the previous implementation (and likely inconsistent with the 1-based citation markers used elsewhere). If only reference can be 0-based, keep rejecting citation:0 (and consider adding a test to lock this behavior).

Copilot uses AI. Check for mistakes.
@CJackHwang
Copy link
Copy Markdown
Owner

项目里面有一个去除reference的功能默认启用的
你的这个改动是在功能关闭的情况下替换的是吗

@CJackHwang CJackHwang merged commit 685b501 into CJackHwang:dev Apr 28, 2026
11 of 12 checks passed
@livesRan
Copy link
Copy Markdown
Contributor Author

@CJackHwang 此次改动只有当 "strip_reference_markers": false时才生效,对于默认启用时没有影响

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants