feat(api): align provider creation with presets/custom/google sdk

This commit is contained in:
zenfun
2025-12-17 22:19:17 +08:00
parent 174735f152
commit 2b0ed3d3d5
5 changed files with 214 additions and 10 deletions

View File

@@ -15,7 +15,7 @@ import (
// CreateProviderPreset godoc
// @Summary Create a preset provider
// @Description Create an official OpenAI/Anthropic/Gemini provider (only api_key is typically required)
// @Description Create an official OpenAI/Anthropic provider (only api_key is typically required)
// @Tags admin
// @Accept json
// @Produce json
@@ -43,16 +43,12 @@ func (h *Handler) CreateProviderPreset(c *gin.Context) {
switch preset {
case providerx.TypeOpenAI:
providerType = providerx.TypeOpenAI
baseURL = "https://api.openai.com"
baseURL = "https://api.openai.com/v1"
case providerx.TypeAnthropic, providerx.TypeClaude:
providerType = providerx.TypeAnthropic
baseURL = "https://api.anthropic.com"
case providerx.TypeGemini, providerx.TypeAIStudio, providerx.TypeGoogle:
// Gemini API / AI Studio (SDK transport). BaseURL is optional but we provide the official endpoint.
providerType = providerx.TypeGemini
baseURL = "https://generativelanguage.googleapis.com"
default:
c.JSON(http.StatusBadRequest, gin.H{"error": "unsupported preset: " + preset})
c.JSON(http.StatusBadRequest, gin.H{"error": "unsupported preset: " + preset + " (use /admin/providers/google for Google SDK providers)"})
return
}
@@ -108,6 +104,104 @@ func (h *Handler) CreateProviderPreset(c *gin.Context) {
c.JSON(http.StatusCreated, p)
}
// CreateProviderGoogle godoc
// @Summary Create a Google SDK provider
// @Description Create a Google SDK provider (Gemini API key or Vertex project/location); base_url is not used
// @Tags admin
// @Accept json
// @Produce json
// @Security AdminAuth
// @Param provider body dto.ProviderGoogleCreateDTO true "Google provider payload"
// @Success 201 {object} model.Provider
// @Failure 400 {object} gin.H
// @Failure 500 {object} gin.H
// @Router /admin/providers/google [post]
func (h *Handler) CreateProviderGoogle(c *gin.Context) {
var req dto.ProviderGoogleCreateDTO
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
pt := providerx.NormalizeType(req.Type)
if pt == "" {
pt = providerx.TypeGemini
}
if !providerx.IsGoogleFamily(pt) {
c.JSON(http.StatusBadRequest, gin.H{"error": "type must be google family"})
return
}
name := strings.TrimSpace(req.Name)
if name == "" {
name = pt + "-" + randomSuffix(4)
}
group := strings.TrimSpace(req.Group)
if group == "" {
group = "default"
}
status := strings.TrimSpace(req.Status)
if status == "" {
status = "active"
}
autoBan := true
if req.AutoBan != nil {
autoBan = *req.AutoBan
}
// Validate fields by type.
apiKey := strings.TrimSpace(req.APIKey)
googleProject := strings.TrimSpace(req.GoogleProject)
googleLocation := providerx.DefaultGoogleLocation(pt, req.GoogleLocation)
if providerx.IsVertexFamily(pt) {
// Vertex uses ADC and project/location; api_key is not required.
if strings.TrimSpace(googleLocation) == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "google_location required"})
return
}
apiKey = ""
} else {
// Gemini API requires api_key.
if apiKey == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "api_key required for gemini api"})
return
}
googleProject = ""
googleLocation = ""
}
p := model.Provider{
Name: name,
Type: pt,
BaseURL: "", // intentionally unused for Google SDK
APIKey: apiKey,
GoogleProject: googleProject,
GoogleLocation: googleLocation,
Group: groupx.Normalize(group),
Models: strings.Join(req.Models, ","),
Status: status,
AutoBan: autoBan,
}
if req.Weight > 0 {
p.Weight = req.Weight
}
if err := h.db.Create(&p).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create provider", "details": err.Error()})
return
}
if err := h.sync.SyncProvider(&p); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync provider", "details": err.Error()})
return
}
if err := h.sync.SyncBindings(h.db); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync bindings", "details": err.Error()})
return
}
c.JSON(http.StatusCreated, p)
}
// CreateProviderCustom godoc
// @Summary Create a custom provider
// @Description Create an OpenAI-compatible provider (base_url + api_key required)