支持 reference 引用标签转链接,并兼容 0 基序号映射#343
Conversation
|
@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. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
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
citationandreference. - 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.
| 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]) |
There was a problem hiding this comment.
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.
|
|
||
| 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]") |
There was a problem hiding this comment.
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.
| 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) |
| idx, err := strconv.Atoi(strings.TrimSpace(sub[2])) | ||
| if err != nil || idx < 0 { | ||
| return match |
There was a problem hiding this comment.
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).
|
项目里面有一个去除reference的功能默认启用的 |
|
@CJackHwang 此次改动只有当 "strip_reference_markers": false时才生效,对于默认启用时没有影响 |
💻 变更类型 | Change Type
🔀 变更说明 | 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 输出中的引用标签替换逻辑
不改变上游请求协议,不影响非引用文本内容