fix: 修复 PR 生成页 INLINECODE 显示及复制 Markdown 失败问题
Deploy PR-Helper / deploy (push) Successful in 26s

- 改进 LLM 提示词,明确要求用反引号包裹代码引用,避免输出 INLINECODE 占位符
- 后端新增 fixInlineCode 后处理,将残留 INLINECODE 占位符转为反引号代码
- 前端在 done 回调中从结构化字段组装完整 markdown 文本,修复复制按钮复制空内容
- 复制按钮增加空内容检查和错误提示
- Markdown 预览区域初始隐藏,生成完成后自动显示
This commit is contained in:
2026-06-21 15:37:10 +08:00
parent 2d3e306067
commit 4a8e43e698
2 changed files with 48 additions and 4 deletions
+19 -1
View File
@@ -4,6 +4,7 @@ import (
"database/sql"
"encoding/json"
"fmt"
"regexp"
"strings"
"github.com/go-git/go-git/v5/plumbing"
@@ -111,7 +112,11 @@ func GeneratePR(db *sql.DB, repoPath, base, head string, userID int64, callback
"summary": "一段话概述变更内容",
"details": "详细的变更说明,按模块分组,使用 Markdown 格式",
"impact": "影响范围说明"
}`, commitStr, diff)
}
重要格式要求:
- details 字段中引用文件名、函数名、变量名等代码标识时,必须用反引号包裹,例如:`+"`services/generate.go`"+`, `+"`GeneratePR()`"+`
- 直接写出实际的代码名称,不要用任何占位符替代`, commitStr, diff)
messages := []goopenai.ChatCompletionMessage{
{Role: goopenai.ChatMessageRoleUser, Content: prompt},
@@ -130,6 +135,9 @@ func GeneratePR(db *sql.DB, repoPath, base, head string, userID int64, callback
return nil, fmt.Errorf("parse PR description: %w (raw: %s)", err, truncateString(fullResponse, 200))
}
// Post-process: convert INLINECODE placeholders back to backtick-enclosed code
pr.Details = fixInlineCode(pr.Details)
// Send structured events
if callback != nil {
callback("title", map[string]interface{}{"content": pr.Title})
@@ -143,6 +151,16 @@ func GeneratePR(db *sql.DB, repoPath, base, head string, userID int64, callback
return &pr, nil
}
// fixInlineCode converts LLM-generated INLINECODE placeholders back to backtick-enclosed inline code.
// Some LLMs output INLINECODE0, INLINECODE1, etc. instead of `code` in JSON string values.
// This wraps them in backticks so the frontend markdown renderer displays them as inline code.
func fixInlineCode(text string) string {
re := regexp.MustCompile(`(?i)INLINECODE[_]?(\d+)`)
return re.ReplaceAllStringFunc(text, func(match string) string {
return "`" + match + "`"
})
}
func truncateString(s string, maxLen int) string {
if len(s) <= maxLen {
return s