feat(agent): enhance suggested questions API with tag scopes
Update the SuggestedQuestionsRequest to include TagScopes for better filtering of knowledge items based on their parent knowledge bases. Modify related API documentation and frontend interfaces to accommodate the new structure. Ensure backend services handle the new tag scope logic correctly, including tests for the updated functionality.
This commit is contained in:
@@ -308,6 +308,18 @@ func (s *sessionService) buildAgentConfig(
|
||||
return nil, fmt.Errorf("build search targets: %w", err)
|
||||
}
|
||||
agentConfig.SearchTargets = searchTargets
|
||||
// Document tags are stored in knowledge_tag_relations, so document-KB tag
|
||||
// scopes are resolved to concrete knowledge IDs before retrieval. Preserve
|
||||
// those resolved IDs as this turn's pinned documents as well: otherwise the
|
||||
// Agent tools are correctly constrained behind the scenes, but the model only
|
||||
// sees a bound KB and does not know which documents the user explicitly chose.
|
||||
if len(req.TagScopes) > 0 {
|
||||
agentConfig.KnowledgeIDs = mergeResolvedTagKnowledgeIDs(
|
||||
agentConfig.KnowledgeIDs,
|
||||
searchTargets,
|
||||
req.TagScopes,
|
||||
)
|
||||
}
|
||||
logger.Infof(ctx, "Agent search targets built: %d targets", len(searchTargets))
|
||||
|
||||
if agentConfig.MaxContextTokens <= 0 {
|
||||
@@ -317,6 +329,31 @@ func (s *sessionService) buildAgentConfig(
|
||||
return agentConfig, nil
|
||||
}
|
||||
|
||||
func mergeResolvedTagKnowledgeIDs(
|
||||
existing []string,
|
||||
searchTargets types.SearchTargets,
|
||||
tagScopes []types.TagScope,
|
||||
) []string {
|
||||
tagKBs := make(map[string]bool, len(tagScopes))
|
||||
for _, scope := range tagScopes {
|
||||
if scope.KnowledgeBaseID != "" && len(scope.TagIDs) > 0 {
|
||||
tagKBs[scope.KnowledgeBaseID] = true
|
||||
}
|
||||
}
|
||||
if len(tagKBs) == 0 {
|
||||
return uniqueNonEmptyStrings(existing)
|
||||
}
|
||||
|
||||
merged := append([]string(nil), existing...)
|
||||
for _, target := range searchTargets {
|
||||
if target == nil || !tagKBs[target.KnowledgeBaseID] || target.Type != types.SearchTargetTypeKnowledge {
|
||||
continue
|
||||
}
|
||||
merged = append(merged, target.KnowledgeIDs...)
|
||||
}
|
||||
return uniqueNonEmptyStrings(merged)
|
||||
}
|
||||
|
||||
// applyPerRequestSkillScope narrows the agent's skill whitelist to the @Skill
|
||||
// mentions for this turn and records the pinned set for the <must_use> hint.
|
||||
// It is a no-op when no skills were mentioned or skills are disabled.
|
||||
|
||||
Reference in New Issue
Block a user