From 3546888317996b3212ea851160924a3e2ab522f9 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Wed, 17 Dec 2025 21:23:24 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E7=9F=A5=E8=AF=86?= =?UTF-8?q?=E5=BA=93=E5=A4=8D=E5=88=B6=E6=97=B6=E7=9A=84=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E6=98=A0=E5=B0=84=E9=80=BB=E8=BE=91=E5=92=8CFAQ=E5=85=8B?= =?UTF-8?q?=E9=9A=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/application/service/knowledge.go | 35 ++++++++++++++++------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/internal/application/service/knowledge.go b/internal/application/service/knowledge.go index 200051e2..46350410 100644 --- a/internal/application/service/knowledge.go +++ b/internal/application/service/knowledge.go @@ -2565,7 +2565,7 @@ func (s *knowledgeService) CloneChunk(ctx context.Context, src, dst *types.Knowl targetTagID = mappedTagID } else { // Try to find or create the tag in target knowledge base - targetTagID = s.getOrCreateTagInTarget(ctx, src, dst, sourceChunk.TagID, tagIDMapping) + targetTagID = s.getOrCreateTagInTarget(ctx, src.TenantID, dst.TenantID, dst.KnowledgeBaseID, sourceChunk.TagID, tagIDMapping) } } @@ -2725,8 +2725,7 @@ func (s *knowledgeService) UpsertFAQEntries(ctx context.Context, return "", fmt.Errorf("failed to initialize task: %w", err) } - logger.Infof(ctx, "Allocated ChunkIndex range [%d, %d] for FAQ import task %s, next ChunkIndex will be %d", - taskID) + logger.Infof(ctx, "FAQ import task initialized: %s, total entries: %d", taskID, len(payload.Entries)) // Enqueue FAQ import task to Asynq logger.Info(ctx, "Enqueuing FAQ import task to Asynq") @@ -5492,6 +5491,7 @@ func (s *knowledgeService) cloneFAQKnowledgeBase( // Clone FAQ chunks from source to destination batch := 50 + tagIDMapping := map[string]string{} // srcTagID -> dstTagID for i := 0; i < len(chunksToAdd); i += batch { end := i + batch if end > len(chunksToAdd) { @@ -5510,12 +5510,23 @@ func (s *knowledgeService) cloneFAQKnowledgeBase( // Create new chunks for destination newChunks := make([]*types.Chunk, 0, len(srcChunks)) for _, srcChunk := range srcChunks { + // Map TagID to target knowledge base + targetTagID := "" + if srcChunk.TagID != "" { + if mappedTagID, ok := tagIDMapping[srcChunk.TagID]; ok { + targetTagID = mappedTagID + } else { + // Try to find or create the tag in target knowledge base + targetTagID = s.getOrCreateTagInTarget(ctx, srcKB.TenantID, dstKB.TenantID, dstKB.ID, srcChunk.TagID, tagIDMapping) + } + } + newChunk := &types.Chunk{ ID: uuid.New().String(), TenantID: dstKB.TenantID, KnowledgeID: dstKnowledge.ID, KnowledgeBaseID: dstKB.ID, - TagID: srcChunk.TagID, + TagID: targetTagID, Content: srcChunk.Content, ChunkIndex: srcChunk.ChunkIndex, IsEnabled: srcChunk.IsEnabled, @@ -5523,7 +5534,10 @@ func (s *knowledgeService) cloneFAQKnowledgeBase( ChunkType: types.ChunkTypeFAQ, Metadata: srcChunk.Metadata, ContentHash: srcChunk.ContentHash, + ImageInfo: srcChunk.ImageInfo, Status: int(types.ChunkStatusStored), // Initially stored, will be indexed + CreatedAt: time.Now(), + UpdatedAt: time.Now(), } newChunks = append(newChunks, newChunk) } @@ -5653,12 +5667,13 @@ func (s *knowledgeService) GetKBCloneProgress(ctx context.Context, taskID string // The mapping is cached in tagIDMapping for subsequent lookups. func (s *knowledgeService) getOrCreateTagInTarget( ctx context.Context, - src, dst *types.Knowledge, + srcTenantID, dstTenantID uint64, + dstKnowledgeBaseID string, srcTagID string, tagIDMapping map[string]string, ) string { // Get source tag - srcTag, err := s.tagRepo.GetByID(ctx, src.TenantID, srcTagID) + srcTag, err := s.tagRepo.GetByID(ctx, srcTenantID, srcTagID) if err != nil || srcTag == nil { logger.Warnf(ctx, "Failed to get source tag %s: %v", srcTagID, err) tagIDMapping[srcTagID] = "" // Cache empty result to avoid repeated lookups @@ -5666,7 +5681,7 @@ func (s *knowledgeService) getOrCreateTagInTarget( } // Try to find existing tag with same name in target KB - dstTag, err := s.tagRepo.GetByName(ctx, dst.TenantID, dst.KnowledgeBaseID, srcTag.Name) + dstTag, err := s.tagRepo.GetByName(ctx, dstTenantID, dstKnowledgeBaseID, srcTag.Name) if err == nil && dstTag != nil { tagIDMapping[srcTagID] = dstTag.ID return dstTag.ID @@ -5675,8 +5690,8 @@ func (s *knowledgeService) getOrCreateTagInTarget( // Create new tag in target KB newTag := &types.KnowledgeTag{ ID: uuid.New().String(), - TenantID: dst.TenantID, - KnowledgeBaseID: dst.KnowledgeBaseID, + TenantID: dstTenantID, + KnowledgeBaseID: dstKnowledgeBaseID, Name: srcTag.Name, Color: srcTag.Color, SortOrder: srcTag.SortOrder, @@ -5690,6 +5705,6 @@ func (s *knowledgeService) getOrCreateTagInTarget( } tagIDMapping[srcTagID] = newTag.ID - logger.Infof(ctx, "Created tag %s (ID: %s) in target KB %s", newTag.Name, newTag.ID, dst.KnowledgeBaseID) + logger.Infof(ctx, "Created tag %s (ID: %s) in target KB %s", newTag.Name, newTag.ID, dstKnowledgeBaseID) return newTag.ID }