feat: Enhance agent service and session management with web search capabilities
- Updated AgentConfig to include web search settings, allowing for dynamic configuration of web search features. - Refactored AgentService to accept SessionService, enabling better management of session-specific web search states. - Implemented methods in SessionService for handling temporary knowledge base states related to web search. - Enhanced PluginSearch to utilize SessionService for managing web search state, improving integration with the overall system. - Updated container setup to ensure proper dependency injection for the new web search service and session management functionalities.
This commit is contained in:
+20
-20
@@ -23,15 +23,15 @@ func generateEventID(suffix string) string {
|
||||
|
||||
// AgentEngine is the core engine for running ReAct agents
|
||||
type AgentEngine struct {
|
||||
config *types.AgentConfig
|
||||
toolRegistry *tools.ToolRegistry
|
||||
chatModel chat.Chat
|
||||
knowledgeService interfaces.KnowledgeBaseService
|
||||
eventBus *event.EventBus
|
||||
knowledgeBasesInfo []*KnowledgeBaseInfo // Detailed knowledge base information for prompt
|
||||
contextManager interfaces.ContextManager // Context manager for writing agent conversation to LLM context
|
||||
sessionID string // Session ID for context management
|
||||
systemPromptTemplate string // System prompt template (optional, uses default if empty)
|
||||
config *types.AgentConfig
|
||||
toolRegistry *tools.ToolRegistry
|
||||
chatModel chat.Chat
|
||||
knowledgeService interfaces.KnowledgeBaseService
|
||||
eventBus *event.EventBus
|
||||
knowledgeBasesInfo []*KnowledgeBaseInfo // Detailed knowledge base information for prompt
|
||||
contextManager interfaces.ContextManager // Context manager for writing agent conversation to LLM context
|
||||
sessionID string // Session ID for context management
|
||||
systemPromptTemplate string // System prompt template (optional, uses default if empty)
|
||||
}
|
||||
|
||||
// listToolNames returns tool.function names for logging
|
||||
@@ -59,14 +59,14 @@ func NewAgentEngine(
|
||||
eventBus = event.NewEventBus()
|
||||
}
|
||||
return &AgentEngine{
|
||||
config: config,
|
||||
toolRegistry: toolRegistry,
|
||||
chatModel: chatModel,
|
||||
knowledgeService: knowledgeService,
|
||||
eventBus: eventBus,
|
||||
knowledgeBasesInfo: knowledgeBasesInfo,
|
||||
contextManager: contextManager,
|
||||
sessionID: sessionID,
|
||||
config: config,
|
||||
toolRegistry: toolRegistry,
|
||||
chatModel: chatModel,
|
||||
knowledgeService: knowledgeService,
|
||||
eventBus: eventBus,
|
||||
knowledgeBasesInfo: knowledgeBasesInfo,
|
||||
contextManager: contextManager,
|
||||
sessionID: sessionID,
|
||||
systemPromptTemplate: systemPromptTemplate,
|
||||
}
|
||||
}
|
||||
@@ -391,7 +391,7 @@ func (e *AgentEngine) executeLoop(
|
||||
|
||||
// buildToolsForLLM builds the tools list for LLM function calling
|
||||
func (e *AgentEngine) buildToolsForLLM() []chat.Tool {
|
||||
functionDefs := e.toolRegistry.GetFunctionDefinitions(e.config.AllowedTools)
|
||||
functionDefs := e.toolRegistry.GetFunctionDefinitions()
|
||||
tools := make([]chat.Tool, 0, len(functionDefs))
|
||||
for _, def := range functionDefs {
|
||||
tools = append(tools, chat.Tool{
|
||||
@@ -601,8 +601,8 @@ func (e *AgentEngine) streamThinkingToEventBus(
|
||||
opts,
|
||||
func(chunk *types.StreamResponse, fullContent string) {
|
||||
if chunk.Content != "" {
|
||||
logger.Debugf(ctx, "[Agent][Thinking][Iteration-%d] Emitting thought chunk: %d chars",
|
||||
iteration+1, len(chunk.Content))
|
||||
// logger.Debugf(ctx, "[Agent][Thinking][Iteration-%d] Emitting thought chunk: %d chars",
|
||||
// iteration+1, len(chunk.Content))
|
||||
e.eventBus.Emit(ctx, event.Event{
|
||||
ID: thinkingID, // Same ID for all chunks in this stream
|
||||
Type: event.EventAgentThought,
|
||||
|
||||
@@ -3,7 +3,6 @@ package tools
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
@@ -58,42 +57,16 @@ func (r *ToolRegistry) ListTools() []string {
|
||||
return names
|
||||
}
|
||||
|
||||
// GetFunctionDefinitions returns function definitions for allowed tools
|
||||
// This is used for LLM function calling
|
||||
// MCP tools (names starting with "mcp_") are always included, regardless of allowedTools list
|
||||
func (r *ToolRegistry) GetFunctionDefinitions(allowedTools []string) []types.FunctionDefinition {
|
||||
// GetFunctionDefinitions returns function definitions for all registered tools
|
||||
func (r *ToolRegistry) GetFunctionDefinitions() []types.FunctionDefinition {
|
||||
definitions := make([]types.FunctionDefinition, 0)
|
||||
|
||||
// If no allowed tools specified, return all tools
|
||||
if len(allowedTools) == 0 {
|
||||
for _, tool := range r.tools {
|
||||
definitions = append(definitions, types.FunctionDefinition{
|
||||
Name: tool.Name(),
|
||||
Description: tool.Description(),
|
||||
Parameters: tool.Parameters(),
|
||||
})
|
||||
}
|
||||
return definitions
|
||||
for _, tool := range r.tools {
|
||||
definitions = append(definitions, types.FunctionDefinition{
|
||||
Name: tool.Name(),
|
||||
Description: tool.Description(),
|
||||
Parameters: tool.Parameters(),
|
||||
})
|
||||
}
|
||||
|
||||
// Create a map for quick lookup
|
||||
allowedMap := make(map[string]bool)
|
||||
for _, name := range allowedTools {
|
||||
allowedMap[name] = true
|
||||
}
|
||||
|
||||
// Include allowed tools AND all MCP tools (names starting with "mcp_")
|
||||
for name, tool := range r.tools {
|
||||
// Include if it's in the allowed list OR if it's an MCP tool
|
||||
if allowedMap[name] || strings.HasPrefix(name, "mcp_") {
|
||||
definitions = append(definitions, types.FunctionDefinition{
|
||||
Name: tool.Name(),
|
||||
Description: tool.Description(),
|
||||
Parameters: tool.Parameters(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return definitions
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,250 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
)
|
||||
|
||||
// WebSearchTool performs web searches and returns results
|
||||
type WebSearchTool struct {
|
||||
BaseTool
|
||||
webSearchService interfaces.WebSearchService
|
||||
knowledgeBaseService interfaces.KnowledgeBaseService
|
||||
knowledgeService interfaces.KnowledgeService
|
||||
sessionService interfaces.SessionService
|
||||
sessionID string
|
||||
maxResults int
|
||||
}
|
||||
|
||||
// NewWebSearchTool creates a new web search tool
|
||||
func NewWebSearchTool(
|
||||
webSearchService interfaces.WebSearchService,
|
||||
knowledgeBaseService interfaces.KnowledgeBaseService,
|
||||
knowledgeService interfaces.KnowledgeService,
|
||||
sessionService interfaces.SessionService,
|
||||
sessionID string,
|
||||
maxResults int,
|
||||
) *WebSearchTool {
|
||||
description := `Search the web for current information and news. This tool searches the internet to find up-to-date information that may not be in the knowledge base.
|
||||
|
||||
## Features
|
||||
- Real-time web search: Search the internet for current information
|
||||
- RAG compression: Automatically compresses and extracts relevant content from search results
|
||||
- Session-scoped caching: Maintains temporary knowledge base for session to avoid re-indexing
|
||||
|
||||
## Usage
|
||||
|
||||
**Use when**:
|
||||
- Need current or real-time information (news, events, recent updates)
|
||||
- Information is not available in knowledge bases
|
||||
- Need to verify or supplement information from knowledge bases
|
||||
- Searching for recent developments or trends
|
||||
|
||||
**Parameters**:
|
||||
- query (required): Search query string
|
||||
|
||||
**Returns**: Web search results with title, URL, snippet, and content (up to ` + fmt.Sprintf("%d", maxResults) + ` results)
|
||||
|
||||
## Examples
|
||||
|
||||
` + "`" + `
|
||||
# Search for current information
|
||||
{
|
||||
"query": "latest developments in AI"
|
||||
}
|
||||
|
||||
# Search for recent news
|
||||
{
|
||||
"query": "Python 3.12 release notes"
|
||||
}
|
||||
` + "`" + `
|
||||
|
||||
## Tips
|
||||
|
||||
- Results are automatically compressed using RAG to extract relevant content
|
||||
- Search results are stored in a temporary knowledge base for the session
|
||||
- Use this tool when knowledge bases don't have the information you need
|
||||
- Results include URL, title, snippet, and full content when available
|
||||
- Maximum ` + fmt.Sprintf("%d", maxResults) + ` results will be returned per search`
|
||||
|
||||
return &WebSearchTool{
|
||||
BaseTool: NewBaseTool("web_search", description),
|
||||
webSearchService: webSearchService,
|
||||
knowledgeBaseService: knowledgeBaseService,
|
||||
knowledgeService: knowledgeService,
|
||||
sessionService: sessionService,
|
||||
sessionID: sessionID,
|
||||
maxResults: maxResults,
|
||||
}
|
||||
}
|
||||
|
||||
// Parameters returns the JSON schema for the tool's parameters
|
||||
func (t *WebSearchTool) Parameters() map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"type": "object",
|
||||
"properties": map[string]interface{}{
|
||||
"query": map[string]interface{}{
|
||||
"type": "string",
|
||||
"description": "Search query string",
|
||||
},
|
||||
},
|
||||
"required": []string{"query"},
|
||||
}
|
||||
}
|
||||
|
||||
// Execute executes the web search tool
|
||||
func (t *WebSearchTool) Execute(ctx context.Context, args map[string]interface{}) (*types.ToolResult, error) {
|
||||
logger.Infof(ctx, "[Tool][WebSearch] Execute started")
|
||||
|
||||
// Parse query
|
||||
query, ok := args["query"].(string)
|
||||
if !ok || query == "" {
|
||||
logger.Errorf(ctx, "[Tool][WebSearch] Query is required")
|
||||
return &types.ToolResult{
|
||||
Success: false,
|
||||
Error: "query parameter is required",
|
||||
}, fmt.Errorf("query parameter is required")
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "[Tool][WebSearch] Searching with query: %s, max_results: %d", query, t.maxResults)
|
||||
|
||||
// Get tenant ID from context
|
||||
tenantID := uint(0)
|
||||
if tid, ok := ctx.Value(types.TenantIDContextKey).(uint); ok {
|
||||
tenantID = tid
|
||||
}
|
||||
|
||||
if tenantID == 0 {
|
||||
logger.Errorf(ctx, "[Tool][WebSearch] Tenant ID not found in context")
|
||||
return &types.ToolResult{
|
||||
Success: false,
|
||||
Error: "tenant ID not found in context",
|
||||
}, fmt.Errorf("tenant ID not found in context")
|
||||
}
|
||||
|
||||
// Get tenant info from context (same approach as search.go)
|
||||
tenant := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
|
||||
if tenant == nil || tenant.WebSearchConfig == nil || tenant.WebSearchConfig.Provider == "" {
|
||||
logger.Errorf(ctx, "[Tool][WebSearch] Web search not configured for tenant %d", tenantID)
|
||||
return &types.ToolResult{
|
||||
Success: false,
|
||||
Error: "web search is not configured for this tenant",
|
||||
}, fmt.Errorf("web search is not configured for tenant %d", tenantID)
|
||||
}
|
||||
|
||||
// Create a copy of web search config with maxResults from agent config
|
||||
searchConfig := *tenant.WebSearchConfig
|
||||
searchConfig.MaxResults = t.maxResults
|
||||
|
||||
// Perform web search
|
||||
logger.Infof(ctx, "[Tool][WebSearch] Performing web search with provider: %s, maxResults: %d", searchConfig.Provider, searchConfig.MaxResults)
|
||||
webResults, err := t.webSearchService.Search(ctx, &searchConfig, query)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "[Tool][WebSearch] Web search failed: %v", err)
|
||||
return &types.ToolResult{
|
||||
Success: false,
|
||||
Error: fmt.Sprintf("web search failed: %v", err),
|
||||
}, fmt.Errorf("web search failed: %w", err)
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "[Tool][WebSearch] Web search returned %d results", len(webResults))
|
||||
|
||||
// Apply RAG compression if configured
|
||||
if len(webResults) > 0 && tenant.WebSearchConfig.CompressionMethod != "none" && tenant.WebSearchConfig.CompressionMethod != "" {
|
||||
// Load session-scoped temp KB state from Redis using SessionService
|
||||
tempKBID, seen, ids := t.sessionService.GetWebSearchTempKBState(ctx, t.sessionID)
|
||||
|
||||
// Build questions for RAG compression
|
||||
questions := []string{strings.TrimSpace(query)}
|
||||
|
||||
logger.Infof(ctx, "[Tool][WebSearch] Applying RAG compression")
|
||||
compressed, kbID, newSeen, newIDs, err := t.webSearchService.CompressWithRAG(
|
||||
ctx, t.sessionID, tempKBID, questions, webResults, tenant.WebSearchConfig,
|
||||
t.knowledgeBaseService, t.knowledgeService, seen, ids,
|
||||
)
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "[Tool][WebSearch] RAG compression failed, using raw results: %v", err)
|
||||
} else {
|
||||
webResults = compressed
|
||||
// Persist temp KB state back into Redis using SessionService
|
||||
t.sessionService.SaveWebSearchTempKBState(ctx, t.sessionID, kbID, newSeen, newIDs)
|
||||
logger.Infof(ctx, "[Tool][WebSearch] RAG compression completed, %d results", len(webResults))
|
||||
}
|
||||
}
|
||||
|
||||
// Format output
|
||||
if len(webResults) == 0 {
|
||||
return &types.ToolResult{
|
||||
Success: true,
|
||||
Output: fmt.Sprintf("No web search results found for query: %s", query),
|
||||
Data: map[string]interface{}{
|
||||
"query": query,
|
||||
"results": []interface{}{},
|
||||
"count": 0,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Build output text
|
||||
output := "=== Web Search Results ===\n"
|
||||
output += fmt.Sprintf("Query: %s\n", query)
|
||||
output += fmt.Sprintf("Found %d result(s)\n\n", len(webResults))
|
||||
|
||||
// Format results
|
||||
formattedResults := make([]map[string]interface{}, 0, len(webResults))
|
||||
for i, result := range webResults {
|
||||
output += fmt.Sprintf("Result #%d:\n", i+1)
|
||||
output += fmt.Sprintf(" Title: %s\n", result.Title)
|
||||
output += fmt.Sprintf(" URL: %s\n", result.URL)
|
||||
if result.Snippet != "" {
|
||||
output += fmt.Sprintf(" Snippet: %s\n", result.Snippet)
|
||||
}
|
||||
if result.Content != "" {
|
||||
// Truncate content if too long
|
||||
content := result.Content
|
||||
if len(content) > 500 {
|
||||
content = content[:500] + "..."
|
||||
}
|
||||
output += fmt.Sprintf(" Content: %s\n", content)
|
||||
}
|
||||
if result.PublishedAt != nil {
|
||||
output += fmt.Sprintf(" Published: %s\n", result.PublishedAt.Format(time.RFC3339))
|
||||
}
|
||||
output += "\n"
|
||||
|
||||
resultData := map[string]interface{}{
|
||||
"result_index": i + 1,
|
||||
"title": result.Title,
|
||||
"url": result.URL,
|
||||
"snippet": result.Snippet,
|
||||
"content": result.Content,
|
||||
"source": result.Source,
|
||||
}
|
||||
if result.PublishedAt != nil {
|
||||
resultData["published_at"] = result.PublishedAt.Format(time.RFC3339)
|
||||
}
|
||||
formattedResults = append(formattedResults, resultData)
|
||||
}
|
||||
|
||||
output += "=== Usage Guidelines ===\n"
|
||||
output += "- Use these results to answer questions about current information\n"
|
||||
output += "- Verify information from multiple sources when possible\n"
|
||||
output += "- Check the publication date to ensure information is current\n"
|
||||
output += "- Results are automatically compressed to extract relevant content\n"
|
||||
|
||||
return &types.ToolResult{
|
||||
Success: true,
|
||||
Output: output,
|
||||
Data: map[string]interface{}{
|
||||
"query": query,
|
||||
"results": formattedResults,
|
||||
"count": len(webResults),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"github.com/Tencent/WeKnora/internal/event"
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/mcp"
|
||||
"github.com/Tencent/WeKnora/internal/models/chat"
|
||||
"github.com/Tencent/WeKnora/internal/models/rerank"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
@@ -30,6 +29,7 @@ type agentService struct {
|
||||
mcpManager *mcp.MCPManager
|
||||
eventBus *event.EventBus
|
||||
db *gorm.DB
|
||||
webSearchService interfaces.WebSearchService
|
||||
}
|
||||
|
||||
// NewAgentService creates a new agent service
|
||||
@@ -43,6 +43,7 @@ func NewAgentService(
|
||||
mcpManager *mcp.MCPManager,
|
||||
eventBus *event.EventBus,
|
||||
db *gorm.DB,
|
||||
webSearchService interfaces.WebSearchService,
|
||||
) interfaces.AgentService {
|
||||
return &agentService{
|
||||
cfg: cfg,
|
||||
@@ -54,6 +55,7 @@ func NewAgentService(
|
||||
mcpManager: mcpManager,
|
||||
eventBus: eventBus,
|
||||
db: db,
|
||||
webSearchService: webSearchService,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,6 +66,7 @@ func (s *agentService) CreateAgentEngine(
|
||||
eventBus *event.EventBus,
|
||||
contextManager interfaces.ContextManager,
|
||||
sessionID string,
|
||||
sessionService interfaces.SessionService,
|
||||
) (interfaces.AgentEngine, error) {
|
||||
logger.Infof(ctx, "Creating agent engine with custom EventBus")
|
||||
|
||||
@@ -94,7 +97,7 @@ func (s *agentService) CreateAgentEngine(
|
||||
toolRegistry := tools.NewToolRegistry(s.knowledgeBaseService, s.knowledgeService, s.chunkService, s.db)
|
||||
|
||||
// Register tools
|
||||
if err := s.registerTools(ctx, toolRegistry, config, chatModel, rerankModel); err != nil {
|
||||
if err := s.registerTools(ctx, toolRegistry, config, rerankModel, sessionID, sessionService); err != nil {
|
||||
return nil, fmt.Errorf("failed to register tools: %w", err)
|
||||
}
|
||||
|
||||
@@ -168,8 +171,9 @@ func (s *agentService) registerTools(
|
||||
ctx context.Context,
|
||||
registry *tools.ToolRegistry,
|
||||
config *types.AgentConfig,
|
||||
chatModel chat.Chat,
|
||||
rerankModel rerank.Reranker,
|
||||
sessionID string,
|
||||
sessionService interfaces.SessionService,
|
||||
) error {
|
||||
// If no specific tools allowed, register default tools
|
||||
allowedTools := config.AllowedTools
|
||||
@@ -177,25 +181,19 @@ func (s *agentService) registerTools(
|
||||
// Register default tools from config
|
||||
if s.cfg.Agent != nil && len(s.cfg.Agent.DefaultTools) > 0 {
|
||||
allowedTools = s.cfg.Agent.DefaultTools
|
||||
} else {
|
||||
// Fallback to all tools
|
||||
allowedTools = []string{
|
||||
"thinking",
|
||||
"todo_write",
|
||||
"knowledge_search",
|
||||
"get_related_chunks",
|
||||
"query_knowledge_graph",
|
||||
"get_document_info",
|
||||
"database_query",
|
||||
}
|
||||
}
|
||||
}
|
||||
// If web search is enabled, add web_search to allowedTools
|
||||
if config.WebSearchEnabled {
|
||||
allowedTools = append(allowedTools, "web_search")
|
||||
}
|
||||
|
||||
// Get tenant ID from context
|
||||
tenantID := uint(0)
|
||||
if tid, ok := ctx.Value(types.TenantIDContextKey).(uint); ok {
|
||||
tenantID = tid
|
||||
}
|
||||
logger.Infof(ctx, "Registering tools: %v, tenant ID: %d", allowedTools, tenantID)
|
||||
logger.Infof(ctx, "Registering tools: %v, tenant ID: %d, webSearchEnabled: %v", allowedTools, tenantID, config.WebSearchEnabled)
|
||||
|
||||
// Register each allowed tool
|
||||
for _, toolName := range allowedTools {
|
||||
@@ -214,6 +212,17 @@ func (s *agentService) registerTools(
|
||||
registry.RegisterTool(tools.NewGetDocumentInfoTool(s.knowledgeService, s.chunkService))
|
||||
case "database_query":
|
||||
registry.RegisterTool(tools.NewDatabaseQueryTool(s.db, tenantID))
|
||||
case "web_search":
|
||||
registry.RegisterTool(tools.NewWebSearchTool(
|
||||
s.webSearchService,
|
||||
s.knowledgeBaseService,
|
||||
s.knowledgeService,
|
||||
sessionService,
|
||||
sessionID,
|
||||
config.WebSearchMaxResults,
|
||||
))
|
||||
logger.Infof(ctx, "Registered web_search tool for session: %s, maxResults: %d", sessionID, config.WebSearchMaxResults)
|
||||
|
||||
default:
|
||||
logger.Warnf(ctx, "Unknown tool: %s", toolName)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package chatpipline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -12,7 +11,6 @@ import (
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// PluginSearch implements search functionality for chat pipeline
|
||||
@@ -23,7 +21,7 @@ type PluginSearch struct {
|
||||
config *config.Config
|
||||
webSearchService interfaces.WebSearchService
|
||||
tenantService interfaces.TenantService
|
||||
redisClient *redis.Client
|
||||
sessionService interfaces.SessionService
|
||||
}
|
||||
|
||||
func NewPluginSearch(eventManager *EventManager,
|
||||
@@ -33,7 +31,7 @@ func NewPluginSearch(eventManager *EventManager,
|
||||
config *config.Config,
|
||||
webSearchService interfaces.WebSearchService,
|
||||
tenantService interfaces.TenantService,
|
||||
redisClient *redis.Client,
|
||||
sessionService interfaces.SessionService,
|
||||
) *PluginSearch {
|
||||
res := &PluginSearch{
|
||||
knowledgeBaseService: knowledgeBaseService,
|
||||
@@ -42,7 +40,7 @@ func NewPluginSearch(eventManager *EventManager,
|
||||
config: config,
|
||||
webSearchService: webSearchService,
|
||||
tenantService: tenantService,
|
||||
redisClient: redisClient,
|
||||
sessionService: sessionService,
|
||||
}
|
||||
eventManager.Register(res)
|
||||
return res
|
||||
@@ -240,25 +238,8 @@ func (p *PluginSearch) searchWebIfEnabled(ctx context.Context, chatManage *types
|
||||
if chatManage.ProcessedQuery != "" && chatManage.ProcessedQuery != chatManage.RewriteQuery {
|
||||
questions = append(questions, strings.TrimSpace(chatManage.ProcessedQuery))
|
||||
}
|
||||
// Load session-scoped temp KB state from Redis
|
||||
var tempKBID string
|
||||
seen := map[string]bool{}
|
||||
ids := []string{}
|
||||
stateKey := fmt.Sprintf("tempkb:%s", chatManage.SessionID)
|
||||
if raw, getErr := p.redisClient.Get(ctx, stateKey).Bytes(); getErr == nil && len(raw) > 0 {
|
||||
var state struct {
|
||||
KBID string `json:"kbID"`
|
||||
KnowledgeIDs []string `json:"knowledgeIDs"`
|
||||
SeenURLs map[string]bool `json:"seenURLs"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &state); err == nil {
|
||||
tempKBID = state.KBID
|
||||
ids = state.KnowledgeIDs
|
||||
if state.SeenURLs != nil {
|
||||
seen = state.SeenURLs
|
||||
}
|
||||
}
|
||||
}
|
||||
// Load session-scoped temp KB state from Redis using SessionService
|
||||
tempKBID, seen, ids := p.sessionService.GetWebSearchTempKBState(ctx, chatManage.SessionID)
|
||||
compressed, kbID, newSeen, newIDs, err := p.webSearchService.CompressWithRAG(
|
||||
ctx, chatManage.SessionID, tempKBID, questions, webResults, tenant.WebSearchConfig,
|
||||
p.knowledgeBaseService, p.knowledgeService, seen, ids,
|
||||
@@ -267,19 +248,8 @@ func (p *PluginSearch) searchWebIfEnabled(ctx context.Context, chatManage *types
|
||||
logger.Warnf(ctx, "RAG compression failed, falling back to raw: %v", err)
|
||||
} else {
|
||||
webResults = compressed
|
||||
// Persist temp KB state back into Redis
|
||||
state := struct {
|
||||
KBID string `json:"kbID"`
|
||||
KnowledgeIDs []string `json:"knowledgeIDs"`
|
||||
SeenURLs map[string]bool `json:"seenURLs"`
|
||||
}{
|
||||
KBID: kbID,
|
||||
KnowledgeIDs: newIDs,
|
||||
SeenURLs: newSeen,
|
||||
}
|
||||
if b, mErr := json.Marshal(state); mErr == nil {
|
||||
_ = p.redisClient.Set(ctx, stateKey, b, 0).Err()
|
||||
}
|
||||
// Persist temp KB state back into Redis using SessionService
|
||||
p.sessionService.SaveWebSearchTempKBState(ctx, chatManage.SessionID, kbID, newSeen, newIDs)
|
||||
}
|
||||
res := convertWebSearchResults(webResults)
|
||||
logger.Infof(ctx, "Web search returned %d results", len(res))
|
||||
|
||||
@@ -53,10 +53,10 @@ func NewChunkExtractService(
|
||||
chunkRepo interfaces.ChunkRepository,
|
||||
graphEngine interfaces.RetrieveGraphRepository,
|
||||
) interfaces.Extracter {
|
||||
generator := chatpipline.NewQAPromptGenerator(chatpipline.NewFormater(), config.ExtractManager.ExtractGraph)
|
||||
ctx := context.Background()
|
||||
logger.Debugf(ctx, "chunk extract system prompt: %s", generator.System(ctx))
|
||||
logger.Debugf(ctx, "chunk extract user prompt: %s", generator.User(ctx, "demo"))
|
||||
// generator := chatpipline.NewQAPromptGenerator(chatpipline.NewFormater(), config.ExtractManager.ExtractGraph)
|
||||
// ctx := context.Background()
|
||||
// logger.Debugf(ctx, "chunk extract system prompt: %s", generator.System(ctx))
|
||||
// logger.Debugf(ctx, "chunk extract user prompt: %s", generator.User(ctx, "demo"))
|
||||
return &ChunkExtractService{
|
||||
template: config.ExtractManager.ExtractGraph,
|
||||
modelService: modelService,
|
||||
|
||||
@@ -211,27 +211,8 @@ func (s *sessionService) DeleteSession(ctx context.Context, id string) error {
|
||||
logger.Infof(ctx, "Deleting session, ID: %s, tenant ID: %d", id, tenantID)
|
||||
|
||||
// Cleanup temporary KB stored in Redis for this session
|
||||
if s.redisClient != nil {
|
||||
stateKey := fmt.Sprintf("tempkb:%s", id)
|
||||
if raw, getErr := s.redisClient.Get(ctx, stateKey).Bytes(); getErr == nil && len(raw) > 0 {
|
||||
var state struct {
|
||||
KBID string `json:"kbID"`
|
||||
KnowledgeIDs []string `json:"knowledgeIDs"`
|
||||
SeenURLs map[string]bool `json:"seenURLs"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &state); err == nil && strings.TrimSpace(state.KBID) != "" {
|
||||
logger.Infof(ctx, "Cleaning temporary KB for session %s: %s", id, state.KBID)
|
||||
for _, kid := range state.KnowledgeIDs {
|
||||
if delErr := s.knowledgeService.DeleteKnowledge(ctx, kid); delErr != nil {
|
||||
logger.Warnf(ctx, "Failed to delete temp knowledge %s: %v", kid, delErr)
|
||||
}
|
||||
}
|
||||
if delErr := s.knowledgeBaseService.DeleteKnowledgeBase(ctx, state.KBID); delErr != nil {
|
||||
logger.Warnf(ctx, "Failed to delete temp knowledge base %s: %v", state.KBID, delErr)
|
||||
}
|
||||
_ = s.redisClient.Del(ctx, stateKey).Err()
|
||||
}
|
||||
}
|
||||
if err := s.DeleteWebSearchTempKBState(ctx, id); err != nil {
|
||||
logger.Warnf(ctx, "Failed to cleanup temporary KB for session %s: %v", id, err)
|
||||
}
|
||||
|
||||
// Delete session from repository
|
||||
@@ -486,13 +467,10 @@ func (s *sessionService) KnowledgeQA(ctx context.Context, session *types.Session
|
||||
},
|
||||
FallbackResponse: session.FallbackResponse,
|
||||
EventBus: eventBus.AsEventBusInterface(), // NEW: For pipeline to emit events directly
|
||||
WebSearchEnabled: webSearchEnabled,
|
||||
TenantID: session.TenantID,
|
||||
}
|
||||
|
||||
// Store web search configuration in chatManage for pipeline processing
|
||||
// PluginSearch will handle web search integration
|
||||
chatManage.TenantID = session.TenantID
|
||||
chatManage.WebSearchEnabled = webSearchEnabled
|
||||
|
||||
// Start knowledge QA event processing
|
||||
logger.Info(ctx, "Triggering knowledge base question answering event")
|
||||
err = s.KnowledgeQAByEvent(ctx, chatManage, types.Pipline["rag_stream"])
|
||||
@@ -817,7 +795,14 @@ func (s *sessionService) AgentQA(ctx context.Context, session *types.Session, qu
|
||||
Temperature: tenantInfo.AgentConfig.Temperature,
|
||||
ThinkingModelID: tenantInfo.AgentConfig.ThinkingModelID,
|
||||
RerankModelID: tenantInfo.AgentConfig.RerankModelID,
|
||||
KnowledgeBases: session.AgentConfig.KnowledgeBases, // Use session's knowledge bases
|
||||
KnowledgeBases: session.AgentConfig.KnowledgeBases, // Use session's knowledge bases
|
||||
WebSearchEnabled: session.AgentConfig.WebSearchEnabled, // Web search enabled from session config
|
||||
}
|
||||
|
||||
// Set web search max results from tenant config (default: 5)
|
||||
agentConfig.WebSearchMaxResults = 5
|
||||
if tenantInfo.WebSearchConfig != nil && tenantInfo.WebSearchConfig.MaxResults > 0 {
|
||||
agentConfig.WebSearchMaxResults = tenantInfo.WebSearchConfig.MaxResults
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Merged agent config from tenant %d and session %s", tenantInfo.ID, sessionID)
|
||||
@@ -880,7 +865,7 @@ func (s *sessionService) AgentQA(ctx context.Context, session *types.Session, qu
|
||||
|
||||
// Create agent engine with EventBus and ContextManager
|
||||
logger.Info(ctx, "Creating agent engine")
|
||||
engine, err := s.agentService.CreateAgentEngine(ctx, agentConfig, eventBus, contextManager, session.ID)
|
||||
engine, err := s.agentService.CreateAgentEngine(ctx, agentConfig, eventBus, contextManager, session.ID, s)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to create agent engine: %v", err)
|
||||
return err
|
||||
@@ -958,4 +943,96 @@ func (s *sessionService) ClearContext(ctx context.Context, sessionID string) err
|
||||
return s.sessionStorage.Delete(ctx, sessionID)
|
||||
}
|
||||
|
||||
// processAgentEvents is no longer needed - events are handled directly by Handler layer via EventBus subscription
|
||||
// GetWebSearchTempKBState retrieves the temporary KB state for web search from Redis
|
||||
func (s *sessionService) GetWebSearchTempKBState(ctx context.Context, sessionID string) (tempKBID string, seenURLs map[string]bool, knowledgeIDs []string) {
|
||||
stateKey := fmt.Sprintf("tempkb:%s", sessionID)
|
||||
if raw, getErr := s.redisClient.Get(ctx, stateKey).Bytes(); getErr == nil && len(raw) > 0 {
|
||||
var state struct {
|
||||
KBID string `json:"kbID"`
|
||||
KnowledgeIDs []string `json:"knowledgeIDs"`
|
||||
SeenURLs map[string]bool `json:"seenURLs"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &state); err == nil {
|
||||
tempKBID = state.KBID
|
||||
ids := state.KnowledgeIDs
|
||||
if state.SeenURLs != nil {
|
||||
seenURLs = state.SeenURLs
|
||||
} else {
|
||||
seenURLs = make(map[string]bool)
|
||||
}
|
||||
return tempKBID, seenURLs, ids
|
||||
}
|
||||
}
|
||||
return "", make(map[string]bool), []string{}
|
||||
}
|
||||
|
||||
// SaveWebSearchTempKBState saves the temporary KB state for web search to Redis
|
||||
func (s *sessionService) SaveWebSearchTempKBState(ctx context.Context, sessionID string, tempKBID string, seenURLs map[string]bool, knowledgeIDs []string) {
|
||||
stateKey := fmt.Sprintf("tempkb:%s", sessionID)
|
||||
state := struct {
|
||||
KBID string `json:"kbID"`
|
||||
KnowledgeIDs []string `json:"knowledgeIDs"`
|
||||
SeenURLs map[string]bool `json:"seenURLs"`
|
||||
}{
|
||||
KBID: tempKBID,
|
||||
KnowledgeIDs: knowledgeIDs,
|
||||
SeenURLs: seenURLs,
|
||||
}
|
||||
if b, err := json.Marshal(state); err == nil {
|
||||
_ = s.redisClient.Set(ctx, stateKey, b, 0).Err()
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteWebSearchTempKBState deletes the temporary KB state for web search from Redis and cleans up associated knowledge base and knowledge items
|
||||
func (s *sessionService) DeleteWebSearchTempKBState(ctx context.Context, sessionID string) error {
|
||||
if s.redisClient == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
stateKey := fmt.Sprintf("tempkb:%s", sessionID)
|
||||
raw, getErr := s.redisClient.Get(ctx, stateKey).Bytes()
|
||||
if getErr != nil || len(raw) == 0 {
|
||||
// No state found, nothing to clean up
|
||||
return nil
|
||||
}
|
||||
|
||||
var state struct {
|
||||
KBID string `json:"kbID"`
|
||||
KnowledgeIDs []string `json:"knowledgeIDs"`
|
||||
SeenURLs map[string]bool `json:"seenURLs"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &state); err != nil {
|
||||
// Invalid state, just delete the key
|
||||
_ = s.redisClient.Del(ctx, stateKey).Err()
|
||||
return nil
|
||||
}
|
||||
|
||||
// If KBID is empty, just delete the Redis key
|
||||
if strings.TrimSpace(state.KBID) == "" {
|
||||
_ = s.redisClient.Del(ctx, stateKey).Err()
|
||||
return nil
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Cleaning temporary KB for session %s: %s", sessionID, state.KBID)
|
||||
|
||||
// Delete all knowledge items
|
||||
for _, kid := range state.KnowledgeIDs {
|
||||
if delErr := s.knowledgeService.DeleteKnowledge(ctx, kid); delErr != nil {
|
||||
logger.Warnf(ctx, "Failed to delete temp knowledge %s: %v", kid, delErr)
|
||||
}
|
||||
}
|
||||
|
||||
// Delete the knowledge base
|
||||
if delErr := s.knowledgeBaseService.DeleteKnowledgeBase(ctx, state.KBID); delErr != nil {
|
||||
logger.Warnf(ctx, "Failed to delete temp knowledge base %s: %v", state.KBID, delErr)
|
||||
}
|
||||
|
||||
// Delete the Redis key
|
||||
if delErr := s.redisClient.Del(ctx, stateKey).Err(); delErr != nil {
|
||||
logger.Warnf(ctx, "Failed to delete Redis key %s: %v", stateKey, delErr)
|
||||
return fmt.Errorf("failed to delete Redis key: %w", delErr)
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Successfully cleaned up temporary KB for session %s", sessionID)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -279,7 +279,7 @@ func (s *WebSearchService) Search(ctx context.Context, config *types.WebSearchCo
|
||||
}
|
||||
|
||||
// NewWebSearchService creates a new web search service
|
||||
func NewWebSearchService(cfg *config.Config) (*WebSearchService, error) {
|
||||
func NewWebSearchService(cfg *config.Config) (interfaces.WebSearchService, error) {
|
||||
if cfg.WebSearch == nil {
|
||||
return nil, fmt.Errorf("web search config is not available")
|
||||
}
|
||||
|
||||
@@ -112,20 +112,18 @@ func BuildContainer(container *dig.Container) *dig.Container {
|
||||
must(container.Provide(service.NewMessageService))
|
||||
must(container.Provide(service.NewMCPServiceService))
|
||||
|
||||
// Agent service layer (requires event bus)
|
||||
// Web search service (needed by AgentService)
|
||||
must(container.Provide(service.NewWebSearchService))
|
||||
|
||||
// Agent service layer (requires event bus, web search service)
|
||||
// SessionService is passed as parameter to CreateAgentEngine method when creating AgentService
|
||||
must(container.Provide(event.NewEventBus))
|
||||
must(container.Provide(service.NewAgentService))
|
||||
|
||||
// Session service (depends on agent service)
|
||||
// SessionService is created after AgentService and passes itself to AgentService.CreateAgentEngine when needed
|
||||
must(container.Provide(service.NewSessionService))
|
||||
|
||||
// Web search service
|
||||
must(container.Provide(service.NewWebSearchService))
|
||||
// Register WebSearchService as interface for dependency injection
|
||||
must(container.Provide(func(ws *service.WebSearchService) interfaces.WebSearchService {
|
||||
return ws
|
||||
}))
|
||||
|
||||
// Chat pipeline components for processing chat requests
|
||||
must(container.Provide(chatpipline.NewEventManager))
|
||||
// Ensure Async task components are registered before invoking plugins that depend on KnowledgeService
|
||||
|
||||
@@ -2,7 +2,9 @@ package session
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/errors"
|
||||
@@ -336,7 +338,13 @@ func (h *Handler) AgentQA(c *gin.Context) {
|
||||
go func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
logger.ErrorWithFields(asyncCtx, errors.NewInternalServerError("Agent QA service panicked"), nil)
|
||||
buf := make([]byte, 1024)
|
||||
runtime.Stack(buf, true)
|
||||
logger.ErrorWithFields(asyncCtx,
|
||||
errors.NewInternalServerError(fmt.Sprintf("Agent QA service panicked: %v\n%s", r, string(buf))),
|
||||
map[string]interface{}{
|
||||
"session_id": sessionID,
|
||||
})
|
||||
}
|
||||
h.completeAssistantMessage(asyncCtx, assistantMessage)
|
||||
logger.Infof(asyncCtx, "Agent QA service completed for session: %s", sessionID)
|
||||
|
||||
+11
-9
@@ -10,15 +10,17 @@ import (
|
||||
// AgentConfig represents the full agent configuration (used at tenant level and runtime)
|
||||
// This includes all configuration parameters for agent execution
|
||||
type AgentConfig struct {
|
||||
Enabled bool `json:"enabled"` // Whether agent mode is enabled
|
||||
MaxIterations int `json:"max_iterations"` // Maximum number of ReAct iterations
|
||||
ReflectionEnabled bool `json:"reflection_enabled"` // Whether to enable reflection
|
||||
AllowedTools []string `json:"allowed_tools"` // List of allowed tool names
|
||||
Temperature float64 `json:"temperature"` // LLM temperature for agent
|
||||
ThinkingModelID string `json:"thinking_model_id"` // Model ID for reasoning
|
||||
RerankModelID string `json:"rerank_model_id"` // Model ID for reranking search results
|
||||
KnowledgeBases []string `json:"knowledge_bases"` // Accessible knowledge base IDs
|
||||
SystemPrompt string `json:"system_prompt,omitempty"` // System prompt template with placeholders (optional)
|
||||
Enabled bool `json:"enabled"` // Whether agent mode is enabled
|
||||
MaxIterations int `json:"max_iterations"` // Maximum number of ReAct iterations
|
||||
ReflectionEnabled bool `json:"reflection_enabled"` // Whether to enable reflection
|
||||
AllowedTools []string `json:"allowed_tools"` // List of allowed tool names
|
||||
Temperature float64 `json:"temperature"` // LLM temperature for agent
|
||||
ThinkingModelID string `json:"thinking_model_id"` // Model ID for reasoning
|
||||
RerankModelID string `json:"rerank_model_id"` // Model ID for reranking search results
|
||||
KnowledgeBases []string `json:"knowledge_bases"` // Accessible knowledge base IDs
|
||||
SystemPrompt string `json:"system_prompt,omitempty"` // System prompt template with placeholders (optional)
|
||||
WebSearchEnabled bool `json:"web_search_enabled"` // Whether web search tool is enabled
|
||||
WebSearchMaxResults int `json:"web_search_max_results"` // Maximum number of web search results (default: 5)
|
||||
}
|
||||
|
||||
// SessionAgentConfig represents session-level agent configuration
|
||||
|
||||
@@ -26,7 +26,7 @@ type AgentEngine interface {
|
||||
// AgentService defines the interface for agent-related operations
|
||||
type AgentService interface {
|
||||
// CreateAgentEngine creates an agent engine with the given configuration, EventBus, and ContextManager
|
||||
CreateAgentEngine(ctx context.Context, config *types.AgentConfig, eventBus *event.EventBus, contextManager ContextManager, sessionID string) (AgentEngine, error)
|
||||
CreateAgentEngine(ctx context.Context, config *types.AgentConfig, eventBus *event.EventBus, contextManager ContextManager, sessionID string, sessionService SessionService) (AgentEngine, error)
|
||||
|
||||
// ValidateConfig validates an agent configuration
|
||||
ValidateConfig(config *types.AgentConfig) error
|
||||
|
||||
@@ -43,6 +43,12 @@ type SessionService interface {
|
||||
AgentQA(ctx context.Context, session *types.Session, query string, assistantMessageID string, eventBus *event.EventBus) error
|
||||
// ClearContext clears the LLM context for a session
|
||||
ClearContext(ctx context.Context, sessionID string) error
|
||||
// GetWebSearchTempKBState retrieves the temporary KB state for web search from Redis
|
||||
GetWebSearchTempKBState(ctx context.Context, sessionID string) (tempKBID string, seenURLs map[string]bool, knowledgeIDs []string)
|
||||
// SaveWebSearchTempKBState saves the temporary KB state for web search to Redis
|
||||
SaveWebSearchTempKBState(ctx context.Context, sessionID string, tempKBID string, seenURLs map[string]bool, knowledgeIDs []string)
|
||||
// DeleteWebSearchTempKBState deletes the temporary KB state for web search from Redis and cleans up associated knowledge base and knowledge items
|
||||
DeleteWebSearchTempKBState(ctx context.Context, sessionID string) error
|
||||
}
|
||||
|
||||
// SessionRepository defines the session repository interface
|
||||
|
||||
Reference in New Issue
Block a user