Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

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

wizardchen3c83cf98736f
diff --git a/internal/application/service/agent_service.go b/internal/application/service/agent_service.go
index 4edd15f0d..deb445796 100644
--- a/internal/application/service/agent_service.go
+++ b/internal/application/service/agent_service.go
@@ -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)
diff --git a/internal/application/service/chat_pipline/search.go b/internal/application/service/chat_pipline/search.go
index 75c0402c3..60e24e608 100644
--- a/internal/application/service/chat_pipline/search.go
+++ b/internal/application/service/chat_pipline/search.go
@@ -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,
})
diff --git a/internal/application/service/session.go b/internal/application/service/session.go
index c277ed57c..9aac121f9 100644
--- a/internal/application/service/session.go
+++ b/internal/application/service/session.go
@@ -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"]
}
diff --git a/internal/handler/session/qa.go b/internal/handler/session/qa.go
index 92dcb56fe..84e7a5713 100644
--- a/internal/handler/session/qa.go
+++ b/internal/handler/session/qa.go
@@ -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)