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:
@@ -20,7 +20,7 @@ type KnowledgeService interface {
|
||||
metadata map[string]string,
|
||||
enableMultimodel *bool,
|
||||
customFileName string,
|
||||
tagID string,
|
||||
tagIDs []string,
|
||||
channel string,
|
||||
processOverrides *types.KnowledgeProcessOverrides,
|
||||
) (*types.Knowledge, error)
|
||||
@@ -36,7 +36,7 @@ type KnowledgeService interface {
|
||||
fileType string,
|
||||
enableMultimodel *bool,
|
||||
title string,
|
||||
tagID string,
|
||||
tagIDs []string,
|
||||
channel string,
|
||||
processOverrides *types.KnowledgeProcessOverrides,
|
||||
) (*types.Knowledge, error)
|
||||
@@ -152,7 +152,11 @@ type KnowledgeService interface {
|
||||
// UpdateKnowledgeTagBatch updates tag for document knowledge items in batch.
|
||||
// authorizedKBID restricts all updates to knowledge items belonging to this KB;
|
||||
// pass empty string to skip (caller must ensure authorization by other means).
|
||||
UpdateKnowledgeTagBatch(ctx context.Context, authorizedKBID string, updates map[string]*string) error
|
||||
UpdateKnowledgeTagBatch(ctx context.Context, authorizedKBID string, updates map[string][]string) error
|
||||
// SetKnowledgeTags replaces all tags for a single knowledge entry.
|
||||
SetKnowledgeTags(ctx context.Context, knowledgeID string, tagIDs []string) error
|
||||
// GetKnowledgeTags returns tags for multiple knowledge IDs.
|
||||
GetKnowledgeTags(ctx context.Context, knowledgeIDs []string) (map[string][]*types.KnowledgeTag, error)
|
||||
// UpdateFAQEntryTagBatch updates tag for FAQ entries in batch.
|
||||
// Key: entry seq_id, Value: tag seq_id (nil to remove tag)
|
||||
UpdateFAQEntryTagBatch(ctx context.Context, kbID string, updates map[int64]*int64) error
|
||||
@@ -259,6 +263,12 @@ type KnowledgeRepository interface {
|
||||
FindByMetadataKey(ctx context.Context, tenantID uint64, kbID string, key string, value string) (*types.Knowledge, error)
|
||||
// SearchKnowledgeInScopes searches knowledge items by keyword within the given (tenant_id, kb_id) scopes (own + shared).
|
||||
SearchKnowledgeInScopes(ctx context.Context, scopes []types.KnowledgeSearchScope, keyword string, offset, limit int, fileTypes []string) ([]*types.Knowledge, bool, error)
|
||||
// ListIDsByTagID returns all knowledge IDs that have the specified tag ID.
|
||||
ListIDsByTagID(ctx context.Context, tenantID uint64, kbID, tagID string) ([]string, error)
|
||||
// ListIDsByTagIDs returns all knowledge IDs that have any of the specified tag IDs (OR semantics).
|
||||
ListIDsByTagIDs(ctx context.Context, tenantID uint64, kbID string, tagIDs []string) ([]string, error)
|
||||
// SetKnowledgeTags replaces all tags for a single knowledge entry (deletes old, inserts new).
|
||||
SetKnowledgeTags(ctx context.Context, knowledgeID string, tagIDs []string) error
|
||||
// GetKnowledgeTags returns tags for multiple knowledge IDs.
|
||||
GetKnowledgeTags(ctx context.Context, knowledgeIDs []string) (map[string][]*types.KnowledgeTag, error)
|
||||
// DeleteKnowledgeTagRelations deletes all tag relations for a knowledge entry.
|
||||
DeleteKnowledgeTagRelations(ctx context.Context, knowledgeID string) error
|
||||
}
|
||||
|
||||
@@ -88,8 +88,8 @@ const (
|
||||
// KnowledgeListFilter aggregates optional filters for listing knowledge entries
|
||||
// under a knowledge base. Empty / zero fields mean "no filter on that dimension".
|
||||
type KnowledgeListFilter struct {
|
||||
// TagID filters by tag_id when non-empty.
|
||||
TagID string
|
||||
// TagIDs filters by multiple tags (OR semantics: match any of the given tags).
|
||||
TagIDs []string
|
||||
// Keyword performs a LIKE match on file_name / title when non-empty.
|
||||
Keyword string
|
||||
// FileType filters by file_type, or by type for the special values "manual" / "url".
|
||||
@@ -116,8 +116,8 @@ type Knowledge struct {
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
// ID of the knowledge base
|
||||
KnowledgeBaseID string `json:"knowledge_base_id"`
|
||||
// Optional tag ID for categorization within a knowledge base
|
||||
TagID string `json:"tag_id" gorm:"type:varchar(36);index"`
|
||||
// Tags holds the tags associated with this knowledge (populated on query, not persisted directly).
|
||||
Tags []*KnowledgeTag `json:"tags" gorm:"-"`
|
||||
// Type of the knowledge
|
||||
Type string `json:"type"`
|
||||
// Title of the knowledge
|
||||
@@ -208,7 +208,7 @@ type ManualKnowledgePayload struct {
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
Status string `json:"status"`
|
||||
TagID string `json:"tag_id"`
|
||||
TagIDs []string `json:"tag_ids"`
|
||||
Channel string `json:"channel"`
|
||||
ProcessConfig *KnowledgeProcessOverrides `json:"process_config,omitempty"`
|
||||
}
|
||||
|
||||
@@ -64,3 +64,16 @@ type TagReferenceCounts struct {
|
||||
KnowledgeCount int64
|
||||
ChunkCount int64
|
||||
}
|
||||
|
||||
// KnowledgeTagRelation represents a many-to-many association between
|
||||
// a document knowledge entry and a tag in the knowledge_tag_relations table.
|
||||
type KnowledgeTagRelation struct {
|
||||
KnowledgeID string `gorm:"type:varchar(36);primaryKey"`
|
||||
TagID string `gorm:"type:varchar(36);primaryKey"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||
}
|
||||
|
||||
// TableName overrides the default table name.
|
||||
func (KnowledgeTagRelation) TableName() string {
|
||||
return "knowledge_tag_relations"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user