mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 08:02:53 +00:00
116 lines
3.0 KiB
Go
116 lines
3.0 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"slices"
|
|
|
|
"opencode2api/internal/config"
|
|
"opencode2api/internal/models"
|
|
"opencode2api/internal/oauth"
|
|
)
|
|
|
|
type modelNames struct {
|
|
public map[string]string
|
|
resolved map[string]string
|
|
}
|
|
|
|
func (g *Gateway) knownModelName(model string) bool {
|
|
if g.catalog.Known(model) || g.cfg.Models.Protocols[model] != "" {
|
|
return true
|
|
}
|
|
for _, connection := range []*oauth.Connection{g.cfg.GoOAuth, g.cfg.ZenOAuth} {
|
|
if connection != nil {
|
|
if _, found := connection.Routes[model]; found {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (g *Gateway) validateModelAliases() error {
|
|
for public, model := range g.cfg.Models.Aliases {
|
|
if public != model && g.knownModelName(public) {
|
|
return fmt.Errorf("models.aliases public name %q shadows an upstream model ID", public)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (g *Gateway) modelNames(routes []models.Route) modelNames {
|
|
names := modelNames{
|
|
public: make(map[string]string, len(routes)),
|
|
resolved: make(map[string]string, len(g.cfg.Models.Aliases)+len(routes)),
|
|
}
|
|
available := make(map[string]bool, len(routes))
|
|
reserved := map[string]bool{g.cfg.Rotation.SOTA.Alias: true, g.cfg.Rotation.Sweet.Alias: true}
|
|
for _, route := range routes {
|
|
available[route.ID] = true
|
|
reserved[route.ID] = true
|
|
names.public[route.ID] = route.ID
|
|
}
|
|
aliases := make([]string, 0, len(g.cfg.Models.Aliases))
|
|
for public := range g.cfg.Models.Aliases {
|
|
aliases = append(aliases, public)
|
|
reserved[public] = true
|
|
reserved[g.cfg.Models.Aliases[public]] = true
|
|
}
|
|
slices.Sort(aliases)
|
|
explicit := make(map[string]bool, len(aliases))
|
|
for _, public := range aliases {
|
|
model := g.cfg.Models.Aliases[public]
|
|
if public != model && (available[public] || g.knownModelName(public)) {
|
|
continue
|
|
}
|
|
names.resolved[public] = model
|
|
if available[model] && !explicit[model] {
|
|
names.public[model] = public
|
|
explicit[model] = true
|
|
}
|
|
}
|
|
if g.cfg.Models.StripFreeSuffix {
|
|
for _, route := range routes {
|
|
if explicit[route.ID] {
|
|
continue
|
|
}
|
|
base, stripped := config.StripFreeSuffix(route.ID)
|
|
if !stripped || reserved[base] || g.knownModelName(base) {
|
|
continue
|
|
}
|
|
names.public[route.ID] = base
|
|
names.resolved[base] = route.ID
|
|
reserved[base] = true
|
|
}
|
|
}
|
|
return names
|
|
}
|
|
|
|
func (g *Gateway) resolveModel(name string) (real, public string) {
|
|
if len(g.cfg.Models.Aliases) == 0 && !g.cfg.Models.StripFreeSuffix {
|
|
return name, name
|
|
}
|
|
routes, _ := g.availableModels()
|
|
names := g.modelNames(routes)
|
|
if model, found := names.resolved[name]; found {
|
|
return model, name
|
|
}
|
|
return name, name
|
|
}
|
|
|
|
func rewriteModelBody(body []byte, model string, stripEnvironment bool) ([]byte, error) {
|
|
var payload map[string]json.RawMessage
|
|
if err := json.Unmarshal(body, &payload); err != nil {
|
|
return nil, err
|
|
}
|
|
encodedModel, err := json.Marshal(model)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
payload["model"] = encodedModel
|
|
if stripEnvironment {
|
|
delete(payload, "client_environment")
|
|
}
|
|
return json.Marshal(payload)
|
|
}
|