Repository navigation
Fix Go: list providers order issue. - #16616
Conversation
Signed-off-by: Jin Hai <[email protected]>
Signed-off-by: Jin Hai <[email protected]>
📝 WalkthroughWalkthroughAdds a numeric ChangesProvider rank support
Estimated code review effort: 1 (Trivial) | ~5 minutes Sequence Diagram(s)sequenceDiagram
participant Client
participant ProviderManager
participant ProviderConfig
Client->>ProviderManager: ListProviders()
ProviderManager->>ProviderConfig: read Rank field per provider
ProviderManager->>ProviderManager: build response map with rank
ProviderManager->>ProviderManager: sort providers by rank descending
ProviderManager-->>Client: sorted providers list
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
internal/entity/models/model.go (1)
375-389: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winSort on the typed
Providerslice instead ofinterface{}maps.Sorting via
providers[i]["rank"].(int)type assertion works today because every entry always sets"rank"as anint, but it's fragile against future changes toproviderDataconstruction. Sortingpm.Providers(or a copy) byprovider.Rankbefore building the output maps avoids the assertion entirely and is more idiomatic.Also note
sort.Slicedoesn't guarantee stable ordering for equal ranks; if two providers ever share a rank value, their relative order becomes nondeterministic across runs.sort.SliceStablewould preserve deterministic ordering for ties.♻️ Suggested refactor
func (pm *ProviderManager) ListProviders() ([]map[string]interface{}, error) { var providers []map[string]interface{} - for _, provider := range pm.Providers { + sortedProviders := make([]Provider, len(pm.Providers)) + copy(sortedProviders, pm.Providers) + sort.SliceStable(sortedProviders, func(i, j int) bool { + return sortedProviders[i].Rank > sortedProviders[j].Rank + }) + + for _, provider := range sortedProviders { modelTypeSet := make(map[string]struct{}) for _, model := range provider.Models { for _, modelType := range model.ModelTypes { modelTypeSet[modelType] = struct{}{} } } modelTypes := make([]string, 0, len(modelTypeSet)) for modelType := range modelTypeSet { modelTypes = append(modelTypes, modelType) } providerData := map[string]interface{}{ "name": provider.Name, "rank": provider.Rank, "url": provider.URL, "model_types": modelTypes, "url_suffix": provider.URLSuffix, } providers = append(providers, providerData) } - - // Sort providers by rank - sort.Slice(providers, func(i, j int) bool { - return providers[i]["rank"].(int) > providers[j]["rank"].(int) - }) if len(providers) == 0 { return nil, fmt.Errorf("no providers found") }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/entity/models/model.go` around lines 375 - 389, The provider sorting in the model-building flow relies on `providerData["rank"].(int)` inside `sort.Slice`, which is fragile and nondeterministic for equal ranks. Update the sorting in the `providers` construction path to sort the typed `pm.Providers` slice (or a copy) by `Provider.Rank` before converting each entry into the `providerData` map, and use stable sorting so ties keep deterministic order. Keep the output map creation unchanged, but remove the `interface{}` rank assertion from the sort logic.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@internal/entity/models/model.go`:
- Around line 375-389: The provider sorting in the model-building flow relies on
`providerData["rank"].(int)` inside `sort.Slice`, which is fragile and
nondeterministic for equal ranks. Update the sorting in the `providers`
construction path to sort the typed `pm.Providers` slice (or a copy) by
`Provider.Rank` before converting each entry into the `providerData` map, and
use stable sorting so ties keep deterministic order. Keep the output map
creation unchanged, but remove the `interface{}` rank assertion from the sort
logic.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 49ee4833-6fe5-4d3c-8fe7-297cbff80e41
📒 Files selected for processing (18)
conf/models/aliyun.jsonconf/models/anthropic.jsonconf/models/cohere.jsonconf/models/deepseek.jsonconf/models/google.jsonconf/models/huggingface.jsonconf/models/minimax.jsonconf/models/moonshot.jsonconf/models/ollama.jsonconf/models/openai.jsonconf/models/openrouter.jsonconf/models/replicate.jsonconf/models/siliconflow.jsonconf/models/vllm.jsonconf/models/volcengine.jsonconf/models/xai.jsonconf/models/zhipu-ai.jsoninternal/entity/models/model.go
### Summary As title. --------- Signed-off-by: Jin Hai <[email protected]>
### Summary As title. --------- Signed-off-by: Jin Hai <[email protected]>
Summary
As title.