feat(chat): add @Skill/@MCP mentions with scoped agent runtime
Wire per-turn @mentions for skills, MCP services, tags, and files through QA handlers into agent config, runtime_context/must_use user blocks, and tool registration. Fix tag-only scopes dropping KB tools, refresh suggested questions on mention changes, and strengthen MCP must_use so @selected services are invoked before local KB search.
This commit is contained in:
@@ -26,7 +26,7 @@ func (s *sessionService) resolveKnowledgeBases(
|
||||
knowledgeIDs = req.KnowledgeIDs
|
||||
customAgent := req.CustomAgent
|
||||
|
||||
hasExplicitMention := len(kbIDs) > 0 || len(knowledgeIDs) > 0
|
||||
hasExplicitMention := len(kbIDs) > 0 || len(knowledgeIDs) > 0 || len(req.TagScopes) > 0
|
||||
if customAgent != nil {
|
||||
logger.Infof(ctx, "KB resolution: hasExplicitMention=%v, RetrieveKBOnlyWhenMentioned=%v, KBSelectionMode=%s",
|
||||
hasExplicitMention, customAgent.Config.RetrieveKBOnlyWhenMentioned, customAgent.Config.KBSelectionMode)
|
||||
@@ -38,6 +38,7 @@ func (s *sessionService) resolveKnowledgeBases(
|
||||
// to prevent users from injecting KB/knowledge IDs outside the agent's configured range.
|
||||
if customAgent != nil && req.Session != nil && req.Session.TenantID != customAgent.TenantID {
|
||||
kbIDs, knowledgeIDs = s.restrictMentionsToAgentScope(ctx, customAgent, req.Session.TenantID, kbIDs, knowledgeIDs)
|
||||
req.TagScopes = s.restrictTagScopesToAgentScope(ctx, customAgent, req.Session.TenantID, req.TagScopes)
|
||||
}
|
||||
} else if customAgent != nil && customAgent.Config.RetrieveKBOnlyWhenMentioned {
|
||||
kbIDs = nil
|
||||
@@ -49,6 +50,31 @@ func (s *sessionService) resolveKnowledgeBases(
|
||||
return kbIDs, knowledgeIDs
|
||||
}
|
||||
|
||||
func (s *sessionService) restrictTagScopesToAgentScope(
|
||||
ctx context.Context,
|
||||
agent *types.CustomAgent,
|
||||
sessionTenantID uint64,
|
||||
tagScopes []types.TagScope,
|
||||
) []types.TagScope {
|
||||
if len(tagScopes) == 0 {
|
||||
return nil
|
||||
}
|
||||
allowedKBIDs := s.resolveKnowledgeBasesFromAgent(ctx, agent, sessionTenantID)
|
||||
allowedSet := make(map[string]bool, len(allowedKBIDs))
|
||||
for _, id := range allowedKBIDs {
|
||||
allowedSet[id] = true
|
||||
}
|
||||
filtered := make([]types.TagScope, 0, len(tagScopes))
|
||||
for _, scope := range tagScopes {
|
||||
if allowedSet[scope.KnowledgeBaseID] {
|
||||
filtered = append(filtered, scope)
|
||||
continue
|
||||
}
|
||||
logger.Warnf(ctx, "Blocking @mentioned tag scope for KB %s: not in shared agent's allowed scope", scope.KnowledgeBaseID)
|
||||
}
|
||||
return filtered
|
||||
}
|
||||
|
||||
// resolveChatModelID resolves the effective chat model ID for a QA request.
|
||||
//
|
||||
// When an agent is selected, its model configuration must be complete and
|
||||
|
||||
Reference in New Issue
Block a user