fix(agent): exclude wiki-only KBs from quick-answer (RAG) mode

Quick-answer agent mode retrieves purely through vector/keyword chunk
search and ships with no `allowed_tools`, so the existing capability
filter (which only reads from `allowed_tools`) let wiki-only KBs through
in every entry point. End result: users could @-mention, select, and
receive suggested questions from wiki-only KBs in quick-answer mode,
but the underlying retrieval always returned empty.

Treat "RAG-only" as an implicit property of `agent_mode = quick-answer`
and union it with the tool-derived filter. The same predicate is now
used everywhere the user can pick or be steered toward a KB:

Backend
- `tools.DeriveKBFilterForAgent` / `KBSatisfiesAgentRequirements`
  layer the implicit quick-answer requirement on top of tool derivation.
- `ListKnowledgeBases`, `SearchKnowledge` (shared-agent `@file`),
  `resolveKnowledgeBasesFromAgent` (chat runtime), `/search` IM command,
  and `GetSuggestedQuestions` now all use the agent-mode-aware variant.
- `GetSuggestedQuestions` also skips the wiki-page fallback for
  quick-answer agents to cover the `selected` / explicit-kb-ids paths
  where a wiki-only KB could still slip through.

Frontend
- `deriveKbFilterForAgent` / `kbSatisfiesAgentRequirements` mirror the
  Go helpers.
- `@` mention dropdown (`Input-field.vue`) uses the new helper.
- Agent editor's "specified KB" picker (`AgentEditorModal.vue`) grays
  out wiki-only KBs for quick-answer agents with a tooltip, and the
  pre-save warning fires for quick-answer mode too.
- i18n: add `agentEditor.agentType.kbMismatch.quickAnswer` across all
  four locales.
