feat(knowledge): enhance FAQ export functionality

- Updated the exportFAQEntries function to support exporting FAQ entries in both CSV and JSON formats.
- Modified the frontend to include options for selecting the export format.
- Enhanced localization files to reflect the new export options in English, Russian, and Chinese.
- Implemented new tests to validate the export functionality for both formats.

This commit improves the flexibility of FAQ exports, allowing users to choose their preferred format for better integration with other tools.
This commit is contained in:
wizardchen
2026-07-22 20:57:31 +08:00
committed by lyingbug
parent 135033e6df
commit b6a3b347da
18 changed files with 1041 additions and 137 deletions
+15
View File
@@ -251,6 +251,21 @@ type FAQEntry struct {
MatchedQuestion string `json:"matched_question,omitempty"`
}
// FAQExportEntry 表示 JSON 导出的 FAQ 条目,与 FAQEntryPayload 的导入格式兼容,
// 便于"导出 → 编辑 → 重新导入"循环。新增字段时务必保留 omitempty,避免破坏
// 历史导出文件的兼容性。
type FAQExportEntry struct {
ID int64 `json:"id"`
TagName string `json:"tag_name,omitempty"`
StandardQuestion string `json:"standard_question"`
SimilarQuestions []string `json:"similar_questions,omitempty"`
NegativeQuestions []string `json:"negative_questions,omitempty"`
Answers []string `json:"answers,omitempty"`
AnswerStrategy AnswerStrategy `json:"answer_strategy,omitempty"`
IsEnabled bool `json:"is_enabled"`
IsRecommended bool `json:"is_recommended"`
}
// FAQEntryPayload 用于创建/更新 FAQ 条目的 payload
type FAQEntryPayload struct {
// ID 可选,用于数据迁移时指定 seq_id(必须小于自增起始值 100000000)
+77
View File
@@ -0,0 +1,77 @@
package types
import "encoding/json"
// FAQChunkSyncPair links source/destination FAQ chunks sharing content_hash.
type FAQChunkSyncPair struct {
SrcChunkID string
DstChunkID string
}
// FAQChunkDiffResult is the output of FAQChunkDiff.
type FAQChunkDiffResult struct {
ChunksToAdd []string
ChunksToDelete []string
MatchedPairs []FAQChunkSyncPair
}
// FAQChunkStatus holds lightweight fields for clone status sync.
type FAQChunkStatus struct {
ID string
TagID string
IsEnabled bool
Flags ChunkFlags
AnswerStrategy AnswerStrategy
Metadata JSON
}
// FAQChunkStatusFieldsEqual compares status fields (tag excluded).
func FAQChunkStatusFieldsEqual(a, b *FAQChunkStatus) bool {
if a == nil || b == nil {
return a == b
}
if a.IsEnabled != b.IsEnabled || a.Flags != b.Flags {
return false
}
return NormalizeAnswerStrategy(a.AnswerStrategy) == NormalizeAnswerStrategy(b.AnswerStrategy)
}
// FAQChunkNeedsStatusSync reports whether a matched pair needs status sync.
func FAQChunkNeedsStatusSync(src, dst *FAQChunkStatus, mappedSrcTagID string) bool {
if src == nil || dst == nil {
return false
}
if mappedSrcTagID != dst.TagID {
return true
}
return !FAQChunkStatusFieldsEqual(src, dst)
}
// NormalizeAnswerStrategy treats empty as "all".
func NormalizeAnswerStrategy(s AnswerStrategy) AnswerStrategy {
if s == "" {
return AnswerStrategyAll
}
return s
}
// PatchFAQAnswerStrategy copies answer_strategy from src metadata onto dst metadata.
func PatchFAQAnswerStrategy(dstMeta, srcMeta JSON) (JSON, error) {
var dstM, srcM FAQChunkMetadata
if len(dstMeta) > 0 {
if err := json.Unmarshal(dstMeta, &dstM); err != nil {
return nil, err
}
}
if len(srcMeta) > 0 {
if err := json.Unmarshal(srcMeta, &srcM); err != nil {
return nil, err
}
}
dstM.AnswerStrategy = srcM.AnswerStrategy
bytes, err := json.Marshal(&dstM)
if err != nil {
return nil, err
}
return JSON(bytes), nil
}
+19
View File
@@ -0,0 +1,19 @@
package types
import "testing"
func TestFAQChunkNeedsStatusSync(t *testing.T) {
src := &FAQChunkStatus{TagID: "a", IsEnabled: true, Flags: ChunkFlagRecommended, AnswerStrategy: AnswerStrategyAll}
dst := &FAQChunkStatus{TagID: "old", IsEnabled: true, Flags: ChunkFlagRecommended, AnswerStrategy: AnswerStrategyAll}
if !FAQChunkNeedsStatusSync(src, dst, "mapped") {
t.Fatal("tag mismatch should need sync")
}
dst.TagID = "mapped"
if FAQChunkNeedsStatusSync(src, dst, "mapped") {
t.Fatal("in sync after tag mapped")
}
dst.IsEnabled = false
if !FAQChunkNeedsStatusSync(src, dst, "mapped") {
t.Fatal("is_enabled mismatch should need sync")
}
}
+3 -2
View File
@@ -97,8 +97,9 @@ type ChunkRepository interface {
// newTagID: if not nil, updates tag_id to this value (empty string means uncategorized)
UpdateChunkFieldsByTagID(ctx context.Context, tenantID uint64, kbID string, tagID string, isEnabled *bool, setFlags types.ChunkFlags, clearFlags types.ChunkFlags, newTagID *string, excludeIDs []string) ([]string, error)
// FAQChunkDiff compares FAQ chunks between two knowledge bases and returns the differences.
// Returns: chunksToAdd (content_hash in src but not in dst), chunksToDelete (content_hash in dst but not in src)
FAQChunkDiff(ctx context.Context, srcTenantID uint64, srcKBID string, dstTenantID uint64, dstKBID string) (chunksToAdd []string, chunksToDelete []string, err error)
FAQChunkDiff(ctx context.Context, srcTenantID uint64, srcKBID string, dstTenantID uint64, dstKBID string) (*types.FAQChunkDiffResult, error)
// ListFAQChunkStatusByIDs loads status fields for FAQ clone sync.
ListFAQChunkStatusByIDs(ctx context.Context, tenantID uint64, ids []string) (map[string]*types.FAQChunkStatus, error)
// ListRecommendedFAQChunks lists FAQ chunks with the recommended flag set.
// Filter by explicitly selected kbIDs, knowledgeIDs, and/or FAQ tagIDs.
+2
View File
@@ -152,6 +152,8 @@ type KnowledgeService interface {
SearchFAQEntries(ctx context.Context, kbID string, req *types.FAQSearchRequest) ([]*types.FAQEntry, error)
// ExportFAQEntries exports all FAQ entries for a knowledge base as CSV data.
ExportFAQEntries(ctx context.Context, kbID string) ([]byte, error)
// ExportFAQEntriesJSON exports all FAQ entries as a JSON array compatible with FAQEntryPayload.
ExportFAQEntriesJSON(ctx context.Context, kbID string) ([]byte, error)
// 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).
+1
View File
@@ -305,6 +305,7 @@ type FAQImportPayload struct {
Mode string `json:"mode"`
DryRun bool `json:"dry_run"` // dry run 模式只验证不导入
EnqueuedAt int64 `json:"enqueued_at"` // 任务入队时间戳,用于区分同一 TaskID 的不同次提交
InstanceID string `json:"instance_id,omitempty"`
Initiator TaskInitiator `json:"initiator,omitempty"`
}