mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 08:02:53 +00:00
66 lines
2.0 KiB
Go
66 lines
2.0 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"unicode"
|
|
|
|
"opencode2api/internal/oauth"
|
|
)
|
|
|
|
const FreeSuffix = "-free"
|
|
|
|
func StripFreeSuffix(model string) (string, bool) {
|
|
base, found := strings.CutSuffix(model, FreeSuffix)
|
|
if !found || base == "" {
|
|
return model, false
|
|
}
|
|
return base, true
|
|
}
|
|
|
|
func normalizeModelAliases(cfg *Config) error {
|
|
if len(cfg.Models.Aliases) == 0 {
|
|
return nil
|
|
}
|
|
aliases := make(map[string]string, len(cfg.Models.Aliases))
|
|
for public, model := range cfg.Models.Aliases {
|
|
if strings.ContainsFunc(public, unicode.IsControl) || strings.ContainsFunc(model, unicode.IsControl) {
|
|
return errors.New("models.aliases must contain nonempty model names without control characters")
|
|
}
|
|
public, model = strings.TrimSpace(public), strings.TrimSpace(model)
|
|
if public == "" || model == "" {
|
|
return errors.New("models.aliases must contain nonempty model names without control characters")
|
|
}
|
|
if existing, found := aliases[public]; found && existing != model {
|
|
return errors.New("models.aliases contains conflicting entries for the same public name")
|
|
}
|
|
if public == cfg.Rotation.SOTA.Alias || public == cfg.Rotation.Sweet.Alias || model == cfg.Rotation.SOTA.Alias || model == cfg.Rotation.Sweet.Alias {
|
|
return errors.New("models.aliases must not use a rotation group alias as a public name or upstream model")
|
|
}
|
|
aliases[public] = model
|
|
}
|
|
known := make(map[string]bool, len(aliases)+len(cfg.Models.Protocols))
|
|
for _, model := range aliases {
|
|
known[model] = true
|
|
}
|
|
for model := range cfg.Models.Protocols {
|
|
known[model] = true
|
|
}
|
|
for _, connection := range []*oauth.Connection{cfg.GoOAuth, cfg.ZenOAuth} {
|
|
if connection == nil {
|
|
continue
|
|
}
|
|
for model := range connection.Routes {
|
|
known[model] = true
|
|
}
|
|
}
|
|
for public, model := range aliases {
|
|
if public != model && known[public] {
|
|
return fmt.Errorf("models.aliases public name %q shadows an upstream model ID", public)
|
|
}
|
|
}
|
|
cfg.Models.Aliases = aliases
|
|
return nil
|
|
}
|