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

@@ -46,6 +46,7 @@ func (h *Handler) CreateProvider(c *gin.Context) {
}
providerType := provider.NormalizeType(req.Type)
baseURL := strings.TrimSpace(req.BaseURL)
googleLocation := provider.DefaultGoogleLocation(providerType, req.GoogleLocation)
group := strings.TrimSpace(req.Group)
@@ -62,10 +63,37 @@ func (h *Handler) CreateProvider(c *gin.Context) {
autoBan = *req.AutoBan
}
// CP-side defaults + validation to prevent DP runtime errors.
switch providerType {
case provider.TypeOpenAI:
if baseURL == "" {
baseURL = "https://api.openai.com/v1"
}
case provider.TypeAnthropic, provider.TypeClaude:
if baseURL == "" {
baseURL = "https://api.anthropic.com"
}
case provider.TypeCompatible:
if baseURL == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "base_url required for compatible providers"})
return
}
default:
// Google SDK providers: base_url is not required.
if provider.IsVertexFamily(providerType) && strings.TrimSpace(googleLocation) == "" {
googleLocation = provider.DefaultGoogleLocation(providerType, "")
}
// For Gemini API providers, api_key is required.
if provider.IsGoogleFamily(providerType) && !provider.IsVertexFamily(providerType) && strings.TrimSpace(req.APIKey) == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "api_key required for gemini api providers"})
return
}
}
provider := model.Provider{
Name: req.Name,
Type: strings.TrimSpace(req.Type),
BaseURL: req.BaseURL,
BaseURL: baseURL,
APIKey: req.APIKey,
GoogleProject: strings.TrimSpace(req.GoogleProject),
GoogleLocation: googleLocation,
@@ -138,6 +166,10 @@ func (h *Handler) UpdateProvider(c *gin.Context) {
nextType = t
}
nextTypeLower := provider.NormalizeType(nextType)
nextBaseURL := strings.TrimSpace(existing.BaseURL)
if strings.TrimSpace(req.BaseURL) != "" {
nextBaseURL = strings.TrimSpace(req.BaseURL)
}
update := map[string]any{}
if strings.TrimSpace(req.Name) != "" {
@@ -186,6 +218,36 @@ func (h *Handler) UpdateProvider(c *gin.Context) {
update["ban_until"] = nil
}
// Defaults/validation after considering intended type/base_url.
switch nextTypeLower {
case provider.TypeOpenAI:
if nextBaseURL == "" {
update["base_url"] = "https://api.openai.com/v1"
}
case provider.TypeAnthropic, provider.TypeClaude:
if nextBaseURL == "" {
update["base_url"] = "https://api.anthropic.com"
}
case provider.TypeCompatible:
if nextBaseURL == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "base_url required for compatible providers"})
return
}
default:
if provider.IsGoogleFamily(nextTypeLower) && !provider.IsVertexFamily(nextTypeLower) {
// Ensure Gemini API providers have api_key.
// If update does not include api_key, keep existing; otherwise require new one not empty.
apiKey := existing.APIKey
if req.APIKey != "" {
apiKey = req.APIKey
}
if strings.TrimSpace(apiKey) == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "api_key required for gemini api providers"})
return
}
}
}
if len(update) == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "no fields to update"})
return