This commit is contained in:
wizardchen
2026-05-12 15:41:47 +08:00
committed by lyingbug
parent a082c04d28
commit d199628701
13 changed files with 185 additions and 23 deletions
+57
View File
@@ -152,6 +152,63 @@ func KBSatisfiesToolRequirements(caps types.KBCapabilities, allowedTools []strin
return false
}
// quickAnswerKBFilter is the implicit capability requirement for the
// "quick-answer" (RAG) agent mode. Quick-answer drives retrieval purely
// through vector/keyword chunk search, so a KB with neither chunk index
// (e.g. wiki-only) cannot contribute any context and should be filtered
// out everywhere the user can pick a KB (agent KB scope, `@` mention list,
// chat KB selector).
//
// We treat this as a property of the agent MODE rather than of any
// specific tool, because quick-answer mode doesn't ship with an
// `allowed_tools` list — its retrieval is implicit.
var quickAnswerKBFilter = KBFilter{AnyOf: []KBCapability{CapVector, CapKeyword}}
// DeriveKBFilterForAgent derives the effective KB capability filter for a
// given agent configuration. It combines the implicit constraint from
// `agentMode` (quick-answer forces vector|keyword) with the tool-derived
// filter (any_of capabilities required by some allowed tool).
//
// The returned filter is the UNION of both contributions, which matches
// the existing any_of semantics: a KB passes iff it exposes at least one
// of the listed capabilities.
func DeriveKBFilterForAgent(agentMode string, allowedTools []string) KBFilter {
seen := make(map[KBCapability]struct{})
if agentMode == "quick-answer" {
for _, c := range quickAnswerKBFilter.AnyOf {
seen[c] = struct{}{}
}
}
for _, c := range DeriveKBFilterFromTools(allowedTools).AnyOf {
seen[c] = struct{}{}
}
if len(seen) == 0 {
return KBFilter{}
}
out := make([]KBCapability, 0, len(seen))
for c := range seen {
out = append(out, c)
}
return KBFilter{AnyOf: out}
}
// KBSatisfiesAgentRequirements is the agent-aware variant of
// KBSatisfiesToolRequirements: it also enforces the implicit capability
// constraints of `agentMode` (currently: quick-answer requires vector or
// keyword indexing on the KB).
func KBSatisfiesAgentRequirements(caps types.KBCapabilities, agentMode string, allowedTools []string) bool {
f := DeriveKBFilterForAgent(agentMode, allowedTools)
if f.IsEmpty() {
return true
}
for _, c := range f.AnyOf {
if hasCap(caps, c) {
return true
}
}
return false
}
// ToolsConsumeFiles reports whether any tool in the allowed-tools list can
// use user-provided file references. Used to gate the `@file` listing in
// the chat input (and potentially SearchKnowledge defensively on the
@@ -7,6 +7,7 @@ import (
"strings"
"time"
"github.com/Tencent/WeKnora/internal/agent/tools"
"github.com/Tencent/WeKnora/internal/application/repository"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/types"
@@ -477,7 +478,16 @@ func (s *customAgentService) GetSuggestedQuestions(
// Return what we have so far (agent_config suggestions)
return s.truncateQuestions(result, limit), nil
}
// Honor the agent's implicit/explicit capability requirements so
// e.g. a quick-answer (RAG-only) agent doesn't surface wiki-only
// KBs whose wiki pages it could never answer from. Same filter
// the @ mention dropdown applies on the frontend.
capFilter := tools.DeriveKBFilterForAgent(agent.Config.AgentMode, agent.Config.AllowedTools)
for _, kb := range kbs {
if !capFilter.IsEmpty() &&
!tools.KBSatisfiesAgentRequirements(kb.Capabilities(), agent.Config.AgentMode, agent.Config.AllowedTools) {
continue
}
effectiveKBIDs = append(effectiveKBIDs, kb.ID)
}
case "selected":
@@ -575,6 +585,14 @@ func (s *customAgentService) GetSuggestedQuestions(
// 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.
//
// Skip entirely for quick-answer (RAG-only) agents: those can't ever
// retrieve a wiki page, so surfacing wiki-derived suggestions would lure
// the user into asking questions the agent will then answer with empty
// context. Smart-reasoning agents that opt in to wiki tools keep this.
if agent.Config.AgentMode == types.AgentModeQuickAnswer {
queryKBIDs = nil
}
if len(queryKBIDs) > 0 && s.wikiPageRepo != nil {
wikiPages, err := s.wikiPageRepo.ListRecentForSuggestions(ctx, tenantID, queryKBIDs, fetchLimit)
if err != nil {
@@ -334,7 +334,7 @@ func (s *sessionService) resolveKnowledgeBasesFromAgent(
// trust the client here: a stale session payload or API caller could
// still ask us to retrieve against an incompatible KB and we'd rather
// just drop it (and log) than feed it to tools that would no-op.
capFilter := tools.DeriveKBFilterFromTools(customAgent.Config.AllowedTools)
capFilter := tools.DeriveKBFilterForAgent(customAgent.Config.AgentMode, customAgent.Config.AllowedTools)
accept := func(kb *types.KnowledgeBase) bool {
if kb == nil {
return false
@@ -342,7 +342,7 @@ func (s *sessionService) resolveKnowledgeBasesFromAgent(
if capFilter.IsEmpty() {
return true
}
return tools.KBSatisfiesToolRequirements(kb.Capabilities(), customAgent.Config.AllowedTools)
return tools.KBSatisfiesAgentRequirements(kb.Capabilities(), customAgent.Config.AgentMode, customAgent.Config.AllowedTools)
}
// Get own knowledge bases (uses ctx TenantID = agent's tenant)
+6 -4
View File
@@ -1517,14 +1517,16 @@ func (h *KnowledgeHandler) SearchKnowledge(c *gin.Context) {
}
// `all` mode: authoritative server-side capability filter. Mirrors the
// logic in ListKnowledgeBases so @file search, KB listing, and runtime
// all agree on what "mode=all" actually means for this agent.
filter := tools.DeriveKBFilterFromTools(agent.Config.AllowedTools)
// all agree on what "mode=all" actually means for this agent. The
// filter is agent-mode aware so quick-answer (RAG-only) skips
// wiki-only KBs even though it has no `allowed_tools`.
filter := tools.DeriveKBFilterForAgent(agent.Config.AgentMode, agent.Config.AllowedTools)
removed := 0
for _, kb := range kbs {
if kb == nil || kb.Type != types.KnowledgeBaseTypeDocument {
continue
}
if !filter.IsEmpty() && !tools.KBSatisfiesToolRequirements(kb.Capabilities(), agent.Config.AllowedTools) {
if !filter.IsEmpty() && !tools.KBSatisfiesAgentRequirements(kb.Capabilities(), agent.Config.AgentMode, agent.Config.AllowedTools) {
removed++
continue
}
@@ -1532,7 +1534,7 @@ func (h *KnowledgeHandler) SearchKnowledge(c *gin.Context) {
}
if removed > 0 {
logger.Infof(ctx,
"SearchKnowledge(agent=%s, mode=all): tool-capability filter removed %d KBs",
"SearchKnowledge(agent=%s, mode=all): capability filter removed %d KBs",
agentID, removed)
}
}
+8 -5
View File
@@ -357,21 +357,24 @@ func (h *KnowledgeBaseHandler) ListKnowledgeBases(c *gin.Context) {
// `all` mode: authoritative server-side capability filter so a client
// that bypassed the frontend (old tab, curl, rogue plugin) can't @ a
// KB whose capabilities don't match any allowed tool of this agent.
// KB whose capabilities don't match this agent. The filter combines
// tool-derived requirements (smart-reasoning) with the implicit
// RAG-only requirement of quick-answer mode (which has no
// `allowed_tools` but still needs vector/keyword chunks to work).
// Non-`all` modes already constrain the scope explicitly.
if mode == "all" && len(agent.Config.AllowedTools) > 0 {
filter := tools.DeriveKBFilterFromTools(agent.Config.AllowedTools)
if mode == "all" {
filter := tools.DeriveKBFilterForAgent(agent.Config.AgentMode, agent.Config.AllowedTools)
if !filter.IsEmpty() {
before := len(kbs)
kept := make([]*types.KnowledgeBase, 0, before)
for _, kb := range kbs {
if tools.KBSatisfiesToolRequirements(kb.Capabilities(), agent.Config.AllowedTools) {
if tools.KBSatisfiesAgentRequirements(kb.Capabilities(), agent.Config.AgentMode, agent.Config.AllowedTools) {
kept = append(kept, kb)
}
}
if removed := before - len(kept); removed > 0 {
logger.Infof(ctx,
"ListKnowledgeBases(agent=%s, mode=all): tool-capability filter removed %d of %d KBs",
"ListKnowledgeBases(agent=%s, mode=all): capability filter removed %d of %d KBs",
agentID, removed, before)
}
kbs = kept
+7 -4
View File
@@ -52,14 +52,17 @@ func (c *SearchCommand) Execute(ctx context.Context, cmdCtx *CommandContext, arg
case "all":
allKBs, err := c.kbService.ListKnowledgeBases(ctx)
if err == nil {
// Same tool-capability filter as the QA pipeline's
// Same capability filter as the QA pipeline's
// resolveKnowledgeBasesFromAgent (`all` branch) so `/search`
// agrees with what the agent's tools can actually reach.
filter := tools.DeriveKBFilterFromTools(cmdCtx.CustomAgent.Config.AllowedTools)
// Agent-mode aware: quick-answer enforces RAG-only KBs.
agentMode := cmdCtx.CustomAgent.Config.AgentMode
allowed := cmdCtx.CustomAgent.Config.AllowedTools
filter := tools.DeriveKBFilterForAgent(agentMode, allowed)
skipped := 0
for _, kb := range allKBs {
if !filter.IsEmpty() &&
!tools.KBSatisfiesToolRequirements(kb.Capabilities(), cmdCtx.CustomAgent.Config.AllowedTools) {
!tools.KBSatisfiesAgentRequirements(kb.Capabilities(), agentMode, allowed) {
skipped++
continue
}
@@ -67,7 +70,7 @@ func (c *SearchCommand) Execute(ctx context.Context, cmdCtx *CommandContext, arg
}
if skipped > 0 {
logger.Infof(ctx,
"/search(agent=%s, mode=all): tool-capability filter removed %d of %d KBs",
"/search(agent=%s, mode=all): capability filter removed %d of %d KBs",
cmdCtx.CustomAgent.ID, skipped, len(allKBs))
}
}