feat: Enhance agent tool compatibility and knowledge base filtering
- Introduced a unified capability requirements system for agent tools, ensuring consistent filtering of knowledge bases based on their capabilities. - Implemented derived `kb_filter` logic to streamline compatibility checks between agent tools and knowledge bases, reducing redundancy in configuration. - Updated frontend components to reflect new filtering logic, including improved handling of empty states and user feedback when no compatible knowledge bases are available. - Enhanced internationalization support by adding relevant messages for tool compatibility issues across multiple languages.
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/agent/tools"
|
||||
chatpipeline "github.com/Tencent/WeKnora/internal/application/service/chat_pipeline"
|
||||
"github.com/Tencent/WeKnora/internal/common"
|
||||
"github.com/Tencent/WeKnora/internal/event"
|
||||
@@ -328,6 +329,22 @@ func (s *sessionService) resolveKnowledgeBasesFromAgent(
|
||||
|
||||
switch customAgent.Config.KBSelectionMode {
|
||||
case "all":
|
||||
// Authoritative capability filter for the runtime path. The frontend
|
||||
// editor and @mention dropdown apply the same filter, but we don't
|
||||
// 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)
|
||||
accept := func(kb *types.KnowledgeBase) bool {
|
||||
if kb == nil {
|
||||
return false
|
||||
}
|
||||
if capFilter.IsEmpty() {
|
||||
return true
|
||||
}
|
||||
return tools.KBSatisfiesToolRequirements(kb.Capabilities(), customAgent.Config.AllowedTools)
|
||||
}
|
||||
|
||||
// Get own knowledge bases (uses ctx TenantID = agent's tenant)
|
||||
allKBs, err := s.knowledgeBaseService.ListKnowledgeBases(ctx)
|
||||
if err != nil {
|
||||
@@ -335,7 +352,12 @@ func (s *sessionService) resolveKnowledgeBasesFromAgent(
|
||||
}
|
||||
kbIDSet := make(map[string]bool)
|
||||
kbIDs := make([]string, 0, len(allKBs))
|
||||
ownSkipped := 0
|
||||
for _, kb := range allKBs {
|
||||
if !accept(kb) {
|
||||
ownSkipped++
|
||||
continue
|
||||
}
|
||||
kbIDs = append(kbIDs, kb.ID)
|
||||
kbIDSet[kb.ID] = true
|
||||
}
|
||||
@@ -344,6 +366,7 @@ func (s *sessionService) resolveKnowledgeBasesFromAgent(
|
||||
// tenant's own KBs. Including the current user's shared KBs would leak
|
||||
// unrelated KBs from other organisations into the agent's retrieval scope.
|
||||
isSharedAgent := sessionTenantID != 0 && sessionTenantID != customAgent.TenantID
|
||||
sharedSkipped := 0
|
||||
if !isSharedAgent {
|
||||
tenantID := types.MustTenantIDFromContext(ctx)
|
||||
userIDVal := ctx.Value(types.UserIDContextKey)
|
||||
@@ -354,10 +377,15 @@ func (s *sessionService) resolveKnowledgeBasesFromAgent(
|
||||
logger.Warnf(ctx, "Failed to list shared knowledge bases: %v", err)
|
||||
} else {
|
||||
for _, info := range sharedList {
|
||||
if info != nil && info.KnowledgeBase != nil && !kbIDSet[info.KnowledgeBase.ID] {
|
||||
kbIDs = append(kbIDs, info.KnowledgeBase.ID)
|
||||
kbIDSet[info.KnowledgeBase.ID] = true
|
||||
if info == nil || info.KnowledgeBase == nil || kbIDSet[info.KnowledgeBase.ID] {
|
||||
continue
|
||||
}
|
||||
if !accept(info.KnowledgeBase) {
|
||||
sharedSkipped++
|
||||
continue
|
||||
}
|
||||
kbIDs = append(kbIDs, info.KnowledgeBase.ID)
|
||||
kbIDSet[info.KnowledgeBase.ID] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -367,6 +395,11 @@ func (s *sessionService) resolveKnowledgeBasesFromAgent(
|
||||
sessionTenantID, customAgent.TenantID)
|
||||
}
|
||||
|
||||
if ownSkipped+sharedSkipped > 0 {
|
||||
logger.Infof(ctx,
|
||||
"KBSelectionMode=all: tool-capability filter removed %d own + %d shared KBs (agent=%s, tools=%v)",
|
||||
ownSkipped, sharedSkipped, customAgent.ID, customAgent.Config.AllowedTools)
|
||||
}
|
||||
logger.Infof(ctx, "KBSelectionMode=all: loaded %d knowledge bases (own + shared)", len(kbIDs))
|
||||
return kbIDs
|
||||
case "selected":
|
||||
|
||||
Reference in New Issue
Block a user