refactor: 后端代码质量优化 - 复用公共库、使用标准库、类型安全错误判断
## 高优先级修复 - stats_service_impl: 使用 strings.SplitN 替代错误的索引分割 - provider_handler: 使用 errors.Is(err, gorm.ErrDuplicatedKey) 替代字符串匹配 - client: 重写 isNetworkError 使用 errors.As/Is 类型安全判断 - proxy_handler: 使用 encoding/json 标准库解析 JSON(extractModelName、isStreamRequest) ## 中优先级修复 - stats_handler: 添加 parseDateParam 辅助函数消除重复日期解析 - pkg/errors: 新增 ErrRequestCreate/Send/ResponseRead 错误类型和 WithCause 方法 - client: 使用结构化错误替代 fmt.Errorf - ConversionEngine: logger 依赖注入,替换所有 zap.L() 调用 ## 低优先级修复 - encoder: 删除 joinStrings,使用 strings.Join - adapter: 删除 modelInfoRegex 正则,使用 isModelInfoPath 字符串函数 ## 文档更新 - README.md: 添加公共库使用指南和编码规范章节 - specs: 同步 delta specs 到 main specs(error-handling、structured-logging、request-validation) ## 归档 - openspec/changes/archive/2026-04-20-refactor-backend-code-quality/
This commit is contained in:
@@ -2,6 +2,7 @@ package openai
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"nex/backend/internal/conversion"
|
||||
@@ -89,7 +90,7 @@ func encodeSystemAndMessages(req *canonical.CanonicalRequest) []map[string]any {
|
||||
for _, b := range v {
|
||||
parts = append(parts, b.Text)
|
||||
}
|
||||
text := joinStrings(parts, "\n\n")
|
||||
text := strings.Join(parts, "\n\n")
|
||||
if text != "" {
|
||||
messages = append(messages, map[string]any{
|
||||
"role": "system",
|
||||
@@ -132,7 +133,7 @@ func encodeMessage(msg canonical.CanonicalMessage) []map[string]any {
|
||||
|
||||
if len(toolUses) > 0 {
|
||||
if len(textParts) > 0 {
|
||||
m["content"] = joinStrings(textParts, "")
|
||||
m["content"] = strings.Join(textParts, "")
|
||||
} else {
|
||||
m["content"] = nil
|
||||
}
|
||||
@@ -149,7 +150,7 @@ func encodeMessage(msg canonical.CanonicalMessage) []map[string]any {
|
||||
}
|
||||
m["tool_calls"] = tcs
|
||||
} else if len(textParts) > 0 {
|
||||
m["content"] = joinStrings(textParts, "")
|
||||
m["content"] = strings.Join(textParts, "")
|
||||
} else {
|
||||
m["content"] = ""
|
||||
}
|
||||
@@ -286,7 +287,7 @@ func encodeResponse(resp *canonical.CanonicalResponse) ([]byte, error) {
|
||||
message := map[string]any{"role": "assistant"}
|
||||
if len(toolUses) > 0 {
|
||||
if len(textParts) > 0 {
|
||||
message["content"] = joinStrings(textParts, "")
|
||||
message["content"] = strings.Join(textParts, "")
|
||||
} else {
|
||||
message["content"] = nil
|
||||
}
|
||||
@@ -303,13 +304,13 @@ func encodeResponse(resp *canonical.CanonicalResponse) ([]byte, error) {
|
||||
}
|
||||
message["tool_calls"] = tcs
|
||||
} else if len(textParts) > 0 {
|
||||
message["content"] = joinStrings(textParts, "")
|
||||
message["content"] = strings.Join(textParts, "")
|
||||
} else {
|
||||
message["content"] = ""
|
||||
}
|
||||
|
||||
if len(thinkingParts) > 0 {
|
||||
message["reasoning_content"] = joinStrings(thinkingParts, "")
|
||||
message["reasoning_content"] = strings.Join(thinkingParts, "")
|
||||
}
|
||||
|
||||
var finishReason *string
|
||||
@@ -488,18 +489,6 @@ func encodeRerankResponse(resp *canonical.CanonicalRerankResponse) ([]byte, erro
|
||||
})
|
||||
}
|
||||
|
||||
// joinStrings 拼接字符串切片
|
||||
func joinStrings(parts []string, sep string) string {
|
||||
result := ""
|
||||
for i, p := range parts {
|
||||
if i > 0 {
|
||||
result += sep
|
||||
}
|
||||
result += p
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// mergeConsecutiveRoles 合并连续同角色消息(拼接内容)
|
||||
func mergeConsecutiveRoles(messages []map[string]any) []map[string]any {
|
||||
if len(messages) <= 1 {
|
||||
|
||||
Reference in New Issue
Block a user