feat(gateway): priority sorted based on config definition

This commit is contained in:
Ray Andrew 2026-02-15 01:37:44 -06:00
parent 344b599bad
commit 8b741e58c7
Signed by: rayandrew
SSH key fingerprint: SHA256:EUCV+qCSqkap8rR+p+zGjxHfKI06G0GJKgo1DIOniQY

View file

@ -19,6 +19,7 @@ type Route struct {
// Registry maps model names to provider routes. // Registry maps model names to provider routes.
type Registry struct { type Registry struct {
routes map[string][]Route routes map[string][]Route
order []string // preserves config order
} }
func NewRegistry(cfg *config.Config) (*Registry, error) { func NewRegistry(cfg *config.Config) (*Registry, error) {
@ -28,8 +29,9 @@ func NewRegistry(cfg *config.Config) (*Registry, error) {
providers[pc.Name] = NewOpenAIProvider(pc.Name, pc.BaseURL, pc.APIKey, pc.Timeout) providers[pc.Name] = NewOpenAIProvider(pc.Name, pc.BaseURL, pc.APIKey, pc.Timeout)
} }
// Build routes // Build routes (preserving config order)
routes := make(map[string][]Route) routes := make(map[string][]Route)
order := make([]string, 0, len(cfg.Models))
for _, mc := range cfg.Models { for _, mc := range cfg.Models {
var modelRoutes []Route var modelRoutes []Route
for _, rc := range mc.Routes { for _, rc := range mc.Routes {
@ -52,9 +54,10 @@ func NewRegistry(cfg *config.Config) (*Registry, error) {
return modelRoutes[i].Priority < modelRoutes[j].Priority return modelRoutes[i].Priority < modelRoutes[j].Priority
}) })
routes[mc.Name] = modelRoutes routes[mc.Name] = modelRoutes
order = append(order, mc.Name)
} }
return &Registry{routes: routes}, nil return &Registry{routes: routes, order: order}, nil
} }
// Lookup returns the routes for a model name. // Lookup returns the routes for a model name.
@ -63,12 +66,7 @@ func (r *Registry) Lookup(model string) ([]Route, bool) {
return routes, ok return routes, ok
} }
// ModelNames returns all registered model names. // ModelNames returns all registered model names in config order.
func (r *Registry) ModelNames() []string { func (r *Registry) ModelNames() []string {
names := make([]string, 0, len(r.routes)) return r.order
for name := range r.routes {
names = append(names, name)
}
sort.Strings(names)
return names
} }