feat(cliproxy): enhance OpenAI compatibility detection and executor registration

- Added `openAICompatInfoFromAuth` helper for streamlined compatibility checks.
- Improved OpenAI compatibility provider handling and executor initialization logic.
- Adjusted model routing to support OpenAI-compatibility attributes.
This commit is contained in:
Luis Pater
2025-10-05 21:44:51 +08:00
parent 389c8ecef1
commit 49c52a01b0

View File

@@ -232,10 +232,40 @@ func (s *Service) applyCoreAuthRemoval(ctx context.Context, id string) {
} }
} }
func openAICompatInfoFromAuth(a *coreauth.Auth) (providerKey string, compatName string, ok bool) {
if a == nil {
return "", "", false
}
if len(a.Attributes) > 0 {
providerKey = strings.TrimSpace(a.Attributes["provider_key"])
compatName = strings.TrimSpace(a.Attributes["compat_name"])
if providerKey != "" || compatName != "" {
if providerKey == "" {
providerKey = compatName
}
return strings.ToLower(providerKey), compatName, true
}
}
if strings.EqualFold(strings.TrimSpace(a.Provider), "openai-compatibility") {
return "openai-compatibility", strings.TrimSpace(a.Label), true
}
return "", "", false
}
func (s *Service) ensureExecutorsForAuth(a *coreauth.Auth) { func (s *Service) ensureExecutorsForAuth(a *coreauth.Auth) {
if s == nil || a == nil { if s == nil || a == nil {
return return
} }
if compatProviderKey, _, isCompat := openAICompatInfoFromAuth(a); isCompat {
if compatProviderKey == "" {
compatProviderKey = strings.ToLower(strings.TrimSpace(a.Provider))
}
if compatProviderKey == "" {
compatProviderKey = "openai-compatibility"
}
s.coreManager.RegisterExecutor(executor.NewOpenAICompatExecutor(compatProviderKey, s.cfg))
return
}
switch strings.ToLower(a.Provider) { switch strings.ToLower(a.Provider) {
case "gemini": case "gemini":
s.coreManager.RegisterExecutor(executor.NewGeminiExecutor(s.cfg)) s.coreManager.RegisterExecutor(executor.NewGeminiExecutor(s.cfg))
@@ -484,6 +514,10 @@ func (s *Service) registerModelsForAuth(a *coreauth.Auth) {
} }
} }
provider := strings.ToLower(strings.TrimSpace(a.Provider)) provider := strings.ToLower(strings.TrimSpace(a.Provider))
compatProviderKey, compatDisplayName, compatDetected := openAICompatInfoFromAuth(a)
if compatDetected {
provider = "openai-compatibility"
}
var models []*ModelInfo var models []*ModelInfo
switch provider { switch provider {
case "gemini": case "gemini":
@@ -506,6 +540,15 @@ func (s *Service) registerModelsForAuth(a *coreauth.Auth) {
providerKey := provider providerKey := provider
compatName := strings.TrimSpace(a.Provider) compatName := strings.TrimSpace(a.Provider)
isCompatAuth := false isCompatAuth := false
if compatDetected {
if compatProviderKey != "" {
providerKey = compatProviderKey
}
if compatDisplayName != "" {
compatName = compatDisplayName
}
isCompatAuth = true
}
if strings.EqualFold(providerKey, "openai-compatibility") { if strings.EqualFold(providerKey, "openai-compatibility") {
isCompatAuth = true isCompatAuth = true
if a.Attributes != nil { if a.Attributes != nil {