Add show / set role default models (#16240)

### What problem does this PR solve?

```
RAGFlow(admin)> show role 'user' default models;
+--------------------------+-----------------------------------------------------------------+-----------+
| command                  | error                                                           | role_name |
+--------------------------+-----------------------------------------------------------------+-----------+
| show_role_default_models | 'show role default models' is implemented in enterprise edition | user      |
+--------------------------+-----------------------------------------------------------------+-----------+

RAGFlow(admin)> set role 'user' default chat 'glm4.5@test@zhipu-ai';
+------------+---------------------------------------------------------------+
| field      | value                                                         |
+------------+---------------------------------------------------------------+
| model_id   |                                                               |
| model_type | chat                                                          |
| role_name  | user                                                          |
| command    | set_role_default_model                                        |
| error      | 'set role default model' is implemented in enterprise edition |
+------------+---------------------------------------------------------------+

RAGFlow(admin)> reset role 'user' default chat;
+------------+-----------------------------------------------------------------+
| field      | value                                                           |
+------------+-----------------------------------------------------------------+
| command    | reset_role_default_model                                        |
| error      | 'reset role default model' is implemented in enterprise edition |
| model_type | chat                                                            |
| role_name  | user                                                            |
+------------+-----------------------------------------------------------------+

```

---------

Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
Jin Hai
2026-06-22 19:03:36 +08:00
committed by GitHub
parent 9eb7cee473
commit 0e6b28a7fe
11 changed files with 428 additions and 299 deletions

View File

@@ -1074,6 +1074,107 @@ func (c *CLI) SetVariable(cmd *Command) (ResponseIf, error) {
return &result, nil
}
// AdminSetRoleDefaultModelsCommand set role default models (admin mode only).
func (c *CLI) AdminSetRoleDefaultModelsCommand(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil {
return nil, fmt.Errorf("this command is only allowed in ADMIN mode or already login")
}
roleName, ok := cmd.Params["role_name"].(string)
if !ok {
return nil, fmt.Errorf("role_name not provided")
}
modelType, ok := cmd.Params["model_type"].(string)
if !ok {
return nil, fmt.Errorf("model_type not provided")
}
payload := map[string]interface{}{
"model_type": modelType,
}
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")
}
}
endPoint := fmt.Sprintf("/admin/roles/%s/default-models", roleName)
resp, err := c.AdminServerClient.Request("PATCH", endPoint, "admin", nil, payload)
if err != nil {
return nil, fmt.Errorf("failed to set role default models: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to set role default models: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result CommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("set role default models failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
// AdminResetRoleDefaultModelsCommand reset role default models (admin mode only).
func (c *CLI) AdminResetRoleDefaultModelsCommand(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil {
return nil, fmt.Errorf("this command is only allowed in ADMIN mode or already login")
}
roleName, ok := cmd.Params["role_name"].(string)
if !ok {
return nil, fmt.Errorf("role_name not provided")
}
modelType, ok := cmd.Params["model_type"].(string)
if !ok {
return nil, fmt.Errorf("model_type not provided")
}
payload := map[string]interface{}{
"model_type": modelType,
}
endPoint := fmt.Sprintf("/admin/roles/%s/default-models", roleName)
resp, err := c.AdminServerClient.Request("DELETE", endPoint, "admin", nil, payload)
if err != nil {
return nil, fmt.Errorf("failed to reset role default models: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to reset role default models: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result CommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("reset role default models failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
// AdminDropUserCommand deletes a user (admin mode only)
func (c *CLI) AdminDropUserCommand(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil {
@@ -1731,6 +1832,38 @@ func (c *CLI) AdminShowRoleCommand(cmd *Command) (ResponseIf, error) {
return &result, nil
}
// AdminShowRoleDefaultModelsCommand show role default models command (admin mode only)
func (c *CLI) AdminShowRoleDefaultModelsCommand(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil {
return nil, fmt.Errorf("this command is only allowed in ADMIN mode or already login")
}
roleName := cmd.Params["role_name"].(string)
endPoint := fmt.Sprintf("/admin/roles/%s/default-models", roleName)
resp, err := c.AdminServerClient.Request("GET", endPoint, "admin", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to show role default models: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to show role default models: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result CommonResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("show role default models failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
func (c *CLI) AdminShowUserActivityCommand(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil {