feat: 新增FAQ推荐标签和批量操作支持,优化标签删除逻辑

This commit is contained in:
wizardchen
2025-12-12 19:57:08 +08:00
committed by lyingbug
parent 5170b11120
commit 3d84bcfd54
23 changed files with 784 additions and 81 deletions
+35
View File
@@ -41,6 +41,38 @@ const (
ChunkStatusIndexed ChunkStatus = 2
)
// ChunkFlags 定义 Chunk 的标志位,用于管理多个布尔状态
type ChunkFlags int
const (
// ChunkFlagRecommended 表示可推荐状态(1 << 0 = 1)
// 当设置此标志时,该 Chunk 可以被推荐给用户
ChunkFlagRecommended ChunkFlags = 1 << 0
// 未来可扩展更多标志位:
// ChunkFlagPinned ChunkFlags = 1 << 1 // 置顶
// ChunkFlagHot ChunkFlags = 1 << 2 // 热门
)
// HasFlag 检查是否设置了指定标志
func (f ChunkFlags) HasFlag(flag ChunkFlags) bool {
return f&flag != 0
}
// SetFlag 设置指定标志
func (f ChunkFlags) SetFlag(flag ChunkFlags) ChunkFlags {
return f | flag
}
// ClearFlag 清除指定标志
func (f ChunkFlags) ClearFlag(flag ChunkFlags) ChunkFlags {
return f &^ flag
}
// ToggleFlag 切换指定标志
func (f ChunkFlags) ToggleFlag(flag ChunkFlags) ChunkFlags {
return f ^ flag
}
// ImageInfo 表示与 Chunk 关联的图片信息
type ImageInfo struct {
// 图片URL(COS)
@@ -80,6 +112,9 @@ type Chunk struct {
ChunkIndex int `json:"chunk_index"`
// Whether the chunk is enabled, can be used to temporarily disable certain chunks
IsEnabled bool `json:"is_enabled" gorm:"default:true"`
// Flags 存储多个布尔状态的位标志(如推荐状态等)
// 默认值为 ChunkFlagRecommended (1),表示默认可推荐
Flags ChunkFlags `json:"flags" gorm:"default:1"`
// Status of the chunk
Status int `json:"status" gorm:"default:0"`
// Starting character position in the original text
+22
View File
@@ -170,6 +170,7 @@ type FAQEntry struct {
KnowledgeBaseID string `json:"knowledge_base_id"`
TagID string `json:"tag_id"`
IsEnabled bool `json:"is_enabled"`
IsRecommended bool `json:"is_recommended"`
StandardQuestion string `json:"standard_question"`
SimilarQuestions []string `json:"similar_questions"`
NegativeQuestions []string `json:"negative_questions"`
@@ -189,7 +190,9 @@ type FAQEntryPayload struct {
NegativeQuestions []string `json:"negative_questions"`
Answers []string `json:"answers" binding:"required"`
TagID string `json:"tag_id"`
TagName string `json:"tag_name"`
IsEnabled *bool `json:"is_enabled,omitempty"`
IsRecommended *bool `json:"is_recommended,omitempty"`
}
const (
@@ -211,6 +214,25 @@ type FAQSearchRequest struct {
MatchCount int `json:"match_count"`
}
// FAQEntryFieldsUpdate 单个FAQ条目的字段更新
type FAQEntryFieldsUpdate struct {
IsEnabled *bool `json:"is_enabled,omitempty"`
IsRecommended *bool `json:"is_recommended,omitempty"`
TagID *string `json:"tag_id,omitempty"`
// 后续可扩展更多字段
}
// FAQEntryFieldsBatchUpdate 批量更新FAQ条目字段的请求
// 支持两种模式:
// 1. 按条目ID更新:使用 ByID 字段
// 2. 按Tag更新:使用 ByTag 字段,将该Tag下所有条目应用相同的更新
type FAQEntryFieldsBatchUpdate struct {
// ByID 按条目ID更新,key为条目ID
ByID map[string]FAQEntryFieldsUpdate `json:"by_id,omitempty"`
// ByTag 按Tag批量更新,key为TagID(空字符串表示未分类)
ByTag map[string]FAQEntryFieldsUpdate `json:"by_tag,omitempty"`
}
// FAQImportTaskStatus 导入任务状态
type FAQImportTaskStatus string
+9
View File
@@ -40,6 +40,8 @@ type ChunkRepository interface {
DeleteChunksByKnowledgeID(ctx context.Context, tenantID uint64, knowledgeID string) error
// DeleteByKnowledgeList deletes all chunks for a knowledge list
DeleteByKnowledgeList(ctx context.Context, tenantID uint64, knowledgeIDs []string) error
// DeleteChunksByTagID deletes all chunks with the specified tag ID
DeleteChunksByTagID(ctx context.Context, tenantID uint64, kbID string, tagID string) error
// CountChunksByKnowledgeBaseID counts the number of chunks in a knowledge base.
CountChunksByKnowledgeBaseID(ctx context.Context, tenantID uint64, kbID string) (int64, error)
// DeleteUnindexedChunks deletes unindexed chunks by knowledge id and chunk index range
@@ -50,6 +52,13 @@ type ChunkRepository interface {
// ListAllFAQChunksWithMetadataByKnowledgeBaseID lists all FAQ chunks for a knowledge base ID
// returns ID and Metadata fields for duplicate question checking
ListAllFAQChunksWithMetadataByKnowledgeBaseID(ctx context.Context, tenantID uint64, kbID string) ([]*types.Chunk, error)
// UpdateChunkFlagsBatch updates flags for multiple chunks in batch using a single SQL statement.
// setFlags: map of chunk ID to flags to set (OR operation)
// clearFlags: map of chunk ID to flags to clear (AND NOT operation)
UpdateChunkFlagsBatch(ctx context.Context, tenantID uint64, kbID string, setFlags map[string]types.ChunkFlags, clearFlags map[string]types.ChunkFlags) error
// UpdateChunkFieldsByTagID updates fields for all chunks with the specified tag ID.
// Supports updating is_enabled and flags fields.
UpdateChunkFieldsByTagID(ctx context.Context, tenantID uint64, kbID string, tagID string, isEnabled *bool, setFlags types.ChunkFlags, clearFlags types.ChunkFlags) ([]string, error)
}
// ChunkService defines the interface for chunk service operations
+3 -2
View File
@@ -88,8 +88,9 @@ type KnowledgeService interface {
CreateFAQEntry(ctx context.Context, kbID string, payload *types.FAQEntryPayload) (*types.FAQEntry, error)
// UpdateFAQEntry updates a single FAQ entry.
UpdateFAQEntry(ctx context.Context, kbID string, entryID string, payload *types.FAQEntryPayload) error
// UpdateFAQEntryStatusBatch updates enable status for FAQ entries in batch.
UpdateFAQEntryStatusBatch(ctx context.Context, kbID string, updates map[string]bool) error
// UpdateFAQEntryFieldsBatch updates multiple fields for FAQ entries in batch.
// Supports updating is_enabled, is_recommended, tag_id, and other fields in a single call.
UpdateFAQEntryFieldsBatch(ctx context.Context, kbID string, req *types.FAQEntryFieldsBatchUpdate) error
// DeleteFAQEntries deletes FAQ entries in batch.
DeleteFAQEntries(ctx context.Context, kbID string, entryIDs []string) error
// SearchFAQEntries searches FAQ entries using hybrid search.
+3
View File
@@ -16,6 +16,8 @@ type KnowledgeTagService interface {
UpdateTag(ctx context.Context, id string, name *string, color *string, sortOrder *int) (*types.KnowledgeTag, error)
// DeleteTag deletes a tag.
DeleteTag(ctx context.Context, id string, force bool) error
// FindOrCreateTagByName finds a tag by name or creates it if not exists.
FindOrCreateTagByName(ctx context.Context, kbID string, name string) (*types.KnowledgeTag, error)
}
// KnowledgeTagRepository defines persistence operations for tags.
@@ -23,6 +25,7 @@ type KnowledgeTagRepository interface {
Create(ctx context.Context, tag *types.KnowledgeTag) error
Update(ctx context.Context, tag *types.KnowledgeTag) error
GetByID(ctx context.Context, tenantID uint64, id string) (*types.KnowledgeTag, error)
GetByName(ctx context.Context, tenantID uint64, kbID string, name string) (*types.KnowledgeTag, error)
ListByKB(
ctx context.Context,
tenantID uint64,