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 handler
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -213,18 +214,14 @@ func (h *ProxyHandler) isStreamRequest(body []byte, clientProtocol, nativePath s
|
||||
if ifaceType != conversion.InterfaceTypeChat {
|
||||
return false
|
||||
}
|
||||
for i, b := range body {
|
||||
if b == '"' && i+8 <= len(body) {
|
||||
if string(body[i:i+8]) == `"stream"` {
|
||||
for j := i + 8; j < len(body) && j < i+20; j++ {
|
||||
if body[j] == 't' && j+3 < len(body) && string(body[j:j+4]) == "true" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Stream bool `json:"stream"`
|
||||
}
|
||||
return false
|
||||
if err := json.Unmarshal(body, &req); err != nil {
|
||||
return false
|
||||
}
|
||||
return req.Stream
|
||||
}
|
||||
|
||||
// writeConversionError 写入转换错误
|
||||
@@ -312,51 +309,13 @@ func (h *ProxyHandler) forwardPassthrough(c *gin.Context, inSpec conversion.HTTP
|
||||
|
||||
// extractModelName 从 JSON body 中提取 model
|
||||
func extractModelName(body []byte) string {
|
||||
inQuote := false
|
||||
escaped := false
|
||||
keyStart := -1
|
||||
keyEnd := -1
|
||||
lookingForKey := true
|
||||
lookingForValue := false
|
||||
valueStart := -1
|
||||
|
||||
for i := 0; i < len(body); i++ {
|
||||
b := body[i]
|
||||
if escaped {
|
||||
escaped = false
|
||||
continue
|
||||
}
|
||||
if b == '\\' {
|
||||
escaped = true
|
||||
continue
|
||||
}
|
||||
if b == '"' {
|
||||
if !inQuote {
|
||||
inQuote = true
|
||||
if lookingForKey {
|
||||
keyStart = i + 1
|
||||
}
|
||||
if lookingForValue {
|
||||
valueStart = i + 1
|
||||
}
|
||||
} else {
|
||||
inQuote = false
|
||||
if lookingForKey && keyStart >= 0 {
|
||||
keyEnd = i
|
||||
if string(body[keyStart:keyEnd]) == "model" {
|
||||
lookingForKey = false
|
||||
lookingForValue = true
|
||||
}
|
||||
} else if lookingForValue && valueStart >= 0 {
|
||||
return string(body[valueStart:i])
|
||||
}
|
||||
}
|
||||
}
|
||||
if !inQuote && lookingForValue && b == ':' {
|
||||
// 等待值开始
|
||||
}
|
||||
var req struct {
|
||||
Model string `json:"model"`
|
||||
}
|
||||
return ""
|
||||
if err := json.Unmarshal(body, &req); err != nil {
|
||||
return ""
|
||||
}
|
||||
return req.Model
|
||||
}
|
||||
|
||||
// extractHeaders 从 Gin context 提取请求头
|
||||
|
||||
Reference in New Issue
Block a user