refactor(cliproxy): extract generic buildConfigModels function for model info generation

This commit is contained in:
hkfires
2025-12-30 13:35:22 +08:00
parent 08ab6a7d77
commit 70fdd70b84
3 changed files with 55 additions and 124 deletions

View File

@@ -268,6 +268,9 @@ type ClaudeModel struct {
Alias string `yaml:"alias" json:"alias"` Alias string `yaml:"alias" json:"alias"`
} }
func (m ClaudeModel) GetName() string { return m.Name }
func (m ClaudeModel) GetAlias() string { return m.Alias }
// CodexKey represents the configuration for a Codex API key, // CodexKey represents the configuration for a Codex API key,
// including the API key itself and an optional base URL for the API endpoint. // including the API key itself and an optional base URL for the API endpoint.
type CodexKey struct { type CodexKey struct {
@@ -303,6 +306,9 @@ type CodexModel struct {
Alias string `yaml:"alias" json:"alias"` Alias string `yaml:"alias" json:"alias"`
} }
func (m CodexModel) GetName() string { return m.Name }
func (m CodexModel) GetAlias() string { return m.Alias }
// GeminiKey represents the configuration for a Gemini API key, // GeminiKey represents the configuration for a Gemini API key,
// including optional overrides for upstream base URL, proxy routing, and headers. // including optional overrides for upstream base URL, proxy routing, and headers.
type GeminiKey struct { type GeminiKey struct {
@@ -337,6 +343,9 @@ type GeminiModel struct {
Alias string `yaml:"alias" json:"alias"` Alias string `yaml:"alias" json:"alias"`
} }
func (m GeminiModel) GetName() string { return m.Name }
func (m GeminiModel) GetAlias() string { return m.Alias }
// OpenAICompatibility represents the configuration for OpenAI API compatibility // OpenAICompatibility represents the configuration for OpenAI API compatibility
// with external providers, allowing model aliases to be routed through OpenAI API format. // with external providers, allowing model aliases to be routed through OpenAI API format.
type OpenAICompatibility struct { type OpenAICompatibility struct {

View File

@@ -42,6 +42,9 @@ type VertexCompatModel struct {
Alias string `yaml:"alias" json:"alias"` Alias string `yaml:"alias" json:"alias"`
} }
func (m VertexCompatModel) GetName() string { return m.Name }
func (m VertexCompatModel) GetAlias() string { return m.Alias }
// SanitizeVertexCompatKeys deduplicates and normalizes Vertex-compatible API key credentials. // SanitizeVertexCompatKeys deduplicates and normalizes Vertex-compatible API key credentials.
func (cfg *Config) SanitizeVertexCompatKeys() { func (cfg *Config) SanitizeVertexCompatKeys() {
if cfg == nil { if cfg == nil {

View File

@@ -1119,17 +1119,22 @@ func matchWildcard(pattern, value string) bool {
return true return true
} }
func buildVertexCompatConfigModels(entry *config.VertexCompatKey) []*ModelInfo { type modelEntry interface {
if entry == nil || len(entry.Models) == 0 { GetName() string
GetAlias() string
}
func buildConfigModels[T modelEntry](models []T, ownedBy, modelType string) []*ModelInfo {
if len(models) == 0 {
return nil return nil
} }
now := time.Now().Unix() now := time.Now().Unix()
out := make([]*ModelInfo, 0, len(entry.Models)) out := make([]*ModelInfo, 0, len(models))
seen := make(map[string]struct{}, len(entry.Models)) seen := make(map[string]struct{}, len(models))
for i := range entry.Models { for i := range models {
model := entry.Models[i] model := models[i]
name := strings.TrimSpace(model.Name) name := strings.TrimSpace(model.GetName())
alias := strings.TrimSpace(model.Alias) alias := strings.TrimSpace(model.GetAlias())
if alias == "" { if alias == "" {
alias = name alias = name
} }
@@ -1149,14 +1154,42 @@ func buildVertexCompatConfigModels(entry *config.VertexCompatKey) []*ModelInfo {
ID: alias, ID: alias,
Object: "model", Object: "model",
Created: now, Created: now,
OwnedBy: "google", OwnedBy: ownedBy,
Type: "vertex", Type: modelType,
DisplayName: display, DisplayName: display,
}) })
} }
return out return out
} }
func buildVertexCompatConfigModels(entry *config.VertexCompatKey) []*ModelInfo {
if entry == nil {
return nil
}
return buildConfigModels(entry.Models, "google", "vertex")
}
func buildGeminiConfigModels(entry *config.GeminiKey) []*ModelInfo {
if entry == nil {
return nil
}
return buildConfigModels(entry.Models, "google", "gemini")
}
func buildClaudeConfigModels(entry *config.ClaudeKey) []*ModelInfo {
if entry == nil {
return nil
}
return buildConfigModels(entry.Models, "anthropic", "claude")
}
func buildCodexConfigModels(entry *config.CodexKey) []*ModelInfo {
if entry == nil {
return nil
}
return buildConfigModels(entry.Models, "openai", "openai")
}
func rewriteModelInfoName(name, oldID, newID string) string { func rewriteModelInfoName(name, oldID, newID string) string {
trimmed := strings.TrimSpace(name) trimmed := strings.TrimSpace(name)
if trimmed == "" { if trimmed == "" {
@@ -1243,117 +1276,3 @@ func applyOAuthModelMappings(cfg *config.Config, provider, authKind string, mode
} }
return out return out
} }
func buildGeminiConfigModels(entry *config.GeminiKey) []*ModelInfo {
if entry == nil || len(entry.Models) == 0 {
return nil
}
now := time.Now().Unix()
out := make([]*ModelInfo, 0, len(entry.Models))
seen := make(map[string]struct{}, len(entry.Models))
for i := range entry.Models {
model := entry.Models[i]
name := strings.TrimSpace(model.Name)
alias := strings.TrimSpace(model.Alias)
if alias == "" {
alias = name
}
if alias == "" {
continue
}
key := strings.ToLower(alias)
if _, exists := seen[key]; exists {
continue
}
seen[key] = struct{}{}
display := name
if display == "" {
display = alias
}
out = append(out, &ModelInfo{
ID: alias,
Object: "model",
Created: now,
OwnedBy: "google",
Type: "gemini",
DisplayName: display,
})
}
return out
}
func buildClaudeConfigModels(entry *config.ClaudeKey) []*ModelInfo {
if entry == nil || len(entry.Models) == 0 {
return nil
}
now := time.Now().Unix()
out := make([]*ModelInfo, 0, len(entry.Models))
seen := make(map[string]struct{}, len(entry.Models))
for i := range entry.Models {
model := entry.Models[i]
name := strings.TrimSpace(model.Name)
alias := strings.TrimSpace(model.Alias)
if alias == "" {
alias = name
}
if alias == "" {
continue
}
key := strings.ToLower(alias)
if _, exists := seen[key]; exists {
continue
}
seen[key] = struct{}{}
display := name
if display == "" {
display = alias
}
out = append(out, &ModelInfo{
ID: alias,
Object: "model",
Created: now,
OwnedBy: "anthropic",
Type: "claude",
DisplayName: display,
})
}
return out
}
func buildCodexConfigModels(entry *config.CodexKey) []*ModelInfo {
if entry == nil || len(entry.Models) == 0 {
return nil
}
now := time.Now().Unix()
out := make([]*ModelInfo, 0, len(entry.Models))
seen := make(map[string]struct{}, len(entry.Models))
for i := range entry.Models {
model := entry.Models[i]
name := strings.TrimSpace(model.Name)
alias := strings.TrimSpace(model.Alias)
if alias == "" {
alias = name
}
if alias == "" {
continue
}
key := strings.ToLower(alias)
if _, exists := seen[key]; exists {
continue
}
seen[key] = struct{}{}
display := name
if display == "" {
display = alias
}
out = append(out, &ModelInfo{
ID: alias,
Object: "model",
Created: now,
OwnedBy: "openai",
Type: "openai",
DisplayName: display,
})
}
return out
}