Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(data-analysis): resolve workspace storage backends in quick answers (#4214)
Clint1b2c7af01aa4
diff --git a/internal/agent/tools/data_analysis.go b/internal/agent/tools/data_analysis.goindex 46cfb4483..fab508a39 100644--- a/internal/agent/tools/data_analysis.go+++ b/internal/agent/tools/data_analysis.go@@ -948,7 +948,8 @@ func (t *DataAnalysisTool) resolveFileServiceForKnowledge(ctx context.Context, kif provider == "" && tenant != nil && tenant.StorageEngineConfig != nil {provider = strings.ToLower(strings.TrimSpace(tenant.StorageEngineConfig.DefaultProvider))}- if t.storageResolver != nil && tenant != nil && (backendID != "" || provider != "") {+ // The resolver also handles workspace defaults without a legacy provider.+ if t.storageResolver != nil && tenant != nil {resolvedSvc, resolvedProvider, err := t.storageResolver.ResolveFileService(ctx, tenant, backendID, provider, t.localBaseDir,)diff --git a/internal/agent/tools/data_analysis_storage_test.go b/internal/agent/tools/data_analysis_storage_test.gonew file mode 100644index 000000000..ee58a9c35--- /dev/null+++ b/internal/agent/tools/data_analysis_storage_test.go@@ -0,0 +1,62 @@+package tools++import (+ "context"+ "io"+ "testing"++ filesvc "github.com/Tencent/WeKnora/internal/application/service/file"+ "github.com/Tencent/WeKnora/internal/types"+ "github.com/Tencent/WeKnora/internal/types/interfaces"+ "github.com/stretchr/testify/require"+)++type dataAnalysisStorageResolver struct {+ interfaces.StorageBackendResolver+ service interfaces.FileService+ calls int+}++func (r *dataAnalysisStorageResolver) ResolveFileService(+ _ context.Context, _ *types.Tenant, _, _, _ string,+) (interfaces.FileService, string, error) {+ r.calls+++ return r.service, "local", nil+}++func TestDataAnalysisResolvesWorkspaceStorage(t *testing.T) {+ for _, explicit := range []bool{false, true} {+ name := "workspace_default"+ if explicit {+ name = "explicit_backend"+ }+ t.Run(name, func(t *testing.T) {+ ctx := context.Background()+ backendID := "workspace-storage"+ workspaceFiles := filesvc.NewLocalFileService(t.TempDir(), "")+ filePath, err := workspaceFiles.SaveBytes(ctx, []byte("amount\n42\n"), 7, "data.csv", false)+ require.NoError(t, err)+ if explicit {+ filePath = "storage://" + backendID + "/" + filePath+ }+ resolver := &dataAnalysisStorageResolver{+ service: filesvc.NewBackendScopedFileService(backendID, workspaceFiles),+ }+ tool := NewDataAnalysisTool(+ &stubKnowledgeBaseService{kb: &types.KnowledgeBase{ID: "kb", TenantID: 7}},+ nil, nil, filesvc.NewLocalFileService(t.TempDir(), ""), nil, "storage-test", resolver,+ )+ ctx = context.WithValue(ctx, types.TenantInfoContextKey, &types.Tenant{+ ID: 7, DefaultStorageBackendID: &backendID,+ })+ knowledge := &types.Knowledge{ID: "document", KnowledgeBaseID: "kb", FilePath: filePath}+ reader, err := tool.resolveFileServiceForKnowledge(ctx, knowledge).GetFile(ctx, filePath)+ require.NoError(t, err)+ defer func() { _ = reader.Close() }()+ data, err := io.ReadAll(reader)+ require.NoError(t, err)+ require.Equal(t, "amount\n42\n", string(data))+ require.Equal(t, 1, resolver.calls)+ })+ }+}diff --git a/internal/application/service/chat_pipeline/data_analysis.go b/internal/application/service/chat_pipeline/data_analysis.goindex 389d9c971..af532bf3d 100644--- a/internal/application/service/chat_pipeline/data_analysis.go+++ b/internal/application/service/chat_pipeline/data_analysis.go@@ -24,6 +24,7 @@ type PluginDataAnalysis struct {chunkRepo interfaces.ChunkRepositorytenantService interfaces.TenantServicedb *sql.DB+ storageResolver interfaces.StorageBackendResolver}func NewPluginDataAnalysis(@@ -35,6 +36,7 @@ func NewPluginDataAnalysis(chunkRepo interfaces.ChunkRepository,tenantService interfaces.TenantService,db *sql.DB,+ storageResolver interfaces.StorageBackendResolver,) *PluginDataAnalysis {p := &PluginDataAnalysis{modelService: modelService,@@ -44,6 +46,7 @@ func NewPluginDataAnalysis(chunkRepo: chunkRepo,tenantService: tenantService,db: db,+ storageResolver: storageResolver,}eventManager.Register(p)return p@@ -89,7 +92,10 @@ func (p *PluginDataAnalysis) OnEvent(}// Initialize DataAnalysisTool- tool := tools.NewDataAnalysisTool(p.knowledgeBaseService, p.knowledgeService, p.tenantService, p.fileService, p.db, chatManage.SessionID)+ tool := tools.NewDataAnalysisTool(+ p.knowledgeBaseService, p.knowledgeService, p.tenantService,+ p.fileService, p.db, chatManage.SessionID, p.storageResolver,+ )defer tool.Cleanup(ctx)// Load data into DuckDBdiff --git a/internal/application/service/chat_pipeline/data_analysis_storage_test.go b/internal/application/service/chat_pipeline/data_analysis_storage_test.gonew file mode 100644index 000000000..09896a939--- /dev/null+++ b/internal/application/service/chat_pipeline/data_analysis_storage_test.go@@ -0,0 +1,93 @@+package chatpipeline++import (+ "context"+ "database/sql"+ "testing"++ filesvc "github.com/Tencent/WeKnora/internal/application/service/file"+ "github.com/Tencent/WeKnora/internal/types"+ "github.com/Tencent/WeKnora/internal/types/interfaces"+ _ "github.com/duckdb/duckdb-go/v2"+ "github.com/stretchr/testify/require"+)++type analysisStorageKnowledgeService struct {+ interfaces.KnowledgeService+ knowledge *types.Knowledge+}++func (s *analysisStorageKnowledgeService) GetKnowledgeByID(context.Context, string) (*types.Knowledge, error) {+ return s.knowledge, nil+}++func (s *analysisStorageKnowledgeService) GetKnowledgeByIDOnly(context.Context, string) (*types.Knowledge, error) {+ return s.knowledge, nil+}++type analysisStorageKBService struct {+ interfaces.KnowledgeBaseService+ kb *types.KnowledgeBase+}++func (s *analysisStorageKBService) GetKnowledgeBaseByID(context.Context, string) (*types.KnowledgeBase, error) {+ return s.kb, nil+}++type analysisStorageResolver struct {+ interfaces.StorageBackendResolver+ service interfaces.FileService+ calls int+}++func (r *analysisStorageResolver) ResolveFileService(+ _ context.Context, _ *types.Tenant, _, _, _ string,+) (interfaces.FileService, string, error) {+ r.calls+++ return r.service, "local", nil+}++func TestQuickAnswerDataAnalysisUsesWorkspaceStorage(t *testing.T) {+ ctx := context.Background()+ backendID := "workspace-storage"+ workspaceFiles := filesvc.NewLocalFileService(t.TempDir(), "")+ path, err := workspaceFiles.SaveBytes(ctx, []byte("amount\n42\n58\n"), 7, "data.csv", false)+ require.NoError(t, err)+ knowledge := &types.Knowledge{+ ID: testKnowledgeID, KnowledgeBaseID: "kb", TenantID: 7,+ FileType: "csv", FileName: "data.csv", FilePath: "storage://" + backendID + "/" + path,+ }+ resolver := &analysisStorageResolver{+ service: filesvc.NewBackendScopedFileService(backendID, workspaceFiles),+ }+ db, err := sql.Open("duckdb", "")+ require.NoError(t, err)+ t.Cleanup(func() { _ = db.Close() })+ model := &scriptedChat{replies: []string{+ `{"needs_analysis":true,"sql":"SELECT COUNT(*) AS total FROM dataset"}`,+ }}+ plugin := NewPluginDataAnalysis(+ NewEventManager(), &stubModelService{model: model},+ &analysisStorageKBService{kb: &types.KnowledgeBase{ID: "kb", TenantID: 7}},+ &analysisStorageKnowledgeService{knowledge: knowledge},+ filesvc.NewLocalFileService(t.TempDir(), ""), nil, nil, db, resolver,+ )+ ctx = context.WithValue(ctx, types.TenantInfoContextKey, &types.Tenant{+ ID: 7, DefaultStorageBackendID: &backendID,+ })+ turn := &types.ChatManage{}+ turn.SessionID = "storage-test"+ turn.Query = "count rows"+ turn.MergeResult = []*types.SearchResult{{KnowledgeID: knowledge.ID, KnowledgeFilename: knowledge.FileName}}+ continued := false+ pluginErr := plugin.OnEvent(ctx, types.DATA_ANALYSIS, turn, func() *PluginError {+ continued = true+ return nil+ })+ require.Nil(t, pluginErr)+ require.True(t, continued)+ require.NotZero(t, resolver.calls)+ require.Len(t, turn.MergeResult, 2)+ require.Equal(t, types.MatchTypeDataAnalysis, turn.MergeResult[1].MatchType)+ require.Contains(t, turn.MergeResult[1].Content, `{"total":"2"}`)+}