From e2234e496045e0a171183ed65a57173b105889b0 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Tue, 3 Mar 2026 22:02:19 +0800 Subject: [PATCH] feat: implement dynamic file service resolution for knowledge extraction - Added a new method `resolveFileServiceForKnowledge` to dynamically resolve the appropriate file service based on the tenant's storage configuration and the knowledge file's provider. - Updated the `extractionResources` struct to include a reference to the tenant, enabling the new resolution logic. - Modified the `processTableData` method to utilize the resolved file service, improving the handling of file operations during data extraction. These changes enhance the flexibility and reliability of file service management in the data extraction process. --- internal/application/service/extract.go | 38 ++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/internal/application/service/extract.go b/internal/application/service/extract.go index a21433e2..5cbce657 100644 --- a/internal/application/service/extract.go +++ b/internal/application/service/extract.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/Tencent/WeKnora/internal/agent/tools" + filesvc "github.com/Tencent/WeKnora/internal/application/service/file" chatpipline "github.com/Tencent/WeKnora/internal/application/service/chat_pipline" "github.com/Tencent/WeKnora/internal/application/service/retriever" "github.com/Tencent/WeKnora/internal/config" @@ -314,6 +315,7 @@ func (s *DataTableSummaryService) Handle(ctx context.Context, t *asynq.Task) err // extractionResources 封装提取过程所需的所有资源 type extractionResources struct { knowledge *types.Knowledge + tenant *types.Tenant chatModel chat.Chat embeddingModel embedding.Embedder retrieveEngine *retriever.CompositeRetrieveEngine @@ -366,18 +368,52 @@ func (s *DataTableSummaryService) prepareResources(ctx context.Context, payload return &extractionResources{ knowledge: knowledge, + tenant: tenantInfo, chatModel: chatModel, embeddingModel: embeddingModel, retrieveEngine: retrieveEngine, }, nil } +// resolveFileServiceForKnowledge resolves a provider-specific file service for the current knowledge file. +// It falls back to the global service when tenant storage config is unavailable. +func (s *DataTableSummaryService) resolveFileServiceForKnowledge(ctx context.Context, resources *extractionResources) interfaces.FileService { + if resources == nil || resources.knowledge == nil { + return s.fileService + } + if resources.tenant == nil || resources.tenant.StorageEngineConfig == nil { + return s.fileService + } + + provider := types.InferStorageFromFilePath(resources.knowledge.FilePath) + if provider == "" { + provider = strings.ToLower(strings.TrimSpace(resources.tenant.StorageEngineConfig.DefaultProvider)) + } + if provider == "" { + return s.fileService + } + + baseDir := strings.TrimSpace(os.Getenv("LOCAL_STORAGE_BASE_DIR")) + resolvedSvc, resolvedProvider, err := filesvc.NewFileServiceFromStorageConfig( + provider, + resources.tenant.StorageEngineConfig, + baseDir, + ) + if err != nil { + logger.Warnf(ctx, "[TableSummary] Failed to resolve file service for provider=%s, fallback to default: %v", provider, err) + return s.fileService + } + logger.Infof(ctx, "[TableSummary] Resolved file service for knowledge=%s provider=%s", resources.knowledge.ID, resolvedProvider) + return resolvedSvc +} + // processTableData 处理表格数据:加载 -> 分析 -> 生成摘要 -> 创建chunks // 思路:将数据处理的核心流程集中在一起,保持逻辑连贯性 func (s *DataTableSummaryService) processTableData(ctx context.Context, resources *extractionResources) ([]*types.Chunk, error) { // 创建DuckDB会话并加载数据 sessionID := fmt.Sprintf("table_summary_%s", resources.knowledge.ID) - duckdbTool := tools.NewDataAnalysisTool(s.knowledgeService, s.fileService, s.sqlDB, sessionID) + fileSvc := s.resolveFileServiceForKnowledge(ctx, resources) + duckdbTool := tools.NewDataAnalysisTool(s.knowledgeService, fileSvc, s.sqlDB, sessionID) defer duckdbTool.Cleanup(ctx) // 使用knowledge.ID作为表名,根据文件类型自动加载数据