feat: 增强知识库和搜索功能的逻辑

This commit is contained in:
wizardchen
2025-12-22 11:23:25 +08:00
committed by lyingbug
parent f7900a5e9a
commit 3c83cf9873
4 changed files with 50 additions and 8 deletions
@@ -195,6 +195,11 @@ func (s *agentService) registerTools(
"database_query": true,
}
// If no knowledge and no web search, also disable todo_write (not useful for simple chat)
if !config.WebSearchEnabled {
kbTools["todo_write"] = true
}
for _, toolName := range allowedTools {
if !kbTools[toolName] {
filteredTools = append(filteredTools, toolName)
@@ -57,8 +57,9 @@ func (p *PluginSearch) ActivationEvents() []types.EventType {
func (p *PluginSearch) OnEvent(ctx context.Context,
eventType types.EventType, chatManage *types.ChatManage, next func() *PluginError,
) *PluginError {
// Check if we have search targets
if len(chatManage.SearchTargets) == 0 && len(chatManage.KnowledgeBaseIDs) == 0 && len(chatManage.KnowledgeIDs) == 0 {
// Check if we have search targets or web search enabled
hasKBTargets := len(chatManage.SearchTargets) > 0 || len(chatManage.KnowledgeBaseIDs) > 0 || len(chatManage.KnowledgeIDs) > 0
if !hasKBTargets && !chatManage.WebSearchEnabled {
pipelineError(ctx, "Search", "kb_not_found", map[string]interface{}{
"session_id": chatManage.SessionID,
})
+10 -5
View File
@@ -558,16 +558,21 @@ func (s *sessionService) KnowledgeQA(
EnableQueryExpansion: enableQueryExpansion,
}
// Determine pipeline based on knowledge bases availability
// If no knowledge bases are selected, use pure chat pipeline
// Determine pipeline based on knowledge bases availability and web search setting
// If no knowledge bases are selected AND web search is disabled, use pure chat pipeline
// Otherwise use rag_stream pipeline (which handles both KB search and web search)
var pipeline []types.EventType
if len(knowledgeBaseIDs) == 0 && len(knowledgeIDs) == 0 {
logger.Info(ctx, "No knowledge bases selected, using chat_stream pipeline")
if len(knowledgeBaseIDs) == 0 && len(knowledgeIDs) == 0 && !webSearchEnabled {
logger.Info(ctx, "No knowledge bases selected and web search disabled, using chat_stream pipeline")
pipeline = types.Pipline["chat_stream"]
// For pure chat, UserContent is the Query (since INTO_CHAT_MESSAGE is skipped)
chatManage.UserContent = query
} else {
logger.Info(ctx, "Knowledge bases selected, using rag_stream pipeline")
if webSearchEnabled && len(knowledgeBaseIDs) == 0 && len(knowledgeIDs) == 0 {
logger.Info(ctx, "Web search enabled without knowledge bases, using rag_stream pipeline for web search only")
} else {
logger.Info(ctx, "Knowledge bases selected, using rag_stream pipeline")
}
pipeline = types.Pipline["rag_stream"]
}
+32 -1
View File
@@ -234,10 +234,11 @@ func (h *Handler) AgentQA(c *gin.Context) {
// Detect if knowledge bases or agent mode has changed
knowledgeBasesChanged := false
knowledgeIdsChanged := false
configChanged := false
// Check if knowledge bases array has changed
if len(request.KnowledgeBaseIDs) > 0 {
if len(request.KnowledgeBaseIDs) > 0 || len(session.AgentConfig.KnowledgeBases) > 0 {
// Compare arrays to detect changes
currentKBs := session.AgentConfig.KnowledgeBases
if len(currentKBs) != len(request.KnowledgeBaseIDs) {
@@ -265,6 +266,35 @@ func (h *Handler) AgentQA(c *gin.Context) {
}
}
// Check if knowledge IDs array has changed
if len(request.KnowledgeIds) > 0 || len(session.AgentConfig.KnowledgeIDs) > 0 {
// Compare arrays to detect changes
currentKIDs := session.AgentConfig.KnowledgeIDs
if len(currentKIDs) != len(request.KnowledgeIds) {
knowledgeIdsChanged = true
configChanged = true
} else {
// Check if contents are different
kidMap := make(map[string]bool)
for _, kid := range currentKIDs {
kidMap[kid] = true
}
for _, kid := range request.KnowledgeIds {
if !kidMap[kid] {
knowledgeIdsChanged = true
configChanged = true
break
}
}
}
if knowledgeIdsChanged {
logger.Infof(ctx, "Knowledge IDs changed from %s to %s",
secutils.SanitizeForLog(fmt.Sprintf("%v", session.AgentConfig.KnowledgeIDs)),
secutils.SanitizeForLog(fmt.Sprintf("%v", request.KnowledgeIds)),
)
}
}
// Check if agent mode has changed
currentAgentEnabled := session.AgentConfig.AgentModeEnabled
if request.AgentEnabled != currentAgentEnabled {
@@ -301,6 +331,7 @@ func (h *Handler) AgentQA(c *gin.Context) {
// Continue anyway - this is not a fatal error
}
session.AgentConfig.KnowledgeBases = secutils.SanitizeForLogArray(request.KnowledgeBaseIDs)
session.AgentConfig.KnowledgeIDs = secutils.SanitizeForLogArray(request.KnowledgeIds)
session.AgentConfig.AgentModeEnabled = request.AgentEnabled
session.AgentConfig.WebSearchEnabled = request.WebSearchEnabled
session.SummaryModelID = secutils.SanitizeForLog(summaryModelID)