feat: 添加FAQ导入成功条目详情展示
This commit is contained in:
+12
-2
@@ -386,6 +386,15 @@ type FAQFailedEntry struct {
|
||||
IsDisabled bool `json:"is_disabled,omitempty"`
|
||||
}
|
||||
|
||||
// FAQSuccessEntry represents a successfully imported FAQ entry.
|
||||
type FAQSuccessEntry struct {
|
||||
Index int `json:"index"` // Entry index in the batch (0-based)
|
||||
SeqID int64 `json:"seq_id"` // Entry sequence ID after import
|
||||
TagID int64 `json:"tag_id,omitempty"` // Tag ID (seq_id)
|
||||
TagName string `json:"tag_name,omitempty"` // Tag name
|
||||
StandardQuestion string `json:"standard_question"` // Standard question
|
||||
}
|
||||
|
||||
// FAQImportProgress represents the progress of an async FAQ import task.
|
||||
// When Status is "completed", the result fields (SkippedCount, ImportMode, ImportedAt, DisplayStatus, ProcessingTime) are populated.
|
||||
type FAQImportProgress struct {
|
||||
@@ -399,8 +408,9 @@ type FAQImportProgress struct {
|
||||
SuccessCount int `json:"success_count"`
|
||||
FailedCount int `json:"failed_count"`
|
||||
SkippedCount int `json:"skipped_count,omitempty"`
|
||||
FailedEntries []FAQFailedEntry `json:"failed_entries,omitempty"`
|
||||
FailedEntriesURL string `json:"failed_entries_url,omitempty"` // CSV download URL when too many failures
|
||||
FailedEntries []FAQFailedEntry `json:"failed_entries,omitempty"`
|
||||
SuccessEntries []FAQSuccessEntry `json:"success_entries,omitempty"` // Successfully imported entries (when count is small)
|
||||
FailedEntriesURL string `json:"failed_entries_url,omitempty"` // CSV download URL when too many failures
|
||||
Message string `json:"message"`
|
||||
Error string `json:"error,omitempty"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
|
||||
@@ -215,6 +215,20 @@ export async function exportFAQEntries(kbId: string): Promise<Blob> {
|
||||
}
|
||||
|
||||
// FAQ Import Progress API
|
||||
export interface FAQBlockedEntry {
|
||||
index: number
|
||||
standard_question: string
|
||||
reason: string
|
||||
}
|
||||
|
||||
export interface FAQSuccessEntry {
|
||||
index: number
|
||||
seq_id: number
|
||||
tag_id?: number
|
||||
tag_name?: string
|
||||
standard_question: string
|
||||
}
|
||||
|
||||
export interface FAQImportProgress {
|
||||
task_id: string
|
||||
kb_id: string
|
||||
@@ -223,6 +237,9 @@ export interface FAQImportProgress {
|
||||
progress: number
|
||||
total: number
|
||||
processed: number
|
||||
blocked: number
|
||||
blocked_entries?: FAQBlockedEntry[]
|
||||
success_entries?: FAQSuccessEntry[]
|
||||
message: string
|
||||
error: string
|
||||
created_at: number
|
||||
|
||||
@@ -1386,6 +1386,13 @@ const importResult = ref<{
|
||||
task_id: string
|
||||
processing_time: number
|
||||
failed_entries_url?: string
|
||||
success_entries?: Array<{
|
||||
index: number
|
||||
seq_id: number
|
||||
tag_id?: number
|
||||
tag_name?: string
|
||||
standard_question: string
|
||||
}>
|
||||
display_status: string
|
||||
} | null>(null)
|
||||
|
||||
@@ -2506,6 +2513,7 @@ const loadImportResult = async () => {
|
||||
imported_at: data.imported_at,
|
||||
task_id: data.task_id,
|
||||
failed_entries_url: data.failed_entries_url,
|
||||
success_entries: data.success_entries,
|
||||
display_status: data.display_status || 'open',
|
||||
processing_time: data.processing_time || 0,
|
||||
}
|
||||
|
||||
@@ -2945,6 +2945,11 @@ func (s *knowledgeService) saveFAQImportResultToDatabase(ctx context.Context,
|
||||
importResult.FailedEntriesURL = progress.FailedEntriesURL
|
||||
}
|
||||
|
||||
// 设置成功条目信息
|
||||
if len(progress.SuccessEntries) > 0 {
|
||||
importResult.SuccessEntries = progress.SuccessEntries
|
||||
}
|
||||
|
||||
// 设置导入结果到Knowledge的metadata中
|
||||
if err := knowledge.SetLastFAQImportResult(importResult); err != nil {
|
||||
return fmt.Errorf("failed to set FAQ import result: %w", err)
|
||||
@@ -3399,6 +3404,7 @@ func (s *knowledgeService) calculateReplaceOperations(ctx context.Context,
|
||||
// executeFAQImport 执行实际的FAQ导入逻辑
|
||||
func (s *knowledgeService) executeFAQImport(ctx context.Context, taskID string, kbID string,
|
||||
payload *types.FAQBatchUpsertPayload, tenantID uint64, processedCount int,
|
||||
progress *types.FAQImportProgress,
|
||||
) (err error) {
|
||||
// 保存知识库和embedding模型信息,用于清理索引
|
||||
var kb *types.KnowledgeBase
|
||||
@@ -3624,6 +3630,32 @@ func (s *knowledgeService) executeFAQImport(ctx context.Context, taskID string,
|
||||
return fmt.Errorf("failed to update chunks status: %w", err)
|
||||
}
|
||||
|
||||
// 收集成功条目信息
|
||||
for idx, chunk := range chunks {
|
||||
entryIdx := i + idx + processedCount // 原始条目索引
|
||||
meta, _ := chunk.FAQMetadata()
|
||||
standardQ := ""
|
||||
if meta != nil {
|
||||
standardQ = meta.StandardQuestion
|
||||
}
|
||||
// 获取 tag info
|
||||
var tagID int64
|
||||
tagName := ""
|
||||
if chunk.TagID != "" {
|
||||
if tag, err := s.tagRepo.GetByID(ctx, tenantID, chunk.TagID); err == nil && tag != nil {
|
||||
tagID = tag.SeqID
|
||||
tagName = tag.Name
|
||||
}
|
||||
}
|
||||
progress.SuccessEntries = append(progress.SuccessEntries, types.FAQSuccessEntry{
|
||||
Index: entryIdx,
|
||||
SeqID: chunk.SeqID,
|
||||
TagID: tagID,
|
||||
TagName: tagName,
|
||||
StandardQuestion: standardQ,
|
||||
})
|
||||
}
|
||||
|
||||
actualProcessed += len(batch)
|
||||
// 更新任务进度
|
||||
progress := int(float64(actualProcessed) / float64(totalEntries) * 100)
|
||||
@@ -4045,20 +4077,6 @@ func (s *knowledgeService) AddSimilarQuestions(ctx context.Context,
|
||||
return s.chunkToFAQEntry(chunk, kb, tagSeqIDMap)
|
||||
}
|
||||
|
||||
// Security check for new questions using CheckFAQContent
|
||||
tempPayload := &types.FAQEntryPayload{
|
||||
StandardQuestion: meta.StandardQuestion,
|
||||
SimilarQuestions: newQuestions,
|
||||
Answers: meta.Answers,
|
||||
}
|
||||
blockStatus, checkErr := s.CheckFAQContent(ctx, tenantID, tempPayload)
|
||||
if checkErr != nil {
|
||||
logger.Warnf(ctx, "AddSimilarQuestions: security check error: %v", checkErr)
|
||||
}
|
||||
if blockStatus != 0 {
|
||||
return nil, werrors.NewBadRequestError("相似问内容违规,请调整后重试")
|
||||
}
|
||||
|
||||
// Check for duplicates with other entries
|
||||
tempMeta := &types.FAQChunkMetadata{
|
||||
StandardQuestion: meta.StandardQuestion,
|
||||
@@ -6191,28 +6209,47 @@ func (s *knowledgeService) ProcessFAQImport(ctx context.Context, t *asynq.Task)
|
||||
|
||||
// 初始化进度
|
||||
progress := &types.FAQImportProgress{
|
||||
TaskID: payload.TaskID,
|
||||
KBID: payload.KBID,
|
||||
Status: types.FAQImportStatusProcessing,
|
||||
Progress: 0,
|
||||
Total: originalTotalEntries,
|
||||
Processed: 0,
|
||||
SuccessCount: 0,
|
||||
FailedCount: 0,
|
||||
FailedEntries: make([]types.FAQFailedEntry, 0),
|
||||
Message: "正在验证条目...",
|
||||
CreatedAt: time.Now().Unix(),
|
||||
UpdatedAt: time.Now().Unix(),
|
||||
DryRun: payload.DryRun,
|
||||
TaskID: payload.TaskID,
|
||||
KBID: payload.KBID,
|
||||
KnowledgeID: payload.KnowledgeID,
|
||||
Status: types.FAQImportStatusProcessing,
|
||||
Progress: 0,
|
||||
Total: originalTotalEntries,
|
||||
Processed: 0,
|
||||
SuccessCount: 0,
|
||||
FailedCount: 0,
|
||||
FailedEntries: make([]types.FAQFailedEntry, 0),
|
||||
SuccessEntries: make([]types.FAQSuccessEntry, 0),
|
||||
Message: "正在验证条目...",
|
||||
CreatedAt: time.Now().Unix(),
|
||||
UpdatedAt: time.Now().Unix(),
|
||||
DryRun: payload.DryRun,
|
||||
}
|
||||
if err := s.saveFAQImportProgress(ctx, progress); err != nil {
|
||||
logger.Warnf(ctx, "Failed to save initial FAQ import progress: %v", err)
|
||||
}
|
||||
|
||||
// 第一步:执行验证(无论是 dry run 还是 import 模式都需要验证)
|
||||
validEntryIndices := s.executeFAQDryRunValidation(ctx, &payload, progress)
|
||||
logger.Infof(ctx, "FAQ validation completed: total=%d, valid=%d, failed=%d",
|
||||
originalTotalEntries, len(validEntryIndices), progress.FailedCount)
|
||||
// 检查是否已有验证结果(用于重试时跳过验证)
|
||||
existingProgress, _ := s.GetFAQImportProgress(ctx, payload.TaskID)
|
||||
var validEntryIndices []int
|
||||
if existingProgress != nil && len(existingProgress.ValidEntryIndices) > 0 {
|
||||
// 重试时直接使用之前的验证结果
|
||||
validEntryIndices = existingProgress.ValidEntryIndices
|
||||
progress.FailedCount = existingProgress.FailedCount
|
||||
progress.FailedEntries = existingProgress.FailedEntries
|
||||
logger.Infof(ctx, "Reusing previous validation result: valid=%d, failed=%d",
|
||||
len(validEntryIndices), progress.FailedCount)
|
||||
} else {
|
||||
// 第一步:执行验证(无论是 dry run 还是 import 模式都需要验证)
|
||||
validEntryIndices = s.executeFAQDryRunValidation(ctx, &payload, progress)
|
||||
// 保存验证通过的索引,用于重试时跳过验证
|
||||
progress.ValidEntryIndices = validEntryIndices
|
||||
if err := s.saveFAQImportProgress(ctx, progress); err != nil {
|
||||
logger.Warnf(ctx, "Failed to save validation result: %v", err)
|
||||
}
|
||||
logger.Infof(ctx, "FAQ validation completed: total=%d, valid=%d, failed=%d",
|
||||
originalTotalEntries, len(validEntryIndices), progress.FailedCount)
|
||||
}
|
||||
|
||||
// Dry run 模式:验证完成后直接返回结果
|
||||
if payload.DryRun {
|
||||
@@ -6261,8 +6298,7 @@ func (s *knowledgeService) ProcessFAQImport(ctx context.Context, t *asynq.Task)
|
||||
return fmt.Errorf("failed to get knowledge base: %w", err)
|
||||
}
|
||||
|
||||
// 检查任务状态 - 幂等性处理(先检查Redis中的进度)
|
||||
existingProgress, _ := s.GetFAQImportProgress(ctx, payload.TaskID)
|
||||
// 检查任务状态 - 幂等性处理(复用之前获取的 existingProgress)
|
||||
var processedCount int
|
||||
if existingProgress != nil {
|
||||
if existingProgress.Status == types.FAQImportStatusCompleted {
|
||||
@@ -6327,7 +6363,7 @@ func (s *knowledgeService) ProcessFAQImport(ctx context.Context, t *asynq.Task)
|
||||
}
|
||||
|
||||
// 执行FAQ导入(传入已处理的偏移量,用于进度计算)
|
||||
if err := s.executeFAQImport(ctx, payload.TaskID, payload.KBID, faqPayload, payload.TenantID, progress.FailedCount+processedCount); err != nil {
|
||||
if err := s.executeFAQImport(ctx, payload.TaskID, payload.KBID, faqPayload, payload.TenantID, progress.FailedCount+processedCount, progress); err != nil {
|
||||
logger.Errorf(ctx, "FAQ import task failed: %s, error: %v", payload.TaskID, err)
|
||||
// 如果是最后一次重试,更新状态为失败
|
||||
if isLastRetry {
|
||||
|
||||
+31
-17
@@ -238,6 +238,15 @@ type FAQFailedEntry struct {
|
||||
IsDisabled bool `json:"is_disabled,omitempty"` // 是否停用
|
||||
}
|
||||
|
||||
// FAQSuccessEntry 表示导入成功的条目简单信息
|
||||
type FAQSuccessEntry struct {
|
||||
Index int `json:"index"` // 条目在批次中的索引(从0开始)
|
||||
SeqID int64 `json:"seq_id"` // 导入后的条目序列ID
|
||||
TagID int64 `json:"tag_id,omitempty"` // 分类ID(seq_id)
|
||||
TagName string `json:"tag_name,omitempty"` // 分类名称
|
||||
StandardQuestion string `json:"standard_question"` // 标准问题
|
||||
}
|
||||
|
||||
// FAQDryRunResult 表示 dry_run 模式的验证结果
|
||||
type FAQDryRunResult struct {
|
||||
TaskID string `json:"task_id,omitempty"` // 异步任务ID(异步模式时返回)
|
||||
@@ -298,23 +307,25 @@ const (
|
||||
// FAQImportProgress represents the progress of an FAQ import task stored in Redis
|
||||
// When Status is "completed", the result fields (SkippedCount, ImportMode, ImportedAt, DisplayStatus, ProcessingTime) are populated.
|
||||
type FAQImportProgress struct {
|
||||
TaskID string `json:"task_id"` // UUID for the import task
|
||||
KBID string `json:"kb_id"` // Knowledge Base ID
|
||||
KnowledgeID string `json:"knowledge_id"` // FAQ Knowledge ID
|
||||
Status FAQImportTaskStatus `json:"status"` // Task status
|
||||
Progress int `json:"progress"` // 0-100 percentage
|
||||
Total int `json:"total"` // Total entries to import
|
||||
Processed int `json:"processed"` // Entries processed so far
|
||||
SuccessCount int `json:"success_count"` // 成功导入/验证通过的条目数
|
||||
FailedCount int `json:"failed_count"` // 失败的条目数
|
||||
SkippedCount int `json:"skipped_count,omitempty"` // 跳过的条目数(如重复等)
|
||||
FailedEntries []FAQFailedEntry `json:"failed_entries,omitempty"` // 失败条目详情(少量时直接返回)
|
||||
FailedEntriesURL string `json:"failed_entries_url,omitempty"` // 失败条目CSV下载URL(大量时返回URL)
|
||||
Message string `json:"message"` // Status message
|
||||
Error string `json:"error"` // Error message if failed
|
||||
CreatedAt int64 `json:"created_at"` // Task creation timestamp
|
||||
UpdatedAt int64 `json:"updated_at"` // Last update timestamp
|
||||
DryRun bool `json:"dry_run,omitempty"` // 是否为 dry run 模式
|
||||
TaskID string `json:"task_id"` // UUID for the import task
|
||||
KBID string `json:"kb_id"` // Knowledge Base ID
|
||||
KnowledgeID string `json:"knowledge_id"` // FAQ Knowledge ID
|
||||
Status FAQImportTaskStatus `json:"status"` // Task status
|
||||
Progress int `json:"progress"` // 0-100 percentage
|
||||
Total int `json:"total"` // Total entries to import
|
||||
Processed int `json:"processed"` // Entries processed so far
|
||||
SuccessCount int `json:"success_count"` // 成功导入/验证通过的条目数
|
||||
FailedCount int `json:"failed_count"` // 失败的条目数
|
||||
SkippedCount int `json:"skipped_count,omitempty"` // 跳过的条目数(如重复等)
|
||||
FailedEntries []FAQFailedEntry `json:"failed_entries,omitempty"` // 失败条目详情(少量时直接返回)
|
||||
FailedEntriesURL string `json:"failed_entries_url,omitempty"` // 失败条目CSV下载URL(大量时返回URL)
|
||||
SuccessEntries []FAQSuccessEntry `json:"success_entries,omitempty"` // 成功条目简单信息(少量时直接返回)
|
||||
ValidEntryIndices []int `json:"valid_entry_indices,omitempty"` // 验证通过的条目索引(用于重试时跳过验证)
|
||||
Message string `json:"message"` // Status message
|
||||
Error string `json:"error"` // Error message if failed
|
||||
CreatedAt int64 `json:"created_at"` // Task creation timestamp
|
||||
UpdatedAt int64 `json:"updated_at"` // Last update timestamp
|
||||
DryRun bool `json:"dry_run,omitempty"` // 是否为 dry run 模式
|
||||
|
||||
// Result fields (populated when Status == "completed")
|
||||
ImportMode string `json:"import_mode,omitempty"` // 导入模式:append 或 replace
|
||||
@@ -348,6 +359,9 @@ type FAQImportResult struct {
|
||||
// 失败详情URL(失败条目较多时提供下载链接)
|
||||
FailedEntriesURL string `json:"failed_entries_url,omitempty"` // 失败条目CSV下载URL
|
||||
|
||||
// 成功条目简单信息
|
||||
SuccessEntries []FAQSuccessEntry `json:"success_entries,omitempty"` // 成功导入的条目简单信息
|
||||
|
||||
// 显示控制
|
||||
DisplayStatus string `json:"display_status"` // 显示状态:open 或 close
|
||||
|
||||
|
||||
@@ -99,6 +99,7 @@ type SearchParams struct {
|
||||
DisableVectorMatch bool `json:"disable_vector_match"`
|
||||
KnowledgeIDs []string `json:"knowledge_ids"`
|
||||
TagIDs []string `json:"tag_ids"` // Tag IDs for filtering (used for FAQ priority filtering)
|
||||
OnlyRecommended bool `json:"only_recommended"`
|
||||
}
|
||||
|
||||
// Value implements the driver.Valuer interface, used to convert SearchResult to database value
|
||||
|
||||
Reference in New Issue
Block a user