Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 56 additions & 13 deletions sdk/cliproxy/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2479,12 +2479,15 @@ func buildOpenAICompatibilityConfigModels(compat *config.OpenAICompatibility) []
return nil
}
now := time.Now().Unix()
models := make([]*ModelInfo, 0, len(compat.Models))
models := make([]*ModelInfo, 0, len(compat.Models)*2)
seen := make(map[string]struct{}, len(compat.Models)*2)
for i := range compat.Models {
model := compat.Models[i]
modelID := strings.TrimSpace(model.Alias)
alias := strings.TrimSpace(model.Alias)
name := strings.TrimSpace(model.Name)
modelID := alias
if modelID == "" {
modelID = strings.TrimSpace(model.Name)
modelID = name
}
if modelID == "" {
continue
Expand All @@ -2497,7 +2500,7 @@ func buildOpenAICompatibilityConfigModels(compat *config.OpenAICompatibility) []
if thinking == nil && !model.Image {
thinking = &registry.ThinkingSupport{Levels: []string{"low", "medium", "high"}}
}
models = append(models, &ModelInfo{
info := &ModelInfo{
ID: modelID,
Object: "model",
Created: now,
Expand All @@ -2506,18 +2509,48 @@ func buildOpenAICompatibilityConfigModels(compat *config.OpenAICompatibility) []
DisplayName: modelID,
UserDefined: false,
Thinking: thinking,
})
}
models = appendModelInfoIfUnique(models, info, seen)
if alias != "" && name != "" && alias != name {
models = appendModelInfoIfUnique(models, &ModelInfo{
ID: name,
Object: "model",
Created: now,
OwnedBy: compat.Name,
Type: modelType,
DisplayName: name,
UserDefined: false,
Thinking: thinking,
}, seen)
}
}
return models
}

func appendModelInfoIfUnique(models []*ModelInfo, info *ModelInfo, seen map[string]struct{}) []*ModelInfo {
if info == nil {
return models
}
modelID := strings.TrimSpace(info.ID)
if modelID == "" {
return models
}
key := modelID

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve folded de-dupe for configured aliases

When a config contains two aliases that differ only by case, this helper now registers both spellings as separate model IDs because the de-dupe key is the exact ID. The execution path still resolves API-key aliases case-insensitively and keeps the first mapping in compileAPIKeyModelAliasForModels (OpenAI-compatible pooling also matches aliases with EqualFold), so a request for the second advertised alias selects this credential but is sent to the first alias's upstream model. Please keep folded de-dupe for configured aliases and apply case-sensitive uniqueness only when adding the extra original upstream ID.

Useful? React with 👍 / 👎.

if _, exists := seen[key]; exists {
return models
}
seen[key] = struct{}{}
info.ID = modelID
return append(models, info)
}

func buildConfigModels[T modelEntry](models []T, ownedBy, modelType string) []*ModelInfo {
if len(models) == 0 {
return nil
}
now := time.Now().Unix()
out := make([]*ModelInfo, 0, len(models))
seen := make(map[string]struct{}, len(models))
out := make([]*ModelInfo, 0, len(models)*2)
seen := make(map[string]struct{}, len(models)*2)
for i := range models {
model := models[i]
name := strings.TrimSpace(model.GetName())
Expand All @@ -2528,11 +2561,6 @@ func buildConfigModels[T modelEntry](models []T, ownedBy, modelType string) []*M
if alias == "" {
continue
}
key := strings.ToLower(alias)
if _, exists := seen[key]; exists {
continue
}
seen[key] = struct{}{}
display := name
if display == "" {
display = alias
Expand All @@ -2551,7 +2579,22 @@ func buildConfigModels[T modelEntry](models []T, ownedBy, modelType string) []*M
info.Thinking = upstream.Thinking
}
}
out = append(out, info)
out = appendModelInfoIfUnique(out, info, seen)
if name != "" && alias != name {
nameInfo := &ModelInfo{
ID: name,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Honor exclusions for alias/name pairs

When an API-key config uses an alias and excludes that alias (the only registered ID before this change), registerModelsForAuth still runs applyExcludedModels on the expanded slice. This new branch adds the upstream name as another ID, so a config like models: [{name: "gpt-5", alias: "my-gpt"}] with excluded-models: ["my-gpt"] now registers gpt-5 and leaves the same credential selectable for the excluded upstream model. Please make exclusions apply to the whole alias/name pair or filter before adding the extra routable ID.

Useful? React with 👍 / 👎.

Object: "model",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve static metadata for original IDs

When one API-key config aliases a built-in model while another credential for the same provider registers the normal catalog, this new original-ID entry can be the last RegisterClient writer for that provider/model. The registry stores a single ModelInfo per provider, so the minimal nameInfo replaces the static entry for e.g. gpt-5.4-mini, dropping context length/supported parameters and marking the built-in as user-defined in lookups/model listings. Build the original-ID entry from the static ModelInfo when it exists (or avoid overwriting it), rather than constructing a fresh minimal record.

Useful? React with 👍 / 👎.

Created: now,
OwnedBy: ownedBy,
Type: modelType,
DisplayName: name,
UserDefined: true,
}
if upstream := registry.LookupStaticModelInfo(name); upstream != nil && upstream.Thinking != nil {
nameInfo.Thinking = upstream.Thinking
}
out = appendModelInfoIfUnique(out, nameInfo, seen)
}
}
return out
}
Expand Down
145 changes: 145 additions & 0 deletions sdk/cliproxy/service_excluded_models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"testing"

