fix: 未设置价格模型不会被拉取,除非设置自用模式 - #2222
Conversation
WalkthroughThe pull request introduces runtime gating for model listing based on user-specific ratio settings. A new function checks if models have configured ratios or prices, and model listing is filtered to exclude models without ratios when the user setting is disabled. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant ModelController
participant UserSetting
participant RatioSetting
Client->>ModelController: ListModels()
alt First call (acceptUnsetRatioModel unset)
ModelController->>UserSetting: GetUserSetting()
UserSetting-->>ModelController: AcceptUnsetRatioModel flag
ModelController->>ModelController: Initialize acceptUnsetRatioModel
end
loop For each model in list
alt acceptUnsetRatioModel is false
ModelController->>RatioSetting: GetModelRatioOrPrice(model)
RatioSetting-->>ModelController: ratio/price, exists flag
alt Model has ratio/price
ModelController->>ModelController: Include model
else Model lacks ratio/price
ModelController->>ModelController: Skip model
end
else acceptUnsetRatioModel is true
ModelController->>ModelController: Include model
end
end
ModelController-->>Client: Filtered model list
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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.
Actionable comments posted: 1
🧹 Nitpick comments (1)
controller/model.go (1)
135-140: Consider extracting the gating logic into a helper function.The same gating logic appears in two places (lines 135-140 and 183-188). While the duplication is minimal, extracting it into a helper function would improve maintainability.
For example:
func shouldIncludeModel(modelName string, acceptUnsetRatioModel bool) bool { if acceptUnsetRatioModel { return true } _, _, exist := ratio_setting.GetModelRatioOrPrice(modelName) return exist }Then use it at both locations:
if !shouldIncludeModel(allowModel, acceptUnsetRatioModel) { continue }Also applies to: 183-188
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
controller/model.go(5 hunks)setting/ratio_setting/model_ratio.go(1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-08-05T17:14:17.246Z
Learnt from: neotf
Repo: QuantumNous/new-api PR: 1511
File: setting/ratio_setting/model_ratio.go:118-123
Timestamp: 2025-08-05T17:14:17.246Z
Learning: Claude models handle "-thinking" variants differently from Gemini models. For Claude models, only the base model (without "-thinking") gets an entry in defaultModelRatio map. The "-thinking" variants rely on the Claude relay handler stripping the suffix using strings.TrimSuffix(textRequest.Model, "-thinking") before looking up the ratio, so they automatically use the base model's ratio.
Applied to files:
setting/ratio_setting/model_ratio.gocontroller/model.go
🧬 Code graph analysis (1)
controller/model.go (3)
setting/operation_setting/operation_setting.go (1)
SelfUseModeEnabled(6-6)model/user.go (1)
GetUserSetting(723-751)setting/ratio_setting/model_ratio.go (1)
GetModelRatioOrPrice(828-838)
🔇 Additional comments (2)
setting/ratio_setting/model_ratio.go (1)
827-838: LGTM! Function correctly implements price/ratio lookup with existence flag.The function provides a clean way to check if a model has a configured price or ratio, which is used for gating model availability in the listing endpoint.
controller/model.go (1)
114-123: Implementation correctly gates models based on ratio/price configuration.The gating logic aligns with the PR objective: models without configured ratios or prices are now excluded from listing unless self-use mode is enabled globally or per-user. The implementation correctly:
- Checks global
SelfUseModeEnabledfirst- Falls back to per-user
AcceptUnsetRatioModelsetting- Applies consistent gating in both token-based and group-based model listing paths
Also applies to: 135-140, 183-188
| acceptUnsetRatioModel := operation_setting.SelfUseModeEnabled | ||
| if !acceptUnsetRatioModel { | ||
| userId := c.GetInt("id") | ||
| if userId > 0 { | ||
| userSettings, _ := model.GetUserSetting(userId, false) | ||
| if userSettings.AcceptUnsetRatioModel { | ||
| acceptUnsetRatioModel = true | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Consider logging the error from GetUserSetting.
The error from GetUserSetting is currently ignored. While the default behavior (treating as AcceptUnsetRatioModel=false) is safe, logging the error would help with debugging user setting retrieval issues.
Apply this diff to log the error:
if !acceptUnsetRatioModel {
userId := c.GetInt("id")
if userId > 0 {
- userSettings, _ := model.GetUserSetting(userId, false)
+ userSettings, err := model.GetUserSetting(userId, false)
+ if err != nil {
+ common.SysLog(fmt.Sprintf("failed to get user setting for user %d: %v", userId, err))
+ }
if userSettings.AcceptUnsetRatioModel {
acceptUnsetRatioModel = true
}🤖 Prompt for AI Agents
In controller/model.go around lines 114 to 123, the call to model.GetUserSetting
ignores its error; update the code to capture the returned error, and if non-nil
log it with context (including the userId) before continuing with the safe
default. Specifically, change the assignment to userSettings, err :=
model.GetUserSetting(userId, false), then if err != nil call the request/logger
(e.g. c.Logger.Errorf or the project’s logger) to record a clear message and the
error value, and only set acceptUnsetRatioModel if err == nil and
userSettings.AcceptUnsetRatioModel is true.
fix: 未设置价格模型不会被拉取,除非设置自用模式
Summary by CodeRabbit