Repository navigation
Add show / set role default models - #16240
Conversation
Signed-off-by: Jin Hai <[email protected]>
📝 WalkthroughWalkthroughProvider/model listing handlers are relocated from ChangesRole Default Model Management
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Suggested labels
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. 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 |
Signed-off-by: Jin Hai <[email protected]>
There was a problem hiding this comment.
Actionable comments posted: 5
🤖 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.
Inline comments:
In `@internal/admin/enterprise_handler.go`:
- Around line 232-233: The success message "Role default model set successfully"
is being used for multiple operations including show and reset handlers on lines
232 and 291, but this message is only semantically correct for the set
operation. Replace the success message on line 232 and line 291 with
operation-specific messages that accurately describe what each handler does,
such as using appropriate messages for show and reset operations instead of the
generic set message.
- Around line 303-320: The handler function is missing a response when the
keywords parameter is not equal to "true", causing the function to fall through
and return an empty 200 response body instead of a proper JSON response. Add a
fallback response after the if block that checks if keywords equals "true" to
handle the case when this condition is false, ensuring that a properly formatted
JSON response with the standard {code, message, data} envelope is always
returned to the client, similar to the success response structure already
present in the ListProviders handler.
In `@internal/admin/enterprise_service.go`:
- Around line 146-154: The SetRoleDefaultModel function signature only accepts
modelID as a parameter, but downstream command flow can send modelName instead,
causing valid requests to be rejected at the boundary. Update the
SetRoleDefaultModel method to accept both modelID and modelName as parameters,
and implement one-of validation to ensure at least one is provided.
Additionally, update the request binding and validation in enterprise_handler.go
to extract both fields from the incoming request and pass them to the
SetRoleDefaultModel service call with proper one-of validation logic.
In `@internal/cli/admin_command.go`:
- Around line 1097-1107: The PATCH payload being constructed in the
admin_command.go code is setting model_name when a composite_model_name is
provided, but the server's SetRoleDefaultModelRequest struct in
internal/admin/enterprise_handler.go only binds model_id and model_type, causing
the model selection to be lost. Either update the SetRoleDefaultModelRequest
struct to accept and handle model_name in addition to model_id and model_type,
ensuring the service layer properly resolves the model by name, or remove the
composite_model_name parsing logic entirely and only support model_id in this
command path. Choose the approach that aligns with your system's long-term
design goals.
In `@internal/cli/admin_parser.go`:
- Around line 544-566: The switch statement in parseAdminShowRole currently only
creates the admin_show_role command when TokenSemicolon is encountered, which
breaks the optional semicolon behavior. To fix this, move the admin_show_role
command creation from the TokenSemicolon case to the default case so that when
no special keywords (PERMISSION or DEFAULT) follow the role name, the bare
admin_show_role command is emitted regardless of whether a semicolon is present.
This preserves backward compatibility with commands like SHOW ROLE 'user' that
don't require a trailing semicolon.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 562ca665-80fa-4698-9b31-1ad608104d50
📒 Files selected for processing (11)
internal/admin/enterprise_handler.gointernal/admin/enterprise_service.gointernal/admin/handler.gointernal/admin/router.gointernal/cli/admin_command.gointernal/cli/admin_parser.gointernal/cli/cli_http.gointernal/cli/lexer.gointernal/cli/parser.gointernal/cli/types.gointernal/service/tenant.go
💤 Files with no reviewable changes (4)
- internal/cli/types.go
- internal/cli/lexer.go
- internal/service/tenant.go
- internal/admin/handler.go
| success(c, result, "Role default model set successfully") | ||
| } |
There was a problem hiding this comment.
Use operation-specific success messages for show/reset handlers.
Line 232 and Line 291 both return "Role default model set successfully", which is misleading for show and reset operations.
💡 Proposed fix
- success(c, result, "Role default model set successfully")
+ success(c, result, "Role default models retrieved successfully")
...
- success(c, result, "Role default model set successfully")
+ success(c, result, "Role default model reset successfully")Also applies to: 291-292
🤖 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/admin/enterprise_handler.go` around lines 232 - 233, The success
message "Role default model set successfully" is being used for multiple
operations including show and reset handlers on lines 232 and 291, but this
message is only semantically correct for the set operation. Replace the success
message on line 232 and line 291 with operation-specific messages that
accurately describe what each handler does, such as using appropriate messages
for show and reset operations instead of the generic set message.
| if keywords == "true" { | ||
| // list pool providers | ||
| providers, err := dao.GetModelProviderManager().ListProviders() | ||
| if err != nil { | ||
| c.JSON(http.StatusOK, gin.H{ | ||
| "code": common.CodeNotFound, | ||
| "message": err.Error(), | ||
| }) | ||
| return | ||
| } | ||
|
|
||
| c.JSON(http.StatusOK, gin.H{ | ||
| "code": 0, | ||
| "message": "success", | ||
| "data": providers, | ||
| }) | ||
| } | ||
| } |
There was a problem hiding this comment.
ListProviders can return an empty HTTP 200 response body.
When available is missing or not "true", execution falls through without writing any response. This creates a non-JSON 200 response that can break callers expecting the standard {code,message,data} envelope.
💡 Proposed fix
func (h *Handler) ListProviders(c *gin.Context) {
@@
keywords = strings.ToLower(keywords)
- if keywords == "true" {
+ if keywords == "true" {
// list pool providers
providers, err := dao.GetModelProviderManager().ListProviders()
@@
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "success",
"data": providers,
})
+ return
}
+
+ c.JSON(http.StatusBadRequest, gin.H{
+ "code": common.CodeBadRequest,
+ "message": "query parameter available=true is required",
+ "data": nil,
+ })
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if keywords == "true" { | |
| // list pool providers | |
| providers, err := dao.GetModelProviderManager().ListProviders() | |
| if err != nil { | |
| c.JSON(http.StatusOK, gin.H{ | |
| "code": common.CodeNotFound, | |
| "message": err.Error(), | |
| }) | |
| return | |
| } | |
| c.JSON(http.StatusOK, gin.H{ | |
| "code": 0, | |
| "message": "success", | |
| "data": providers, | |
| }) | |
| } | |
| } | |
| if keywords == "true" { | |
| // list pool providers | |
| providers, err := dao.GetModelProviderManager().ListProviders() | |
| if err != nil { | |
| c.JSON(http.StatusOK, gin.H{ | |
| "code": common.CodeNotFound, | |
| "message": err.Error(), | |
| }) | |
| return | |
| } | |
| c.JSON(http.StatusOK, gin.H{ | |
| "code": 0, | |
| "message": "success", | |
| "data": providers, | |
| }) | |
| return | |
| } | |
| c.JSON(http.StatusBadRequest, gin.H{ | |
| "code": common.CodeBadRequest, | |
| "message": "query parameter available=true is required", | |
| "data": nil, | |
| }) | |
| } |
🤖 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/admin/enterprise_handler.go` around lines 303 - 320, The handler
function is missing a response when the keywords parameter is not equal to
"true", causing the function to fall through and return an empty 200 response
body instead of a proper JSON response. Add a fallback response after the if
block that checks if keywords equals "true" to handle the case when this
condition is false, ensuring that a properly formatted JSON response with the
standard {code, message, data} envelope is always returned to the client,
similar to the success response structure already present in the ListProviders
handler.
| func (s *Service) SetRoleDefaultModel(roleName, modelID, modelType string) (map[string]interface{}, error) { | ||
| return map[string]interface{}{ | ||
| "command": "set_role_default_model", | ||
| "role_name": roleName, | ||
| "model_id": modelID, | ||
| "model_type": modelType, | ||
| "error": "'set role default model' is implemented in enterprise edition", | ||
| }, nil | ||
| } |
There was a problem hiding this comment.
SetRoleDefaultModel contract drops model_name callers.
Line 146 only accepts modelID, but downstream command flow can send model_name instead of model_id. That payload cannot be represented through this method contract, so one valid request form is lost across the handler/service boundary.
💡 Proposed fix
- func (s *Service) SetRoleDefaultModel(roleName, modelID, modelType string) (map[string]interface{}, error) {
- return map[string]interface{}{
+ func (s *Service) SetRoleDefaultModel(roleName, modelID, modelName, modelType string) (map[string]interface{}, error) {
+ result := map[string]interface{}{
"command": "set_role_default_model",
"role_name": roleName,
- "model_id": modelID,
"model_type": modelType,
"error": "'set role default model' is implemented in enterprise edition",
- }, nil
+ }
+ if modelID != "" {
+ result["model_id"] = modelID
+ }
+ if modelName != "" {
+ result["model_name"] = modelName
+ }
+ return result, nil
}Also update internal/admin/enterprise_handler.go request binding/validation and service call to pass model_name (with one-of validation for model_id vs model_name).
🤖 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/admin/enterprise_service.go` around lines 146 - 154, The
SetRoleDefaultModel function signature only accepts modelID as a parameter, but
downstream command flow can send modelName instead, causing valid requests to be
rejected at the boundary. Update the SetRoleDefaultModel method to accept both
modelID and modelName as parameters, and implement one-of validation to ensure
at least one is provided. Additionally, update the request binding and
validation in enterprise_handler.go to extract both fields from the incoming
request and pass them to the SetRoleDefaultModel service call with proper one-of
validation logic.
| var modelName string | ||
| modelID, ok := cmd.Params["model_id"].(string) | ||
| if ok { | ||
| payload["model_id"] = modelID | ||
| } else { | ||
| modelName, ok = cmd.Params["composite_model_name"].(string) | ||
| if ok { | ||
| payload["model_name"] = modelName | ||
| } else { | ||
| return nil, fmt.Errorf("model_id or model_name not provided") | ||
| } |
There was a problem hiding this comment.
Align PATCH payload with server request contract.
Line 1104 sends model_name, but the PATCH handler binds only model_id and model_type (internal/admin/enterprise_handler.go SetRoleDefaultModelRequest). For composite-name inputs, the selected model is dropped at the API boundary.
💡 Suggested direction
- modelName, ok = cmd.Params["composite_model_name"].(string)
- if ok {
- payload["model_name"] = modelName
- } else {
+ modelName, ok = cmd.Params["composite_model_name"].(string)
+ if ok {
+ return nil, fmt.Errorf("composite_model_name is not supported by this endpoint; use model_id")
+ } else {
return nil, fmt.Errorf("model_id or model_name not provided")
}Preferred long-term fix is to update the HTTP request contract to explicitly accept model name (and propagate it through service), or remove composite-name parsing for this command path.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| var modelName string | |
| modelID, ok := cmd.Params["model_id"].(string) | |
| if ok { | |
| payload["model_id"] = modelID | |
| } else { | |
| modelName, ok = cmd.Params["composite_model_name"].(string) | |
| if ok { | |
| payload["model_name"] = modelName | |
| } else { | |
| return nil, fmt.Errorf("model_id or model_name not provided") | |
| } | |
| var modelName string | |
| modelID, ok := cmd.Params["model_id"].(string) | |
| if ok { | |
| payload["model_id"] = modelID | |
| } else { | |
| modelName, ok = cmd.Params["composite_model_name"].(string) | |
| if ok { | |
| return nil, fmt.Errorf("composite_model_name is not supported by this endpoint; use model_id") | |
| } else { | |
| return nil, fmt.Errorf("model_id or model_name not provided") | |
| } |
🤖 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/cli/admin_command.go` around lines 1097 - 1107, The PATCH payload
being constructed in the admin_command.go code is setting model_name when a
composite_model_name is provided, but the server's SetRoleDefaultModelRequest
struct in internal/admin/enterprise_handler.go only binds model_id and
model_type, causing the model selection to be lost. Either update the
SetRoleDefaultModelRequest struct to accept and handle model_name in addition to
model_id and model_type, ensuring the service layer properly resolves the model
by name, or remove the composite_model_name parsing logic entirely and only
support model_id in this command path. Choose the approach that aligns with your
system's long-term design goals.
| var cmd *Command | ||
| if p.curToken.Type == TokenPermission { | ||
|
|
||
| switch p.curToken.Type { | ||
| case TokenPermission: | ||
| p.nextToken() | ||
| cmd = NewCommand("admin_show_role_permission") | ||
| cmd.Params["role_name"] = roleName | ||
| } else { | ||
| case TokenDefault: | ||
| p.nextToken() | ||
| if p.curToken.Type != TokenModels { | ||
| return nil, fmt.Errorf("expect MODELS after DEFAULT") | ||
| } | ||
| p.nextToken() | ||
| cmd = NewCommand("admin_show_role_default_models") | ||
| cmd.Params["role_name"] = roleName | ||
| case TokenSemicolon: | ||
| p.nextToken() | ||
| cmd = NewCommand("admin_show_role") | ||
| cmd.Params["role_name"] = roleName | ||
| default: | ||
| return nil, fmt.Errorf("invalid command %s", tokenTypeToString(p.curToken.Type)) | ||
| } | ||
|
|
There was a problem hiding this comment.
Preserve semicolon-optional behavior for bare SHOW ROLE.
parseAdminShowRole now only emits admin_show_role when it sees TokenSemicolon (Line 559). That makes SHOW ROLE 'user' fail when no ; is present, which regresses the previous optional-terminator behavior.
💡 Suggested patch
switch p.curToken.Type {
case TokenPermission:
p.nextToken()
cmd = NewCommand("admin_show_role_permission")
cmd.Params["role_name"] = roleName
case TokenDefault:
p.nextToken()
if p.curToken.Type != TokenModels {
return nil, fmt.Errorf("expect MODELS after DEFAULT")
}
p.nextToken()
cmd = NewCommand("admin_show_role_default_models")
cmd.Params["role_name"] = roleName
-case TokenSemicolon:
- p.nextToken()
+case TokenSemicolon, TokenEOF:
cmd = NewCommand("admin_show_role")
cmd.Params["role_name"] = roleName
default:
return nil, fmt.Errorf("invalid command %s", tokenTypeToString(p.curToken.Type))
}
+
+if p.curToken.Type == TokenSemicolon {
+ p.nextToken()
+}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| var cmd *Command | |
| if p.curToken.Type == TokenPermission { | |
| switch p.curToken.Type { | |
| case TokenPermission: | |
| p.nextToken() | |
| cmd = NewCommand("admin_show_role_permission") | |
| cmd.Params["role_name"] = roleName | |
| } else { | |
| case TokenDefault: | |
| p.nextToken() | |
| if p.curToken.Type != TokenModels { | |
| return nil, fmt.Errorf("expect MODELS after DEFAULT") | |
| } | |
| p.nextToken() | |
| cmd = NewCommand("admin_show_role_default_models") | |
| cmd.Params["role_name"] = roleName | |
| case TokenSemicolon: | |
| p.nextToken() | |
| cmd = NewCommand("admin_show_role") | |
| cmd.Params["role_name"] = roleName | |
| default: | |
| return nil, fmt.Errorf("invalid command %s", tokenTypeToString(p.curToken.Type)) | |
| } | |
| var cmd *Command | |
| switch p.curToken.Type { | |
| case TokenPermission: | |
| p.nextToken() | |
| cmd = NewCommand("admin_show_role_permission") | |
| cmd.Params["role_name"] = roleName | |
| case TokenDefault: | |
| p.nextToken() | |
| if p.curToken.Type != TokenModels { | |
| return nil, fmt.Errorf("expect MODELS after DEFAULT") | |
| } | |
| p.nextToken() | |
| cmd = NewCommand("admin_show_role_default_models") | |
| cmd.Params["role_name"] = roleName | |
| case TokenSemicolon, TokenEOF: | |
| cmd = NewCommand("admin_show_role") | |
| cmd.Params["role_name"] = roleName | |
| default: | |
| return nil, fmt.Errorf("invalid command %s", tokenTypeToString(p.curToken.Type)) | |
| } | |
| if p.curToken.Type == TokenSemicolon { | |
| p.nextToken() | |
| } |
🤖 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/cli/admin_parser.go` around lines 544 - 566, The switch statement in
parseAdminShowRole currently only creates the admin_show_role command when
TokenSemicolon is encountered, which breaks the optional semicolon behavior. To
fix this, move the admin_show_role command creation from the TokenSemicolon case
to the default case so that when no special keywords (PERMISSION or DEFAULT)
follow the role name, the bare admin_show_role command is emitted regardless of
whether a semicolon is present. This preserves backward compatibility with
commands like SHOW ROLE 'user' that don't require a trailing semicolon.
What problem does this PR solve?