Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
70 changes: 57 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,49 @@ func buildOpenAICompatibilityConfigModels(compat *config.OpenAICompatibility) []
DisplayName: modelID,
UserDefined: false,
Thinking: thinking,
})
}
models = appendModelInfoIfUnique(models, info, seen)
if alias != "" && name != "" && !strings.EqualFold(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 := strings.ToLower(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 case-distinct original model IDs

When a configured alias differs from the upstream name only by capitalization, this case-folded de-dupe (together with the EqualFold checks in the two builders) still registers only the alias. Provider lookup is an exact map lookup by the requested model ID, so Name: "gpt-5" with Alias: "GPT-5" leaves requests for the real upstream gpt-5 failing with unknown provider for model, even though this change is intended to keep original IDs routable.

Useful? React with 👍 / 👎.

if _, exists := seen[key]; exists {
return models
}
seen[key] = struct{}{}
clone := *info
clone.ID = modelID
return append(models, &clone)
Comment thread
daiaji marked this conversation as resolved.
Outdated
}

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 +2562,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 +2580,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 != "" && !strings.EqualFold(alias, name) {
nameInfo := &ModelInfo{
ID: name,
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
93 changes: 93 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,98 @@ 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",
},
}

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