引入 Canonical Model 和 ProtocolAdapter 架构,支持 OpenAI/Anthropic 协议间 无缝转换,统一 ProxyHandler 替代分散的 OpenAI/Anthropic Handler,简化 ProviderClient 为协议无关的 HTTP 发送器,Provider 新增 protocol 字段。
167 lines
3.4 KiB
Go
167 lines
3.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
|
|
appErrors "nex/backend/pkg/errors"
|
|
|
|
"nex/backend/internal/domain"
|
|
"nex/backend/internal/service"
|
|
)
|
|
|
|
// ProviderHandler 供应商管理处理器
|
|
type ProviderHandler struct {
|
|
providerService service.ProviderService
|
|
}
|
|
|
|
// NewProviderHandler 创建供应商处理器
|
|
func NewProviderHandler(providerService service.ProviderService) *ProviderHandler {
|
|
return &ProviderHandler{providerService: providerService}
|
|
}
|
|
|
|
// CreateProvider 创建供应商
|
|
func (h *ProviderHandler) CreateProvider(c *gin.Context) {
|
|
var req struct {
|
|
ID string `json:"id" binding:"required"`
|
|
Name string `json:"name" binding:"required"`
|
|
APIKey string `json:"api_key" binding:"required"`
|
|
BaseURL string `json:"base_url" binding:"required"`
|
|
Protocol string `json:"protocol"`
|
|
}
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "缺少必需字段: id, name, api_key, base_url",
|
|
})
|
|
return
|
|
}
|
|
|
|
protocol := req.Protocol
|
|
if protocol == "" {
|
|
protocol = "openai"
|
|
}
|
|
|
|
provider := &domain.Provider{
|
|
ID: req.ID,
|
|
Name: req.Name,
|
|
APIKey: req.APIKey,
|
|
BaseURL: req.BaseURL,
|
|
Protocol: protocol,
|
|
}
|
|
|
|
err := h.providerService.Create(provider)
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "UNIQUE constraint failed") {
|
|
c.JSON(http.StatusConflict, gin.H{
|
|
"error": "供应商 ID 已存在",
|
|
})
|
|
return
|
|
}
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
|
|
provider.MaskAPIKey()
|
|
c.JSON(http.StatusCreated, provider)
|
|
}
|
|
|
|
// ListProviders 列出所有供应商
|
|
func (h *ProviderHandler) ListProviders(c *gin.Context) {
|
|
providers, err := h.providerService.List()
|
|
if err != nil {
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, providers)
|
|
}
|
|
|
|
// GetProvider 获取供应商
|
|
func (h *ProviderHandler) GetProvider(c *gin.Context) {
|
|
id := c.Param("id")
|
|
|
|
provider, err := h.providerService.Get(id, true)
|
|
if err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
c.JSON(http.StatusNotFound, gin.H{
|
|
"error": "供应商未找到",
|
|
})
|
|
return
|
|
}
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, provider)
|
|
}
|
|
|
|
// UpdateProvider 更新供应商
|
|
func (h *ProviderHandler) UpdateProvider(c *gin.Context) {
|
|
id := c.Param("id")
|
|
|
|
var req map[string]interface{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "无效的请求格式",
|
|
})
|
|
return
|
|
}
|
|
|
|
err := h.providerService.Update(id, req)
|
|
if err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
c.JSON(http.StatusNotFound, gin.H{
|
|
"error": "供应商未找到",
|
|
})
|
|
return
|
|
}
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
|
|
provider, err := h.providerService.Get(id, true)
|
|
if err != nil {
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, provider)
|
|
}
|
|
|
|
// DeleteProvider 删除供应商
|
|
func (h *ProviderHandler) DeleteProvider(c *gin.Context) {
|
|
id := c.Param("id")
|
|
|
|
err := h.providerService.Delete(id)
|
|
if err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
c.JSON(http.StatusNotFound, gin.H{
|
|
"error": "供应商未找到",
|
|
})
|
|
return
|
|
}
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
|
|
// writeError 统一错误响应处理
|
|
func writeError(c *gin.Context, err error) {
|
|
if appErr, ok := appErrors.AsAppError(err); ok {
|
|
c.JSON(appErr.HTTPStatus, gin.H{
|
|
"error": appErr.Message,
|
|
"code": appErr.Code,
|
|
})
|
|
return
|
|
}
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"error": err.Error(),
|
|
})
|
|
}
|