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