feat(agent): enhance suggested questions API with tag scopes

Update the SuggestedQuestionsRequest to include TagScopes for better filtering of knowledge items based on their parent knowledge bases. Modify related API documentation and frontend interfaces to accommodate the new structure. Ensure backend services handle the new tag scope logic correctly, including tests for the updated functionality.
This commit is contained in:
wizardchen
2026-07-17 10:16:58 +08:00
committed by lyingbug
parent c21c34b3c0
commit eedd7d4641
34 changed files with 1041 additions and 338 deletions
+17 -7
View File
@@ -957,19 +957,20 @@ func (r *chunkRepository) FAQChunkDiff(
}
// ListRecommendedFAQChunks lists FAQ chunks with the recommended flag set.
// Filter by kbIDs and/or knowledgeIDs (OR relationship). At least one must be non-empty.
// Filter by explicitly selected kbIDs, knowledgeIDs, and/or FAQ tagIDs (OR relationship).
// Returns up to `limit` chunks sorted by updated_at descending.
func (r *chunkRepository) ListRecommendedFAQChunks(
ctx context.Context,
tenantID uint64,
kbIDs []string,
knowledgeIDs []string,
tagIDs []string,
limit int,
) ([]*types.Chunk, error) {
if limit <= 0 {
limit = 10
}
if len(kbIDs) == 0 && len(knowledgeIDs) == 0 {
if len(kbIDs) == 0 && len(knowledgeIDs) == 0 && len(tagIDs) == 0 {
return nil, nil
}
var chunks []*types.Chunk
@@ -977,12 +978,21 @@ func (r *chunkRepository) ListRecommendedFAQChunks(
Select("id, knowledge_id, knowledge_base_id, chunk_type, metadata, flags, updated_at").
Where("tenant_id = ? AND chunk_type = ? AND status IN ? AND is_enabled = ? AND flags & ? != 0",
tenantID, types.ChunkTypeFAQ, []int{int(types.ChunkStatusIndexed), int(types.ChunkStatusDefault)}, true, int(types.ChunkFlagRecommended))
if len(knowledgeIDs) > 0 {
// 指定了具体知识文档,直接按 knowledge_id 过滤(忽略 kbIDs)
query = query.Where("knowledge_id IN ?", knowledgeIDs)
} else {
query = query.Where("knowledge_base_id IN ?", kbIDs)
var scopeClauses []string
var scopeArgs []interface{}
if len(kbIDs) > 0 {
scopeClauses = append(scopeClauses, "knowledge_base_id IN ?")
scopeArgs = append(scopeArgs, kbIDs)
}
if len(knowledgeIDs) > 0 {
scopeClauses = append(scopeClauses, "knowledge_id IN ?")
scopeArgs = append(scopeArgs, knowledgeIDs)
}
if len(tagIDs) > 0 {
scopeClauses = append(scopeClauses, "tag_id IN ?")
scopeArgs = append(scopeArgs, tagIDs)
}
query = query.Where("("+strings.Join(scopeClauses, " OR ")+")", scopeArgs...)
orderClause := "RANDOM()"
if r.db.Dialector.Name() == "mysql" {
@@ -215,3 +215,88 @@ func TestUpdateChunk_SQLite_NoNOWError(t *testing.T) {
require.NoError(t, db.First(&saved, "id = ?", chunk.ID).Error)
assert.Equal(t, "updated content", saved.Content)
}
func makeSuggestedFAQChunk(t *testing.T, kbID, knowledgeID, tagID, question string) *types.Chunk {
t.Helper()
chunk := makeChunk(kbID, knowledgeID, types.ChunkTypeFAQ)
chunk.TagID = tagID
chunk.Flags = types.ChunkFlagRecommended
require.NoError(t, chunk.SetFAQMetadata(&types.FAQChunkMetadata{StandardQuestion: question}))
return chunk
}
func makeSuggestedDocumentChunk(t *testing.T, kbID, knowledgeID, question string) *types.Chunk {
t.Helper()
chunk := makeChunk(kbID, knowledgeID, types.ChunkTypeText)
require.NoError(t, chunk.SetDocumentMetadata(&types.DocumentChunkMetadata{
GeneratedQuestions: []types.GeneratedQuestion{{ID: uuid.NewString(), Question: question}},
}))
return chunk
}
func TestListRecommendedFAQChunks_FiltersByTagWithoutWideningToParentKB(t *testing.T) {
db := setupChunkTestDB(t)
repo := NewChunkRepository(db)
ctx := context.Background()
selectedTag := uuid.NewString()
otherTag := uuid.NewString()
selected := makeSuggestedFAQChunk(t, "kb-1", "faq-knowledge", selectedTag, "selected question")
other := makeSuggestedFAQChunk(t, "kb-1", "faq-knowledge", otherTag, "other question")
require.NoError(t, repo.CreateChunks(ctx, []*types.Chunk{selected, other}))
got, err := repo.ListRecommendedFAQChunks(ctx, 1, nil, nil, []string{selectedTag}, 10)
require.NoError(t, err)
require.Len(t, got, 1)
assert.Equal(t, selected.ID, got[0].ID)
}
func TestListRecommendedFAQChunks_UnionsOnlyExplicitScopes(t *testing.T) {
db := setupChunkTestDB(t)
repo := NewChunkRepository(db)
ctx := context.Background()
selectedTag := uuid.NewString()
tagged := makeSuggestedFAQChunk(t, "kb-tag", "faq-tag", selectedTag, "tagged question")
explicitKB := makeSuggestedFAQChunk(t, "kb-explicit", "faq-explicit", uuid.NewString(), "explicit KB question")
unselected := makeSuggestedFAQChunk(t, "kb-other", "faq-other", uuid.NewString(), "unselected question")
require.NoError(t, repo.CreateChunks(ctx, []*types.Chunk{tagged, explicitKB, unselected}))
got, err := repo.ListRecommendedFAQChunks(ctx, 1, []string{"kb-explicit"}, nil, []string{selectedTag}, 10)
require.NoError(t, err)
require.Len(t, got, 2)
assert.ElementsMatch(t, []string{tagged.ID, explicitKB.ID}, []string{got[0].ID, got[1].ID})
}
func TestListRecentDocumentChunksWithQuestions_KnowledgeScopeDoesNotIncludeSiblingDocuments(t *testing.T) {
db := setupChunkTestDB(t)
repo := NewChunkRepository(db)
ctx := context.Background()
selected := makeSuggestedDocumentChunk(t, "kb-1", "doc-selected", "selected document question")
sibling := makeSuggestedDocumentChunk(t, "kb-1", "doc-sibling", "sibling document question")
require.NoError(t, repo.CreateChunks(ctx, []*types.Chunk{selected, sibling}))
got, err := repo.ListRecentDocumentChunksWithQuestions(ctx, 1, nil, []string{"doc-selected"}, 10)
require.NoError(t, err)
require.Len(t, got, 1)
assert.Equal(t, selected.ID, got[0].ID)
}
func TestListRecentDocumentChunksWithQuestions_UnionsExplicitKBAndKnowledge(t *testing.T) {
db := setupChunkTestDB(t)
repo := NewChunkRepository(db)
ctx := context.Background()
fromExplicitKB := makeSuggestedDocumentChunk(t, "kb-explicit", "doc-1", "explicit KB question")
fromExplicitDocument := makeSuggestedDocumentChunk(t, "kb-other", "doc-selected", "selected document question")
unselected := makeSuggestedDocumentChunk(t, "kb-other", "doc-other", "unselected question")
require.NoError(t, repo.CreateChunks(ctx, []*types.Chunk{fromExplicitKB, fromExplicitDocument, unselected}))
got, err := repo.ListRecentDocumentChunksWithQuestions(
ctx, 1, []string{"kb-explicit"}, []string{"doc-selected"}, 10,
)
require.NoError(t, err)
require.Len(t, got, 2)
assert.ElementsMatch(t, []string{fromExplicitKB.ID, fromExplicitDocument.ID}, []string{got[0].ID, got[1].ID})
}