feat: support multi-tag for document knowledge (#1790)
* feat: support multi-tag for document knowledge - Add knowledge_tag_relations join table for many-to-many tag-knowledge mapping - Migrate existing single tag_id data to the new table - Update Knowledge struct, filter, payload types (TagID -> Tags/TagIDs) - Rewrite repository layer: SetKnowledgeTags, GetKnowledgeTags, ListIDsByTagIDs - Update tag count references to query knowledge_tag_relations - Update service/handler layers for multi-tag create/list/batch-update - Frontend: multi-select tag sidebar, multi-tag chip display, tag toggle in popup - FAQ tags remain single-tag model (unchanged) * feat(knowledge): tag edit dialog with dynamic overflow and inline creation - Add TagEditDialog component with search, multi-select, and confirm - Replace inline t-popup tag selector with dialog-based editing - Tags overflow with +N badge, visible count adapts to container width via ResizeObserver - Date/card layout: fixed date width, tags flex to fill remaining space - Add persistent new-tag input bar in dialog (Enter to create, auto-select) - Tag creation emits callback to refresh parent tag list immediately - Apply same overflow + dialog trigger to both card view and list view - Add i18n keys: tagEditDialogTitle, tagEditSearch, tagSelectedCount, tagOverflowTip, tagNewPlaceholder * fix: exclude soft-deleted knowledge from tag reference counts CountReferences, BatchCountReferences, and DeleteUnusedTags now JOIN with knowledges table to filter out soft-deleted records when counting knowledge_tag_relations. Previously they queried knowledge_tag_relations directly without the deleted_at check, causing stale tag counts after knowledge deletion. * fix: refresh tag counts after async knowledge deletion completes Add a second delayed tag list refresh (2.5s) after tag deletion to capture updated reference counts for remaining tags. The original immediate refresh handles removing the deleted tag from the list, while this delayed refresh ensures other tags' counts are correct after the async deletion finishes. --------- Co-authored-by: lyingbug <xiayula2014@gmail.com>
This commit is contained in:
@@ -463,6 +463,14 @@ func (s *knowledgeService) GetKnowledgeByID(ctx context.Context, id string) (*ty
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Load tags for this knowledge
|
||||
tagMap, err := s.repo.GetKnowledgeTags(ctx, []string{knowledge.ID})
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "Failed to load tags for knowledge %s: %v", knowledge.ID, err)
|
||||
} else if tags, ok := tagMap[knowledge.ID]; ok {
|
||||
knowledge.Tags = tags
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Knowledge retrieved successfully, ID: %s, type: %s", knowledge.ID, knowledge.Type)
|
||||
return knowledge, nil
|
||||
}
|
||||
@@ -512,6 +520,25 @@ func (s *knowledgeService) ListPagedKnowledgeByKnowledgeBaseID(ctx context.Conte
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Batch load tags for all knowledge entries
|
||||
if len(knowledges) > 0 {
|
||||
ids := make([]string, len(knowledges))
|
||||
for i, k := range knowledges {
|
||||
ids[i] = k.ID
|
||||
}
|
||||
tagMap, err := s.repo.GetKnowledgeTags(ctx, ids)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to load tags for knowledge list: %v", err)
|
||||
// Non-fatal: continue without tags
|
||||
} else {
|
||||
for _, k := range knowledges {
|
||||
if tags, ok := tagMap[k.ID]; ok {
|
||||
k.Tags = tags
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return types.NewPageResult(total, page, knowledges), nil
|
||||
}
|
||||
|
||||
@@ -629,34 +656,44 @@ func (s *knowledgeService) GetKnowledgeBatchWithSharedAccess(ctx context.Context
|
||||
return ownList, nil
|
||||
}
|
||||
|
||||
// UpdateKnowledgeTag updates the tag assigned to a knowledge document.
|
||||
func (s *knowledgeService) UpdateKnowledgeTag(ctx context.Context, knowledgeID string, tagID *string) error {
|
||||
// SetKnowledgeTags replaces all tags for a single knowledge entry.
|
||||
func (s *knowledgeService) SetKnowledgeTags(ctx context.Context, knowledgeID string, tagIDs []string) error {
|
||||
return s.repo.SetKnowledgeTags(ctx, knowledgeID, tagIDs)
|
||||
}
|
||||
|
||||
// GetKnowledgeTags returns tags for multiple knowledge IDs.
|
||||
func (s *knowledgeService) GetKnowledgeTags(ctx context.Context, knowledgeIDs []string) (map[string][]*types.KnowledgeTag, error) {
|
||||
return s.repo.GetKnowledgeTags(ctx, knowledgeIDs)
|
||||
}
|
||||
|
||||
// UpdateKnowledgeTag updates the tags assigned to a knowledge document.
|
||||
func (s *knowledgeService) UpdateKnowledgeTag(ctx context.Context, knowledgeID string, tagIDs []string) error {
|
||||
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
|
||||
knowledge, err := s.repo.GetKnowledgeByID(ctx, tenantID, knowledgeID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var resolvedTagID string
|
||||
if tagID != nil && *tagID != "" {
|
||||
tag, err := s.tagRepo.GetByID(ctx, tenantID, *tagID)
|
||||
if err != nil {
|
||||
return err
|
||||
// Validate all tag IDs
|
||||
if len(tagIDs) > 0 {
|
||||
for _, tagID := range tagIDs {
|
||||
tag, err := s.tagRepo.GetByID(ctx, tenantID, tagID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.KnowledgeBaseID != knowledge.KnowledgeBaseID {
|
||||
return werrors.NewBadRequestError("标签不属于当前知识库")
|
||||
}
|
||||
}
|
||||
if tag.KnowledgeBaseID != knowledge.KnowledgeBaseID {
|
||||
return werrors.NewBadRequestError("标签不属于当前知识库")
|
||||
}
|
||||
resolvedTagID = tag.ID
|
||||
}
|
||||
|
||||
knowledge.TagID = resolvedTagID
|
||||
return s.repo.UpdateKnowledge(ctx, knowledge)
|
||||
return s.repo.SetKnowledgeTags(ctx, knowledgeID, tagIDs)
|
||||
}
|
||||
|
||||
// UpdateKnowledgeTagBatch updates tags for document knowledge items in batch.
|
||||
// authorizedKBID restricts all updates to knowledge items belonging to this KB;
|
||||
// pass empty string to skip the check (caller must ensure authorization by other means).
|
||||
func (s *knowledgeService) UpdateKnowledgeTagBatch(ctx context.Context, authorizedKBID string, updates map[string]*string) error {
|
||||
func (s *knowledgeService) UpdateKnowledgeTagBatch(ctx context.Context, authorizedKBID string, updates map[string][]string) error {
|
||||
if len(updates) == 0 {
|
||||
return nil
|
||||
}
|
||||
@@ -692,22 +729,24 @@ func (s *knowledgeService) UpdateKnowledgeTagBatch(ctx context.Context, authoriz
|
||||
}
|
||||
}
|
||||
|
||||
// Build tag ID map for validation
|
||||
// Collect all unique tag IDs for validation
|
||||
tagIDSet := make(map[string]bool)
|
||||
for _, tagID := range updates {
|
||||
if tagID != nil && *tagID != "" {
|
||||
tagIDSet[*tagID] = true
|
||||
for _, tagIDs := range updates {
|
||||
for _, tagID := range tagIDs {
|
||||
if tagID != "" {
|
||||
tagIDSet[tagID] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Validate all tags in batch
|
||||
// Validate all tags exist and belong to the correct KB
|
||||
tagMap := make(map[string]*types.KnowledgeTag)
|
||||
if len(tagIDSet) > 0 {
|
||||
tagIDs := make([]string, 0, len(tagIDSet))
|
||||
tagIDList := make([]string, 0, len(tagIDSet))
|
||||
for tagID := range tagIDSet {
|
||||
tagIDs = append(tagIDs, tagID)
|
||||
tagIDList = append(tagIDList, tagID)
|
||||
}
|
||||
for _, tagID := range tagIDs {
|
||||
for _, tagID := range tagIDList {
|
||||
tag, err := s.tagRepo.GetByID(ctx, tenantID, tagID)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -716,32 +755,31 @@ func (s *knowledgeService) UpdateKnowledgeTagBatch(ctx context.Context, authoriz
|
||||
}
|
||||
}
|
||||
|
||||
// Update knowledge items
|
||||
knowledgeToUpdate := make([]*types.Knowledge, 0)
|
||||
// Validate tag ownership per knowledge
|
||||
for _, knowledge := range knowledgeList {
|
||||
tagID, exists := updates[knowledge.ID]
|
||||
tagIDs, exists := updates[knowledge.ID]
|
||||
if !exists {
|
||||
continue
|
||||
}
|
||||
|
||||
var resolvedTagID string
|
||||
if tagID != nil && *tagID != "" {
|
||||
tag, ok := tagMap[*tagID]
|
||||
for _, tagID := range tagIDs {
|
||||
if tagID == "" {
|
||||
continue
|
||||
}
|
||||
tag, ok := tagMap[tagID]
|
||||
if !ok {
|
||||
return werrors.NewBadRequestError(fmt.Sprintf("标签 %s 不存在", *tagID))
|
||||
return werrors.NewBadRequestError(fmt.Sprintf("标签 %s 不存在", tagID))
|
||||
}
|
||||
if tag.KnowledgeBaseID != knowledge.KnowledgeBaseID {
|
||||
return werrors.NewBadRequestError(fmt.Sprintf("标签 %s 不属于知识库 %s", *tagID, knowledge.KnowledgeBaseID))
|
||||
return werrors.NewBadRequestError(fmt.Sprintf("标签 %s 不属于知识库 %s", tagID, knowledge.KnowledgeBaseID))
|
||||
}
|
||||
resolvedTagID = tag.ID
|
||||
}
|
||||
|
||||
knowledge.TagID = resolvedTagID
|
||||
knowledgeToUpdate = append(knowledgeToUpdate, knowledge)
|
||||
}
|
||||
|
||||
if len(knowledgeToUpdate) > 0 {
|
||||
return s.repo.UpdateKnowledgeBatch(ctx, knowledgeToUpdate)
|
||||
// Set tags for each knowledge
|
||||
for knowledgeID, tagIDs := range updates {
|
||||
if err := s.repo.SetKnowledgeTags(ctx, knowledgeID, tagIDs); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user