Repository navigation
Go CLI: admin list providers - #16243
Conversation
Signed-off-by: Jin Hai <[email protected]>
📝 WalkthroughWalkthroughAdds admin API endpoints and CLI commands for managing model providers, model instances, and models in a three-level hierarchy. Enterprise service stubs are added for all operations. The admin HTTP router is rewired to renamed handler functions. CLI parser grammar is extended for ChangesAdmin model provider/instance/model management
Sequence Diagram(s)sequenceDiagram
participant AdminCLI
participant ExecuteAdminCommand
participant AdminCommand
participant AdminHTTPAPI
participant enterprise_handler
participant enterprise_service
AdminCLI->>ExecuteAdminCommand: admin_add_provider / admin_add_model_instance / admin_add_models
ExecuteAdminCommand->>AdminCommand: AdminAddProviderCommand / AdminAddModelInstanceCommand / AdminAddModelsCommand
AdminCommand->>AdminHTTPAPI: POST /admin/providers[/{provider}/instances[/{instance}/models]]
AdminHTTPAPI->>enterprise_handler: AddModelProvider / AddModelInstance / AddModels
enterprise_handler->>enterprise_service: AddModelProvider / AddModelInstance / AddModels(userID, ...)
enterprise_service-->>enterprise_handler: {command, error: "enterprise edition"}, nil
enterprise_handler-->>AdminHTTPAPI: success(data)
AdminHTTPAPI-->>AdminCommand: HTTP 200 + CommonDataResponse
AdminCommand-->>ExecuteAdminCommand: &result
ExecuteAdminCommand-->>AdminCLI: ResponseIf
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Suggested labels
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 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]>
Signed-off-by: Jin Hai <[email protected]>
Signed-off-by: Jin Hai <[email protected]>
Signed-off-by: Jin Hai <[email protected]>
There was a problem hiding this comment.
Actionable comments posted: 5
🧹 Nitpick comments (2)
internal/admin/enterprise_handler.go (1)
294-311: 🧹 Nitpick | 🔵 Trivial | ⚡ Quick winDead code:
keywordsvariable is computed but never used.The handler reads and processes the
availablequery parameter into thekeywordsvariable (lines 296-302) but never passes it toh.service.ListModelProviders()or uses it in any way.♻️ Remove unused code
func (h *Handler) ListModelProviders(c *gin.Context) { - - keywords := "" - if queryKeywords := c.Query("available"); queryKeywords != "" { - keywords = queryKeywords - } - - // convert keywords to small case - keywords = strings.ToLower(keywords) - result, err := h.service.ListModelProviders() if err != nil { errorResponse(c, err.Error(), 500)🤖 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 294 - 311, The ListModelProviders handler is reading and processing the "available" query parameter into the keywords variable and converting it to lowercase, but this variable is never passed to the h.service.ListModelProviders() call or used anywhere else in the function. Remove the dead code by deleting the variable declaration and assignment lines that extract the query parameter (the code that retrieves c.Query("available") and calls strings.ToLower()), as this logic serves no purpose if the keywords value is not being utilized by the service method.internal/cli/admin_command.go (1)
3415-3418: 🧹 Nitpick | 🔵 Trivial | 💤 Low valueError message refers to singular
instance_namebut param key isinstance_names.The error message says "instance_name not provided" but the code checks for
instance_names(plural). This could confuse users debugging issues.Proposed fix
instanceNames, ok := cmd.Params["instance_names"].([]string) if !ok { - return nil, fmt.Errorf("instance_name not provided") + return nil, fmt.Errorf("instance_names 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 3415 - 3418, The error message in the type assertion check for the instance_names parameter is inconsistent with the actual parameter key being validated. Update the error message in the fmt.Errorf call to match the parameter name being checked. Change the error message from "instance_name not provided" to "instance_names not provided" (using the plural form that matches the cmd.Params key "instance_names") so the error message accurately reflects what parameter is expected.
🤖 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 484-512: The success message in the DeleteModelInstance function
method is incorrect. At the end of the function where success(c, result, "Model
provider added successfully") is called, change the message string to accurately
reflect that model instances were deleted rather than a provider being added.
Update the message parameter to something like "Model instances deleted
successfully" to match the operation being performed.
- Around line 317-337: The AddModelProvider method call in the AddModelProvider
handler function is passing arguments in the wrong order. The service method
signature expects userID as the first parameter and providerName as the second
parameter, but the handler is currently calling it with req.ProviderName first
and userID second. Swap the argument order in the h.service.AddModelProvider
call to match the expected signature by passing userID first, followed by
req.ProviderName.
In `@internal/cli/admin_parser.go`:
- Around line 1811-1823: In the parseAdminDeleteCommands function, the error
message returned in the default case contains a copy-paste error. Change the
error message from "unknown ADD target" to "unknown DELETE target" to accurately
reflect that this is a DELETE command parser, not an ADD command parser.
In `@internal/cli/response.go`:
- Around line 91-97: The UserIndexResponse struct is missing three required
methods to fully implement the ResponseIf interface. In addition to the existing
TimeCost() method, you need to implement Type(), PrintOut(), and
SetOutputFormat() methods for UserIndexResponse. Reference the implementations
in other response types like ModelsResponse, CommonDataResponse, and
SimpleResponse in the same file to understand the expected signatures and
behavior patterns, then add these three missing methods to UserIndexResponse
following the same pattern.
In `@internal/handler/providers.go`:
- Line 147: The DeleteModelProvider method call in the handler is passing
arguments in the wrong order. The h.modelProviderService.DeleteModelProvider
call currently passes userID first and providerName second, but the service
method signature expects providerName as the first parameter and userID as the
second parameter. Swap the order of the arguments in the DeleteModelProvider
call to match the service method signature.
---
Nitpick comments:
In `@internal/admin/enterprise_handler.go`:
- Around line 294-311: The ListModelProviders handler is reading and processing
the "available" query parameter into the keywords variable and converting it to
lowercase, but this variable is never passed to the
h.service.ListModelProviders() call or used anywhere else in the function.
Remove the dead code by deleting the variable declaration and assignment lines
that extract the query parameter (the code that retrieves c.Query("available")
and calls strings.ToLower()), as this logic serves no purpose if the keywords
value is not being utilized by the service method.
In `@internal/cli/admin_command.go`:
- Around line 3415-3418: The error message in the type assertion check for the
instance_names parameter is inconsistent with the actual parameter key being
validated. Update the error message in the fmt.Errorf call to match the
parameter name being checked. Change the error message from "instance_name not
provided" to "instance_names not provided" (using the plural form that matches
the cmd.Params key "instance_names") so the error message accurately reflects
what parameter is expected.
🪄 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: 9a51f4bf-268d-4196-84f6-f9225fddd478
📒 Files selected for processing (11)
internal/admin/enterprise_handler.gointernal/admin/enterprise_service.gointernal/admin/router.gointernal/cli/admin_command.gointernal/cli/admin_parser.gointernal/cli/cli_http.gointernal/cli/parser.gointernal/cli/response.gointernal/cli/user_command.gointernal/cli/user_parser.gointernal/handler/providers.go
💤 Files with no reviewable changes (2)
- internal/cli/user_command.go
- internal/cli/user_parser.go
| func (h *Handler) AddModelProvider(c *gin.Context) { | ||
| var req AddProviderRequest | ||
| if err := c.ShouldBindJSON(&req); err != nil { | ||
| c.JSON(http.StatusOK, gin.H{ | ||
| "code": 0, | ||
| "message": "success", | ||
| "data": providers, | ||
| "code": common.CodeBadRequest, | ||
| "message": err.Error(), | ||
| "data": false, | ||
| }) | ||
| return | ||
| } | ||
|
|
||
| userID := c.GetString("user_id") | ||
|
|
||
| result, err := h.service.AddModelProvider(req.ProviderName, userID) | ||
| if err != nil { | ||
| errorResponse(c, err.Error(), 500) | ||
| return | ||
| } | ||
|
|
||
| success(c, result, "Model provider added successfully") | ||
| } |
There was a problem hiding this comment.
Critical: Arguments passed in wrong order to AddModelProvider.
Line 330 calls h.service.AddModelProvider(req.ProviderName, userID), but the service method signature at line 177 of internal/admin/enterprise_service.go is AddModelProvider(userID, providerName string). This causes the provider name to be stored as the user ID and vice versa.
🐛 Swap the argument order
- result, err := h.service.AddModelProvider(req.ProviderName, userID)
+ result, err := h.service.AddModelProvider(userID, req.ProviderName)
if err != nil {
errorResponse(c, err.Error(), 500)📝 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.
| func (h *Handler) AddModelProvider(c *gin.Context) { | |
| var req AddProviderRequest | |
| if err := c.ShouldBindJSON(&req); err != nil { | |
| c.JSON(http.StatusOK, gin.H{ | |
| "code": 0, | |
| "message": "success", | |
| "data": providers, | |
| "code": common.CodeBadRequest, | |
| "message": err.Error(), | |
| "data": false, | |
| }) | |
| return | |
| } | |
| userID := c.GetString("user_id") | |
| result, err := h.service.AddModelProvider(req.ProviderName, userID) | |
| if err != nil { | |
| errorResponse(c, err.Error(), 500) | |
| return | |
| } | |
| success(c, result, "Model provider added successfully") | |
| } | |
| func (h *Handler) AddModelProvider(c *gin.Context) { | |
| var req AddProviderRequest | |
| if err := c.ShouldBindJSON(&req); err != nil { | |
| c.JSON(http.StatusOK, gin.H{ | |
| "code": common.CodeBadRequest, | |
| "message": err.Error(), | |
| "data": false, | |
| }) | |
| return | |
| } | |
| userID := c.GetString("user_id") | |
| result, err := h.service.AddModelProvider(userID, req.ProviderName) | |
| if err != nil { | |
| errorResponse(c, err.Error(), 500) | |
| return | |
| } | |
| success(c, result, "Model provider added successfully") | |
| } |
🤖 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 317 - 337, The
AddModelProvider method call in the AddModelProvider handler function is passing
arguments in the wrong order. The service method signature expects userID as the
first parameter and providerName as the second parameter, but the handler is
currently calling it with req.ProviderName first and userID second. Swap the
argument order in the h.service.AddModelProvider call to match the expected
signature by passing userID first, followed by req.ProviderName.
| func (h *Handler) DeleteModelInstance(c *gin.Context) { | ||
| providerName := c.Param("provider_name") | ||
| if providerName == "" { | ||
| c.JSON(http.StatusBadRequest, gin.H{ | ||
| "code": 400, | ||
| "message": "Provider name is required", | ||
| }) | ||
| return | ||
| } | ||
|
|
||
| var req DropModelInstanceRequest | ||
| if err := c.ShouldBindJSON(&req); err != nil { | ||
| c.JSON(http.StatusOK, gin.H{ | ||
| "code": common.CodeBadRequest, | ||
| "message": err.Error(), | ||
| }) | ||
| return | ||
| } | ||
|
|
||
| userID := c.GetString("user_id") | ||
|
|
||
| result, err := h.service.DeleteModelInstances(userID, providerName, req.InstanceNames) | ||
| if err != nil { | ||
| errorResponse(c, err.Error(), 500) | ||
| return | ||
| } | ||
|
|
||
| success(c, result, "Model provider added successfully") | ||
| } |
There was a problem hiding this comment.
Incorrect success message for delete operation.
Line 511 returns "Model provider added successfully" for a delete operation. The message should indicate that model instances were deleted.
📝 Fix the success message
- success(c, result, "Model provider added successfully")
+ success(c, result, "Model instances deleted successfully")
}📝 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.
| func (h *Handler) DeleteModelInstance(c *gin.Context) { | |
| providerName := c.Param("provider_name") | |
| if providerName == "" { | |
| c.JSON(http.StatusBadRequest, gin.H{ | |
| "code": 400, | |
| "message": "Provider name is required", | |
| }) | |
| return | |
| } | |
| var req DropModelInstanceRequest | |
| if err := c.ShouldBindJSON(&req); err != nil { | |
| c.JSON(http.StatusOK, gin.H{ | |
| "code": common.CodeBadRequest, | |
| "message": err.Error(), | |
| }) | |
| return | |
| } | |
| userID := c.GetString("user_id") | |
| result, err := h.service.DeleteModelInstances(userID, providerName, req.InstanceNames) | |
| if err != nil { | |
| errorResponse(c, err.Error(), 500) | |
| return | |
| } | |
| success(c, result, "Model provider added successfully") | |
| } | |
| func (h *Handler) DeleteModelInstance(c *gin.Context) { | |
| providerName := c.Param("provider_name") | |
| if providerName == "" { | |
| c.JSON(http.StatusBadRequest, gin.H{ | |
| "code": 400, | |
| "message": "Provider name is required", | |
| }) | |
| return | |
| } | |
| var req DropModelInstanceRequest | |
| if err := c.ShouldBindJSON(&req); err != nil { | |
| c.JSON(http.StatusOK, gin.H{ | |
| "code": common.CodeBadRequest, | |
| "message": err.Error(), | |
| }) | |
| return | |
| } | |
| userID := c.GetString("user_id") | |
| result, err := h.service.DeleteModelInstances(userID, providerName, req.InstanceNames) | |
| if err != nil { | |
| errorResponse(c, err.Error(), 500) | |
| return | |
| } | |
| success(c, result, "Model instances deleted successfully") | |
| } |
🤖 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 484 - 512, The success
message in the DeleteModelInstance function method is incorrect. At the end of
the function where success(c, result, "Model provider added successfully") is
called, change the message string to accurately reflect that model instances
were deleted rather than a provider being added. Update the message parameter to
something like "Model instances deleted successfully" to match the operation
being performed.
| func (p *Parser) parseAdminDeleteCommands() (*Command, error) { | ||
| p.nextToken() // consume DELETE | ||
| switch p.curToken.Type { | ||
| case TokenAPI: | ||
| return p.parseDeleteAPIServer() | ||
| case TokenAdmin: | ||
| return p.parseDeleteAdminServer() | ||
| case TokenProvider: | ||
| return p.parseAdminDeleteProvider() | ||
| default: | ||
| return nil, fmt.Errorf("unknown ADD target: %s", p.curToken.Value) | ||
| } | ||
| } |
There was a problem hiding this comment.
Copy-paste error in error message.
The error message says "unknown ADD target" but this is in parseAdminDeleteCommands. Should be "unknown DELETE target".
Proposed fix
func (p *Parser) parseAdminDeleteCommands() (*Command, error) {
p.nextToken() // consume DELETE
switch p.curToken.Type {
case TokenAPI:
return p.parseDeleteAPIServer()
case TokenAdmin:
return p.parseDeleteAdminServer()
case TokenProvider:
return p.parseAdminDeleteProvider()
default:
- return nil, fmt.Errorf("unknown ADD target: %s", p.curToken.Value)
+ return nil, fmt.Errorf("unknown DELETE target: %s", p.curToken.Value)
}
}📝 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.
| func (p *Parser) parseAdminDeleteCommands() (*Command, error) { | |
| p.nextToken() // consume DELETE | |
| switch p.curToken.Type { | |
| case TokenAPI: | |
| return p.parseDeleteAPIServer() | |
| case TokenAdmin: | |
| return p.parseDeleteAdminServer() | |
| case TokenProvider: | |
| return p.parseAdminDeleteProvider() | |
| default: | |
| return nil, fmt.Errorf("unknown ADD target: %s", p.curToken.Value) | |
| } | |
| } | |
| func (p *Parser) parseAdminDeleteCommands() (*Command, error) { | |
| p.nextToken() // consume DELETE | |
| switch p.curToken.Type { | |
| case TokenAPI: | |
| return p.parseDeleteAPIServer() | |
| case TokenAdmin: | |
| return p.parseDeleteAdminServer() | |
| case TokenProvider: | |
| return p.parseAdminDeleteProvider() | |
| default: | |
| return nil, fmt.Errorf("unknown DELETE target: %s", p.curToken.Value) | |
| } | |
| } |
🤖 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 1811 - 1823, In the
parseAdminDeleteCommands function, the error message returned in the default
case contains a copy-paste error. Change the error message from "unknown ADD
target" to "unknown DELETE target" to accurately reflect that this is a DELETE
command parser, not an ADD command parser.
| type UserIndexResponse struct { | ||
| InternalData CommonDataResponse | ||
| } | ||
|
|
||
| func (r *UserIndexResponse) TimeCost() float64 { | ||
| return r.InternalData.Duration | ||
| } |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Check usage of UserIndexResponse
rg -n 'UserIndexResponse' --type=goRepository: infiniflow/ragflow
Length of output: 199
🏁 Script executed:
#!/bin/bash
# Find the ResponseIf interface definition
rg -n 'type ResponseIf' --type=go -A 10
# Also check what other response types exist in the same file
rg -n 'type.*Response struct' internal/cli/response.goRepository: infiniflow/ragflow
Length of output: 1378
🏁 Script executed:
#!/bin/bash
# Check implementations of the interface in other response types
# Look at ModelsResponse, CommonDataResponse, and a few others to see if they implement all methods
echo "=== ModelsResponse implementation ==="
rg -n 'func \(r \*ModelsResponse\)' internal/cli/response.go
echo "=== CommonDataResponse implementation ==="
rg -n 'func \(r \*CommonDataResponse\)' internal/cli/response.go
echo "=== SimpleResponse implementation ==="
rg -n 'func \(r \*SimpleResponse\)' internal/cli/response.go
echo "=== Full UserIndexResponse definition ==="
sed -n '91,98p' internal/cli/response.goRepository: infiniflow/ragflow
Length of output: 1001
UserIndexResponse does not fully implement ResponseIf interface.
The ResponseIf interface requires Type(), PrintOut(), TimeCost(), and SetOutputFormat() methods. UserIndexResponse only implements TimeCost(), missing the other three methods required by the interface. Other response types in this file (e.g., ModelsResponse, CommonDataResponse, SimpleResponse) properly implement all four methods. Either implement the missing methods or clarify if this type is intentionally not a ResponseIf.
🤖 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/response.go` around lines 91 - 97, The UserIndexResponse struct
is missing three required methods to fully implement the ResponseIf interface.
In addition to the existing TimeCost() method, you need to implement Type(),
PrintOut(), and SetOutputFormat() methods for UserIndexResponse. Reference the
implementations in other response types like ModelsResponse, CommonDataResponse,
and SimpleResponse in the same file to understand the expected signatures and
behavior patterns, then add these three missing methods to UserIndexResponse
following the same pattern.
| userID := c.GetString("user_id") | ||
|
|
||
| errorCode, err := h.modelProviderService.DeleteModelProvider(providerName, userID) | ||
| errorCode, err := h.modelProviderService.DeleteModelProvider(userID, providerName) |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Description: Verify DeleteModelProvider signature and find all callers
# Find the service method definition
echo "=== DeleteModelProvider method definition ==="
ast-grep --pattern $'func ($$$) DeleteModelProvider($$$) ($$$) {
$$$
}'
# Find all callers of DeleteModelProvider
echo -e "\n=== All calls to DeleteModelProvider ==="
rg -nP '\bDeleteModelProvider\s*\(' -A 2 -B 2 --type=goRepository: infiniflow/ragflow
Length of output: 2405
Fix argument order in DeleteModelProvider call.
The handler is calling DeleteModelProvider(userID, providerName) but the service method signature at internal/service/model_service.go:190 expects DeleteModelProvider(providerName, userID string). Swap the argument order to match the signature. There are no other callers of this method that need the same fix.
🤖 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/handler/providers.go` at line 147, The DeleteModelProvider method
call in the handler is passing arguments in the wrong order. The
h.modelProviderService.DeleteModelProvider call currently passes userID first
and providerName second, but the service method signature expects providerName
as the first parameter and userID as the second parameter. Swap the order of the
arguments in the DeleteModelProvider call to match the service method signature.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #16243 +/- ##
==========================================
- Coverage 94.56% 93.16% -1.40%
==========================================
Files 10 10
Lines 717 717
Branches 118 118
==========================================
- Hits 678 668 -10
- Misses 25 29 +4
- Partials 14 20 +6 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
What problem does this PR solve?
Type of change