Go CLI: admin list providers (#16243)

### What problem does this PR solve?

```
RAGFlow(admin)> list providers;
+----------------------+-------------------------------------------------------------+
| command              | error                                                       |
+----------------------+-------------------------------------------------------------+
| list_model_providers | 'list model providers' is implemented in enterprise edition |
+----------------------+-------------------------------------------------------------+

RAGFlow(admin)> add provider 'zhipu-ai';
+-------------+-----------------------------------------------------------+
| field       | value                                                     |
+-------------+-----------------------------------------------------------+
| command     | add_model_provider                                        |
| error       | 'add model provider' is implemented in enterprise edition |
| provider_id | admin                                                     |
| user_id     | zhipu-ai                                                  |
+-------------+-----------------------------------------------------------+

RAGFlow(admin)> delete provider 'zhipu-ai';
+-------------+--------------------------------------------------------------+
| field       | value                                                        |
+-------------+--------------------------------------------------------------+
| command     | delete_model_provider                                        |
| error       | 'delete model provider' is implemented in enterprise edition |
| provider_id | admin                                                        |
| user_id     | zhipu-ai                                                     |
+-------------+--------------------------------------------------------------+

RAGFlow(admin)> add provider 'zhipu-ai' instance 'instance1';
+---------------+-----------------------------------------------------------+
| field         | value                                                     |
+---------------+-----------------------------------------------------------+
| command       | add_model_instance                                        |
| error         | 'add model instance' is implemented in enterprise edition |
| instance_name | instance1                                                 |
| provider_id   | zhipu-ai                                                  |
| user_id       | admin                                                     |
+---------------+-----------------------------------------------------------+

RAGFlow(admin)> delete provider 'zhipu-ai' instance 'test'
+-------------+--------------------------------------------------------------+
| field       | value                                                        |
+-------------+--------------------------------------------------------------+
| instances   | [test]                                                       |
| provider_id | zhipu-ai                                                     |
| user_id     | admin                                                        |
| command     | delete_model_provider                                        |
| error       | 'delete model instance' is implemented in enterprise edition |
+-------------+--------------------------------------------------------------+

RAGFlow(admin)> add provider 'zhipu-ai' instance 'instance1' model 'xxx';
+---------------+--------------------------------------------------+
| field         | value                                            |
+---------------+--------------------------------------------------+
| command       | add_model                                        |
| error         | 'add model' is implemented in enterprise edition |
| instance_name | instance1                                        |
| model_names   | [xxx]                                            |
| provider_id   | zhipu-ai                                         |
| user_id       | admin                                            |
+---------------+--------------------------------------------------+

RAGFlow(admin)> delete provider 'zhipu-ai' instance 'test' model 'xxx';
+---------------+------------------------------------------------------+
| field         | value                                                |
+---------------+------------------------------------------------------+
| command       | delete_model_provider                                |
| error         | 'delete models' is implemented in enterprise edition |
| instance_name | test                                                 |
| models        | [xxx]                                                |
| provider_id   | zhipu-ai                                             |
| user_id       | admin                                                |
+---------------+------------------------------------------------------+

```

### Type of change

- [x] New Feature (non-breaking change which adds functionality)

---------

Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
Jin Hai
2026-06-23 10:26:31 +08:00
committed by GitHub
parent 06ededb26a
commit b661e9c19e
11 changed files with 790 additions and 65 deletions

View File

@@ -150,6 +150,36 @@ func (c *CLI) AdminListRolesCommand(cmd *Command) (ResponseIf, error) {
return &result, nil
}
// AdminListProvidersCommand to list providers command (admin mode only)
func (c *CLI) AdminListProvidersCommand(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")
}
apiURL := fmt.Sprintf("/admin/providers")
resp, err := c.AdminServerClient.Request("GET", apiURL, "admin", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to list providers: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to list providers: 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("list providers failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
// AdminCreateRoleCommand creates a new role (admin mode only)
func (c *CLI) AdminCreateRoleCommand(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil {
@@ -3194,3 +3224,273 @@ func (c *CLI) AdminRemoveUserIngestionTasksCommand(cmd *Command) (ResponseIf, er
result.Duration = resp.Duration
return &result, nil
}
// AdminAddProviderCommand add provider
func (c *CLI) AdminAddProviderCommand(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")
}
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider_name not provided")
}
payload := map[string]interface{}{
"provider_name": providerName,
}
apiURL := fmt.Sprintf("/admin/providers")
resp, err := c.AdminServerClient.Request("POST", apiURL, "admin", nil, payload)
if err != nil {
return nil, fmt.Errorf("failed to add provider %s: %w", providerName, err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to add provider %s: HTTP %d, body: %s", providerName, resp.StatusCode, string(resp.Body))
}
var result CommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("add provider %s failed: invalid JSON (%w)", providerName, err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
// AdminAddModelInstanceCommand add model instance
func (c *CLI) AdminAddModelInstanceCommand(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")
}
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider_name not provided")
}
instanceName, ok := cmd.Params["instance_name"].(string)
if !ok {
return nil, fmt.Errorf("instance_name not provided")
}
payload := map[string]interface{}{
"instance_name": instanceName,
}
apiURL := fmt.Sprintf("/admin/providers/%s/instances", providerName)
resp, err := c.AdminServerClient.Request("POST", apiURL, "admin", nil, payload)
if err != nil {
return nil, fmt.Errorf("failed to add model instance %s: %w", instanceName, err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to add model instance %s: HTTP %d, body: %s", instanceName, resp.StatusCode, string(resp.Body))
}
var result CommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("add model instance %s failed: invalid JSON (%w)", instanceName, err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
// AdminAddModelsCommand add models
func (c *CLI) AdminAddModelsCommand(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")
}
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider_name not provided")
}
instanceName, ok := cmd.Params["instance_name"].(string)
if !ok {
return nil, fmt.Errorf("instance_name not provided")
}
modelNames, ok := cmd.Params["model_names"].([]string)
if !ok {
return nil, fmt.Errorf("model_names not provided")
}
payload := map[string]interface{}{
"model_names": modelNames,
}
apiURL := fmt.Sprintf("/admin/providers/%s/instances/%s/models", providerName, instanceName)
resp, err := c.AdminServerClient.Request("POST", apiURL, "admin", nil, payload)
if err != nil {
return nil, fmt.Errorf("failed to add models %s: %w", modelNames, err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to add models %s: HTTP %d, body: %s", modelNames, resp.StatusCode, string(resp.Body))
}
var result CommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("add models %s failed: invalid JSON (%w)", modelNames, err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
// AdminDeleteProvidersCommand delete providers
func (c *CLI) AdminDeleteProvidersCommand(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")
}
providerNames, ok := cmd.Params["provider_names"].([]string)
if !ok {
return nil, fmt.Errorf("provider_names not provided")
}
payload := map[string]interface{}{
"provider_names": providerNames,
}
apiURL := fmt.Sprintf("/admin/providers/")
resp, err := c.AdminServerClient.Request("DELETE", apiURL, "admin", nil, payload)
if err != nil {
return nil, fmt.Errorf("failed to remove providers %s: %w", providerNames, err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to remove providers %s: HTTP %d, body: %s", providerNames, resp.StatusCode, string(resp.Body))
}
var result CommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("remove providers %s failed: invalid JSON (%w)", providerNames, err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
// AdminDeleteInstancesCommand delete instances
func (c *CLI) AdminDeleteInstancesCommand(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")
}
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider_name not provided")
}
instanceNames, ok := cmd.Params["instance_names"].([]string)
if !ok {
return nil, fmt.Errorf("instance_name not provided")
}
payload := map[string]interface{}{
"instance_names": instanceNames,
}
apiURL := fmt.Sprintf("/admin/providers/%s/instances", providerName)
resp, err := c.AdminServerClient.Request("DELETE", apiURL, "admin", nil, payload)
if err != nil {
return nil, fmt.Errorf("failed to remove instance %s: %w", instanceNames, err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to remove instance %s: HTTP %d, body: %s", instanceNames, resp.StatusCode, string(resp.Body))
}
var result CommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("remove instance %s failed: invalid JSON (%w)", instanceNames, err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
// AdminDeleteModelsCommand delete models
func (c *CLI) AdminDeleteModelsCommand(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")
}
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider_name not provided")
}
instanceName, ok := cmd.Params["instance_name"].(string)
if !ok {
return nil, fmt.Errorf("instance_name not provided")
}
modelNames, ok := cmd.Params["model_names"].([]string)
if !ok {
return nil, fmt.Errorf("model_names not provided")
}
payload := map[string]interface{}{
"model_names": modelNames,
}
apiURL := fmt.Sprintf("/admin/providers/%s/instances/%s/models", providerName, instanceName)
resp, err := c.AdminServerClient.Request("DELETE", apiURL, "admin", nil, payload)
if err != nil {
return nil, fmt.Errorf("failed to remove model %s: %w", modelNames, err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to remove model %s: HTTP %d, body: %s", modelNames, resp.StatusCode, string(resp.Body))
}
var result CommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("remove model %s failed: invalid JSON (%w)", modelNames, err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}

View File

@@ -42,7 +42,8 @@ func (p *Parser) parseAdminLoginUser() (*Command, error) {
// Optional: PASSWORD 'password'
if p.curToken.Type == TokenPassword {
p.nextToken()
password, err := p.parseQuotedString()
var password string
password, err = p.parseQuotedString()
if err != nil {
return nil, err
}
@@ -81,7 +82,7 @@ func (p *Parser) parseAdminPingServer() (*Command, error) {
// endregion
// region LIST commands
func (p *Parser) parseAdminListCommand() (*Command, error) {
func (p *Parser) parseAdminListCommands() (*Command, error) {
p.nextToken() // consume LIST
switch p.curToken.Type {
@@ -103,6 +104,8 @@ func (p *Parser) parseAdminListCommand() (*Command, error) {
return p.parseListAvailableProviders()
case TokenProvider:
return p.parseAdminListProviderModels()
case TokenProviders:
return p.parseAdminListProviders()
case TokenModels:
return p.parseAdminListModels()
case TokenUser:
@@ -263,6 +266,16 @@ func (p *Parser) parseAdminListProviderModels() (*Command, error) {
return cmd, nil
}
// parseAdminListProviders parses LIST PROVIDERS command
func (p *Parser) parseAdminListProviders() (*Command, error) {
p.nextToken() // consume PROVIDERS
// Semicolon is optional
if p.curToken.Type == TokenSemicolon {
p.nextToken()
}
return NewCommand("admin_list_providers"), nil
}
func (p *Parser) parseAdminListModels() (*Command, error) {
p.nextToken() // consume MODELS
cmd := NewCommand("admin_list_all_models")
@@ -1007,29 +1020,6 @@ func (p *Parser) parseAdminDropRole() (*Command, error) {
return cmd, nil
}
func (p *Parser) parseAdminDropModelProvider() (*Command, error) {
p.nextToken() // consume MODEL
if p.curToken.Type != TokenProvider {
return nil, fmt.Errorf("expected PROVIDER")
}
p.nextToken()
providerName, err := p.parseQuotedString()
if err != nil {
return nil, err
}
cmd := NewCommand("drop_model_provider")
cmd.Params["provider_name"] = providerName
p.nextToken()
// Semicolon is optional
if p.curToken.Type == TokenSemicolon {
p.nextToken()
}
return cmd, nil
}
func (p *Parser) parseAdminDropDataset() (*Command, error) {
p.nextToken() // consume DATASET
datasetName, err := p.parseQuotedString()
@@ -1741,23 +1731,171 @@ func (p *Parser) parseAdminAddCommand() (*Command, error) {
return p.parseAddAPIServer()
case TokenAdmin:
return p.parseAddAdminServer()
case TokenProvider:
return p.parseAdminAddModelProvider()
default:
return nil, fmt.Errorf("unknown ADD target: %s", p.curToken.Value)
}
}
func (p *Parser) parseAdminDeleteCommand() (*Command, error) {
// ADD PROVIDER <name>
// ADD PROVIDER <name> INSTANCE <name>
func (p *Parser) parseAdminAddModelProvider() (*Command, error) {
p.nextToken() // consume PROVIDER
providerName, err := p.parseQuotedString()
if err != nil {
return nil, fmt.Errorf("expected provider name: %w", err)
}
p.nextToken()
if p.curToken.Type == TokenInstance {
return p.parseAdminAddModelInstance(providerName)
}
cmd := NewCommand("admin_add_provider")
cmd.Params["provider_name"] = providerName
// Semicolon is optional
if p.curToken.Type == TokenSemicolon {
p.nextToken()
}
return cmd, nil
}
func (p *Parser) parseAdminAddModelInstance(providerName string) (*Command, error) {
p.nextToken() // consume INSTANCE
instanceName, err := p.parseQuotedString()
if err != nil {
return nil, fmt.Errorf("expected model instance name: %w", err)
}
p.nextToken()
if p.curToken.Type == TokenModel {
return p.parseAdminAddModel(providerName, instanceName)
}
cmd := NewCommand("admin_add_model_instance")
cmd.Params["provider_name"] = providerName
cmd.Params["instance_name"] = instanceName
// Semicolon is optional
if p.curToken.Type == TokenSemicolon {
p.nextToken()
}
return cmd, nil
}
func (p *Parser) parseAdminAddModel(providerName, instanceName string) (*Command, error) {
p.nextToken() // consume MODEL
modelName, err := p.parseQuotedString()
if err != nil {
return nil, fmt.Errorf("expected model name: %w", err)
}
p.nextToken()
cmd := NewCommand("admin_add_models")
cmd.Params["provider_name"] = providerName
cmd.Params["instance_name"] = instanceName
cmd.Params["model_names"] = []string{modelName}
// Semicolon is optional
if p.curToken.Type == TokenSemicolon {
p.nextToken()
}
return cmd, nil
}
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)
}
}
// DELETE PROVIDER <name> command
// DELETE PROVIDER <name> INSTANCE <name> command
// DELETE PROVIDER <name> INSTANCE <name> MODEL <name>
func (p *Parser) parseAdminDeleteProvider() (*Command, error) {
p.nextToken() // consume PROVIDER
providerName, err := p.parseQuotedString()
if err != nil {
return nil, fmt.Errorf("expected provider name: %w", err)
}
p.nextToken()
if p.curToken.Type == TokenInstance {
return p.parseAdminDeleteModelInstance(providerName)
}
cmd := NewCommand("admin_delete_model_providers")
cmd.Params["provider_names"] = []string{providerName}
// Semicolon is optional
if p.curToken.Type == TokenSemicolon {
p.nextToken()
}
return cmd, nil
}
// DELETE PROVIDER <name> INSTANCE <name> command
// DELETE PROVIDER <name> INSTANCE <name> MODEL <name>
func (p *Parser) parseAdminDeleteModelInstance(providerName string) (*Command, error) {
p.nextToken() // consume INSTANCE
instanceName, err := p.parseQuotedString()
if err != nil {
return nil, fmt.Errorf("expected model instance name: %w", err)
}
p.nextToken()
if p.curToken.Type == TokenModel {
return p.parseAdminDeleteModel(providerName, instanceName)
}
cmd := NewCommand("admin_delete_model_instance")
cmd.Params["provider_name"] = providerName
cmd.Params["instance_names"] = []string{instanceName}
// Semicolon is optional
if p.curToken.Type == TokenSemicolon {
p.nextToken()
}
return cmd, nil
}
// DELETE PROVIDER <name> INSTANCE <name> MODEL <name>
func (p *Parser) parseAdminDeleteModel(providerName, instanceName string) (*Command, error) {
p.nextToken() // consume MODEL
modelName, err := p.parseQuotedString()
if err != nil {
return nil, fmt.Errorf("expected model name: %w", err)
}
p.nextToken()
cmd := NewCommand("admin_delete_model")
cmd.Params["provider_name"] = providerName
cmd.Params["instance_name"] = instanceName
cmd.Params["model_names"] = []string{modelName}
// Semicolon is optional
if p.curToken.Type == TokenSemicolon {
p.nextToken()
}
return cmd, nil
}
func (p *Parser) parseAdminSaveCommand() (*Command, error) {
p.nextToken() // consume SAVE
switch p.curToken.Type {

View File

@@ -125,6 +125,8 @@ func (c *CLI) ExecuteAdminCommand(cmd *Command) (ResponseIf, error) {
return c.ListInstanceModels(cmd)
case "admin_show_model":
return c.CommonShowModel(cmd)
case "admin_list_providers":
return c.AdminListProvidersCommand(cmd)
case "admin_list_all_models":
return c.ListAllModels(cmd)
case "list_admin_tasks":
@@ -227,7 +229,19 @@ func (c *CLI) ExecuteAdminCommand(cmd *Command) (ResponseIf, error) {
return c.AdminStopUserIngestionTasksCommand(cmd)
case "admin_remove_user_ingestion_tasks_command":
return c.AdminRemoveUserIngestionTasksCommand(cmd)
// TODO: Implement other commands
case "admin_add_provider":
return c.AdminAddProviderCommand(cmd)
case "admin_add_model_instance":
return c.AdminAddModelInstanceCommand(cmd)
case "admin_add_models":
return c.AdminAddModelsCommand(cmd)
case "admin_delete_model_providers":
return c.AdminDeleteProvidersCommand(cmd)
case "admin_delete_model_instance":
return c.AdminDeleteInstancesCommand(cmd)
case "admin_delete_model":
return c.AdminDeleteModelsCommand(cmd)
// TODO: Implement other commands
case "show_admin_server":
return c.ShowAdminServer(cmd)
case "show_api_server":

View File

@@ -87,7 +87,7 @@ func (p *Parser) parseAdminCommand() (*Command, error) {
case TokenPing:
return p.parseAdminPingServer()
case TokenList:
return p.parseAdminListCommand()
return p.parseAdminListCommands()
case TokenShow:
return p.parseAdminShowCommands()
case TokenCheck:
@@ -123,7 +123,7 @@ func (p *Parser) parseAdminCommand() (*Command, error) {
case TokenAdd:
return p.parseAdminAddCommand()
case TokenDelete:
return p.parseAdminDeleteCommand()
return p.parseAdminDeleteCommands()
case TokenSave:
return p.parseAdminSaveCommand()
case TokenUse:

View File

@@ -88,6 +88,14 @@ func (r *ModelsResponse) PrintOut() {
}
}
type UserIndexResponse struct {
InternalData CommonDataResponse
}
func (r *UserIndexResponse) TimeCost() float64 {
return r.InternalData.Duration
}
type CommonDataResponse struct {
Code int `json:"code"`
Data map[string]interface{} `json:"data"`

View File

@@ -1105,7 +1105,6 @@ func (c *CLI) DropMetadataStore(cmd *Command) (ResponseIf, error) {
// AddProvider creates a new model provider
// ADD PROVIDER <name>
// ADD PROVIDER <name> <api_key>
func (c *CLI) AddProvider(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != APIMode {
return nil, fmt.Errorf("this command is only allowed in USER mode")

View File

@@ -736,7 +736,6 @@ func (p *Parser) parseCreateModelProvider() (*Command, error) {
// parseAddProvider parses ADD PROVIDER commands
// ADD PROVIDER <name>
// ADD PROVIDER <name> <api_key>
func (p *Parser) parseAddProvider() (*Command, error) {
p.nextToken() // consume PROVIDER
@@ -750,16 +749,6 @@ func (p *Parser) parseAddProvider() (*Command, error) {
p.nextToken()
// Check if api_key is provided (optional)
if p.curToken.Type == TokenQuotedString {
apiKey, err := p.parseQuotedString()
if err != nil {
return nil, fmt.Errorf("expected api key: %w", err)
}
cmd.Params["api_key"] = apiKey
p.nextToken()
}
// Semicolon is optional
if p.curToken.Type == TokenSemicolon {
p.nextToken()