Go CLI: Add create and drop commands (#16430)

### What problem does this PR solve?

1. Add CREATE and DROP DATASET / MEMORY / AGENT / SEARCH / CHAT.
2. Add option to build.sh to strip RAGFlow binary.

### Type of change

- [x] Refactoring

---------

Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
Jin Hai
2026-06-29 11:13:14 +08:00
committed by GitHub
parent b6dbb2f71e
commit d4ef3d21d1
11 changed files with 530 additions and 203 deletions

View File

@@ -248,16 +248,10 @@ func (c *CLI) APISetLogLevelCommand(cmd *Command) (ResponseIf, error) {
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to register user: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
return nil, fmt.Errorf("failed to change log level: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result SimpleResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("change log level failed: invalid JSON (%w)", err)
}
result.Code = 0
result.Duration = resp.Duration
return &result, nil
return HandleSimpleResponse(resp, "change log level")
}
func (c *CLI) RegisterUser(cmd *Command) (ResponseIf, error) {
@@ -602,6 +596,50 @@ func (c *CLI) APIListSearchesCommand(cmd *Command) (ResponseIf, error) {
return &result, nil
}
// APIListMemoriesCommand lists memories
func (c *CLI) APIListMemoriesCommand(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != APIMode {
return nil, fmt.Errorf("this command is only allowed in USER mode")
}
httpClient := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer]
if httpClient.LoginToken == nil && !c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].useAPIKey {
return nil, fmt.Errorf("no authorization")
}
authKind := "web"
if httpClient.useAPIKey {
authKind = "api"
}
if httpClient.LoginToken != nil {
authKind = "web"
}
// Normal mode
resp, err := httpClient.Request("GET", "/memories", authKind, nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to list memories: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to list memories: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result ListMemoriesResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("list memories failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
// ListDatasetDocumentUserCommand lists dataset documents
func (c *CLI) ListDatasetDocumentUserCommand(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != APIMode {
@@ -1000,7 +1038,16 @@ func (c *CLI) APICreateDatasetCommand(cmd *Command) (ResponseIf, error) {
return nil, fmt.Errorf("no authorization")
}
resp, err := httpClient.Request("POST", "/datasets", "web", nil, nil)
datasetName, ok := cmd.Params["dataset_name"].(string)
if !ok {
return nil, fmt.Errorf("dataset_name parameter is required")
}
payload := map[string]interface{}{
"name": datasetName,
}
resp, err := httpClient.Request("POST", "/datasets", "web", nil, payload)
if err != nil {
return nil, fmt.Errorf("failed to create dataset: %w", err)
}
@@ -1009,20 +1056,7 @@ func (c *CLI) APICreateDatasetCommand(cmd *Command) (ResponseIf, error) {
return nil, fmt.Errorf("failed to create dataset: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var createResult CommonDataResponse
if err = json.Unmarshal(resp.Body, &createResult); err != nil {
return nil, fmt.Errorf("create dataset failed: invalid JSON (%w)", err)
}
if createResult.Code != 0 {
return nil, fmt.Errorf("error code: %d, message: %s", createResult.Code, createResult.Message)
}
var result SimpleResponse
result.Code = 0
result.Message = "Dataset created successfully"
result.Duration = resp.Duration
return &result, nil
return HandleSimpleResponse(resp, "create dataset")
}
func (c *CLI) APICreateAgentCommand(cmd *Command) (ResponseIf, error) {
@@ -1111,7 +1145,16 @@ func (c *CLI) APICreateSearchCommand(cmd *Command) (ResponseIf, error) {
return nil, fmt.Errorf("no authorization")
}
resp, err := httpClient.Request("POST", "/searches", "web", nil, nil)
searchName, ok := cmd.Params["search_name"].(string)
if !ok {
return nil, fmt.Errorf("search_name parameter is required")
}
payload := map[string]interface{}{
"name": searchName,
}
resp, err := httpClient.Request("POST", "/searches", "web", nil, payload)
if err != nil {
return nil, fmt.Errorf("failed to create search: %w", err)
}
@@ -1148,7 +1191,16 @@ func (c *CLI) APICreateMemoryCommand(cmd *Command) (ResponseIf, error) {
return nil, fmt.Errorf("no authorization")
}
resp, err := httpClient.Request("POST", "/memories", "web", nil, nil)
memoryName, ok := cmd.Params["memory_name"].(string)
if !ok {
return nil, fmt.Errorf("memory_name parameter is required")
}
payload := map[string]interface{}{
"name": memoryName,
}
resp, err := httpClient.Request("POST", "/memories", "web", nil, payload)
if err != nil {
return nil, fmt.Errorf("failed to create memory: %w", err)
}
@@ -1221,16 +1273,7 @@ func (c *CLI) APIDeleteAPIKeyCommand(cmd *Command) (ResponseIf, error) {
return nil, fmt.Errorf("failed to delete key: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result SimpleResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("delete key failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
return HandleSimpleResponse(resp, "delete key")
}
// APISetAPIKeyCommand sets the API key after validating it
@@ -1628,17 +1671,7 @@ func (c *CLI) AddProvider(cmd *Command) (ResponseIf, error) {
return nil, fmt.Errorf("failed to add provider: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result SimpleResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("add provider failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
return HandleSimpleResponse(resp, "add provider")
}
// APIListProviders lists added providers
@@ -1702,17 +1735,196 @@ func (c *CLI) DeleteProvider(cmd *Command) (ResponseIf, error) {
return nil, fmt.Errorf("failed to delete provider: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result SimpleResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("delete provider failed: invalid JSON (%w)", err)
return HandleSimpleResponse(resp, "delete provider")
}
// APIDropDatasetCommand DROP DATASET 'dataset_name'
func (c *CLI) APIDropDatasetCommand(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != APIMode {
return nil, fmt.Errorf("this command is only allowed in USER mode")
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
httpClient := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer]
if httpClient.LoginToken == nil && !c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].useAPIKey {
return nil, fmt.Errorf("no authorization")
}
result.Duration = resp.Duration
return &result, nil
datasetName, ok := cmd.Params["dataset_name"].(string)
if !ok {
return nil, fmt.Errorf("dataset_name parameter is required")
}
datasetID, err := c.getDatasetIDByName(datasetName)
if err != nil {
return nil, fmt.Errorf("failed to get dataset ID: %w by dataset name: %s", err, datasetName)
}
payload := map[string]interface{}{
"ids": []string{datasetID},
"delete_all": true,
}
resp, err := httpClient.Request("DELETE", "/datasets", "web", nil, payload)
if err != nil {
return nil, fmt.Errorf("failed to create dataset: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to create dataset: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
return HandleSimpleResponse(resp, "create provider instance")
}
// APIDropAgentCommand DROP AGENT 'agent_name'
func (c *CLI) APIDropAgentCommand(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != APIMode {
return nil, fmt.Errorf("this command is only allowed in USER mode")
}
httpClient := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer]
if httpClient.LoginToken == nil && !c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].useAPIKey {
return nil, fmt.Errorf("no authorization")
}
agentName, ok := cmd.Params["agent_name"].(string)
if !ok {
return nil, fmt.Errorf("agent_name parameter is required")
}
agentID, err := c.getAgentIDByName(agentName)
if err != nil {
return nil, fmt.Errorf("failed to get agent ID: %w by agent name: %s", err, agentName)
}
payload := map[string]interface{}{
"ids": []string{agentID},
"delete_all": true,
}
resp, err := httpClient.Request("DELETE", "/agents", "web", nil, payload)
if err != nil {
return nil, fmt.Errorf("failed to create agent: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to create agent: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
return HandleSimpleResponse(resp, "delete agent")
}
// APIDropChatCommand DROP CHAT 'chat_name'
func (c *CLI) APIDropChatCommand(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != APIMode {
return nil, fmt.Errorf("this command is only allowed in USER mode")
}
httpClient := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer]
if httpClient.LoginToken == nil && !c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].useAPIKey {
return nil, fmt.Errorf("no authorization")
}
chatName, ok := cmd.Params["chat_name"].(string)
if !ok {
return nil, fmt.Errorf("chat_name parameter is required")
}
chatID, err := c.getChatIDByName(chatName)
if err != nil {
return nil, fmt.Errorf("failed to get chat ID: %w by chat name: %s", err, chatName)
}
payload := map[string]interface{}{
"ids": []string{chatID},
"delete_all": true,
}
resp, err := httpClient.Request("DELETE", "/chats", "web", nil, payload)
if err != nil {
return nil, fmt.Errorf("failed to create chat: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to create chat: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
return HandleSimpleResponse(resp, "delete chat")
}
// APIDropSearchCommand DROP SEARCH 'search_name'
func (c *CLI) APIDropSearchCommand(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != APIMode {
return nil, fmt.Errorf("this command is only allowed in USER mode")
}
httpClient := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer]
if httpClient.LoginToken == nil && !c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].useAPIKey {
return nil, fmt.Errorf("no authorization")
}
searchName, ok := cmd.Params["search_name"].(string)
if !ok {
return nil, fmt.Errorf("search_name parameter is required")
}
searchID, err := c.getSearchIDByName(searchName)
if err != nil {
return nil, fmt.Errorf("failed to get search ID: %w by search name: %s", err, searchName)
}
endPoint := fmt.Sprintf("/searches/%s", searchID)
resp, err := httpClient.Request("DELETE", endPoint, "web", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to delete search: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to delete search: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
return HandleSimpleResponse(resp, "delete search")
}
// APIDropMemoryCommand DROP MEMORY 'memory_name'
func (c *CLI) APIDropMemoryCommand(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != APIMode {
return nil, fmt.Errorf("this command is only allowed in USER mode")
}
httpClient := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer]
if httpClient.LoginToken == nil && !c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].useAPIKey {
return nil, fmt.Errorf("no authorization")
}
memoryName, ok := cmd.Params["memory_name"].(string)
if !ok {
return nil, fmt.Errorf("memory_name parameter is required")
}
memoryID, err := c.getMemoryIDByName(memoryName)
if err != nil {
return nil, fmt.Errorf("failed to get memory ID: %w by memory name: %s", err, memoryName)
}
endPoint := fmt.Sprintf("/memories/%s", memoryID)
resp, err := httpClient.Request("DELETE", endPoint, "web", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to delete memory: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to delete memory: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
return HandleSimpleResponse(resp, "delete memory")
}
// APICreateProviderInstanceCommand creates a new provider instance
@@ -1770,17 +1982,7 @@ func (c *CLI) APICreateProviderInstanceCommand(cmd *Command) (ResponseIf, error)
return nil, fmt.Errorf("failed to create provider instance: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result SimpleResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("create provider instance failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
return HandleSimpleResponse(resp, "create provider instance")
}
// ShowInstanceBalance shows balance of a specific instance
@@ -1856,17 +2058,7 @@ func (c *CLI) DropProviderInstance(cmd *Command) (ResponseIf, error) {
return nil, fmt.Errorf("failed to drop instance: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result SimpleResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("drop instance failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
return HandleSimpleResponse(resp, "drop instance")
}
// DROP MODEL <name1 name2 name3> FROM <provider_name> <instance_name>
@@ -1899,24 +2091,14 @@ func (c *CLI) DropInstanceModel(cmd *Command) (ResponseIf, error) {
resp, err := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("DELETE", url, "web", nil, payload)
if err != nil {
return nil, fmt.Errorf("failed to drop instance: %w", err)
return nil, fmt.Errorf("failed to drop model: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to drop instance: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
return nil, fmt.Errorf("failed to drop model: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result SimpleResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("drop instance failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
return HandleSimpleResponse(resp, "drop model")
}
func isValidURL(str string) bool {
@@ -3043,16 +3225,8 @@ func (c *CLI) AddCustomModel(cmd *Command) (ResponseIf, error) {
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to add custom model: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result SimpleResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("add custom model failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
return HandleSimpleResponse(resp, "add custom model")
}
// InsertChunksFromFile inserts chunks from a JSON file
@@ -3522,17 +3696,7 @@ func (c *CLI) ParseDocumentsUserCommand(cmd *Command) (ResponseIf, error) {
return nil, fmt.Errorf("failed to list documents: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result SimpleResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("list documents failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
return HandleSimpleResponse(resp, "list documents")
}
func (c *CLI) UserParseLocalFile(cmd *Command) (ResponseIf, error) {