Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Go cli: refactor (#20249)

Jin Hai950b0fa4a3df
diff --git a/internal/cli/admin_command.go b/internal/cli/admin_command.go
index 4f2e11058e..551df0fc76 100644
--- a/internal/cli/admin_command.go
+++ b/internal/cli/admin_command.go
@@ -20,12 +20,12 @@ import (
"encoding/json"
"fmt"
"net/url"
+ "ragflow/internal/cli/utils"
"ragflow/internal/common"
"strconv"
)
-// PingServer pings the server to check if it's alive
-// Returns benchmark result map if iterations > 1, otherwise prints status
+// PingAdmin pings the server to check if it's alive
func (c *CLI) PingAdmin(commandCount int, cmd *Command) (ResponseIf, error) {
// Single mode
resp, err := c.AdminServerClient.Request(commandCount, "GET", "/admin/ping", "web", nil, nil)
@@ -206,7 +206,7 @@ func (c *CLI) AdminDropRoleCommand(commandCount int, cmd *Command) (ResponseIf,
return nil, fmt.Errorf("role_name not provided")
}
- resp, err := c.AdminServerClient.Request(commandCount, "DELETE", fmt.Sprintf("/admin/roles/%s", roleName), "admin", nil, nil)
+ resp, err := c.AdminServerClient.Request(commandCount, "DELETE", utils.APIPath("/admin/roles", roleName), "admin", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to drop role: %w", err)
}
@@ -233,7 +233,7 @@ func (c *CLI) AdminAlterRole(commandCount int, cmd *Command) (ResponseIf, error)
payload["description"] = description
}
- resp, err := c.AdminServerClient.Request(commandCount, "PUT", fmt.Sprintf("/admin/roles/%s", roleName), "admin", nil, payload)
+ resp, err := c.AdminServerClient.Request(commandCount, "PUT", utils.APIPath("/admin/roles", roleName), "admin", nil, payload)
if err != nil {
return nil, fmt.Errorf("failed to alter role: %w", err)
}
@@ -311,7 +311,7 @@ func (c *CLI) AdminGrantRolePermissionCommand(commandCount int, cmd *Command) (R
"resource": resource,
}
- apiURL := fmt.Sprintf("/admin/roles/%s/permission", roleName)
+ apiURL := utils.APIPath("/admin/roles", roleName, "permission")
resp, err := c.AdminServerClient.Request(commandCount, "POST", apiURL, "admin", nil, payload)
if err != nil {
@@ -347,7 +347,7 @@ func (c *CLI) AdminRevokeRolePermissionCommand(commandCount int, cmd *Command) (
"resource": resource,
}
- apiURL := fmt.Sprintf("/admin/roles/%s/permission", roleName)
+ apiURL := utils.APIPath("/admin/roles", roleName, "permission")
resp, err := c.AdminServerClient.Request(commandCount, "DELETE", apiURL, "admin", nil, payload)
if err != nil {
@@ -368,7 +368,7 @@ func (c *CLI) AdminShowRolePermissionCommand(commandCount int, cmd *Command) (Re
return nil, fmt.Errorf("role_name not provided")
}
- apiURL := fmt.Sprintf("/admin/roles/%s/permission", roleName)
+ apiURL := utils.APIPath("/admin/roles", roleName, "permission")
resp, err := c.AdminServerClient.Request(commandCount, "GET", apiURL, "admin", nil, nil)
if err != nil {
@@ -569,7 +569,7 @@ func (c *CLI) AdminStartServiceCommand(commandCount int, cmd *Command) (Response
return nil, fmt.Errorf("service_name not provided")
}
- endPoint := fmt.Sprintf("/admin/services/%s", serviceName)
+ endPoint := utils.APIPath("/admin/services", serviceName)
resp, err := c.AdminServerClient.Request(commandCount, "POST", endPoint, "admin", nil, nil)
if err != nil {
@@ -590,7 +590,7 @@ func (c *CLI) AdminRestartServiceCommand(commandCount int, cmd *Command) (Respon
return nil, fmt.Errorf("service_name not provided")
}
- endPoint := fmt.Sprintf("/admin/services/%s", serviceName)
+ endPoint := utils.APIPath("/admin/services", serviceName)
resp, err := c.AdminServerClient.Request(commandCount, "PUT", endPoint, "admin", nil, nil)
if err != nil {
@@ -611,7 +611,7 @@ func (c *CLI) AdminShutdownServiceCommand(commandCount int, cmd *Command) (Respo
return nil, fmt.Errorf("service_name not provided")
}
- endPoint := fmt.Sprintf("/admin/services/%s", serviceName)
+ endPoint := utils.APIPath("/admin/services", serviceName)
resp, err := c.AdminServerClient.Request(commandCount, "DELETE", endPoint, "admin", nil, nil)
if err != nil {
@@ -632,7 +632,7 @@ func (c *CLI) AdminShowService(commandCount int, cmd *Command) (ResponseIf, erro
return nil, fmt.Errorf("service_name not provided")
}
- endPoint := fmt.Sprintf("/admin/services/%s", serviceName)
+ endPoint := utils.APIPath("/admin/services", serviceName)
resp, err := c.AdminServerClient.Request(commandCount, "GET", endPoint, "admin", nil, nil)
if err != nil {
@@ -907,7 +907,7 @@ func (c *CLI) AdminSetRoleDefaultModelsCommand(commandCount int, cmd *Command) (
}
}
- endPoint := fmt.Sprintf("/admin/roles/%s/default-models", roleName)
+ endPoint := utils.APIPath("/admin/roles", roleName, "default-models")
resp, err := c.AdminServerClient.Request(commandCount, "PATCH", endPoint, "admin", nil, payload)
if err != nil {
@@ -960,7 +960,7 @@ func (c *CLI) AdminResetRoleDefaultModelsCommand(commandCount int, cmd *Command)
"model_type": modelType,
}
- endPoint := fmt.Sprintf("/admin/roles/%s/default-models", roleName)
+ endPoint := utils.APIPath("/admin/roles", roleName, "default-models")
resp, err := c.AdminServerClient.Request(commandCount, "DELETE", endPoint, "admin", nil, payload)
if err != nil {
@@ -1398,7 +1398,7 @@ func (c *CLI) AdminShowRoleCommand(commandCount int, cmd *Command) (ResponseIf,
roleName := cmd.Params["role_name"].(string)
- endPoint := fmt.Sprintf("/admin/roles/%s/", roleName)
+ endPoint := utils.APIPath("/admin/roles", roleName) + "/"
resp, err := c.AdminServerClient.Request(commandCount, "GET", endPoint, "admin", nil, nil)
if err != nil {
@@ -1416,7 +1416,7 @@ func (c *CLI) AdminShowRoleDefaultModelsCommand(commandCount int, cmd *Command)
roleName := cmd.Params["role_name"].(string)
- endPoint := fmt.Sprintf("/admin/roles/%s/default-models", roleName)
+ endPoint := utils.APIPath("/admin/roles", roleName, "default-models")
resp, err := c.AdminServerClient.Request(commandCount, "GET", endPoint, "admin", nil, nil)
if err != nil {
@@ -2480,7 +2480,7 @@ func (c *CLI) AdminListUserProviderInstancesCommand(commandCount int, cmd *Comma
}
encodedUserName := url.PathEscape(userName)
- apiURL := fmt.Sprintf("/admin/users/%s/providers/%s/instances", encodedUserName, providerName)
+ apiURL := fmt.Sprintf("/admin/users/%s/%s", encodedUserName, utils.APIPath("providers", providerName, "instances"))
resp, err := c.AdminServerClient.Request(commandCount, "GET", apiURL, "admin", nil, nil)
if err != nil {
@@ -2510,7 +2510,7 @@ func (c *CLI) AdminListUserProviderInstanceModelsCommand(commandCount int, cmd *
}
encodedUserName := url.PathEscape(userName)
- apiURL := fmt.Sprintf("/admin/users/%s/providers/%s/instances/%s/models", encodedUserName, providerName, instanceName)
+ apiURL := fmt.Sprintf("/admin/users/%s/%s", encodedUserName, utils.APIPath("providers", providerName, "instances", instanceName, "models"))
resp, err := c.AdminServerClient.Request(commandCount, "GET", apiURL, "admin", nil, nil)
if err != nil {
diff --git a/internal/cli/benchmark.go b/internal/cli/benchmark.go
index 050311d3bf..38e5df7367 100644
--- a/internal/cli/benchmark.go
+++ b/internal/cli/benchmark.go
@@ -18,6 +18,7 @@ package cli
import (
"fmt"
+ "ragflow/internal/cli/utils"
"strings"
"sync"
"time"
@@ -229,7 +230,7 @@ func (c *CLI) executeBenchmarkSilent(cmd *Command, iterations int) []*Response {
resp, err = httpClient.Request(1, "POST", "/kb/list", "web", nil, nil)
case "api_list_datasets":
userName, _ := cmd.Params["user_name"].(string)
- resp, err = httpClient.Request(1, "GET", fmt.Sprintf("/admin/users/%s/datasets", userName), "admin", nil, nil)
+ resp, err = httpClient.Request(1, "GET", utils.APIPath("/admin/users", userName, "datasets"), "admin", nil, nil)
case "search_on_datasets":
question, _ := cmd.Params["question"].(string)
datasetIDs, _ := cmd.Params["dataset_ids"].([]string)
diff --git a/internal/cli/common_command.go b/internal/cli/common_command.go
index 369e66a058..e17d2b85f6 100644
--- a/internal/cli/common_command.go
+++ b/internal/cli/common_command.go
@@ -23,6 +23,7 @@ import (
"fmt"
"net/http"
"os"
+ "ragflow/internal/cli/utils"
"ragflow/internal/common"
"strings"
"time"
@@ -302,11 +303,11 @@ func (c *CLI) CommonShowProviderCommand(commandCount int, cmd *Command) (Respons
switch c.Config.CLIMode {
case AdminMode:
httpClient := c.AdminServerClient
- endPoint = fmt.Sprintf("/admin/providers/%s", providerName)
+ endPoint = utils.APIPath("/admin/providers", providerName)
resp, err = httpClient.Request(commandCount, "GET", endPoint, httpClient.AuthKind(), nil, nil)
case APIMode:
httpClient := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer]
- endPoint = fmt.Sprintf("/providers/%s", providerName)
+ endPoint = utils.APIPath("/providers", providerName)
resp, err = httpClient.Request(commandCount, "GET", endPoint, httpClient.AuthKind(), nil, nil)
default:
return nil, fmt.Errorf("invalid server type")
@@ -337,11 +338,11 @@ func (c *CLI) CommonShowProviderInstanceCommand(commandCount int, cmd *Command)
switch c.Config.CLIMode {
case AdminMode:
httpClient := c.AdminServerClient
- endPoint = fmt.Sprintf("/admin/providers/%s/instances/%s", providerName, instanceName)
+ endPoint = utils.APIPath("/admin/providers", providerName, "instances", instanceName)
resp, err = httpClient.Request(commandCount, "GET", endPoint, httpClient.AuthKind(), nil, nil)
case APIMode:
httpClient := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer]
- endPoint = fmt.Sprintf("/providers/%s/instances/%s", providerName, instanceName)
+ endPoint = utils.APIPath("/providers", providerName, "instances", instanceName)
resp, err = httpClient.Request(commandCount, "GET", endPoint, httpClient.AuthKind(), nil, nil)
default:
return nil, fmt.Errorf("invalid server type")
@@ -373,11 +374,11 @@ func (c *CLI) CommonShowProviderInstanceBalanceCommand(commandCount int, cmd *Co
switch c.Config.CLIMode {
case AdminMode:
httpClient := c.AdminServerClient
- endPoint = fmt.Sprintf("/admin/providers/%s/instances/%s/balance", providerName, instanceName)
+ endPoint = utils.APIPath("/admin/providers", providerName, "instances", instanceName, "balance")
resp, err = httpClient.Request(commandCount, "GET", endPoint, httpClient.AuthKind(), nil, nil)
case APIMode:
httpClient := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer]
- endPoint = fmt.Sprintf("/providers/%s/instances/%s/balance", providerName, instanceName)
+ endPoint = utils.APIPath("/providers", providerName, "instances", instanceName, "balance")
resp, err = httpClient.Request(commandCount, "GET", endPoint, httpClient.AuthKind(), nil, nil)
default:
return nil, fmt.Errorf("invalid server type")
@@ -405,11 +406,11 @@ func (c *CLI) CommonListProviderInstancesCommand(commandCount int, cmd *Command)
switch c.Config.CLIMode {
case AdminMode:
httpClient := c.AdminServerClient
- endPoint = fmt.Sprintf("/admin/providers/%s/instances", providerName)
+ endPoint = utils.APIPath("/admin/providers", providerName, "instances")
resp, err = httpClient.Request(commandCount, "GET", endPoint, httpClient.AuthKind(), nil, nil)
case APIMode:
httpClient := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer]
- endPoint = fmt.Sprintf("/providers/%s/instances", providerName)
+ endPoint = utils.APIPath("/providers", providerName, "instances")
resp, err = httpClient.Request(commandCount, "GET", endPoint, httpClient.AuthKind(), nil, nil)
default:
return nil, fmt.Errorf("invalid server type")
@@ -453,11 +454,11 @@ func (c *CLI) CommonListInstanceModelsCommand(commandCount int, cmd *Command) (R
switch c.Config.CLIMode {
case AdminMode:
httpClient := c.AdminServerClient
- endPoint = fmt.Sprintf("/admin/providers/%s/instances/%s/models", providerName, instanceName)
+ endPoint = utils.APIPath("/admin/providers", providerName, "instances", instanceName, "models")
resp, err = httpClient.Request(commandCount, "GET", endPoint, httpClient.AuthKind(), nil, nil)
case APIMode:
httpClient := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer]
- endPoint = fmt.Sprintf("/providers/%s/instances/%s/models", providerName, instanceName)
+ endPoint = utils.APIPath("/providers", providerName, "instances", instanceName, "models")
resp, err = httpClient.Request(commandCount, "GET", endPoint, httpClient.AuthKind(), nil, nil)
default:
return nil, fmt.Errorf("invalid server type")
@@ -496,11 +497,11 @@ func (c *CLI) CommonListModelsCommand(commandCount int, cmd *Command) (ResponseI
switch c.Config.CLIMode {
case AdminMode:
httpClient := c.AdminServerClient
- endPoint = fmt.Sprintf("/admin/providers/%s/models", providerName)
+ endPoint = utils.APIPath("/admin/providers", providerName, "models")
resp, err = httpClient.Request(commandCount, "GET", endPoint, httpClient.AuthKind(), nil, nil)
case APIMode:
httpClient := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer]
- endPoint = fmt.Sprintf("/providers/%s/models", providerName)
+ endPoint = utils.APIPath("/providers", providerName, "models")
resp, err = httpClient.Request(commandCount, "GET", endPoint, httpClient.AuthKind(), nil, nil)
default:
return nil, fmt.Errorf("invalid server type")
@@ -543,11 +544,11 @@ func (c *CLI) CommonListInstanceModelsSyncCommand(commandCount int, cmd *Command
switch c.Config.CLIMode {
case AdminMode:
httpClient := c.AdminServerClient
- endPoint = fmt.Sprintf("/admin/providers/%s/instances/%s/models?supported=true", providerName, instanceName)
+ endPoint = utils.APIPath("/admin/providers", providerName, "instances", instanceName, "models") + "?supported=true"
resp, err = httpClient.Request(commandCount, "GET", endPoint, httpClient.AuthKind(), nil, nil)
case APIMode:
httpClient := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer]
- endPoint = fmt.Sprintf("/providers/%s/instances/%s/models?supported=true", providerName, instanceName)
+ endPoint = utils.APIPath("/providers", providerName, "instances", instanceName, "models") + "?supported=true"
resp, err = httpClient.Request(commandCount, "GET", endPoint, httpClient.AuthKind(), nil, nil)
default:
return nil, fmt.Errorf("invalid server type")
@@ -589,11 +590,11 @@ func (c *CLI) CommonShowProviderModelCommand(commandCount int, cmd *Command) (Re
switch c.Config.CLIMode {
case AdminMode:
httpClient := c.AdminServerClient
- endPoint = fmt.Sprintf("/admin/providers/%s/models/%s", providerName, modelName)
+ endPoint = utils.APIPath("/admin/providers", providerName, "models", modelName)
resp, err = httpClient.Request(commandCount, "GET", endPoint, httpClient.AuthKind(), nil, nil)
case APIMode:
httpClient := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer]
- endPoint = fmt.Sprintf("/providers/%s/models/%s", providerName, modelName)
+ endPoint = utils.APIPath("/providers", providerName, "models", modelName)
resp, err = httpClient.Request(commandCount, "GET", endPoint, httpClient.AuthKind(), nil, nil)
default:
return nil, fmt.Errorf("invalid server type")
@@ -642,11 +643,11 @@ func (c *CLI) CommonCheckProviderWithKeyCommand(commandCount int, cmd *Command)
switch c.Config.CLIMode {
case AdminMode:
httpClient := c.AdminServerClient
- endPoint = fmt.Sprintf("/admin/providers/%s/connection", providerName)
+ endPoint = utils.APIPath("/admin/providers", providerName, "connection")
resp, err = httpClient.Request(commandCount, "POST", endPoint, httpClient.AuthKind(), nil, payload)
case APIMode:
httpClient := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer]
- endPoint = fmt.Sprintf("/providers/%s/connection", providerName)
+ endPoint = utils.APIPath("/providers", providerName, "connection")
resp, err = httpClient.Request(commandCount, "POST", endPoint, httpClient.AuthKind(), nil, payload)
default:
return nil, fmt.Errorf("invalid server type")
@@ -684,11 +685,11 @@ func (c *CLI) CommonCheckProviderConnectionCommand(commandCount int, cmd *Comman
switch c.Config.CLIMode {
case AdminMode:
httpClient := c.AdminServerClient
- endPoint = fmt.Sprintf("/admin/providers/%s/instances/%s/connection", providerName, instanceName)
+ endPoint = utils.APIPath("/admin/providers", providerName, "instances", instanceName, "connection")
resp, err = httpClient.Request(commandCount, "POST", endPoint, httpClient.AuthKind(), nil, nil)
case APIMode:
httpClient := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer]
- endPoint = fmt.Sprintf("/providers/%s/instances/%s/connection", providerName, instanceName)
+ endPoint = utils.APIPath("/providers", providerName, "instances", instanceName, "connection")
resp, err = httpClient.Request(commandCount, "POST", endPoint, httpClient.AuthKind(), nil, nil)
default:
return nil, fmt.Errorf("invalid server type")
@@ -739,11 +740,11 @@ func (c *CLI) CommonAlterProviderInstanceCommand(commandCount int, cmd *Command)
switch c.Config.CLIMode {
case AdminMode:
httpClient := c.AdminServerClient
- endPoint = fmt.Sprintf("/admin/providers/%s/instances/%s", providerName, instanceName)
+ endPoint = utils.APIPath("/admin/providers", providerName, "instances", instanceName)
resp, err = httpClient.Request(commandCount, "PUT", endPoint, httpClient.AuthKind(), nil, payload)
case APIMode:
httpClient := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer]
- endPoint = fmt.Sprintf("/providers/%s/instances/%s", providerName, instanceName)
+ endPoint = utils.APIPath("/providers", providerName, "instances", instanceName)
resp, err = httpClient.Request(commandCount, "PUT", endPoint, httpClient.AuthKind(), nil, payload)
default:
return nil, fmt.Errorf("invalid server type")
@@ -790,11 +791,11 @@ func (c *CLI) CommonEnableOrDisableModelCommand(cmd *Command, status string) (Re
switch c.Config.CLIMode {
case AdminMode:
httpClient := c.AdminServerClient
- endPoint = fmt.Sprintf("/admin/providers/%s/instances/%s/models/%s", providerName, instanceName, modelName)
+ endPoint = utils.APIPath("/admin/providers", providerName, "instances", instanceName, "models", modelName)
resp, err = httpClient.Request(1, "PATCH", endPoint, httpClient.AuthKind(), nil, payload)
case APIMode:
httpClient := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer]
- endPoint = fmt.Sprintf("/providers/%s/instances/%s/models/%s", providerName, instanceName, modelName)
+ endPoint = utils.APIPath("/providers", providerName, "instances", instanceName, "models", modelName)
resp, err = httpClient.Request(1, "PATCH", endPoint, httpClient.AuthKind(), nil, payload)
default:
return nil, fmt.Errorf("invalid server type")
diff --git a/internal/cli/filesystem/dataset.go b/internal/cli/filesystem/dataset.go
index 594aca7312..310ca8e0db 100644
--- a/internal/cli/filesystem/dataset.go
+++ b/internal/cli/filesystem/dataset.go
@@ -21,6 +21,7 @@ import (
"encoding/json"
"fmt"
"io"
+ "ragflow/internal/cli/utils"
"strconv"
"strings"
"time"
@@ -509,7 +510,7 @@ func (p *DatasetProvider) listDocuments(ctx stdctx.Context, datasetName string,
}
}
- path := fmt.Sprintf("/datasets/%s/documents", datasetID)
+ path := utils.APIPath("/datasets", datasetID, "documents")
resp, err := p.httpClient.Request("GET", path, "auto", params, nil)
if err != nil {
return nil, err
diff --git a/internal/cli/filesystem/file.go b/internal/cli/filesystem/file.go
index 6c64453f40..814f11b45a 100644
--- a/internal/cli/filesystem/file.go
+++ b/internal/cli/filesystem/file.go
@@ -20,6 +20,7 @@ import (
stdctx "context"
"encoding/json"
"fmt"
+ "ragflow/internal/cli/utils"
"strings"
)
@@ -520,7 +521,7 @@ func (p *FileProvider) getFileNode(ctx stdctx.Context, folderName, fileName stri
// downloadFile downloads file content
func (p *FileProvider) downloadFile(ctx stdctx.Context, fileID string) ([]byte, error) {
- path := fmt.Sprintf("/files/%s", fileID)
+ path := utils.APIPath("/files", fileID)
resp, err := p.httpClient.Request("GET", path, "auto", nil, nil)
if err != nil {
return nil, err
diff --git a/internal/cli/filesystem/skill.go b/internal/cli/filesystem/skill.go
index 4388031026..aae3ad7a4e 100644
--- a/internal/cli/filesystem/skill.go
+++ b/internal/cli/filesystem/skill.go
@@ -25,6 +25,7 @@ import (
"net/url"
"os"
"path/filepath"
+ "ragflow/internal/cli/utils"
"ragflow/internal/common"
"regexp"
"strings"
@@ -327,7 +328,7 @@ func (p *SkillProvider) Cat(ctx stdctx.Context, path string) ([]byte, error) {
}
// Find the version folder
- filesResp, err := p.httpClient.Request("GET", fmt.Sprintf("/files?parent_id=%s", skillFolderID), "auto", nil, nil)
+ filesResp, err := p.httpClient.Request("GET", "/files?"+utils.APIQuery("parent_id", skillFolderID), "auto", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to list versions: %w", err)
}
@@ -371,7 +372,7 @@ func (p *SkillProvider) Cat(ctx stdctx.Context, path string) ([]byte, error) {
// If there's a directory path before the file, navigate through it
for i := 0; i < len(pathParts)-1; i++ {
- subResp, err := p.httpClient.Request("GET", fmt.Sprintf("/files?parent_id=%s", currentFolderID), "auto", nil, nil)
+ subResp, err := p.httpClient.Request("GET", "/files?"+utils.APIQuery("parent_id", currentFolderID), "auto", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to navigate path: %w", err)
}
@@ -415,7 +416,7 @@ func (p *SkillProvider) Cat(ctx stdctx.Context, path string) ([]byte, error) {
// Step 5: Find the file in the current directory
fileName := pathParts[len(pathParts)-1]
- finalResp, err := p.httpClient.Request("GET", fmt.Sprintf("/files?parent_id=%s", currentFolderID), "auto", nil, nil)
+ finalResp, err := p.httpClient.Request("GET", "/files?"+utils.APIQuery("parent_id", currentFolderID), "auto", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to list directory: %w", err)
}
@@ -456,7 +457,7 @@ func (p *SkillProvider) Cat(ctx stdctx.Context, path string) ([]byte, error) {
// Step 6: Download the file content
// First get file info to get the download URL
- contentResp, err := p.httpClient.Request("GET", fmt.Sprintf("/files/%s", fileID), "auto", nil, nil)
+ contentResp, err := p.httpClient.Request("GET", utils.APIPath("/files", fileID), "auto", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to get file info: %w", err)
}
@@ -620,7 +621,7 @@ func (p *SkillProvider) listSkillsInSpaceFromFileSystem(ctx stdctx.Context, spac
common.Debug("Got space folder ID", zap.String("spaceName", spaceName), zap.String("spaceFolderID", spaceFolderID))
// List all subfolders in the space folder (each subfolder is a skill)
- skillsResp, err := p.httpClient.Request("GET", fmt.Sprintf("/files?parent_id=%s", spaceFolderID), "auto", nil, nil)
+ skillsResp, err := p.httpClient.Request("GET", "/files?"+utils.APIQuery("parent_id", spaceFolderID), "auto", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to list skills: %w", err)
}
@@ -722,7 +723,7 @@ func (p *SkillProvider) getSkillsFolderID(ctx stdctx.Context) (string, error) {
// findFolderID finds a folder by name under a parent folder
func (p *SkillProvider) findFolderID(ctx stdctx.Context, parentID, folderName string) (string, error) {
- resp, err := p.httpClient.Request("GET", fmt.Sprintf("/files?parent_id=%s", parentID), "auto", nil, nil)
+ resp, err := p.httpClient.Request("GET", "/files?"+utils.APIQuery("parent_id", parentID), "auto", nil, nil)
if err != nil {
return "", fmt.Errorf("failed to list folders: %w", err)
}
@@ -812,7 +813,7 @@ func (p *SkillProvider) listSkillVersions(ctx stdctx.Context, spaceID, skillName
}
// List the skill folder to get versions (subdirectories)
- filesResp, err := p.httpClient.Request("GET", fmt.Sprintf("/files?parent_id=%s", skillFolderID), "auto", nil, nil)
+ filesResp, err := p.httpClient.Request("GET", "/files?"+utils.APIQuery("parent_id", skillFolderID), "auto", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to list versions: %w", err)
}
@@ -873,7 +874,7 @@ func (p *SkillProvider) listSkillContent(ctx stdctx.Context, spaceID, skillName,
}
// List the version folder under the skill folder
- filesResp, err := p.httpClient.Request("GET", fmt.Sprintf("/files?parent_id=%s", skillFolderID), "auto", nil, nil)
+ filesResp, err := p.httpClient.Request("GET", "/files?"+utils.APIQuery("parent_id", skillFolderID), "auto", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to list skill versions: %w", err)
}
@@ -930,7 +931,7 @@ func (p *SkillProvider) listSkillContent(ctx stdctx.Context, spaceID, skillName,
isLastPart := (i == len(extraParts)-1)
// List current folder to find the next part
- subResp, err := p.httpClient.Request("GET", fmt.Sprintf("/files?parent_id=%s", currentFolderID), "auto", nil, nil)
+ subResp, err := p.httpClient.Request("GET", "/files?"+utils.APIQuery("parent_id", currentFolderID), "auto", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to navigate path: %w", err)
}
@@ -1001,7 +1002,7 @@ func (p *SkillProvider) listSkillContent(ctx stdctx.Context, spaceID, skillName,
}
// Step 5: List the final folder contents
- finalResp, err := p.httpClient.Request("GET", fmt.Sprintf("/files?parent_id=%s", currentFolderID), "auto", nil, nil)
+ finalResp, err := p.httpClient.Request("GET", "/files?"+utils.APIQuery("parent_id", currentFolderID), "auto", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to list folder contents: %w", err)
}
diff --git a/internal/cli/helper.go b/internal/cli/helper.go
index f5d82988f4..0f696156a3 100644
--- a/internal/cli/helper.go
+++ b/internal/cli/helper.go
@@ -16,7 +16,9 @@
package cli
-import "fmt"
+import (
+ "fmt"
+)
func (c *CLI) apiModeClient() (*HTTPClient, error) {
if c.Config.CLIMode != APIMode {
diff --git a/internal/cli/user_command.go b/internal/cli/user_command.go
index db82576817..280ee08cdf 100644
--- a/internal/cli/user_command.go
+++ b/internal/cli/user_command.go
@@ -29,6 +29,7 @@ import (
"os"
"os/exec"
"path/filepath"
+ "ragflow/internal/cli/utils"
"ragflow/internal/common"
"ragflow/internal/parser/chunk"
"ragflow/internal/parser/parser"
@@ -182,11 +183,15 @@ func (c *CLI) APIListDatasetDocumentsCommand(commandCount int, cmd *Command) (Re
return nil, fmt.Errorf("no dataset id")
}
- page := 1
- pageSize := 10
- keywords := ""
- returnEmptyMetadata := "true"
- url := fmt.Sprintf("/datasets/%s/documents?page=%d&page_size=%d&keywords=%s&return_empty_metadata=%s", datasetID, page, pageSize, keywords, returnEmptyMetadata)
+ // Build the query with url.Values rather than Sprintf: datasetID is
+ // caller-supplied, and string interpolation would let it inject extra
+ // query params or escape the path segment.
+ query := netUrl.Values{}
+ query.Set("page", "1")
+ query.Set("page_size", "10")
+ query.Set("keywords", "")
+ query.Set("return_empty_metadata", "true")
+ url := fmt.Sprintf("/datasets/%s/documents?%s", netUrl.PathEscape(datasetID), query.Encode())
// Normal mode
resp, err := httpClient.Request(commandCount, "GET", url, httpClient.AuthKind(), nil, nil)
@@ -227,7 +232,7 @@ func (c *CLI) APIListDatasetFilesCommand(commandCount int, cmd *Command) (Respon
return nil, fmt.Errorf("failed to get dataset id: %w", err)
}
- url := fmt.Sprintf("/datasets/%s/documents", datasetID)
+ url := utils.APIPath("/datasets", datasetID, "documents")
// Normal mode
resp, err := httpClient.Request(commandCount, "GET", url, httpClient.AuthKind(), nil, nil)
@@ -372,47 +377,6 @@ func (c *CLI) APIListMemoriesCommand(commandCount int, cmd *Command) (ResponseIf
return &result, nil
}
-// ListDatasetDocumentUserCommand lists dataset documents
-func (c *CLI) ListDatasetDocumentUserCommand(commandCount int, cmd *Command) (ResponseIf, error) {
- httpClient, err := c.apiModeClient()
- if err != nil {
- return nil, err
- }
-
- datasetID, ok := cmd.Params["dataset_id"].(string)
- if !ok {
- return nil, fmt.Errorf("no dataset id")
- }
-
- page := 1
- pageSize := 10
- keywords := ""
- returnEmptyMetadata := "true"
- url := fmt.Sprintf("/datasets/%s/documents?page=%d&page_size=%d&keywords=%s&return_empty_metadata=%s", datasetID, page, pageSize, keywords, returnEmptyMetadata)
-
- // Normal mode
- resp, err := httpClient.Request(commandCount, "GET", url, httpClient.AuthKind(), nil, nil)
- if err != nil {
- return nil, fmt.Errorf("failed to list documents: %w", err)
- }
-
- if resp.StatusCode != 200 {
- return nil, fmt.Errorf("failed to list documents: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
- }
-
- var result ListDocumentsResponse
- 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
-}
-
// DevGetMetadataCommand gets metadata for one or more datasets
func (c *CLI) DevGetMetadataCommand(commandCount int, cmd *Command) (ResponseIf, error) {
httpClient, err := c.apiModeClient()
@@ -877,7 +841,7 @@ func (c *CLI) APIDeleteAPIKeyCommand(commandCount int, cmd *Command) (ResponseIf
return nil, fmt.Errorf("key not provided")
}
- resp, err := httpClient.Request(commandCount, "DELETE", fmt.Sprintf("/system/keys/%s", apiKey), httpClient.AuthKind(), nil, nil)
+ resp, err := httpClient.Request(commandCount, "DELETE", utils.APIPath("/system/keys", apiKey), httpClient.AuthKind(), nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to delete key: %w", err)
}
@@ -1303,7 +1267,7 @@ func (c *CLI) APIDeleteProviderCommand(commandCount int, cmd *Command) (Response
return nil, fmt.Errorf("provider name not provided")
}
- url := fmt.Sprintf("/providers/%s", providerName)
+ url := utils.APIPath("/providers", providerName)
resp, err := httpClient.Request(commandCount, "DELETE", url, httpClient.AuthKind(), nil, nil)
if err != nil {
@@ -1327,7 +1291,7 @@ func (c *CLI) APIDropDatasetCommand(commandCount int, cmd *Command) (ResponseIf,
datasetID, err := c.getDatasetIDByName(datasetName)
if err != nil {
- return nil, fmt.Errorf("failed to get dataset ID: %w by dataset name: %s", err, datasetName)
+ return nil, fmt.Errorf("failed to get dataset ID by name %q: %w", datasetName, err)
}
payload := map[string]interface{}{
@@ -1357,7 +1321,7 @@ func (c *CLI) APIDropAgentCommand(commandCount int, cmd *Command) (ResponseIf, e
agentID, err := c.getAgentIDByName(agentName)
if err != nil {
- return nil, fmt.Errorf("failed to get agent ID: %w by agent name: %s", err, agentName)
+ return nil, fmt.Errorf("failed to get agent ID by name %q: %w", agentName, err)
}
payload := map[string]interface{}{
@@ -1387,7 +1351,7 @@ func (c *CLI) APIDropChatCommand(commandCount int, cmd *Command) (ResponseIf, er
chatID, err := c.getChatIDByName(chatName)
if err != nil {
- return nil, fmt.Errorf("failed to get chat ID: %w by chat name: %s", err, chatName)
+ return nil, fmt.Errorf("failed to get chat ID by name %q: %w", chatName, err)
}
payload := map[string]interface{}{
@@ -1417,10 +1381,10 @@ func (c *CLI) APIDropSearchCommand(commandCount int, cmd *Command) (ResponseIf,
searchID, err := c.getSearchIDByName(searchName)
if err != nil {
- return nil, fmt.Errorf("failed to get search ID: %w by search name: %s", err, searchName)
+ return nil, fmt.Errorf("failed to get search ID by name %q: %w", searchName, err)
}
- endPoint := fmt.Sprintf("/searches/%s", searchID)
+ endPoint := utils.APIPath("/searches", searchID)
resp, err := httpClient.Request(commandCount, "DELETE", endPoint, httpClient.AuthKind(), nil, nil)
if err != nil {
@@ -1444,10 +1408,10 @@ func (c *CLI) APIDropMemoryCommand(commandCount int, cmd *Command) (ResponseIf,
memoryID, err := c.getMemoryIDByName(memoryName)
if err != nil {
- return nil, fmt.Errorf("failed to get memory ID: %w by memory name: %s", err, memoryName)
+ return nil, fmt.Errorf("failed to get memory ID by name %q: %w", memoryName, err)
}
- endPoint := fmt.Sprintf("/memories/%s", memoryID)
+ endPoint := utils.APIPath("/memories", memoryID)
resp, err := httpClient.Request(commandCount, "DELETE", endPoint, httpClient.AuthKind(), nil, nil)
if err != nil {
@@ -1490,7 +1454,7 @@ func (c *CLI) APIAddProviderInstanceCommand(commandCount int, cmd *Command) (Res
region = ""
}
- url := fmt.Sprintf("/providers/%s/instances", providerName)
+ url := utils.APIPath("/providers", providerName, "instances")
payload := map[string]interface{}{
"instance_name": instanceName,
@@ -1528,7 +1492,7 @@ func (c *CLI) APIDeleteProviderInstanceCommand(commandCount int, cmd *Command) (
"instances": []string{instanceName},
}
- url := fmt.Sprintf("/providers/%s/instances", providerName)
+ url := utils.APIPath("/providers", providerName, "instances")
resp, err := httpClient.Request(commandCount, "DELETE", url, httpClient.AuthKind(), nil, payload)
if err != nil {
@@ -1564,7 +1528,7 @@ func (c *CLI) APIDeleteProviderInstanceModelCommand(commandCount int, cmd *Comma
"models": modelNames,
}
- url := fmt.Sprintf("/providers/%s/instances/%s/models", providerName, instanceName)
+ url := utils.APIPath("/providers", providerName, "instances", instanceName, "models")
resp, err := httpClient.Request(commandCount, "DELETE", url, httpClient.AuthKind(), nil, payload)
if err != nil {
@@ -2491,7 +2455,7 @@ func (c *CLI) APIListModelInstanceTasksCommand(commandCount int, cmd *Command) (
return nil, fmt.Errorf("no instance name")
}
- url := fmt.Sprintf("/providers/%s/instances/%s/tasks", providerName, instanceName)
+ url := utils.APIPath("/providers", providerName, "instances", instanceName, "tasks")
resp, err := httpClient.Request(commandCount, "GET", url, httpClient.AuthKind(), nil, nil)
if err != nil {
@@ -2523,7 +2487,7 @@ func (c *CLI) APIShowProviderInstanceTaskCommand(commandCount int, cmd *Command)
return nil, fmt.Errorf("task id not provided")
}
- url := fmt.Sprintf("/providers/%s/instances/%s/tasks/%s", providerName, instanceName, taskID)
+ url := utils.APIPath("/providers", providerName, "instances", instanceName, "tasks", taskID)
resp, err := httpClient.Request(commandCount, "GET", url, httpClient.AuthKind(), nil, nil)
if err != nil {
@@ -2602,7 +2566,7 @@ func (c *CLI) APIAddCustomModelCommand(commandCount int, cmd *Command) (Response
return nil, fmt.Errorf("model name not provided")
}
- url := fmt.Sprintf("/providers/%s/instances/%s/models", providerName, instanceName)
+ url := utils.APIPath("/providers", providerName, "instances", instanceName, "models")
payload := map[string]interface{}{
"provider_name": providerName,
@@ -2714,7 +2678,7 @@ func (c *CLI) DevGetChunkCommand(commandCount int, cmd *Command) (ResponseIf, er
return nil, fmt.Errorf("dataset_id not provided")
}
- resp, err := httpClient.Request(commandCount, "GET", fmt.Sprintf("/datasets/%s/documents/%s/chunks/%s", datasetID, docID, chunkID), httpClient.AuthKind(), nil, nil)
+ resp, err := httpClient.Request(commandCount, "GET", utils.APIPath("/datasets", datasetID, "documents", docID, "chunks", chunkID), httpClient.AuthKind(), nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to get chunk: %w", err)
}
@@ -2928,7 +2892,7 @@ func (c *CLI) APIParseDocumentsCommand(commandCount int, cmd *Command) (Response
return nil, fmt.Errorf("documents not provided")
}
- url := fmt.Sprintf("/datasets/%s/documents/parse", datasetID)
+ url := utils.APIPath("/datasets", datasetID, "documents", "parse")
payload := map[string]interface{}{
"documents": documents,
@@ -3181,7 +3145,7 @@ func (c *CLI) APIStartIngestionCommand(commandCount int, cmd *Command) (Response
"dataset_id": datasetID,
}
- url := fmt.Sprintf("/datasets/%s/documents/parse", datasetID)
+ url := utils.APIPath("/datasets", datasetID, "documents", "parse")
resp, err := httpClient.Request(commandCount, "POST", url, httpClient.AuthKind(), nil, payload)
if err != nil {
@@ -3331,7 +3295,7 @@ func (c *CLI) APIOpenaiChatCommand(commandCount int, cmd *Command) (ResponseIf,
}
chatID, _ := cmd.Params["chat_id"].(string)
- url := fmt.Sprintf("/openai/%s/chat/completions", chatID)
+ url := utils.APIPath("/openai", chatID, "chat", "completions")
stream, _ := cmd.Params["stream"].(bool)
if stream {
diff --git a/internal/cli/user_parser.go b/internal/cli/user_parser.go
index 892ac148bd..fd9dc8b7b1 100644
--- a/internal/cli/user_parser.go
+++ b/internal/cli/user_parser.go
@@ -990,6 +990,7 @@ func (p *Parser) parseAPICreateDataset() (*Command, error) {
if err != nil {
return nil, err
}
+ p.nextToken()
// Semicolon is optional
if p.curToken.Type == TokenSemicolon {
@@ -2922,7 +2923,7 @@ func (p *Parser) parseAPIChat() (*Command, error) {
}
var err error
- var modelNameOrID string = ""
+ var modelNameOrID = ""
var messages []string
var images []string
var videos []string
diff --git a/internal/cli/utils/path.go b/internal/cli/utils/path.go
new file mode 100644
index 0000000000..e01e6813b5
--- /dev/null
+++ b/internal/cli/utils/path.go
@@ -0,0 +1,49 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the specific language governing permissions and limitations
+// under the License.
+//
+
+package utils
+
+import (
+ "net/url"
+ "strings"
+)
+
+// APIPath joins a prefix and path segments into an API path, escaping every
+// segment so a value containing "/" or "?" cannot break out of its own
+// segment. CLI paths are built from user-supplied names and IDs, and
+// fmt.Sprintf leaves those values unescaped.
+//
+// Pass RAW values only. A value that is already percent-encoded (such callers
+// in the CLI name it with an "Encoded"/"encoded" prefix) must NOT go through
+// here again — PathEscape would rewrite "%2F" as "%252F".
+func APIPath(prefix string, segments ...string) string {
+ parts := make([]string, 0, len(segments)+1)
+ parts = append(parts, prefix)
+ for _, s := range segments {
+ parts = append(parts, url.PathEscape(s))
+ }
+ return strings.Join(parts, "/")
+}
+
+// APIQuery builds an encoded query string from key/value pairs, so a value
+// containing "&" or "=" cannot inject extra parameters.
+func APIQuery(pairs ...string) string {
+ q := url.Values{}
+ for i := 0; i+1 < len(pairs); i += 2 {
+ q.Set(pairs[i], pairs[i+1])
+ }
+ return q.Encode()
+}