diff --git a/internal/application/service/chat_pipeline/data_analysis.go b/internal/application/service/chat_pipeline/data_analysis.go index 02a636c3..389d9c97 100644 --- a/internal/application/service/chat_pipeline/data_analysis.go +++ b/internal/application/service/chat_pipeline/data_analysis.go @@ -125,14 +125,15 @@ func (p *PluginDataAnalysis) OnEvent( // 5. Store result // Create a new SearchResult for the analysis output analysisResult := &types.SearchResult{ - ID: "analysis_" + knowledge.ID, - Content: toolResult.Output, - Score: 1.0, - MatchType: types.MatchTypeDataAnalysis, - KnowledgeID: knowledge.ID, - KnowledgeTitle: knowledge.Title, - KnowledgeFilename: knowledge.FileName, - KnowledgeDescription: knowledge.Description, + ID: "analysis_" + knowledge.ID, + Content: toolResult.Output, + Score: 1.0, + MatchType: types.MatchTypeDataAnalysis, + KnowledgeID: knowledge.ID, + KnowledgeTitle: knowledge.Title, + KnowledgeFilename: knowledge.FileName, + KnowledgeDescription: knowledge.Description, + KnowledgeCustomMetadata: knowledge.CustomMetadataText(), } chatManage.MergeResult = append(chatManage.MergeResult, analysisResult) diff --git a/internal/application/service/chat_pipeline/search_entity.go b/internal/application/service/chat_pipeline/search_entity.go index 25cd9867..59b20022 100644 --- a/internal/application/service/chat_pipeline/search_entity.go +++ b/internal/application/service/chat_pipeline/search_entity.go @@ -241,26 +241,28 @@ func filterSeenChunk(ctx context.Context, graph *types.GraphData, searchResult [ // score for the missing retrieval score. func chunk2SearchResult(chunk *types.Chunk, knowledge *types.Knowledge) *types.SearchResult { return &types.SearchResult{ - ID: chunk.ID, - Content: chunk.Content, - ContentRevision: chunk.ContentRevision, - KnowledgeID: chunk.KnowledgeID, - ChunkIndex: chunk.ChunkIndex, - KnowledgeTitle: knowledge.Title, - StartAt: chunk.StartAt, - EndAt: chunk.EndAt, - Seq: chunk.ChunkIndex, - Score: 0, - MatchType: types.MatchTypeGraph, - Metadata: knowledge.GetMetadata(), - ChunkType: string(chunk.ChunkType), - ParentChunkID: chunk.ParentChunkID, - ImageInfo: chunk.ImageInfo, - KnowledgeFilename: knowledge.FileName, - KnowledgeSource: knowledge.Source, - KnowledgeChannel: knowledge.Channel, - ChunkMetadata: chunk.Metadata, - KnowledgeBaseID: knowledge.KnowledgeBaseID, - SourceLocators: chunk.SourceLocators, + ID: chunk.ID, + Content: chunk.Content, + ContentRevision: chunk.ContentRevision, + KnowledgeID: chunk.KnowledgeID, + ChunkIndex: chunk.ChunkIndex, + KnowledgeTitle: knowledge.Title, + StartAt: chunk.StartAt, + EndAt: chunk.EndAt, + Seq: chunk.ChunkIndex, + Score: 0, + MatchType: types.MatchTypeGraph, + Metadata: knowledge.GetMetadata(), + ChunkType: string(chunk.ChunkType), + ParentChunkID: chunk.ParentChunkID, + ImageInfo: chunk.ImageInfo, + KnowledgeFilename: knowledge.FileName, + KnowledgeSource: knowledge.Source, + KnowledgeChannel: knowledge.Channel, + KnowledgeDescription: knowledge.Description, + KnowledgeCustomMetadata: knowledge.CustomMetadataText(), + ChunkMetadata: chunk.Metadata, + KnowledgeBaseID: knowledge.KnowledgeBaseID, + SourceLocators: chunk.SourceLocators, } } diff --git a/internal/application/service/chat_pipeline/search_entity_metadata_test.go b/internal/application/service/chat_pipeline/search_entity_metadata_test.go new file mode 100644 index 00000000..1ba11a56 --- /dev/null +++ b/internal/application/service/chat_pipeline/search_entity_metadata_test.go @@ -0,0 +1,40 @@ +package chatpipeline + +import ( + "testing" + + "github.com/Tencent/WeKnora/internal/types" + "github.com/stretchr/testify/require" +) + +func TestChunk2SearchResultIncludesCustomMetadata(t *testing.T) { + t.Parallel() + knowledge := &types.Knowledge{ + Title: "Course", + Description: "desc", + FileName: "lesson.md", + Source: "api", + Channel: "web", + KnowledgeBaseID: "kb-1", + CustomMetadata: types.JSON(`{ + "lesson_url":"https://example.com/lesson", + "course_url":"https://example.com/course" + }`), + } + chunk := &types.Chunk{ + ID: "chunk-1", + KnowledgeID: "knowledge-1", + Content: "body", + ChunkType: types.ChunkTypeText, + } + + got := chunk2SearchResult(chunk, knowledge) + + require.Equal(t, + "course_url: https://example.com/course\nlesson_url: https://example.com/lesson", + got.KnowledgeCustomMetadata, + ) + require.Equal(t, "desc", got.KnowledgeDescription) + require.Equal(t, "web", got.KnowledgeChannel) + require.Equal(t, 0.0, got.Score) +} diff --git a/internal/handler/session/helpers.go b/internal/handler/session/helpers.go index 091d6ddc..41a7cbea 100644 --- a/internal/handler/session/helpers.go +++ b/internal/handler/session/helpers.go @@ -463,22 +463,25 @@ func getFloat64(m map[string]interface{}, key string) float64 { // through JSON/Redis serialization, preserving all fields including metadata. func searchResultFromMap(refMap map[string]interface{}) *types.SearchResult { sr := &types.SearchResult{ - ID: getString(refMap, "id"), - Content: getString(refMap, "content"), - KnowledgeID: getString(refMap, "knowledge_id"), - ChunkIndex: int(getFloat64(refMap, "chunk_index")), - KnowledgeTitle: getString(refMap, "knowledge_title"), - StartAt: int(getFloat64(refMap, "start_at")), - EndAt: int(getFloat64(refMap, "end_at")), - Seq: int(getFloat64(refMap, "seq")), - Score: getFloat64(refMap, "score"), - ChunkType: getString(refMap, "chunk_type"), - ParentChunkID: getString(refMap, "parent_chunk_id"), - ImageInfo: getString(refMap, "image_info"), - KnowledgeFilename: getString(refMap, "knowledge_filename"), - KnowledgeSource: getString(refMap, "knowledge_source"), - KnowledgeDescription: getString(refMap, "knowledge_description"), - KnowledgeBaseID: getString(refMap, "knowledge_base_id"), + ID: getString(refMap, "id"), + Content: getString(refMap, "content"), + KnowledgeID: getString(refMap, "knowledge_id"), + ChunkIndex: int(getFloat64(refMap, "chunk_index")), + KnowledgeTitle: getString(refMap, "knowledge_title"), + StartAt: int(getFloat64(refMap, "start_at")), + EndAt: int(getFloat64(refMap, "end_at")), + Seq: int(getFloat64(refMap, "seq")), + Score: getFloat64(refMap, "score"), + ChunkType: getString(refMap, "chunk_type"), + ParentChunkID: getString(refMap, "parent_chunk_id"), + ImageInfo: getString(refMap, "image_info"), + KnowledgeFilename: getString(refMap, "knowledge_filename"), + KnowledgeSource: getString(refMap, "knowledge_source"), + KnowledgeChannel: getString(refMap, "knowledge_channel"), + KnowledgeDescription: getString(refMap, "knowledge_description"), + KnowledgeCustomMetadata: getString(refMap, "knowledge_custom_metadata"), + MatchedContent: getString(refMap, "matched_content"), + KnowledgeBaseID: getString(refMap, "knowledge_base_id"), } if meta, ok := refMap["metadata"].(map[string]interface{}); ok { metadata := make(map[string]string) diff --git a/internal/handler/session/helpers_test.go b/internal/handler/session/helpers_test.go index 1682fc5a..c105bc24 100644 --- a/internal/handler/session/helpers_test.go +++ b/internal/handler/session/helpers_test.go @@ -62,23 +62,26 @@ func TestValidateUnscopedTagIDs(t *testing.T) { // survive. func TestSearchResultFromMap_RoundTrip(t *testing.T) { original := &types.SearchResult{ - ID: "chunk-1", - Content: "first part\nsecond part", - KnowledgeID: "knowledge-1", - ChunkIndex: 3, - KnowledgeTitle: "title", - StartAt: 10, - EndAt: 20, - Seq: 2, - Score: 4.5, - ChunkType: "text", - ParentChunkID: "parent-1", - ImageInfo: `[{"url":"cdn.example.com"}]`, - KnowledgeFilename: "doc.txt", - KnowledgeSource: "upload", - KnowledgeDescription: "desc", - KnowledgeBaseID: "kb-1", - Metadata: map[string]string{"page": "3"}, + ID: "chunk-1", + Content: "first part\nsecond part", + KnowledgeID: "knowledge-1", + ChunkIndex: 3, + KnowledgeTitle: "title", + StartAt: 10, + EndAt: 20, + Seq: 2, + Score: 4.5, + ChunkType: "text", + ParentChunkID: "parent-1", + ImageInfo: `[{"url":"cdn.example.com"}]`, + KnowledgeFilename: "doc.txt", + KnowledgeSource: "upload", + KnowledgeChannel: "api", + KnowledgeDescription: "desc", + KnowledgeCustomMetadata: "course_url: https://example.com/course\nlesson_url: https://example.com/lesson", + MatchedContent: "matched question", + KnowledgeBaseID: "kb-1", + Metadata: map[string]string{"page": "3"}, MatchedImages: []types.MatchedImage{ {ChunkID: "image-1", KnowledgeBaseID: "kb-1", URL: "resource://image"}, }, @@ -110,6 +113,9 @@ func TestSearchResultFromMap_RoundTrip(t *testing.T) { assert.Equal(t, original.KnowledgeFilename, got.KnowledgeFilename) assert.Equal(t, original.KnowledgeSource, got.KnowledgeSource) assert.Equal(t, original.KnowledgeDescription, got.KnowledgeDescription) + assert.Equal(t, original.KnowledgeChannel, got.KnowledgeChannel) + assert.Equal(t, original.KnowledgeCustomMetadata, got.KnowledgeCustomMetadata) + assert.Equal(t, original.MatchedContent, got.MatchedContent) assert.Equal(t, original.KnowledgeBaseID, got.KnowledgeBaseID) assert.Equal(t, original.Metadata, got.Metadata) assert.Equal(t, original.SourceLocators, got.SourceLocators)