internalconfig "github.com/router-for-me/CLIProxyAPI/v7/internal/config"
internalregistry "github.com/router-for-me/CLIProxyAPI/v7/internal/registry"
coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth"
"github.com/router-for-me/CLIProxyAPI/v7/sdk/config"
Expand Down Expand Up @@ -136,6 +137,150 @@ func TestRegisterModelsForAuth_OpenAICompatibilityImageModelType(t *testing.T) {
}
}

func TestRegisterModelsForAuth_ConfigAliasKeepsOriginalModelRoutable(t *testing.T) {
testCases := []struct {
name string
service *Service
auth *coreauth.Auth
provider string
originalModel string
aliasModel string
}{
{
name: "codex api key",
service: &Service{cfg: &config.Config{
CodexKey: []internalconfig.CodexKey{{
APIKey: "codex-key",
BaseURL: "https://example.com",
Models: []internalconfig.CodexModel{{
Name: "gpt-5.4-mini",
Alias: "GPT-5.4 Mini",
}},
}},
}},
auth: &coreauth.Auth{
ID: "auth-codex-alias-route",
Provider: "codex",
Status: coreauth.StatusActive,
Attributes: map[string]string{
"auth_kind": "api_key",
"api_key": "codex-key",
"base_url": "https://example.com",
},
},
provider: "codex",
originalModel: "gpt-5.4-mini",
aliasModel: "GPT-5.4 Mini",
},
{
name: "openai compatibility",
service: &Service{cfg: &config.Config{
OpenAICompatibility: []config.OpenAICompatibility{{
Name: "compat",
BaseURL: "https://example.com/v1",
Models: []config.OpenAICompatibilityModel{{
Name: "gpt-5.4-mini",
Alias: "GPT-5.4 Mini",
}},
}},
}},
auth: &coreauth.Auth{
ID: "auth-openai-compat-alias-route",
Provider: "openai-compatibility",
Status: coreauth.StatusActive,
Attributes: map[string]string{
"auth_kind": "api_key",
"compat_name": "compat",
"provider_key": "compat",
},
},
provider: "compat",
originalModel: "gpt-5.4-mini",
aliasModel: "GPT-5.4 Mini",
},
{
name: "codex case-distinct alias",
service: &Service{cfg: &config.Config{
CodexKey: []internalconfig.CodexKey{{
APIKey: "codex-case-key",
BaseURL: "https://example.com",
Models: []internalconfig.CodexModel{{
Name: "gpt-5",
Alias: "GPT-5",
}},
}},
}},
auth: &coreauth.Auth{
ID: "auth-codex-case-alias-route",
Provider: "codex",
Status: coreauth.StatusActive,
Attributes: map[string]string{
"auth_kind": "api_key",
"api_key": "codex-case-key",
"base_url": "https://example.com",
},
},
provider: "codex",
originalModel: "gpt-5",
aliasModel: "GPT-5",
},
{
name: "openai compatibility case-distinct alias",
service: &Service{cfg: &config.Config{
OpenAICompatibility: []config.OpenAICompatibility{{
Name: "compat-case",
BaseURL: "https://example.com/v1",
Models: []config.OpenAICompatibilityModel{{
Name: "gpt-5",
Alias: "GPT-5",
}},
}},
}},
auth: &coreauth.Auth{
ID: "auth-openai-compat-case-alias-route",
Provider: "openai-compatibility",
Status: coreauth.StatusActive,
Attributes: map[string]string{
"auth_kind": "api_key",
"compat_name": "compat-case",
"provider_key": "compat-case",
},
},
provider: "compat-case",
originalModel: "gpt-5",
aliasModel: "GPT-5",
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
modelRegistry := internalregistry.GetGlobalRegistry()
modelRegistry.UnregisterClient(tt.auth.ID)
t.Cleanup(func() {
modelRegistry.UnregisterClient(tt.auth.ID)
})

tt.service.registerModelsForAuth(context.Background(), tt.auth)

if !providersContain(modelRegistry.GetModelProviders(tt.aliasModel), tt.provider) {
t.Fatalf("alias model %q providers = %v, want %q", tt.aliasModel, modelRegistry.GetModelProviders(tt.aliasModel), tt.provider)
}
if !providersContain(modelRegistry.GetModelProviders(tt.originalModel), tt.provider) {
t.Fatalf("original model %q providers = %v, want %q", tt.originalModel, modelRegistry.GetModelProviders(tt.originalModel), tt.provider)
}
})
}
}

func providersContain(providers []string, want string) bool {
for _, provider := range providers {
if strings.EqualFold(strings.TrimSpace(provider), want) {
return true
}
}
return false
}

func TestRegisterModelsForAuth_AntigravityFetchesWebSearchCapability(t *testing.T) {
var sawFetch bool
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
Loading