Skip to content

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

fix(cli): refresh filesystem providers when switching API servers (#20498)

helloxjade690c4e7c3fab
diff --git a/internal/cli/cli.go b/internal/cli/cli.go
index 99ea22bcf7..2a96ef9abf 100644
--- a/internal/cli/cli.go
+++ b/internal/cli/cli.go
@@ -538,15 +538,7 @@ func NewCLIWithConfig(commandLineConfig *CommandLineConfig) (*CLI, error) {
}
}
- engine := filesystem.NewEngine()
-
- // Register providers
- // TODO: if http config change, engine http config won't be updated. They should share the same config
- engine.RegisterProvider(filesystem.NewDatasetProvider(&httpClientAdapter{httpClient}))
- engine.RegisterProvider(filesystem.NewFileProvider(&httpClientAdapter{httpClient}))
- engine.RegisterProvider(filesystem.NewSkillProvider(&httpClientAdapter{httpClient}))
-
- cli.ContextEngine = engine
+ cli.setFilesystemClient(httpClient)
} else if commandLineConfig.CLIMode == AdminMode {
httpClient := NewHTTPClient()
httpClient.Host = commandLineConfig.AdminClientConfig.AdminHost
@@ -569,6 +561,15 @@ func NewCLIWithConfig(commandLineConfig *CommandLineConfig) (*CLI, error) {
return cli, nil
}
+func (c *CLI) setFilesystemClient(client *HTTPClient) {
+ engine := filesystem.NewEngine()
+ adapter := &httpClientAdapter{client: client}
+ engine.RegisterProvider(filesystem.NewDatasetProvider(adapter))
+ engine.RegisterProvider(filesystem.NewFileProvider(adapter))
+ engine.RegisterProvider(filesystem.NewSkillProvider(adapter))
+ c.ContextEngine = engine
+}
+
// sanitizeCLIError returns an operator-safe rendering of a CLI
// command error. Many command handlers build their errors via
// fmt.Errorf("... %s ...", userInput) where userInput can be a
diff --git a/internal/cli/common_command.go b/internal/cli/common_command.go
index e17d2b85f6..8c3bcc943b 100644
--- a/internal/cli/common_command.go
+++ b/internal/cli/common_command.go
@@ -1459,6 +1459,7 @@ func (c *CLI) CommonUseAPIServerCommand(commandCount int, cmd *Command) (Respons
c.Config.APIClientConfig.CurrentAPIServer = serverName
c.Config.CLIMode = APIMode
+ c.setFilesystemClient(httpClient)
var result SimpleResponse
result.Code = 0
diff --git a/internal/cli/filesystem_command_test.go b/internal/cli/filesystem_command_test.go
index 31de7339e4..8749e32470 100644
--- a/internal/cli/filesystem_command_test.go
+++ b/internal/cli/filesystem_command_test.go
@@ -18,6 +18,8 @@ package cli
import (
"context"
+ "fmt"
+ "net/http"
"testing"
"ragflow/internal/cli/filesystem"
@@ -27,6 +29,55 @@ type recordingFileProvider struct {
searchOptions *filesystem.SearchOptions
}
+func TestFilesystemUsesSelectedAPIServer(t *testing.T) {
+ newServer := func(name string) *HTTPClient {
+ return newTestHTTPClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path != "/api/v1/datasets" {
+ t.Errorf("%s received unexpected path %s", name, r.URL.Path)
+ }
+ _, _ = fmt.Fprintf(w, `{"code":0,"data":[{"id":"%s","name":"%s"}]}`, name, name)
+ }))
+ }
+ first := newServer("first")
+ second := newServer("second")
+ config := &CommandLineConfig{
+ CLIMode: APIMode,
+ APIClientConfig: APIModeConfig{
+ CurrentAPIServer: "first",
+ APIServerMap: map[string]*APIServerConfig{
+ "first": {IP: first.Host, Port: first.Port},
+ "second": {IP: second.Host, Port: second.Port},
+ },
+ },
+ }
+ cli, err := NewCLIWithConfig(config)
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { cli.line.Close() })
+ cli.APIServerClientMap["second"] = second
+
+ check := func(name string) {
+ t.Helper()
+ result, err := cli.ContextEngine.List(t.Context(), "datasets", nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(result.Nodes) != 1 || result.Nodes[0].Name != name {
+ t.Fatalf("selected %s, got datasets %+v", name, result.Nodes)
+ }
+ }
+ check("first")
+ if _, err := cli.CommonUseAPIServerCommand(0, &Command{Params: map[string]interface{}{"server_name": "second"}}); err != nil {
+ t.Fatal(err)
+ }
+ check("second")
+ if _, err := cli.CommonUseAPIServerCommand(0, &Command{Params: map[string]interface{}{"server_name": "first"}}); err != nil {
+ t.Fatal(err)
+ }
+ check("first")
+}
+
func (p *recordingFileProvider) Name() string { return "files" }
func (p *recordingFileProvider) Description() string { return "test file provider" }
func (p *recordingFileProvider) Supports(path string) bool {