From bb95488ac81ab64a22f0dca61c80829eaa3b9759 Mon Sep 17 00:00:00 2001 From: wolfkill <27876473+wolfkill@users.noreply.github.com> Date: Wed, 20 May 2026 18:02:22 +0800 Subject: [PATCH] fix(knowledge): complete indexed documents immediately --- .../application/service/knowledge_process.go | 43 +++++++++--- .../service/knowledge_process_status_test.go | 70 +++++++++++++++++++ 2 files changed, 105 insertions(+), 8 deletions(-) create mode 100644 internal/application/service/knowledge_process_status_test.go diff --git a/internal/application/service/knowledge_process.go b/internal/application/service/knowledge_process.go index bd1ddd8d..da01cce5 100644 --- a/internal/application/service/knowledge_process.go +++ b/internal/application/service/knowledge_process.go @@ -142,6 +142,34 @@ type ProcessChunksOptions struct { Metadata map[string]string } +// finalizeIndexedKnowledgeState marks a document searchable as soon as chunks +// and indexes are persisted; post-processing tasks should not keep parsing UI +// indicators alive once retrieval can use the document. +func finalizeIndexedKnowledgeState( + knowledge *types.Knowledge, + totalStorageSize int64, + textChunkCount int, + hasPendingMultimodal bool, + now time.Time, +) { + if hasPendingMultimodal { + knowledge.ParseStatus = types.ParseStatusProcessing + knowledge.SummaryStatus = types.SummaryStatusNone + } else { + knowledge.ParseStatus = types.ParseStatusCompleted + if textChunkCount > 0 { + knowledge.SummaryStatus = types.SummaryStatusPending + } else { + knowledge.SummaryStatus = types.SummaryStatusNone + } + } + + knowledge.EnableStatus = "enabled" + knowledge.StorageSize = totalStorageSize + knowledge.ProcessedAt = &now + knowledge.UpdatedAt = now +} + // buildSplitterConfig creates a SplitterConfig with fallbacks from a KnowledgeBase. // Defaults mirror chunker.DefaultChunkSize / DefaultChunkOverlap so behavior is // identical whether callers come through this path or invoke the chunker @@ -559,15 +587,14 @@ func (s *knowledgeService) processChunks(ctx context.Context, pendingMultimodal := isImage && options.EnableMultimodel && len(options.StoredImages) > 0 pendingPDFMultimodal := !isImage && !isVideo && options.EnableMultimodel && len(options.StoredImages) > 0 - // For image files or documents with pending multimodal processing, keep "processing" status - if pendingMultimodal || pendingPDFMultimodal { - knowledge.ParseStatus = types.ParseStatusProcessing - } - knowledge.EnableStatus = "enabled" - knowledge.StorageSize = totalStorageSize now := time.Now() - knowledge.ProcessedAt = &now - knowledge.UpdatedAt = now + finalizeIndexedKnowledgeState( + knowledge, + totalStorageSize, + len(textChunks), + pendingMultimodal || pendingPDFMultimodal, + now, + ) if err := s.repo.UpdateKnowledge(ctx, knowledge); err != nil { logger.GetLogger(ctx).WithField("error", err).Errorf("processChunks update knowledge failed") diff --git a/internal/application/service/knowledge_process_status_test.go b/internal/application/service/knowledge_process_status_test.go new file mode 100644 index 00000000..d7e815bc --- /dev/null +++ b/internal/application/service/knowledge_process_status_test.go @@ -0,0 +1,70 @@ +package service + +import ( + "testing" + "time" + + "github.com/Tencent/WeKnora/internal/types" +) + +func TestFinalizeIndexedKnowledgeState(t *testing.T) { + now := time.Date(2026, 5, 20, 12, 0, 0, 0, time.UTC) + + tests := []struct { + name string + hasPendingMultimodal bool + textChunkCount int + wantParseStatus string + wantSummaryStatus string + }{ + { + name: "text document is completed once chunks are indexed", + textChunkCount: 2, + wantParseStatus: types.ParseStatusCompleted, + wantSummaryStatus: types.SummaryStatusPending, + }, + { + name: "empty indexed document is completed without summary work", + textChunkCount: 0, + wantParseStatus: types.ParseStatusCompleted, + wantSummaryStatus: types.SummaryStatusNone, + }, + { + name: "document waits while multimodal image work is pending", + hasPendingMultimodal: true, + textChunkCount: 2, + wantParseStatus: types.ParseStatusProcessing, + wantSummaryStatus: types.SummaryStatusNone, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + knowledge := &types.Knowledge{ + ParseStatus: types.ParseStatusProcessing, + SummaryStatus: types.SummaryStatusCompleted, + } + + finalizeIndexedKnowledgeState(knowledge, 4096, tt.textChunkCount, tt.hasPendingMultimodal, now) + + if knowledge.ParseStatus != tt.wantParseStatus { + t.Fatalf("ParseStatus = %q, want %q", knowledge.ParseStatus, tt.wantParseStatus) + } + if knowledge.SummaryStatus != tt.wantSummaryStatus { + t.Fatalf("SummaryStatus = %q, want %q", knowledge.SummaryStatus, tt.wantSummaryStatus) + } + if knowledge.EnableStatus != "enabled" { + t.Fatalf("EnableStatus = %q, want enabled", knowledge.EnableStatus) + } + if knowledge.StorageSize != 4096 { + t.Fatalf("StorageSize = %d, want 4096", knowledge.StorageSize) + } + if knowledge.ProcessedAt == nil || !knowledge.ProcessedAt.Equal(now) { + t.Fatalf("ProcessedAt = %v, want %v", knowledge.ProcessedAt, now) + } + if !knowledge.UpdatedAt.Equal(now) { + t.Fatalf("UpdatedAt = %v, want %v", knowledge.UpdatedAt, now) + } + }) + } +}