Merge branch 'master' into custom_agent

This commit is contained in:
wizardchen
2025-12-24 15:53:04 +08:00
47 changed files with 2574 additions and 2648 deletions
+87 -2
View File
@@ -2548,6 +2548,8 @@ func (s *knowledgeService) CloneChunk(ctx context.Context, src, dst *types.Knowl
chunkType,
"",
"",
"",
"",
)
chunkPage++
if err != nil {
@@ -2642,7 +2644,7 @@ func (s *knowledgeService) CloneChunk(ctx context.Context, src, dst *types.Knowl
// ListFAQEntries lists FAQ entries under a FAQ knowledge base.
func (s *knowledgeService) ListFAQEntries(ctx context.Context,
kbID string, page *types.Pagination, tagID string, keyword string,
kbID string, page *types.Pagination, tagID string, keyword string, searchField string, sortOrder string,
) (*types.PageResult, error) {
if page == nil {
page = &types.Pagination{}
@@ -2662,11 +2664,33 @@ func (s *knowledgeService) ListFAQEntries(ctx context.Context,
}
chunkType := []types.ChunkType{types.ChunkTypeFAQ}
chunks, total, err := s.chunkRepo.ListPagedChunksByKnowledgeID(
ctx, tenantID, faqKnowledge.ID, page, chunkType, tagID, keyword,
ctx, tenantID, faqKnowledge.ID, page, chunkType, tagID, keyword, searchField, sortOrder,
)
if err != nil {
return nil, err
}
// Build tag ID to name mapping for all unique tag IDs (batch query)
tagNameMap := make(map[string]string)
tagIDs := make([]string, 0)
tagIDSet := make(map[string]struct{})
for _, chunk := range chunks {
if chunk.TagID != "" {
if _, exists := tagIDSet[chunk.TagID]; !exists {
tagIDSet[chunk.TagID] = struct{}{}
tagIDs = append(tagIDs, chunk.TagID)
}
}
}
if len(tagIDs) > 0 {
tags, err := s.tagRepo.GetByIDs(ctx, tenantID, tagIDs)
if err == nil {
for _, tag := range tags {
tagNameMap[tag.ID] = tag.Name
}
}
}
kb.EnsureDefaults()
entries := make([]*types.FAQEntry, 0, len(chunks))
for _, chunk := range chunks {
@@ -2674,6 +2698,10 @@ func (s *knowledgeService) ListFAQEntries(ctx context.Context,
if err != nil {
return nil, err
}
// Set tag name from mapping
if entry.TagID != "" {
entry.TagName = tagNameMap[entry.TagID]
}
entries = append(entries, entry)
}
return types.NewPageResult(total, page, entries), nil
@@ -3315,6 +3343,63 @@ func (s *knowledgeService) CreateFAQEntry(ctx context.Context,
return nil, err
}
// 查询TagName
if entry.TagID != "" {
tag, tagErr := s.tagRepo.GetByID(ctx, tenantID, entry.TagID)
if tagErr == nil && tag != nil {
entry.TagName = tag.Name
}
}
return entry, nil
}
// GetFAQEntry retrieves a single FAQ entry by ID.
func (s *knowledgeService) GetFAQEntry(ctx context.Context,
kbID string, entryID string,
) (*types.FAQEntry, error) {
if entryID == "" {
return nil, werrors.NewBadRequestError("条目ID不能为空")
}
kb, err := s.validateFAQKnowledgeBase(ctx, kbID)
if err != nil {
return nil, err
}
kb.EnsureDefaults()
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
// 获取chunk
chunk, err := s.chunkService.GetChunkByID(ctx, entryID)
if err != nil {
return nil, err
}
// 验证chunk属于当前知识库
if chunk.KnowledgeBaseID != kb.ID || chunk.TenantID != tenantID {
return nil, werrors.NewNotFoundError("FAQ条目不存在")
}
// 验证是FAQ类型
if chunk.ChunkType != types.ChunkTypeFAQ {
return nil, werrors.NewNotFoundError("FAQ条目不存在")
}
// 转换为FAQEntry返回
entry, err := s.chunkToFAQEntry(chunk, kb)
if err != nil {
return nil, err
}
// 查询TagName
if entry.TagID != "" {
tag, tagErr := s.tagRepo.GetByID(ctx, tenantID, entry.TagID)
if tagErr == nil && tag != nil {
entry.TagName = tag.Name
}
}
return entry, nil
}