From dafb95d46a89896309566c1c61c2476cea7c0aac Mon Sep 17 00:00:00 2001 From: wizardchen Date: Wed, 3 Dec 2025 20:35:55 +0800 Subject: [PATCH] feat: Add summary status tracking for async summary generation in knowledge base --- frontend/src/i18n/locales/en-US.ts | 1 + frontend/src/i18n/locales/ru-RU.ts | 1 + frontend/src/i18n/locales/zh-CN.ts | 1 + .../src/views/knowledge/KnowledgeBase.vue | 28 ++++++++++++----- internal/application/service/knowledge.go | 31 +++++++++++++++++++ internal/types/knowledge.go | 16 ++++++++++ .../000018_add_summary_status.down.sql | 4 +++ .../000018_add_summary_status.up.sql | 8 +++++ 8 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 migrations/versioned/000018_add_summary_status.down.sql create mode 100644 migrations/versioned/000018_add_summary_status.up.sql diff --git a/frontend/src/i18n/locales/en-US.ts b/frontend/src/i18n/locales/en-US.ts index 2b4a64aa..c50d80c3 100644 --- a/frontend/src/i18n/locales/en-US.ts +++ b/frontend/src/i18n/locales/en-US.ts @@ -142,6 +142,7 @@ export default { deleteDocument: 'Delete Document', parsingFailed: 'Parsing failed', parsingInProgress: 'Parsing...', + generatingSummary: 'Generating summary...', deleteConfirmation: 'Delete Confirmation', confirmDeleteDocument: 'Confirm deletion of document "{fileName}", recovery will be impossible after deletion', cancel: 'Cancel', diff --git a/frontend/src/i18n/locales/ru-RU.ts b/frontend/src/i18n/locales/ru-RU.ts index 646a9f27..8f765071 100644 --- a/frontend/src/i18n/locales/ru-RU.ts +++ b/frontend/src/i18n/locales/ru-RU.ts @@ -142,6 +142,7 @@ export default { deleteDocument: 'Удалить документ', parsingFailed: 'Парсинг не удался', parsingInProgress: 'Парсинг...', + generatingSummary: 'Генерация резюме...', deleteConfirmation: 'Подтверждение удаления', confirmDeleteDocument: 'Подтвердить удаление документа "{fileName}", после удаления восстановление невозможно', cancel: 'Отмена', diff --git a/frontend/src/i18n/locales/zh-CN.ts b/frontend/src/i18n/locales/zh-CN.ts index 6b601b5a..5b597bdb 100644 --- a/frontend/src/i18n/locales/zh-CN.ts +++ b/frontend/src/i18n/locales/zh-CN.ts @@ -142,6 +142,7 @@ export default { deleteDocument: "删除文档", parsingFailed: "解析失败", parsingInProgress: "解析中...", + generatingSummary: "生成摘要中...", deleteConfirmation: "删除确认", confirmDeleteDocument: '确认删除文档"{fileName}",删除后将无法恢复', cancel: "取消", diff --git a/frontend/src/views/knowledge/KnowledgeBase.vue b/frontend/src/views/knowledge/KnowledgeBase.vue index b1062963..65793acd 100644 --- a/frontend/src/views/knowledge/KnowledgeBase.vue +++ b/frontend/src/views/knowledge/KnowledgeBase.vue @@ -482,8 +482,12 @@ onUnmounted(() => { watch(() => cardList.value, (newValue) => { if (isFAQ.value) return; let analyzeList = []; + // Filter items that need polling: parsing in progress OR summary generation in progress analyzeList = newValue.filter(item => { - return item.parse_status == 'pending' || item.parse_status == 'processing'; + const isParsing = item.parse_status == 'pending' || item.parse_status == 'processing'; + const isSummaryPending = item.parse_status == 'completed' && + (item.summary_status == 'pending' || item.summary_status == 'processing'); + return isParsing || isSummaryPending; }) if (timeout !== null) { clearInterval(timeout); @@ -498,6 +502,7 @@ type KnowledgeCard = { id: string; knowledge_base_id?: string; parse_status: string; + summary_status?: string; description?: string; file_name?: string; original_file_name?: string; @@ -520,13 +525,13 @@ const updateStatus = (analyzeList: KnowledgeCard[]) => { batchQueryKnowledge(query).then((result: any) => { if (result.success && result.data) { (result.data as KnowledgeCard[]).forEach((item: KnowledgeCard) => { - if (item.parse_status == 'failed' || item.parse_status == 'completed') { - let index = cardList.value.findIndex(card => card.id == item.id); - if (index != -1) { - cardList.value[index].parse_status = item.parse_status; - cardList.value[index].description = item.description; - } - } + const index = cardList.value.findIndex(card => card.id == item.id); + if (index == -1) return; + + // Always update the card data + cardList.value[index].parse_status = item.parse_status; + cardList.value[index].summary_status = item.summary_status; + cardList.value[index].description = item.description; }); } }).catch((_err) => { @@ -1170,6 +1175,13 @@ async function createNewSession(value: string): Promise { {{ t('knowledgeBase.draft') }} {{ t('knowledgeBase.draftTip') }} +
+ + {{ t('knowledgeBase.generatingSummary') }} +
{{ item.description }}
diff --git a/internal/application/service/knowledge.go b/internal/application/service/knowledge.go index d3346611..9525c76d 100644 --- a/internal/application/service/knowledge.go +++ b/internal/application/service/knowledge.go @@ -1332,6 +1332,14 @@ func (s *knowledgeService) processChunks(ctx context.Context, now := time.Now() knowledge.ProcessedAt = &now knowledge.UpdatedAt = now + + // Set summary status based on whether summary generation will be triggered + if len(textChunks) > 0 { + knowledge.SummaryStatus = types.SummaryStatusPending + } else { + knowledge.SummaryStatus = types.SummaryStatusNone + } + if err := s.repo.UpdateKnowledge(ctx, knowledge); err != nil { logger.GetLogger(ctx).WithField("error", err).Errorf("processChunks update knowledge failed") } @@ -1538,10 +1546,27 @@ func (s *knowledgeService) ProcessSummaryGeneration(ctx context.Context, t *asyn return nil } + // Update summary status to processing + knowledge.SummaryStatus = types.SummaryStatusProcessing + knowledge.UpdatedAt = time.Now() + if err := s.repo.UpdateKnowledge(ctx, knowledge); err != nil { + logger.Warnf(ctx, "Failed to update summary status to processing: %v", err) + } + + // Helper function to mark summary as failed + markSummaryFailed := func() { + knowledge.SummaryStatus = types.SummaryStatusFailed + knowledge.UpdatedAt = time.Now() + if err := s.repo.UpdateKnowledge(ctx, knowledge); err != nil { + logger.Warnf(ctx, "Failed to update summary status to failed: %v", err) + } + } + // Get text chunks for this knowledge chunks, err := s.chunkService.ListChunksByKnowledgeID(ctx, payload.KnowledgeID) if err != nil { logger.Errorf(ctx, "Failed to get chunks: %v", err) + markSummaryFailed() return nil } @@ -1555,6 +1580,10 @@ func (s *knowledgeService) ProcessSummaryGeneration(ctx context.Context, t *asyn if len(textChunks) == 0 { logger.Infof(ctx, "No text chunks found for knowledge: %s", payload.KnowledgeID) + // Mark as completed since there's nothing to summarize + knowledge.SummaryStatus = types.SummaryStatusCompleted + knowledge.UpdatedAt = time.Now() + s.repo.UpdateKnowledge(ctx, knowledge) return nil } @@ -1567,6 +1596,7 @@ func (s *knowledgeService) ProcessSummaryGeneration(ctx context.Context, t *asyn chatModel, err := s.modelService.GetChatModel(ctx, kb.SummaryModelID) if err != nil { logger.Errorf(ctx, "Failed to get chat model: %v", err) + markSummaryFailed() return fmt.Errorf("failed to get chat model: %w", err) } @@ -1585,6 +1615,7 @@ func (s *knowledgeService) ProcessSummaryGeneration(ctx context.Context, t *asyn // Update knowledge description knowledge.Description = summary + knowledge.SummaryStatus = types.SummaryStatusCompleted knowledge.UpdatedAt = time.Now() if err := s.repo.UpdateKnowledge(ctx, knowledge); err != nil { logger.Errorf(ctx, "Failed to update knowledge description: %v", err) diff --git a/internal/types/knowledge.go b/internal/types/knowledge.go index bd8e6b5c..77627b2e 100644 --- a/internal/types/knowledge.go +++ b/internal/types/knowledge.go @@ -30,6 +30,20 @@ const ( ParseStatusDeleting = "deleting" ) +// Summary status constants for async summary generation +const ( + // SummaryStatusNone indicates no summary task is needed + SummaryStatusNone = "none" + // SummaryStatusPending indicates the summary task is waiting to be processed + SummaryStatusPending = "pending" + // SummaryStatusProcessing indicates the summary is being generated + SummaryStatusProcessing = "processing" + // SummaryStatusCompleted indicates the summary has been generated successfully + SummaryStatusCompleted = "completed" + // SummaryStatusFailed indicates the summary generation failed + SummaryStatusFailed = "failed" +) + // ManualKnowledgeFormat represents the format of the manual knowledge const ( ManualKnowledgeFormatMarkdown = "markdown" @@ -59,6 +73,8 @@ type Knowledge struct { Source string `json:"source"` // Parse status of the knowledge ParseStatus string `json:"parse_status"` + // Summary status for async summary generation + SummaryStatus string `json:"summary_status" gorm:"type:varchar(32);default:none"` // Enable status of the knowledge EnableStatus string `json:"enable_status"` // ID of the embedding model diff --git a/migrations/versioned/000018_add_summary_status.down.sql b/migrations/versioned/000018_add_summary_status.down.sql new file mode 100644 index 00000000..8a95f208 --- /dev/null +++ b/migrations/versioned/000018_add_summary_status.down.sql @@ -0,0 +1,4 @@ +-- Remove summary_status column from knowledges table + +DROP INDEX IF EXISTS idx_knowledges_summary_status; +ALTER TABLE knowledges DROP COLUMN IF EXISTS summary_status; diff --git a/migrations/versioned/000018_add_summary_status.up.sql b/migrations/versioned/000018_add_summary_status.up.sql new file mode 100644 index 00000000..1e87c3c2 --- /dev/null +++ b/migrations/versioned/000018_add_summary_status.up.sql @@ -0,0 +1,8 @@ +-- Add summary_status column to knowledges table +-- This column tracks the status of async summary generation task +-- Values: none, pending, processing, completed, failed + +ALTER TABLE knowledges ADD COLUMN IF NOT EXISTS summary_status VARCHAR(32) DEFAULT 'none'; + +-- Add index for efficient querying +CREATE INDEX IF NOT EXISTS idx_knowledges_summary_status ON knowledges(summary_status);