-
-
Notifications
You must be signed in to change notification settings - Fork 687
feat: add global model aliases with cross-provider fallback #765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PancakeZik
wants to merge
2
commits into
router-for-me:main
Choose a base branch
from
PancakeZik:feature/model-aliases
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package alias | ||
|
|
||
| import ( | ||
| "sync" | ||
|
|
||
| "github.com/router-for-me/CLIProxyAPI/v6/internal/config" | ||
| ) | ||
|
|
||
| var ( | ||
| globalResolver *Resolver | ||
| globalResolverOnce sync.Once | ||
| globalResolverMu sync.RWMutex | ||
| ) | ||
|
|
||
| // GetGlobalResolver returns the global alias resolver instance. | ||
| // Creates a new empty resolver if not initialized. | ||
| func GetGlobalResolver() *Resolver { | ||
| globalResolverOnce.Do(func() { | ||
| globalResolver = NewResolver(nil) | ||
| }) | ||
| globalResolverMu.RLock() | ||
| defer globalResolverMu.RUnlock() | ||
| return globalResolver | ||
| } | ||
|
|
||
| // InitGlobalResolver initializes the global resolver with configuration. | ||
| // Should be called during server startup. | ||
| func InitGlobalResolver(cfg *config.ModelAliasConfig) { | ||
| globalResolverOnce.Do(func() { | ||
| globalResolver = NewResolver(cfg) | ||
| }) | ||
| globalResolverMu.Lock() | ||
| defer globalResolverMu.Unlock() | ||
| if globalResolver != nil && cfg != nil { | ||
| globalResolver.Update(cfg) | ||
| } | ||
| } | ||
|
|
||
| // UpdateGlobalResolver updates the global resolver configuration. | ||
| // Used for hot-reload. | ||
| func UpdateGlobalResolver(cfg *config.ModelAliasConfig) { | ||
| r := GetGlobalResolver() | ||
| if r != nil && cfg != nil { | ||
| r.Update(cfg) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| //go:build integration | ||
|
|
||
| package alias | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/router-for-me/CLIProxyAPI/v6/internal/config" | ||
| ) | ||
|
|
||
| func TestGlobalResolverIntegration(t *testing.T) { | ||
| cfg := &config.ModelAliasConfig{ | ||
| DefaultStrategy: "round-robin", | ||
| Aliases: []config.ModelAlias{ | ||
| { | ||
| Alias: "test-alias", | ||
| Providers: []config.AliasProvider{ | ||
| {Provider: "test-provider", Model: "test-model"}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| InitGlobalResolver(cfg) | ||
|
|
||
| r := GetGlobalResolver() | ||
| if r == nil { | ||
| t.Fatal("expected global resolver") | ||
| } | ||
|
|
||
| resolved := r.Resolve("test-alias") | ||
| if resolved == nil { | ||
| t.Fatal("expected resolved alias") | ||
| } | ||
|
|
||
| // Test update | ||
| newCfg := &config.ModelAliasConfig{ | ||
| Aliases: []config.ModelAlias{ | ||
| { | ||
| Alias: "new-alias", | ||
| Providers: []config.AliasProvider{ | ||
| {Provider: "new-provider", Model: "new-model"}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| UpdateGlobalResolver(newCfg) | ||
|
|
||
| resolved = r.Resolve("new-alias") | ||
| if resolved == nil { | ||
| t.Fatal("expected new alias after update") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| // Package alias provides global model alias resolution for cross-provider routing. | ||
| package alias | ||
|
|
||
| import ( | ||
| "strings" | ||
| "sync" | ||
|
|
||
| "github.com/router-for-me/CLIProxyAPI/v6/internal/config" | ||
| "github.com/router-for-me/CLIProxyAPI/v6/internal/util" | ||
| log "github.com/sirupsen/logrus" | ||
| ) | ||
|
|
||
| // ResolvedAlias contains the resolution result for a model alias. | ||
| type ResolvedAlias struct { | ||
| // OriginalAlias is the alias that was resolved. | ||
| OriginalAlias string | ||
| // Strategy is the routing strategy for this alias. | ||
| Strategy string | ||
| // Providers is the ordered list of provider mappings. | ||
| Providers []config.AliasProvider | ||
| } | ||
|
|
||
| // SelectedProvider contains the selected provider and model for a request. | ||
| type SelectedProvider struct { | ||
| // Provider is the selected provider name. | ||
| Provider string | ||
| // Model is the provider-specific model name. | ||
| Model string | ||
| // Index is the index in the providers list (for tracking). | ||
| Index int | ||
| } | ||
|
|
||
| // Resolver handles global model alias resolution with routing strategies. | ||
| type Resolver struct { | ||
| mu sync.RWMutex | ||
| aliases map[string]*ResolvedAlias // lowercase alias -> resolved | ||
| defaultStrategy string | ||
| counters map[string]int // alias -> round-robin counter | ||
| } | ||
|
|
||
| // NewResolver creates a new alias resolver with the given configuration. | ||
| func NewResolver(cfg *config.ModelAliasConfig) *Resolver { | ||
| r := &Resolver{ | ||
| aliases: make(map[string]*ResolvedAlias), | ||
| defaultStrategy: "round-robin", | ||
| counters: make(map[string]int), | ||
| } | ||
| if cfg != nil { | ||
| r.Update(cfg) | ||
| } | ||
| return r | ||
| } | ||
|
|
||
| // Update refreshes the resolver configuration (for hot-reload). | ||
| func (r *Resolver) Update(cfg *config.ModelAliasConfig) { | ||
| if cfg == nil { | ||
| return | ||
| } | ||
| r.mu.Lock() | ||
| defer r.mu.Unlock() | ||
|
|
||
| r.defaultStrategy = cfg.DefaultStrategy | ||
| if r.defaultStrategy == "" { | ||
| r.defaultStrategy = "round-robin" | ||
| } | ||
|
|
||
| r.aliases = make(map[string]*ResolvedAlias, len(cfg.Aliases)) | ||
| for _, alias := range cfg.Aliases { | ||
| key := strings.ToLower(alias.Alias) | ||
| strategy := alias.Strategy | ||
| if strategy == "" { | ||
| strategy = r.defaultStrategy | ||
| } | ||
| r.aliases[key] = &ResolvedAlias{ | ||
| OriginalAlias: alias.Alias, | ||
| Strategy: strategy, | ||
| Providers: alias.Providers, | ||
| } | ||
| log.Debugf("model alias registered: %s -> %d providers (strategy: %s)", | ||
| alias.Alias, len(alias.Providers), strategy) | ||
| } | ||
|
|
||
| if len(r.aliases) > 0 { | ||
| log.Infof("model aliases: loaded %d alias(es)", len(r.aliases)) | ||
| } | ||
| } | ||
|
|
||
| // Resolve checks if the model name is an alias and returns resolution info. | ||
| // Returns nil if the model is not an alias. | ||
| func (r *Resolver) Resolve(modelName string) *ResolvedAlias { | ||
| if modelName == "" { | ||
| return nil | ||
| } | ||
| r.mu.RLock() | ||
| defer r.mu.RUnlock() | ||
|
|
||
| key := strings.ToLower(strings.TrimSpace(modelName)) | ||
| return r.aliases[key] | ||
| } | ||
|
|
||
| // SelectProvider selects the next provider based on the routing strategy. | ||
| // It filters out providers that don't have available credentials. | ||
| func (r *Resolver) SelectProvider(resolved *ResolvedAlias) *SelectedProvider { | ||
| if resolved == nil || len(resolved.Providers) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| // Filter to providers that have registered models | ||
| available := make([]int, 0, len(resolved.Providers)) | ||
| for i, p := range resolved.Providers { | ||
| if providers := util.GetProviderName(p.Model); len(providers) > 0 { | ||
| available = append(available, i) | ||
| } | ||
| } | ||
|
|
||
| if len(available) == 0 { | ||
| log.Debugf("model alias %s: no providers have available credentials", resolved.OriginalAlias) | ||
| return nil | ||
| } | ||
|
|
||
| var selectedIdx int | ||
| switch resolved.Strategy { | ||
| case "fill-first", "fillfirst", "ff": | ||
| // Always pick first available | ||
| selectedIdx = available[0] | ||
| default: // round-robin | ||
| r.mu.Lock() | ||
| counter := r.counters[resolved.OriginalAlias] | ||
| r.counters[resolved.OriginalAlias] = counter + 1 | ||
| if counter >= 2_147_483_640 { | ||
| r.counters[resolved.OriginalAlias] = 0 | ||
| } | ||
| r.mu.Unlock() | ||
| selectedIdx = available[counter%len(available)] | ||
| } | ||
|
|
||
| p := resolved.Providers[selectedIdx] | ||
| log.Debugf("model alias %s: selected provider %s with model %s (strategy: %s)", | ||
| resolved.OriginalAlias, p.Provider, p.Model, resolved.Strategy) | ||
|
|
||
| return &SelectedProvider{ | ||
| Provider: p.Provider, | ||
| Model: p.Model, | ||
| Index: selectedIdx, | ||
| } | ||
| } | ||
|
|
||
| // GetAliases returns a copy of current aliases (for debugging/status). | ||
| func (r *Resolver) GetAliases() map[string]*ResolvedAlias { | ||
| r.mu.RLock() | ||
| defer r.mu.RUnlock() | ||
|
|
||
| result := make(map[string]*ResolvedAlias, len(r.aliases)) | ||
| for k, v := range r.aliases { | ||
| result[k] = v | ||
| } | ||
| return result | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation of the global resolver singleton is overly complex. The use of
sync.RWMutexis redundant given that theResolvertype is already internally thread-safe for its operations. The initialization logic inInitGlobalResolveris also confusing and performs a redundantUpdatecall if it's the first function to initialize the resolver.This can be greatly simplified to improve readability and maintainability by always initializing an empty resolver with
sync.Onceand then updating it. This removes the need for the global mutex and simplifies the initialization flow.