feat: Enhance suggested questions functionality with wiki integration

- Updated the SuggestedQuestion interface to include 'wiki' as a new source type, allowing for more diverse question origins.
- Implemented ListRecentForSuggestions method in the wikiPageRepository to retrieve recent wiki pages for fallback suggestions, improving the agent's ability to generate relevant questions when other sources are unavailable.
- Enhanced the customAgentService to utilize the new wiki page suggestions, ensuring a broader range of questions can be generated from wiki content.

These changes significantly improve the agent's suggestion capabilities by incorporating wiki content, enhancing user interaction and information retrieval.
This commit is contained in:
wizardchen
2026-04-21 15:20:41 +08:00
parent 3f97a7a090
commit 0531700518
5 changed files with 121 additions and 9 deletions
+1 -1
View File
@@ -222,7 +222,7 @@ export function toggleIMChannel(id: string) {
// 推荐问题
export interface SuggestedQuestion {
question: string;
source: 'faq' | 'document' | 'agent_config';
source: 'faq' | 'document' | 'agent_config' | 'wiki';
knowledge_base_id?: string;
}
@@ -229,6 +229,34 @@ func (r *wikiPageRepository) ListAll(ctx context.Context, kbID string) ([]*types
return pages, nil
}
// ListRecentForSuggestions returns recent user-visible wiki pages across the given
// knowledge bases, used as a fallback source for agent suggested questions when
// the KB has no FAQ entries or AI-generated document questions (typical for
// Wiki-only KBs). Excludes index/log pages and archived pages.
func (r *wikiPageRepository) ListRecentForSuggestions(
ctx context.Context,
tenantID uint64,
kbIDs []string,
limit int,
) ([]*types.WikiPage, error) {
if len(kbIDs) == 0 || limit <= 0 {
return nil, nil
}
var pages []*types.WikiPage
if err := r.db.WithContext(ctx).
Where("tenant_id = ?", tenantID).
Where("knowledge_base_id IN ?", kbIDs).
Where("page_type NOT IN ?", []string{types.WikiPageTypeIndex, types.WikiPageTypeLog}).
Where("status = ?", types.WikiPageStatusPublished).
Where("title <> ''").
Order("updated_at DESC").
Limit(limit).
Find(&pages).Error; err != nil {
return nil, err
}
return pages, nil
}
// Delete soft-deletes a wiki page by knowledge base ID and slug
func (r *wikiPageRepository) Delete(ctx context.Context, kbID string, slug string) error {
result := r.db.WithContext(ctx).
+83 -6
View File
@@ -24,9 +24,10 @@ var (
// customAgentService implements the CustomAgentService interface
type customAgentService struct {
repo interfaces.CustomAgentRepository
chunkRepo interfaces.ChunkRepository
kbService interfaces.KnowledgeBaseService
repo interfaces.CustomAgentRepository
chunkRepo interfaces.ChunkRepository
kbService interfaces.KnowledgeBaseService
wikiPageRepo interfaces.WikiPageRepository
}
// NewCustomAgentService creates a new custom agent service
@@ -34,11 +35,13 @@ func NewCustomAgentService(
repo interfaces.CustomAgentRepository,
chunkRepo interfaces.ChunkRepository,
kbService interfaces.KnowledgeBaseService,
wikiPageRepo interfaces.WikiPageRepository,
) interfaces.CustomAgentService {
return &customAgentService{
repo: repo,
chunkRepo: chunkRepo,
kbService: kbService,
repo: repo,
chunkRepo: chunkRepo,
kbService: kbService,
wikiPageRepo: wikiPageRepo,
}
}
@@ -567,6 +570,36 @@ func (s *customAgentService) GetSuggestedQuestions(
}
}
// Collect Wiki pages as a fallback source. This covers Wiki-only KBs where no
// document chunks carry AI-generated questions (question_generation is skipped
// when the KB does not need an embedding model). knowledge_id filter is
// intentionally ignored here because wiki pages are authored at the KB level
// and are not 1:1 with source knowledge items.
if len(queryKBIDs) > 0 && s.wikiPageRepo != nil {
wikiPages, err := s.wikiPageRepo.ListRecentForSuggestions(ctx, tenantID, queryKBIDs, fetchLimit)
if err != nil {
logger.ErrorWithFields(ctx, err, map[string]interface{}{
"agent_id": agentID,
})
} else {
locale, _ := types.LanguageFromContext(ctx)
for _, page := range wikiPages {
q := wikiSuggestionFromPage(page, locale)
if q == "" || seen[q] {
continue
}
seen[q] = true
// Use page.ID as the bucket key so round-robin mixes pages from
// different wiki entries rather than clumping them.
buckets[page.ID] = append(buckets[page.ID], types.SuggestedQuestion{
Question: q,
Source: "wiki",
KnowledgeBaseID: page.KnowledgeBaseID,
})
}
}
}
// 4. Shuffle within each bucket, then round-robin across buckets
// to ensure diversity across different documents.
bucketKeys := make([]string, 0, len(buckets))
@@ -610,3 +643,47 @@ func (s *customAgentService) truncateQuestions(questions []types.SuggestedQuesti
}
return questions
}
// wikiSuggestionFromPage converts a wiki page into a human-readable suggested
// question string. The template is chosen per page type so the chip reads
// naturally for that kind of content:
// - concept: "What is <title>?" works for abstract terms (RAG, embedding,
// idempotency…).
// - entity / summary: "Tell me about <title>" is neutral and works for
// people, places, organizations, products and document summaries where
// "what is <name>?" would read awkwardly ("什么是张三?").
// - everything else (synthesis, comparison, …): the raw title is already a
// good topical query on its own.
func wikiSuggestionFromPage(page *types.WikiPage, locale string) string {
if page == nil {
return ""
}
title := strings.TrimSpace(page.Title)
if title == "" {
return ""
}
switch page.PageType {
case types.WikiPageTypeConcept:
if isEnglishLocale(locale) {
return "What is " + title + "?"
}
return "什么是" + title + "?"
case types.WikiPageTypeEntity, types.WikiPageTypeSummary:
if isEnglishLocale(locale) {
return "Tell me about " + title
}
return "介绍一下" + title
default:
return title
}
}
// isEnglishLocale reports whether the locale string is an English variant.
// Unknown / empty locales fall back to Chinese, matching the product default.
func isEnglishLocale(locale string) bool {
switch locale {
case "en-US", "en", "en-GB":
return true
}
return false
}
+2 -2
View File
@@ -290,9 +290,9 @@ func (a *CustomAgent) IsAgentMode() bool {
type SuggestedQuestion struct {
// 问题文本
Question string `json:"question"`
// 来源类型: "faq", "document", "agent_config"
// 来源类型: "agent_config", "faq", "document", "wiki"
Source string `json:"source"`
// 来源知识库ID(仅 faq/document 来源时有值)
// 来源知识库ID(仅 faq/document/wiki 来源时有值)
KnowledgeBaseID string `json:"knowledge_base_id,omitempty"`
}
+7
View File
@@ -111,6 +111,13 @@ type WikiPageRepository interface {
// ListAll retrieves all wiki pages in a knowledge base (for link rebuilding, graph generation).
ListAll(ctx context.Context, kbID string) ([]*types.WikiPage, error)
// ListRecentForSuggestions returns recent user-visible wiki pages under the given
// knowledge bases, used to produce fallback suggested questions for Wiki-only KBs
// that do not have AI-generated document questions or recommended FAQ entries.
// Excludes index/log pages and archived pages. Returns up to `limit` rows sorted
// by updated_at descending.
ListRecentForSuggestions(ctx context.Context, tenantID uint64, kbIDs []string, limit int) ([]*types.WikiPage, error)
// Delete soft-deletes a wiki page by knowledge base ID and slug.
Delete(ctx context.Context, kbID string, slug string) error