feat: Add tag management and FAQ management APIs, enhance knowledge base functionality with keyword search and custom file naming support
This commit is contained in:
@@ -139,7 +139,7 @@ func (t *GetDocumentInfoTool) Execute(ctx context.Context, args map[string]inter
|
||||
ListPagedChunksByKnowledgeID(ctx, t.tenantID, id, &types.Pagination{
|
||||
Page: 1,
|
||||
PageSize: 1000,
|
||||
}, []types.ChunkType{"text"}, "")
|
||||
}, []types.ChunkType{"text"}, "", "")
|
||||
if err != nil {
|
||||
mu.Lock()
|
||||
results[id] = &docInfo{
|
||||
|
||||
@@ -1091,7 +1091,8 @@ func (t *KnowledgeSearchTool) formatOutput(
|
||||
_, total, err := t.chunkService.GetRepository().ListPagedChunksByKnowledgeID(ctx,
|
||||
t.tenantID, result.KnowledgeID,
|
||||
&types.Pagination{Page: 1, PageSize: 1},
|
||||
[]types.ChunkType{types.ChunkTypeText}, "")
|
||||
[]types.ChunkType{types.ChunkTypeText}, "", "",
|
||||
)
|
||||
if err != nil {
|
||||
logger.Warnf(
|
||||
ctx,
|
||||
|
||||
@@ -116,7 +116,7 @@ func (t *ListKnowledgeChunksTool) Execute(ctx context.Context, args map[string]i
|
||||
}
|
||||
|
||||
chunks, total, err := t.chunkService.GetRepository().ListPagedChunksByKnowledgeID(ctx,
|
||||
t.tenantID, knowledgeID, pagination, []types.ChunkType{types.ChunkTypeText, types.ChunkTypeFAQ}, "")
|
||||
t.tenantID, knowledgeID, pagination, []types.ChunkType{types.ChunkTypeText, types.ChunkTypeFAQ}, "", "")
|
||||
if err != nil {
|
||||
return &types.ToolResult{
|
||||
Success: false,
|
||||
|
||||
@@ -3,6 +3,7 @@ package repository
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/common"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
@@ -76,30 +77,37 @@ func (r *chunkRepository) ListPagedChunksByKnowledgeID(
|
||||
page *types.Pagination,
|
||||
chunkType []types.ChunkType,
|
||||
tagID string,
|
||||
keyword string,
|
||||
) ([]*types.Chunk, int64, error) {
|
||||
var chunks []*types.Chunk
|
||||
var total int64
|
||||
keyword = strings.TrimSpace(keyword)
|
||||
|
||||
query := r.db.WithContext(ctx).Model(&types.Chunk{}).
|
||||
Where("tenant_id = ? AND knowledge_id = ? AND chunk_type IN (?) AND status in (?)",
|
||||
baseFilter := func(db *gorm.DB) *gorm.DB {
|
||||
db = db.Where("tenant_id = ? AND knowledge_id = ? AND chunk_type IN (?) AND status in (?)",
|
||||
tenantID, knowledgeID, chunkType, []types.ChunkStatus{types.ChunkStatusIndexed, types.ChunkStatusDefault})
|
||||
if tagID != "" {
|
||||
query = query.Where("tag_id = ?", tagID)
|
||||
if tagID != "" {
|
||||
db = db.Where("tag_id = ?", tagID)
|
||||
}
|
||||
if keyword != "" {
|
||||
like := "%" + keyword + "%"
|
||||
db = db.Where("(content LIKE ? OR metadata::text LIKE ?)", like, like)
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
query := baseFilter(r.db.WithContext(ctx).Model(&types.Chunk{}))
|
||||
|
||||
// First query the total count
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// Then query the paginated data
|
||||
dataQuery := r.db.WithContext(ctx).
|
||||
Select("id, content, knowledge_id, knowledge_base_id, start_at, end_at, chunk_index, is_enabled, chunk_type, parent_chunk_id, image_info, metadata, tag_id")
|
||||
dataQuery = dataQuery.Where("tenant_id = ? AND knowledge_id = ? AND chunk_type IN (?) AND status in (?)",
|
||||
tenantID, knowledgeID, chunkType, []types.ChunkStatus{types.ChunkStatusIndexed, types.ChunkStatusDefault})
|
||||
if tagID != "" {
|
||||
dataQuery = dataQuery.Where("tag_id = ?", tagID)
|
||||
}
|
||||
dataQuery := baseFilter(
|
||||
r.db.WithContext(ctx).
|
||||
Select("id, content, knowledge_id, knowledge_base_id, start_at, end_at, chunk_index, is_enabled, chunk_type, parent_chunk_id, image_info, metadata, tag_id"),
|
||||
)
|
||||
|
||||
if err := dataQuery.
|
||||
Order("chunk_index ASC").
|
||||
|
||||
@@ -2,6 +2,7 @@ package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
@@ -39,20 +40,46 @@ func (r *knowledgeTagRepository) GetByID(ctx context.Context, tenantID uint64, i
|
||||
return &tag, nil
|
||||
}
|
||||
|
||||
// ListByKB lists knowledge tags by knowledge base ID
|
||||
// ListByKB lists knowledge tags by knowledge base ID with pagination and optional keyword filtering.
|
||||
func (r *knowledgeTagRepository) ListByKB(
|
||||
ctx context.Context,
|
||||
tenantID uint64,
|
||||
kbID string,
|
||||
) ([]*types.KnowledgeTag, error) {
|
||||
var tags []*types.KnowledgeTag
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND knowledge_base_id = ?", tenantID, kbID).
|
||||
Order("sort_order ASC, created_at ASC").
|
||||
Find(&tags).Error; err != nil {
|
||||
return nil, err
|
||||
page *types.Pagination,
|
||||
keyword string,
|
||||
) ([]*types.KnowledgeTag, int64, error) {
|
||||
if page == nil {
|
||||
page = &types.Pagination{}
|
||||
}
|
||||
return tags, nil
|
||||
keyword = strings.TrimSpace(keyword)
|
||||
|
||||
var total int64
|
||||
baseQuery := r.db.WithContext(ctx).Model(&types.KnowledgeTag{}).
|
||||
Where("tenant_id = ? AND knowledge_base_id = ?", tenantID, kbID)
|
||||
if keyword != "" {
|
||||
baseQuery = baseQuery.Where("name LIKE ?", "%"+keyword+"%")
|
||||
}
|
||||
|
||||
if err := baseQuery.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
dataQuery := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND knowledge_base_id = ?", tenantID, kbID)
|
||||
if keyword != "" {
|
||||
dataQuery = dataQuery.Where("name LIKE ?", "%"+keyword+"%")
|
||||
}
|
||||
|
||||
var tags []*types.KnowledgeTag
|
||||
if err := dataQuery.
|
||||
Order("sort_order ASC, created_at ASC").
|
||||
Offset(page.Offset()).
|
||||
Limit(page.Limit()).
|
||||
Find(&tags).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return tags, total, nil
|
||||
}
|
||||
|
||||
// Delete deletes a knowledge tag
|
||||
|
||||
@@ -300,7 +300,7 @@ func (s *agentService) getKnowledgeBaseInfos(ctx context.Context, kbIDs []string
|
||||
pageResult, err := s.knowledgeService.ListFAQEntries(ctx, kbID, &types.Pagination{
|
||||
Page: 1,
|
||||
PageSize: 10,
|
||||
}, "")
|
||||
}, "", "")
|
||||
if err == nil && pageResult != nil {
|
||||
docCount = int(pageResult.Total)
|
||||
if entries, ok := pageResult.Data.([]*types.FAQEntry); ok {
|
||||
|
||||
@@ -145,6 +145,7 @@ func (s *chunkService) ListPagedChunksByKnowledgeID(ctx context.Context,
|
||||
page,
|
||||
chunkType,
|
||||
"",
|
||||
"",
|
||||
)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
||||
|
||||
@@ -1875,6 +1875,7 @@ func (s *knowledgeService) CloneChunk(ctx context.Context, src, dst *types.Knowl
|
||||
},
|
||||
chunkType,
|
||||
"",
|
||||
"",
|
||||
)
|
||||
chunkPage++
|
||||
if err != nil {
|
||||
@@ -1949,11 +1950,12 @@ 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,
|
||||
kbID string, page *types.Pagination, tagID string, keyword string,
|
||||
) (*types.PageResult, error) {
|
||||
if page == nil {
|
||||
page = &types.Pagination{}
|
||||
}
|
||||
keyword = strings.TrimSpace(keyword)
|
||||
kb, err := s.validateFAQKnowledgeBase(ctx, kbID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -1968,7 +1970,7 @@ 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,
|
||||
ctx, tenantID, faqKnowledge.ID, page, chunkType, tagID, keyword,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -30,10 +30,19 @@ func NewKnowledgeTagService(
|
||||
}
|
||||
|
||||
// ListTags lists all tags for a knowledge base with usage stats.
|
||||
func (s *knowledgeTagService) ListTags(ctx context.Context, kbID string) ([]*types.KnowledgeTagWithStats, error) {
|
||||
func (s *knowledgeTagService) ListTags(
|
||||
ctx context.Context,
|
||||
kbID string,
|
||||
page *types.Pagination,
|
||||
keyword string,
|
||||
) (*types.PageResult, error) {
|
||||
if kbID == "" {
|
||||
return nil, werrors.NewBadRequestError("知识库ID不能为空")
|
||||
}
|
||||
if page == nil {
|
||||
page = &types.Pagination{}
|
||||
}
|
||||
keyword = strings.TrimSpace(keyword)
|
||||
// Ensure KB exists and belongs to current tenant
|
||||
kb, err := s.kbService.GetKnowledgeBaseByID(ctx, kbID)
|
||||
if err != nil {
|
||||
@@ -41,7 +50,7 @@ func (s *knowledgeTagService) ListTags(ctx context.Context, kbID string) ([]*typ
|
||||
}
|
||||
tenantID := kb.TenantID
|
||||
|
||||
tags, err := s.repo.ListByKB(ctx, tenantID, kbID)
|
||||
tags, total, err := s.repo.ListByKB(ctx, tenantID, kbID, page, keyword)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -65,7 +74,7 @@ func (s *knowledgeTagService) ListTags(ctx context.Context, kbID string) ([]*typ
|
||||
ChunkCount: cCount,
|
||||
})
|
||||
}
|
||||
return results, nil
|
||||
return types.NewPageResult(total, page, results), nil
|
||||
}
|
||||
|
||||
// CreateTag creates a new tag under a KB.
|
||||
|
||||
@@ -33,8 +33,9 @@ func (h *FAQHandler) ListEntries(c *gin.Context) {
|
||||
}
|
||||
|
||||
tagID := secutils.SanitizeForLog(c.Query("tag_id"))
|
||||
keyword := secutils.SanitizeForLog(c.Query("keyword"))
|
||||
|
||||
result, err := h.knowledgeService.ListFAQEntries(ctx, secutils.SanitizeForLog(c.Param("id")), &page, tagID)
|
||||
result, err := h.knowledgeService.ListFAQEntries(ctx, secutils.SanitizeForLog(c.Param("id")), &page, tagID, keyword)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(err)
|
||||
|
||||
+11
-1
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/errors"
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
secutils "github.com/Tencent/WeKnora/internal/utils"
|
||||
)
|
||||
@@ -26,7 +27,16 @@ func (h *TagHandler) ListTags(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
kbID := secutils.SanitizeForLog(c.Param("id"))
|
||||
|
||||
tags, err := h.tagService.ListTags(ctx, kbID)
|
||||
var page types.Pagination
|
||||
if err := c.ShouldBindQuery(&page); err != nil {
|
||||
logger.Error(ctx, "Failed to bind pagination query", err)
|
||||
c.Error(errors.NewBadRequestError("分页参数不合法").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
keyword := secutils.SanitizeForLog(c.Query("keyword"))
|
||||
|
||||
tags, err := h.tagService.ListTags(ctx, kbID, &page, keyword)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(err)
|
||||
|
||||
@@ -25,6 +25,7 @@ type ChunkRepository interface {
|
||||
page *types.Pagination,
|
||||
chunkType []types.ChunkType,
|
||||
tagID string,
|
||||
keyword string,
|
||||
) ([]*types.Chunk, int64, error)
|
||||
ListChunkByParentID(ctx context.Context, tenantID uint64, parentID string) ([]*types.Chunk, error)
|
||||
// UpdateChunk updates a chunk
|
||||
|
||||
@@ -69,7 +69,13 @@ type KnowledgeService interface {
|
||||
UpdateImageInfo(ctx context.Context, knowledgeID string, chunkID string, imageInfo string) error
|
||||
// ListFAQEntries lists FAQ entries under a FAQ knowledge base.
|
||||
// When tagID is non-empty, results are filtered by tag_id on FAQ chunks.
|
||||
ListFAQEntries(ctx context.Context, kbID string, page *types.Pagination, tagID string) (*types.PageResult, error)
|
||||
ListFAQEntries(
|
||||
ctx context.Context,
|
||||
kbID string,
|
||||
page *types.Pagination,
|
||||
tagID string,
|
||||
keyword string,
|
||||
) (*types.PageResult, error)
|
||||
// UpsertFAQEntries imports or appends FAQ entries asynchronously.
|
||||
// Returns task ID (Knowledge ID) for tracking import progress.
|
||||
UpsertFAQEntries(ctx context.Context, kbID string, payload *types.FAQBatchUpsertPayload) (string, error)
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
// KnowledgeTagService defines operations on knowledge base scoped tags.
|
||||
type KnowledgeTagService interface {
|
||||
// ListTags lists all tags under a knowledge base with associated statistics.
|
||||
ListTags(ctx context.Context, kbID string) ([]*types.KnowledgeTagWithStats, error)
|
||||
ListTags(ctx context.Context, kbID string, page *types.Pagination, keyword string) (*types.PageResult, error)
|
||||
// CreateTag creates a new tag under a knowledge base.
|
||||
CreateTag(ctx context.Context, kbID string, name string, color string, sortOrder int) (*types.KnowledgeTag, error)
|
||||
// UpdateTag updates tag basic information.
|
||||
@@ -23,7 +23,13 @@ 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)
|
||||
ListByKB(ctx context.Context, tenantID uint64, kbID string) ([]*types.KnowledgeTag, error)
|
||||
ListByKB(
|
||||
ctx context.Context,
|
||||
tenantID uint64,
|
||||
kbID string,
|
||||
page *types.Pagination,
|
||||
keyword string,
|
||||
) ([]*types.KnowledgeTag, int64, error)
|
||||
Delete(ctx context.Context, tenantID uint64, id string) error
|
||||
// CountReferences returns number of knowledges and chunks that reference the tag.
|
||||
CountReferences(
|
||||
|
||||
Reference in New Issue
Block a user