diff --git a/frontend/src/api/agent/index.ts b/frontend/src/api/agent/index.ts index 38269d9d..6df3725e 100644 --- a/frontend/src/api/agent/index.ts +++ b/frontend/src/api/agent/index.ts @@ -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; } diff --git a/internal/application/repository/wiki_page.go b/internal/application/repository/wiki_page.go index 0f2d7d12..8c744dc8 100644 --- a/internal/application/repository/wiki_page.go +++ b/internal/application/repository/wiki_page.go @@ -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). diff --git a/internal/application/service/custom_agent.go b/internal/application/service/custom_agent.go index bcf74138..56ece807 100644 --- a/internal/application/service/custom_agent.go +++ b/internal/application/service/custom_agent.go @@ -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 ?" 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 +} diff --git a/internal/types/custom_agent.go b/internal/types/custom_agent.go index 2622fce8..c35a6018 100644 --- a/internal/types/custom_agent.go +++ b/internal/types/custom_agent.go @@ -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"` } diff --git a/internal/types/interfaces/wiki_page.go b/internal/types/interfaces/wiki_page.go index 63949670..5af91469 100644 --- a/internal/types/interfaces/wiki_page.go +++ b/internal/types/interfaces/wiki_page.go @@ -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