diff --git a/internal/agent/engine.go b/internal/agent/engine.go index 7059465c..c73892d4 100644 --- a/internal/agent/engine.go +++ b/internal/agent/engine.go @@ -18,14 +18,19 @@ import ( "github.com/Tencent/WeKnora/internal/models/chat" "github.com/Tencent/WeKnora/internal/tracing/langfuse" "github.com/Tencent/WeKnora/internal/types" - "github.com/Tencent/WeKnora/internal/types/interfaces" ) // langfuseQueryPreview caps the query length we ship as the agent.execute // span input — long quoted-context queries can be many KB of prose. const langfuseQueryPreview = 2000 -// AgentEngine is the core engine for running ReAct agents +// AgentEngine is the core engine for running ReAct agents. +// +// History persistence note: the engine is stateless across turns. Conversation +// history is rebuilt from the DB once per turn by the caller +// (see service.LoadAgentHistory) and passed into Execute as llmContext. The +// engine therefore does not maintain its own cache, system-prompt store, or +// cross-turn buffer. type AgentEngine struct { config *types.AgentConfig toolRegistry *agenttools.ToolRegistry @@ -33,8 +38,7 @@ type AgentEngine struct { eventBus *event.EventBus knowledgeBasesInfo []*KnowledgeBaseInfo // Detailed knowledge base information for prompt selectedDocs []*SelectedDocumentInfo // User-selected documents (via @ mention) - contextManager interfaces.ContextManager // Context manager for writing agent conversation to LLM context - sessionID string // Session ID for context management + sessionID string // Session ID for logging and event emission systemPromptTemplate string // System prompt template (optional, uses default if empty) skillsManager *skills.Manager // Skills manager for Progressive Disclosure (optional) appConfig *appconfig.Config // Application config for prompt template resolution (optional) @@ -57,7 +61,6 @@ func NewAgentEngine( eventBus *event.EventBus, knowledgeBasesInfo []*KnowledgeBaseInfo, selectedDocs []*SelectedDocumentInfo, - contextManager interfaces.ContextManager, sessionID string, systemPromptTemplate string, ) *AgentEngine { @@ -75,7 +78,6 @@ func NewAgentEngine( eventBus: eventBus, knowledgeBasesInfo: knowledgeBasesInfo, selectedDocs: selectedDocs, - contextManager: contextManager, sessionID: sessionID, systemPromptTemplate: systemPromptTemplate, tokenEstimator: tokenEst, @@ -99,7 +101,6 @@ func NewAgentEngineWithSkills( eventBus *event.EventBus, knowledgeBasesInfo []*KnowledgeBaseInfo, selectedDocs []*SelectedDocumentInfo, - contextManager interfaces.ContextManager, sessionID string, systemPromptTemplate string, skillsManager *skills.Manager, @@ -111,7 +112,6 @@ func NewAgentEngineWithSkills( eventBus, knowledgeBasesInfo, selectedDocs, - contextManager, sessionID, systemPromptTemplate, ) @@ -630,7 +630,7 @@ func (e *AgentEngine) runReActIteration( // 4. Observe: Add tool results to messages and write to context state.RoundSteps = append(state.RoundSteps, step) - *messagesPtr = e.appendToolResults(ctx, *messagesPtr, step) + *messagesPtr = e.appendToolResults(*messagesPtr, step) common.PipelineInfo(ctx, "Agent", "round_end", map[string]interface{}{ "iteration": state.CurrentRound, "round": round, diff --git a/internal/agent/engine_test.go b/internal/agent/engine_test.go index 57f2ed40..8871a55b 100644 --- a/internal/agent/engine_test.go +++ b/internal/agent/engine_test.go @@ -79,7 +79,6 @@ func newTestEngine(t *testing.T, chatModel chat.Chat, opts ...testEngineOption) event.NewEventBus(), nil, nil, - nil, "test-session", "", ) diff --git a/internal/agent/observe.go b/internal/agent/observe.go index 76dfbf95..f3a226a3 100644 --- a/internal/agent/observe.go +++ b/internal/agent/observe.go @@ -368,10 +368,11 @@ func (e *AgentEngine) buildToolsForLLM() []chat.Tool { return tools } -// appendToolResults adds tool results to the message history following OpenAI's tool calling format -// Also writes these messages to the context manager for persistence +// appendToolResults adds tool results to the in-turn message history following +// OpenAI's tool-calling format. Cross-turn persistence is handled separately: +// the final AgentSteps are written to the assistant message by the SSE handler, +// and rebuilt from DB on the next turn by service.LoadAgentHistory. func (e *AgentEngine) appendToolResults( - ctx context.Context, messages []chat.Message, step types.AgentStep, ) []chat.Message { @@ -401,15 +402,6 @@ func (e *AgentEngine) appendToolResults( } messages = append(messages, assistantMsg) - - // Write assistant message to context - if e.contextManager != nil { - if err := e.contextManager.AddMessage(ctx, e.sessionID, assistantMsg); err != nil { - logger.Warnf(ctx, "[Agent] Failed to add assistant message to context: %v", err) - } else { - logger.Debugf(ctx, "[Agent] Added assistant message to context (session: %s)", e.sessionID) - } - } } // Add tool result messages (role: "tool", following OpenAI format) @@ -427,15 +419,6 @@ func (e *AgentEngine) appendToolResults( } messages = append(messages, toolMsg) - - // Write tool message to context - if e.contextManager != nil { - if err := e.contextManager.AddMessage(ctx, e.sessionID, toolMsg); err != nil { - logger.Warnf(ctx, "[Agent] Failed to add tool message to context: %v", err) - } else { - logger.Debugf(ctx, "[Agent] Added tool message to context (session: %s, tool: %s)", e.sessionID, toolCall.Name) - } - } } return messages diff --git a/internal/application/service/agent_history.go b/internal/application/service/agent_history.go new file mode 100644 index 00000000..60072e38 --- /dev/null +++ b/internal/application/service/agent_history.go @@ -0,0 +1,229 @@ +package service + +import ( + "context" + "encoding/json" + "fmt" + "regexp" + "sort" + "strings" + "time" + + agenttools "github.com/Tencent/WeKnora/internal/agent/tools" + "github.com/Tencent/WeKnora/internal/models/chat" + "github.com/Tencent/WeKnora/internal/types" + "github.com/Tencent/WeKnora/internal/types/interfaces" +) + +// agentHistoryFetchMultiplier controls how many raw DB messages to fetch +// when assembling history. Each turn contributes ~2 rows (user + assistant); +// we ask for a generous multiple so we never under-fetch when some pairs are +// incomplete (e.g. an in-flight turn). +const agentHistoryFetchMultiplier = 4 + +// agentHistoryFetchMin is the floor for the DB fetch limit, used when +// maxRounds is small or unset. +const agentHistoryFetchMin = 50 + +var agentHistoryThinkTagRegex = regexp.MustCompile(`(?s).*?`) + +// LoadAgentHistory rebuilds the multi-turn LLM context for an Agent-mode +// session directly from the persistent messages table. The result is a +// chronologically ordered list of chat.Message entries suitable for prepending +// to the current turn (without system prompt; the engine adds that itself). +// +// For each historical turn it emits: +// 1. A user message (RenderedContent if present, else Content, plus any +// image captions appended). +// 2. For each AgentStep with non-terminal tool calls (i.e. excluding +// final_answer), an assistant message carrying the step's thought and +// tool_calls, followed by one tool message per tool result. +// 3. A final assistant message with the canonical answer (msg.Content with +// blocks stripped). +// +// Turns lacking either user or assistant content are skipped. The newest +// maxRounds turns are returned in chronological order. +// +// DB is treated as the single source of truth — there is no Redis/in-memory +// cache layer above this function. Callers are expected to invoke it once +// per turn before handing the messages to the agent engine. +func LoadAgentHistory( + ctx context.Context, + messageRepo interfaces.MessageRepository, + sessionID string, + maxRounds int, +) ([]chat.Message, error) { + if maxRounds <= 0 { + return []chat.Message{}, nil + } + + fetchLimit := maxRounds * agentHistoryFetchMultiplier + if fetchLimit < agentHistoryFetchMin { + fetchLimit = agentHistoryFetchMin + } + + rows, err := messageRepo.GetRecentMessagesBySession(ctx, sessionID, fetchLimit) + if err != nil { + return nil, fmt.Errorf("load agent history: %w", err) + } + if len(rows) == 0 { + return []chat.Message{}, nil + } + + type pair struct { + user *types.Message + assistant *types.Message + createdAt time.Time + } + pairs := make(map[string]*pair) + for _, msg := range rows { + p, ok := pairs[msg.RequestID] + if !ok { + p = &pair{} + pairs[msg.RequestID] = p + } + switch msg.Role { + case "user": + p.user = msg + if p.createdAt.IsZero() || msg.CreatedAt.Before(p.createdAt) { + p.createdAt = msg.CreatedAt + } + case "assistant": + p.assistant = msg + } + } + + completePairs := make([]*pair, 0, len(pairs)) + for _, p := range pairs { + if p.user != nil && p.assistant != nil && p.assistant.IsCompleted { + completePairs = append(completePairs, p) + } + } + + sort.Slice(completePairs, func(i, j int) bool { + return completePairs[i].createdAt.Before(completePairs[j].createdAt) + }) + + if len(completePairs) > maxRounds { + completePairs = completePairs[len(completePairs)-maxRounds:] + } + + out := make([]chat.Message, 0, len(completePairs)*4) + for _, p := range completePairs { + out = append(out, buildUserHistoryMessage(p.user)) + out = append(out, buildAssistantHistoryMessages(p.assistant)...) + } + return out, nil +} + +// buildUserHistoryMessage converts a stored user message into the chat.Message +// form that should appear in LLM history. It prefers RenderedContent (which +// already contains any retrieval-context augmentation from prior turns) and +// falls back to Content. When the user uploaded images, the captions extracted +// during ingestion are appended so subsequent turns retain a textual reference +// to those images even after the binary payload is gone. +func buildUserHistoryMessage(m *types.Message) chat.Message { + content := m.RenderedContent + if content == "" { + content = m.Content + } + if captions := extractImageCaptionsFromMessage(m.Images); captions != "" && m.RenderedContent == "" { + content += "\n\n[用户上传图片内容]\n" + captions + } + return chat.Message{Role: "user", Content: content} +} + +// buildAssistantHistoryMessages reconstructs the assistant side of one +// historical turn. It walks AgentSteps to expand intermediate tool calls into +// proper OpenAI-shaped assistant + tool messages, then emits the canonical +// final answer as a trailing assistant message. +// +// AgentSteps from KnowledgeQA-mode turns are empty, in which case the result +// is just the single final-answer assistant message — exactly mirroring how +// the KnowledgeQA pipeline replays history today. +func buildAssistantHistoryMessages(m *types.Message) []chat.Message { + msgs := make([]chat.Message, 0, len(m.AgentSteps)*2+1) + for _, step := range m.AgentSteps { + nonTerminalCalls := filterNonTerminalToolCalls(step.ToolCalls) + if len(nonTerminalCalls) == 0 { + continue + } + assistantMsg := chat.Message{ + Role: "assistant", + Content: step.Thought, + ToolCalls: make([]chat.ToolCall, 0, len(nonTerminalCalls)), + } + for _, tc := range nonTerminalCalls { + argsJSON, _ := json.Marshal(tc.Args) + assistantMsg.ToolCalls = append(assistantMsg.ToolCalls, chat.ToolCall{ + ID: tc.ID, + Type: "function", + Function: chat.FunctionCall{ + Name: tc.Name, + Arguments: string(argsJSON), + }, + }) + } + msgs = append(msgs, assistantMsg) + for _, tc := range nonTerminalCalls { + msgs = append(msgs, chat.Message{ + Role: "tool", + Content: toolCallOutput(tc), + ToolCallID: tc.ID, + Name: tc.Name, + }) + } + } + + finalContent := agentHistoryThinkTagRegex.ReplaceAllString(m.Content, "") + finalContent = strings.TrimSpace(finalContent) + if finalContent != "" { + msgs = append(msgs, chat.Message{Role: "assistant", Content: finalContent}) + } + return msgs +} + +// filterNonTerminalToolCalls drops final_answer entries since those are +// terminal signals — the canonical answer text is replayed via the trailing +// assistant message instead, so re-injecting final_answer here would either +// duplicate the answer or confuse the model into thinking the previous turn +// is still mid-flight. +func filterNonTerminalToolCalls(calls []types.ToolCall) []types.ToolCall { + out := make([]types.ToolCall, 0, len(calls)) + for _, tc := range calls { + if tc.Name == agenttools.ToolFinalAnswer { + continue + } + out = append(out, tc) + } + return out +} + +// toolCallOutput returns the textual content to use for a historical tool +// message: the recorded Output on success, or an "Error: …" line otherwise so +// the model can tell that an earlier tool call failed. +func toolCallOutput(tc types.ToolCall) string { + if tc.Result == nil { + return "" + } + if !tc.Result.Success { + if tc.Result.Error != "" { + return "Error: " + tc.Result.Error + } + return "Error: tool call failed" + } + return tc.Result.Output +} + +// extractImageCaptionsFromMessage concatenates non-empty Caption fields from +// stored message images. Mirrors the helper used in chat_pipeline so both +// modes surface previous-turn image descriptions identically. +func extractImageCaptionsFromMessage(images types.MessageImages) string { + var parts []string + for _, img := range images { + if img.Caption != "" { + parts = append(parts, img.Caption) + } + } + return strings.Join(parts, "\n") +} diff --git a/internal/application/service/agent_history_test.go b/internal/application/service/agent_history_test.go new file mode 100644 index 00000000..07e64314 --- /dev/null +++ b/internal/application/service/agent_history_test.go @@ -0,0 +1,190 @@ +package service + +import ( + "testing" + + agenttools "github.com/Tencent/WeKnora/internal/agent/tools" + "github.com/Tencent/WeKnora/internal/models/chat" + "github.com/Tencent/WeKnora/internal/types" + "github.com/stretchr/testify/assert" +) + +// TestBuildUserHistoryMessage_PrefersRenderedContent verifies the user side of +// history rebuild: RenderedContent (the RAG-augmented version) wins over the +// raw Content, and image captions are appended only when there's no rendered +// override (the rendered version already carries any retrieval context that +// would have included the caption upstream). +func TestBuildUserHistoryMessage_PrefersRenderedContent(t *testing.T) { + msg := &types.Message{ + Role: "user", + Content: "what about the chart?", + RenderedContent: "what about the chart? [augmented]", + Images: types.MessageImages{ + {Caption: "a bar chart"}, + }, + } + got := buildUserHistoryMessage(msg) + assert.Equal(t, "user", got.Role) + assert.Equal(t, "what about the chart? [augmented]", got.Content) +} + +func TestBuildUserHistoryMessage_FallsBackToContentWithCaptions(t *testing.T) { + msg := &types.Message{ + Role: "user", + Content: "look at this", + Images: types.MessageImages{ + {Caption: "a bar chart"}, + {Caption: "a pie chart"}, + }, + } + got := buildUserHistoryMessage(msg) + assert.Equal(t, "user", got.Role) + assert.Equal(t, "look at this\n\n[用户上传图片内容]\na bar chart\na pie chart", got.Content) +} + +// TestBuildAssistantHistoryMessages_NaturalFinishEmitsSingleAnswer covers the +// most common path: a turn with no tool calls (model answered directly). The +// result must be a single assistant message holding the canonical answer — +// duplicates would inflate token usage every turn. +func TestBuildAssistantHistoryMessages_NaturalFinishEmitsSingleAnswer(t *testing.T) { + msg := &types.Message{ + Role: "assistant", + Content: "Hello, nice to meet you!", + AgentSteps: types.AgentSteps{ + {Iteration: 0, Thought: "Hello, nice to meet you!", ToolCalls: nil}, + }, + } + got := buildAssistantHistoryMessages(msg) + if assert.Len(t, got, 1) { + assert.Equal(t, "assistant", got[0].Role) + assert.Equal(t, "Hello, nice to meet you!", got[0].Content) + assert.Empty(t, got[0].ToolCalls) + } +} + +// TestBuildAssistantHistoryMessages_StripsThinkBlocks ensures the trailing +// final-answer assistant message has any … blocks stripped, so +// internal-reasoning text doesn't leak into the next turn's context. +func TestBuildAssistantHistoryMessages_StripsThinkBlocks(t *testing.T) { + msg := &types.Message{ + Role: "assistant", + Content: "plotting...The answer is 42.", + } + got := buildAssistantHistoryMessages(msg) + if assert.Len(t, got, 1) { + assert.Equal(t, "The answer is 42.", got[0].Content) + } +} + +// TestBuildAssistantHistoryMessages_ToolCallsExpandIntoOpenAIShape covers the +// option-B replay: non-terminal tool calls from AgentSteps become proper +// assistant_with_tool_calls + tool messages, and the canonical final answer is +// appended last. final_answer entries are filtered because they're terminal +// signals — the trailing assistant message already carries the answer. +func TestBuildAssistantHistoryMessages_ToolCallsExpandIntoOpenAIShape(t *testing.T) { + msg := &types.Message{ + Role: "assistant", + Content: "Found 3 matches in the docs.", + AgentSteps: types.AgentSteps{ + { + Iteration: 0, + Thought: "Let me search.", + ToolCalls: []types.ToolCall{ + { + ID: "call_1", + Name: agenttools.ToolKnowledgeSearch, + Args: map[string]interface{}{"query": "foo"}, + Result: &types.ToolResult{ + Success: true, + Output: "doc A, doc B, doc C", + }, + }, + }, + }, + { + Iteration: 1, + Thought: "", + ToolCalls: []types.ToolCall{ + { + ID: "call_2", + Name: agenttools.ToolFinalAnswer, + Args: map[string]interface{}{"answer": "Found 3 matches in the docs."}, + Result: &types.ToolResult{Success: true}, + }, + }, + }, + }, + } + got := buildAssistantHistoryMessages(msg) + if !assert.Len(t, got, 3) { + return + } + // 1. assistant message announcing the tool call + assert.Equal(t, "assistant", got[0].Role) + assert.Equal(t, "Let me search.", got[0].Content) + if assert.Len(t, got[0].ToolCalls, 1) { + assert.Equal(t, "call_1", got[0].ToolCalls[0].ID) + assert.Equal(t, agenttools.ToolKnowledgeSearch, got[0].ToolCalls[0].Function.Name) + assert.Contains(t, got[0].ToolCalls[0].Function.Arguments, "foo") + } + // 2. tool result paired with the call ID + assert.Equal(t, "tool", got[1].Role) + assert.Equal(t, "call_1", got[1].ToolCallID) + assert.Equal(t, "doc A, doc B, doc C", got[1].Content) + // 3. canonical final answer (final_answer tool call itself was filtered) + assert.Equal(t, "assistant", got[2].Role) + assert.Equal(t, "Found 3 matches in the docs.", got[2].Content) + assert.Empty(t, got[2].ToolCalls) +} + +// TestBuildAssistantHistoryMessages_ToolFailureSurfacesAsError ensures a +// historical failed tool call is replayed as an "Error: …" tool message so the +// model can see (and avoid retrying) the same failure path. +func TestBuildAssistantHistoryMessages_ToolFailureSurfacesAsError(t *testing.T) { + msg := &types.Message{ + Role: "assistant", + Content: "Sorry, I could not complete the search.", + AgentSteps: types.AgentSteps{ + { + Iteration: 0, + Thought: "Trying search.", + ToolCalls: []types.ToolCall{ + { + ID: "call_err", + Name: agenttools.ToolKnowledgeSearch, + Args: map[string]interface{}{"query": "x"}, + Result: &types.ToolResult{ + Success: false, + Error: "kb unreachable", + }, + }, + }, + }, + }, + } + got := buildAssistantHistoryMessages(msg) + if !assert.Len(t, got, 3) { + return + } + assert.Equal(t, chat.Message{ + Role: "tool", + Content: "Error: kb unreachable", + ToolCallID: "call_err", + Name: agenttools.ToolKnowledgeSearch, + }, got[1]) +} + +// TestFilterNonTerminalToolCalls confirms only final_answer is dropped — every +// other tool (KB search, web search, MCP tools…) must survive the filter. +func TestFilterNonTerminalToolCalls(t *testing.T) { + in := []types.ToolCall{ + {Name: agenttools.ToolKnowledgeSearch}, + {Name: agenttools.ToolFinalAnswer}, + {Name: agenttools.ToolWebSearch}, + } + out := filterNonTerminalToolCalls(in) + if assert.Len(t, out, 2) { + assert.Equal(t, agenttools.ToolKnowledgeSearch, out[0].Name) + assert.Equal(t, agenttools.ToolWebSearch, out[1].Name) + } +} diff --git a/internal/application/service/agent_service.go b/internal/application/service/agent_service.go index 7cb0b664..70e4a466 100644 --- a/internal/application/service/agent_service.go +++ b/internal/application/service/agent_service.go @@ -102,14 +102,15 @@ func NewAgentService( } } -// CreateAgentEngineWithEventBus creates an agent engine with the given configuration and EventBus +// CreateAgentEngine creates an agent engine with the given configuration and EventBus. +// History is loaded once per turn by the caller (see service.LoadAgentHistory) +// and handed to AgentEngine.Execute as llmContext; the engine is stateless across turns. func (s *agentService) CreateAgentEngine( ctx context.Context, config *types.AgentConfig, chatModel chat.Chat, rerankModel rerank.Reranker, eventBus *event.EventBus, - contextManager interfaces.ContextManager, sessionID string, ) (interfaces.AgentEngine, error) { logger.Infof(ctx, "Creating agent engine with custom EventBus") @@ -144,7 +145,7 @@ func (s *agentService) CreateAgentEngine( // 5. Create engine engine := agent.NewAgentEngine( config, chatModel, toolRegistry, eventBus, - kbInfos, selectedDocs, contextManager, sessionID, + kbInfos, selectedDocs, sessionID, systemPromptTemplate, ) engine.SetAppConfig(s.cfg) diff --git a/internal/application/service/llmcontext/context_manager.go b/internal/application/service/llmcontext/context_manager.go deleted file mode 100644 index 42e720e7..00000000 --- a/internal/application/service/llmcontext/context_manager.go +++ /dev/null @@ -1,221 +0,0 @@ -package llmcontext - -import ( - "context" - "fmt" - "regexp" - "sort" - "strings" - "time" - - "github.com/Tencent/WeKnora/internal/logger" - "github.com/Tencent/WeKnora/internal/models/chat" - "github.com/Tencent/WeKnora/internal/types" - "github.com/Tencent/WeKnora/internal/types/interfaces" -) - -// dbFallbackFetchCount is the number of raw DB messages to fetch when -// rebuilding context from persistent storage. This should be generous -// because user+assistant messages are paired by RequestID and some -// incomplete pairs are discarded. -const dbFallbackFetchCount = 200 - -var regThinkTags = regexp.MustCompile(`(?s).*?`) - -// contextManager implements the ContextManager interface. -// It is a cache-backed storage layer: messages are persisted per session in -// a fast store (Redis / memory). When the cache is empty (e.g. TTL expired), -// it falls back to the persistent messages table via MessageService to -// rebuild context. -// -// All LLM-aware compression (summarisation, tool-boundary-aware truncation) -// is handled by the Agent Engine's Consolidator before messages are sent to -// the model. -type contextManager struct { - storage ContextStorage - messageRepo interfaces.MessageRepository // optional; enables DB fallback -} - -// NewContextManager creates a context manager. -// messageRepo is optional — when provided, GetContext will reconstruct -// history from the DB if the cache is empty. -func NewContextManager(storage ContextStorage, messageRepo interfaces.MessageRepository) interfaces.ContextManager { - return &contextManager{ - storage: storage, - messageRepo: messageRepo, - } -} - -// AddMessage appends a message to the session context and persists it. -func (cm *contextManager) AddMessage(ctx context.Context, sessionID string, message chat.Message) error { - messages, err := cm.storage.Load(ctx, sessionID) - if err != nil { - return fmt.Errorf("failed to load context: %w", err) - } - - messages = append(messages, message) - - if err := cm.storage.Save(ctx, sessionID, messages); err != nil { - return fmt.Errorf("failed to save context: %w", err) - } - - logger.Debugf(ctx, "[ContextManager][Session-%s] Message saved (total: %d)", sessionID, len(messages)) - return nil -} - -// GetContext retrieves the stored context for a session. -// If the cache is empty and a MessageService is available, it rebuilds -// the context from the persistent messages table and warms the cache. -func (cm *contextManager) GetContext(ctx context.Context, sessionID string) ([]chat.Message, error) { - messages, err := cm.storage.Load(ctx, sessionID) - if err != nil { - return nil, fmt.Errorf("failed to load context: %w", err) - } - - if len(messages) > 0 { - logger.Debugf(ctx, "[ContextManager][Session-%s] Cache hit: %d messages", sessionID, len(messages)) - return messages, nil - } - - if cm.messageRepo == nil { - return messages, nil - } - - // Cache miss — rebuild from DB - rebuilt, err := cm.rebuildFromDB(ctx, sessionID) - if err != nil { - logger.Warnf(ctx, "[ContextManager][Session-%s] Failed to rebuild context from DB: %v", sessionID, err) - return []chat.Message{}, nil - } - - if len(rebuilt) > 0 { - if saveErr := cm.storage.Save(ctx, sessionID, rebuilt); saveErr != nil { - logger.Warnf(ctx, "[ContextManager][Session-%s] Failed to warm cache: %v", sessionID, saveErr) - } - logger.Infof(ctx, "[ContextManager][Session-%s] Rebuilt %d messages from DB", sessionID, len(rebuilt)) - } - - return rebuilt, nil -} - -// rebuildFromDB loads recent messages from the persistent messages table -// and converts them into chat.Message pairs (user + assistant). -func (cm *contextManager) rebuildFromDB(ctx context.Context, sessionID string) ([]chat.Message, error) { - dbMessages, err := cm.messageRepo.GetRecentMessagesBySession(ctx, sessionID, dbFallbackFetchCount) - if err != nil { - return nil, fmt.Errorf("failed to load messages from DB: %w", err) - } - if len(dbMessages) == 0 { - return nil, nil - } - - // Group by RequestID into Q&A pairs, same logic as chat_pipeline/common.go - type pair struct { - query string - answer string - createdAt time.Time - } - pairMap := make(map[string]*pair) - for _, msg := range dbMessages { - p, ok := pairMap[msg.RequestID] - if !ok { - p = &pair{} - pairMap[msg.RequestID] = p - } - switch msg.Role { - case "user": - if msg.RenderedContent != "" { - p.query = msg.RenderedContent - } else { - p.query = msg.Content - } - p.createdAt = msg.CreatedAt - if desc := extractImageCaptions(msg.Images); desc != "" && msg.RenderedContent == "" { - p.query += "\n\n[用户上传图片内容]\n" + desc - } - case "assistant": - p.answer = regThinkTags.ReplaceAllString(msg.Content, "") - } - } - - pairs := make([]*pair, 0, len(pairMap)) - for _, p := range pairMap { - if p.query != "" && p.answer != "" { - pairs = append(pairs, p) - } - } - - sort.Slice(pairs, func(i, j int) bool { - return pairs[i].createdAt.Before(pairs[j].createdAt) - }) - - result := make([]chat.Message, 0, len(pairs)*2) - for _, p := range pairs { - result = append(result, - chat.Message{Role: "user", Content: p.query}, - chat.Message{Role: "assistant", Content: p.answer}, - ) - } - - return result, nil -} - -// extractImageCaptions concatenates non-empty Caption fields from message -// images so that previous turns' image descriptions are included in context. -func extractImageCaptions(images types.MessageImages) string { - var parts []string - for _, img := range images { - if img.Caption != "" { - parts = append(parts, img.Caption) - } - } - return strings.Join(parts, "\n") -} - -// ClearContext removes all context for a session. -func (cm *contextManager) ClearContext(ctx context.Context, sessionID string) error { - if err := cm.storage.Delete(ctx, sessionID); err != nil { - return fmt.Errorf("failed to clear context: %w", err) - } - logger.Infof(ctx, "[ContextManager][Session-%s] Context cleared", sessionID) - return nil -} - -// GetContextStats returns statistics about the stored context. -func (cm *contextManager) GetContextStats(ctx context.Context, sessionID string) (*interfaces.ContextStats, error) { - messages, err := cm.storage.Load(ctx, sessionID) - if err != nil { - return nil, fmt.Errorf("failed to load context: %w", err) - } - - return &interfaces.ContextStats{ - MessageCount: len(messages), - OriginalMessageCount: len(messages), - }, nil -} - -// SetSystemPrompt sets or updates the system prompt for a session. -func (cm *contextManager) SetSystemPrompt(ctx context.Context, sessionID string, systemPrompt string) error { - messages, err := cm.storage.Load(ctx, sessionID) - if err != nil { - return fmt.Errorf("failed to load context: %w", err) - } - - systemMessage := chat.Message{ - Role: "system", - Content: systemPrompt, - } - - if len(messages) > 0 && messages[0].Role == "system" { - messages[0] = systemMessage - } else { - messages = append([]chat.Message{systemMessage}, messages...) - } - - if err := cm.storage.Save(ctx, sessionID, messages); err != nil { - return fmt.Errorf("failed to save context: %w", err) - } - - logger.Debugf(ctx, "[ContextManager][Session-%s] System prompt set (length=%d)", sessionID, len(systemPrompt)) - return nil -} diff --git a/internal/application/service/llmcontext/context_manager_factory.go b/internal/application/service/llmcontext/context_manager_factory.go deleted file mode 100644 index 0482afe3..00000000 --- a/internal/application/service/llmcontext/context_manager_factory.go +++ /dev/null @@ -1,18 +0,0 @@ -package llmcontext - -import ( - "github.com/Tencent/WeKnora/internal/types/interfaces" -) - -// NewContextManagerFromConfig creates a ContextManager. -// messageRepo is optional — when provided, context will be rebuilt from -// the persistent messages table if the cache (Redis/memory) is empty. -func NewContextManagerFromConfig( - storage ContextStorage, - messageRepo interfaces.MessageRepository, -) interfaces.ContextManager { - if storage == nil { - storage = NewMemoryStorage() - } - return NewContextManager(storage, messageRepo) -} diff --git a/internal/application/service/llmcontext/memory_storage.go b/internal/application/service/llmcontext/memory_storage.go deleted file mode 100644 index 0a6188b3..00000000 --- a/internal/application/service/llmcontext/memory_storage.go +++ /dev/null @@ -1,65 +0,0 @@ -package llmcontext - -import ( - "context" - "sync" - - "github.com/Tencent/WeKnora/internal/logger" - "github.com/Tencent/WeKnora/internal/models/chat" -) - -// memoryStorage implements ContextStorage using in-memory storage -type memoryStorage struct { - sessions map[string][]chat.Message - mu sync.RWMutex -} - -// NewMemoryStorage creates a new memory-based storage -func NewMemoryStorage() ContextStorage { - return &memoryStorage{ - sessions: make(map[string][]chat.Message), - } -} - -// Save saves messages for a session to memory -func (ms *memoryStorage) Save(ctx context.Context, sessionID string, messages []chat.Message) error { - ms.mu.Lock() - defer ms.mu.Unlock() - - // Make a copy to avoid external modifications - messageCopy := make([]chat.Message, len(messages)) - copy(messageCopy, messages) - - ms.sessions[sessionID] = messageCopy - logger.Debugf(ctx, "[MemoryStorage][Session-%s] Saved %d messages to memory", sessionID, len(messages)) - return nil -} - -// Load loads messages for a session from memory -func (ms *memoryStorage) Load(ctx context.Context, sessionID string) ([]chat.Message, error) { - ms.mu.RLock() - defer ms.mu.RUnlock() - - messages, exists := ms.sessions[sessionID] - if !exists { - logger.Debugf(ctx, "[MemoryStorage][Session-%s] No context found in memory", sessionID) - return []chat.Message{}, nil - } - - // Return a copy to avoid external modifications - messageCopy := make([]chat.Message, len(messages)) - copy(messageCopy, messages) - - logger.Debugf(ctx, "[MemoryStorage][Session-%s] Loaded %d messages from memory", sessionID, len(messages)) - return messageCopy, nil -} - -// Delete deletes all messages for a session from memory -func (ms *memoryStorage) Delete(ctx context.Context, sessionID string) error { - ms.mu.Lock() - defer ms.mu.Unlock() - - delete(ms.sessions, sessionID) - logger.Debugf(ctx, "[MemoryStorage][Session-%s] Deleted context from memory", sessionID) - return nil -} diff --git a/internal/application/service/llmcontext/redis_storage.go b/internal/application/service/llmcontext/redis_storage.go deleted file mode 100644 index b09efb93..00000000 --- a/internal/application/service/llmcontext/redis_storage.go +++ /dev/null @@ -1,112 +0,0 @@ -package llmcontext - -import ( - "context" - "encoding/json" - "fmt" - "time" - - "github.com/Tencent/WeKnora/internal/logger" - "github.com/Tencent/WeKnora/internal/models/chat" - "github.com/redis/go-redis/v9" -) - -// redisStorage implements ContextStorage using Redis -type redisStorage struct { - client *redis.Client - ttl time.Duration - prefix string -} - -// NewRedisStorage creates a new Redis-based storage -func NewRedisStorage(client *redis.Client, ttl time.Duration, prefix string) (ContextStorage, error) { - // Validate connection - _, err := client.Ping(context.Background()).Result() - if err != nil { - return nil, fmt.Errorf("failed to connect to Redis: %w", err) - } - - if ttl == 0 { - ttl = 24 * time.Hour // Default TTL 24 hours - } - - if prefix == "" { - prefix = "context:" // Default prefix - } - - return &redisStorage{ - client: client, - ttl: ttl, - prefix: prefix, - }, nil -} - -// buildKey builds the Redis key for a session -func (rs *redisStorage) buildKey(sessionID string) string { - return fmt.Sprintf("%s%s", rs.prefix, sessionID) -} - -// Save saves messages for a session to Redis -func (rs *redisStorage) Save(ctx context.Context, sessionID string, messages []chat.Message) error { - key := rs.buildKey(sessionID) - - // Marshal messages to JSON - data, err := json.Marshal(messages) - if err != nil { - logger.Errorf(ctx, "[RedisStorage][Session-%s] Failed to marshal messages: %v", sessionID, err) - return fmt.Errorf("failed to marshal messages: %w", err) - } - - // Save to Redis with TTL - err = rs.client.Set(ctx, key, data, rs.ttl).Err() - if err != nil { - logger.Errorf(ctx, "[RedisStorage][Session-%s] Failed to save to Redis: %v", sessionID, err) - return fmt.Errorf("failed to save to Redis: %w", err) - } - - logger.Debugf(ctx, "[RedisStorage][Session-%s] Saved %d messages to Redis (TTL: %s)", - sessionID, len(messages), rs.ttl) - return nil -} - -// Load loads messages for a session from Redis -func (rs *redisStorage) Load(ctx context.Context, sessionID string) ([]chat.Message, error) { - key := rs.buildKey(sessionID) - - // Get from Redis - data, err := rs.client.Get(ctx, key).Bytes() - if err != nil { - if err == redis.Nil { - // No context exists yet, return empty slice - logger.Debugf(ctx, "[RedisStorage][Session-%s] No context found in Redis", sessionID) - return []chat.Message{}, nil - } - logger.Errorf(ctx, "[RedisStorage][Session-%s] Failed to get from Redis: %v", sessionID, err) - return nil, fmt.Errorf("failed to get from Redis: %w", err) - } - - // Unmarshal messages - var messages []chat.Message - err = json.Unmarshal(data, &messages) - if err != nil { - logger.Errorf(ctx, "[RedisStorage][Session-%s] Failed to unmarshal messages: %v", sessionID, err) - return nil, fmt.Errorf("failed to unmarshal messages: %w", err) - } - - logger.Debugf(ctx, "[RedisStorage][Session-%s] Loaded %d messages from Redis", sessionID, len(messages)) - return messages, nil -} - -// Delete deletes all messages for a session from Redis -func (rs *redisStorage) Delete(ctx context.Context, sessionID string) error { - key := rs.buildKey(sessionID) - - err := rs.client.Del(ctx, key).Err() - if err != nil { - logger.Errorf(ctx, "[RedisStorage][Session-%s] Failed to delete from Redis: %v", sessionID, err) - return fmt.Errorf("failed to delete from Redis: %w", err) - } - - logger.Debugf(ctx, "[RedisStorage][Session-%s] Deleted context from Redis", sessionID) - return nil -} diff --git a/internal/application/service/llmcontext/storage.go b/internal/application/service/llmcontext/storage.go deleted file mode 100644 index 2c928dda..00000000 --- a/internal/application/service/llmcontext/storage.go +++ /dev/null @@ -1,20 +0,0 @@ -package llmcontext - -import ( - "context" - - "github.com/Tencent/WeKnora/internal/models/chat" -) - -// ContextStorage defines the interface for storing and retrieving conversation context -// This separates storage implementation from business logic -type ContextStorage interface { - // Save saves messages for a session - Save(ctx context.Context, sessionID string, messages []chat.Message) error - - // Load loads messages for a session - Load(ctx context.Context, sessionID string) ([]chat.Message, error) - - // Delete deletes all messages for a session - Delete(ctx context.Context, sessionID string) error -} diff --git a/internal/application/service/session.go b/internal/application/service/session.go index b2b96ed7..710eb61e 100644 --- a/internal/application/service/session.go +++ b/internal/application/service/session.go @@ -15,7 +15,6 @@ import ( "github.com/google/uuid" chatpipeline "github.com/Tencent/WeKnora/internal/application/service/chat_pipeline" - llmcontext "github.com/Tencent/WeKnora/internal/application/service/llmcontext" ) // generateEventID generates a unique event ID with type suffix for better traceability @@ -23,7 +22,10 @@ func generateEventID(suffix string) string { return fmt.Sprintf("%s-%s", uuid.New().String()[:8], suffix) } -// sessionService implements the SessionService interface for managing conversation sessions +// sessionService implements the SessionService interface for managing conversation sessions. +// History for multi-turn conversations is rebuilt from the messages table on demand +// (see service.LoadAgentHistory and chat_pipeline history loading) — there is no +// separate cross-turn cache layer. type sessionService struct { cfg *config.Config // Application configuration sessionRepo interfaces.SessionRepository // Repository for session data @@ -33,7 +35,6 @@ type sessionService struct { tenantService interfaces.TenantService // Service for tenant operations eventManager *chatpipeline.EventManager // Event manager for chat pipeline agentService interfaces.AgentService // Service for agent operations - sessionStorage llmcontext.ContextStorage // Session storage knowledgeService interfaces.KnowledgeService // Service for knowledge operations chunkService interfaces.ChunkService // Service for chunk operations webSearchStateRepo interfaces.WebSearchStateService // Service for web search state @@ -53,7 +54,6 @@ func NewSessionService(cfg *config.Config, tenantService interfaces.TenantService, eventManager *chatpipeline.EventManager, agentService interfaces.AgentService, - sessionStorage llmcontext.ContextStorage, webSearchStateRepo interfaces.WebSearchStateService, webSearchProviderRepo interfaces.WebSearchProviderRepository, kbShareService interfaces.KBShareService, @@ -70,7 +70,6 @@ func NewSessionService(cfg *config.Config, tenantService: tenantService, eventManager: eventManager, agentService: agentService, - sessionStorage: sessionStorage, webSearchStateRepo: webSearchStateRepo, webSearchProviderRepo: webSearchProviderRepo, kbShareService: kbShareService, @@ -266,11 +265,6 @@ func (s *sessionService) DeleteSession(ctx context.Context, id string) error { logger.Warnf(ctx, "Failed to cleanup temporary KB for session %s: %v", id, err) } - // Cleanup conversation context stored in Redis for this session - if err := s.sessionStorage.Delete(ctx, id); err != nil { - logger.Warnf(ctx, "Failed to cleanup conversation context for session %s: %v", id, err) - } - // Delete session from repository err := s.sessionRepo.Delete(ctx, tenantID, id) if err != nil { @@ -314,9 +308,6 @@ func (s *sessionService) BatchDeleteSessions(ctx context.Context, ids []string) if err := s.webSearchStateRepo.DeleteWebSearchTempKBState(ctx, id); err != nil { logger.Warnf(ctx, "Failed to cleanup temporary KB for session %s: %v", id, err) } - if err := s.sessionStorage.Delete(ctx, id); err != nil { - logger.Warnf(ctx, "Failed to cleanup conversation context for session %s: %v", id, err) - } } // Batch delete sessions from repository @@ -359,9 +350,6 @@ func (s *sessionService) DeleteAllSessions(ctx context.Context) error { if err := s.webSearchStateRepo.DeleteWebSearchTempKBState(ctx, session.ID); err != nil { logger.Warnf(ctx, "Failed to cleanup temporary KB for session %s: %v", session.ID, err) } - if err := s.sessionStorage.Delete(ctx, session.ID); err != nil { - logger.Warnf(ctx, "Failed to cleanup conversation context for session %s: %v", session.ID, err) - } } } @@ -560,10 +548,3 @@ func (s *sessionService) GenerateTitleAsync( } }() } - -// ClearContext clears the LLM context for a session -// This is useful when switching knowledge bases or agent modes to prevent context contamination -func (s *sessionService) ClearContext(ctx context.Context, sessionID string) error { - logger.Infof(ctx, "Clearing context for session: %s", sessionID) - return s.sessionStorage.Delete(ctx, sessionID) -} diff --git a/internal/application/service/session_agent_qa.go b/internal/application/service/session_agent_qa.go index 6bdbf4f3..9113e319 100644 --- a/internal/application/service/session_agent_qa.go +++ b/internal/application/service/session_agent_qa.go @@ -8,13 +8,11 @@ import ( "os" "github.com/Tencent/WeKnora/internal/agent/tools" - llmcontext "github.com/Tencent/WeKnora/internal/application/service/llmcontext" "github.com/Tencent/WeKnora/internal/event" "github.com/Tencent/WeKnora/internal/logger" "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" ) // AgentQA performs agent-based question answering with conversation history and streaming support @@ -117,38 +115,29 @@ func (s *sessionService) AgentQA( logger.Infof(ctx, "knowledge_search tool not enabled, skipping rerank model initialization") } - // Get or create contextManager for this session - contextManager := s.getContextManagerForSession() - - // Set system prompt for the current agent in context manager - // This ensures the context uses the correct system prompt when switching agents - systemPrompt := agentConfig.ResolveSystemPrompt(agentConfig.WebSearchEnabled) - if systemPrompt != "" { - if err := contextManager.SetSystemPrompt(ctx, sessionID, systemPrompt); err != nil { - logger.Warnf(ctx, "Failed to set system prompt in context manager: %v", err) - } else { - logger.Infof(ctx, "System prompt updated in context manager for agent") + // Load multi-turn history directly from DB (the single source of truth). + // AgentSteps on each historical assistant message are expanded into proper + // assistant_with_tool_calls + tool messages so the model can see what was + // tried last turn — except final_answer, which is replayed as the trailing + // canonical assistant message. + var llmContext []chat.Message + if agentConfig.MultiTurnEnabled { + historyTurns := agentConfig.HistoryTurns + if historyTurns <= 0 { + historyTurns = 5 } - } - - // Get LLM context from context manager - llmContext, err := s.getContextForSession(ctx, contextManager, sessionID) - if err != nil { - logger.Warnf(ctx, "Failed to get LLM context: %v, continuing without history", err) - llmContext = []chat.Message{} - } - logger.Infof(ctx, "Loaded %d messages from LLM context manager", len(llmContext)) - - // Apply multi-turn configuration for Agent mode - // Note: In Agent mode, context is managed by contextManager with compression strategies, - // so we don't apply HistoryTurns limit here. HistoryTurns is used in normal (KnowledgeQA) mode. - if !agentConfig.MultiTurnEnabled { - // Multi-turn disabled, clear history - logger.Infof(ctx, "Multi-turn disabled for this agent, clearing history context") + llmContext, err = LoadAgentHistory(ctx, s.messageRepo, sessionID, historyTurns) + if err != nil { + logger.Warnf(ctx, "Failed to load agent history from DB: %v, continuing without history", err) + llmContext = []chat.Message{} + } + logger.Infof(ctx, "Loaded %d history messages from DB (turns=%d)", len(llmContext), historyTurns) + } else { + logger.Infof(ctx, "Multi-turn disabled for this agent, running without history") llmContext = []chat.Message{} } - // Create agent engine with EventBus and ContextManager + // Create agent engine with EventBus logger.Info(ctx, "Creating agent engine") engine, err := s.agentService.CreateAgentEngine( ctx, @@ -156,7 +145,6 @@ func (s *sessionService) AgentQA( summaryModel, rerankModel, eventBus, - contextManager, sessionID, ) if err != nil { @@ -357,29 +345,3 @@ func (s *sessionService) configureSkillsFromAgent( } } - -// getContextManagerForSession creates a context manager for the session. -func (s *sessionService) getContextManagerForSession() interfaces.ContextManager { - return llmcontext.NewContextManagerFromConfig(s.sessionStorage, s.messageRepo) -} - -// getContextForSession retrieves LLM context for a session -func (s *sessionService) getContextForSession( - ctx context.Context, - contextManager interfaces.ContextManager, - sessionID string, -) ([]chat.Message, error) { - history, err := contextManager.GetContext(ctx, sessionID) - if err != nil { - return nil, fmt.Errorf("failed to get context: %w", err) - } - - // Log context statistics - stats, _ := contextManager.GetContextStats(ctx, sessionID) - if stats != nil { - logger.Infof(ctx, "LLM context stats for session %s: messages=%d, tokens=~%d, compressed=%v", - sessionID, stats.MessageCount, stats.TokenCount, stats.IsCompressed) - } - - return history, nil -} diff --git a/internal/container/container.go b/internal/container/container.go index c4f835e1..20283a0c 100644 --- a/internal/container/container.go +++ b/internal/container/container.go @@ -48,7 +48,6 @@ import ( "github.com/Tencent/WeKnora/internal/application/service" chatpipeline "github.com/Tencent/WeKnora/internal/application/service/chat_pipeline" "github.com/Tencent/WeKnora/internal/application/service/file" - "github.com/Tencent/WeKnora/internal/application/service/llmcontext" memoryService "github.com/Tencent/WeKnora/internal/application/service/memory" "github.com/Tencent/WeKnora/internal/application/service/retriever" "github.com/Tencent/WeKnora/internal/config" @@ -110,7 +109,6 @@ func BuildContainer(container *dig.Container) *dig.Container { must(container.Provide(initFileService)) must(container.Provide(initRedisClient)) must(container.Provide(initAntsPool)) - must(container.Provide(initContextStorage)) // Register tracer cleanup handler (tracer needs to be available for cleanup registration) must(container.Invoke(registerTracerCleanup)) @@ -382,18 +380,6 @@ func initRedisClient() (*redis.Client, error) { return client, nil } -func initContextStorage(redisClient *redis.Client) (llmcontext.ContextStorage, error) { - if redisClient == nil { - logger.Infof(context.Background(), "[ContextStorage] Redis not available, using in-memory storage") - return llmcontext.NewMemoryStorage(), nil - } - storage, err := llmcontext.NewRedisStorage(redisClient, 24*time.Hour, "context:") - if err != nil { - return nil, err - } - return storage, nil -} - // initDatabase initializes database connection // Creates and configures database connection based on environment configuration // Supports multiple database backends (PostgreSQL) diff --git a/internal/handler/session/handler.go b/internal/handler/session/handler.go index 1612695d..5aa7747a 100644 --- a/internal/handler/session/handler.go +++ b/internal/handler/session/handler.go @@ -374,10 +374,6 @@ func (h *Handler) ClearSessionMessages(c *gin.Context) { return } - if err := h.sessionService.ClearContext(ctx, id); err != nil { - logger.Warnf(ctx, "Failed to clear LLM context for session %s: %v", id, err) - } - logger.Infof(ctx, "Session messages cleared successfully, ID: %s", id) c.JSON(http.StatusOK, gin.H{ "success": true, diff --git a/internal/im/service.go b/internal/im/service.go index 2522a27d..e90e0121 100644 --- a/internal/im/service.go +++ b/internal/im/service.go @@ -1230,16 +1230,15 @@ func (s *Service) handleCommand( // Handle service-level side effects. switch result.Action { case ActionClear: - // Soft-delete the current ChannelSession and clear the LLM context - // so the next message creates a completely fresh conversation. + // Soft-delete the current ChannelSession so the next IM message + // starts a completely fresh WeKnora session. Conversation history + // is keyed by session ID and rebuilt from DB on demand, so no + // separate cache invalidation step is needed. if err := s.db.Model(&ChannelSession{}). Where("id = ?", channelSession.ID). Update("deleted_at", time.Now()).Error; err != nil { logger.Warnf(ctx, "[IM] Failed to soft-delete channel session: %v", err) } - if err := s.sessionService.ClearContext(ctx, channelSession.SessionID); err != nil { - logger.Warnf(ctx, "[IM] Failed to clear session context: %v", err) - } case ActionStop: stopThreadID := "" if channel.SessionMode == string(SessionModeThread) { diff --git a/internal/types/interfaces/agent.go b/internal/types/interfaces/agent.go index c149b3c7..eb4d8a21 100644 --- a/internal/types/interfaces/agent.go +++ b/internal/types/interfaces/agent.go @@ -32,14 +32,15 @@ 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 creates an agent engine with the given configuration and EventBus. + // Conversation history is loaded by the caller (see service.LoadAgentHistory) and + // passed into AgentEngine.Execute; the engine itself is stateless across turns. CreateAgentEngine( ctx context.Context, config *types.AgentConfig, chatModel chat.Chat, rerankModel rerank.Reranker, eventBus *event.EventBus, - contextManager ContextManager, sessionID string, ) (AgentEngine, error) diff --git a/internal/types/interfaces/context_manager.go b/internal/types/interfaces/context_manager.go deleted file mode 100644 index 30ef8080..00000000 --- a/internal/types/interfaces/context_manager.go +++ /dev/null @@ -1,44 +0,0 @@ -package interfaces - -import ( - "context" - - "github.com/Tencent/WeKnora/internal/models/chat" -) - -// ContextManager manages LLM context for sessions -// It maintains conversation context separately from message storage -// and provides context compression when context window is exceeded -type ContextManager interface { - // AddMessage adds a message to the session context - // The message will be added to the context window for LLM - AddMessage(ctx context.Context, sessionID string, message chat.Message) error - - // GetContext retrieves the current context for a session - // Returns messages that fit within the context window - // May apply compression if context is too large - GetContext(ctx context.Context, sessionID string) ([]chat.Message, error) - - // ClearContext clears all context for a session - ClearContext(ctx context.Context, sessionID string) error - - // GetContextStats returns statistics about the context - GetContextStats(ctx context.Context, sessionID string) (*ContextStats, error) - - // SetSystemPrompt sets or updates the system prompt for a session - // If a system message exists, it will be replaced; otherwise, a new one will be added at the beginning - SetSystemPrompt(ctx context.Context, sessionID string, systemPrompt string) error -} - -// ContextStats contains statistics about session context -type ContextStats struct { - // Total number of messages in context - MessageCount int `json:"message_count"` - // Estimated token count - TokenCount int `json:"token_count"` - // Whether context was compressed - IsCompressed bool `json:"is_compressed"` - // Number of original messages before compression - OriginalMessageCount int `json:"original_message_count"` -} - diff --git a/internal/types/interfaces/session.go b/internal/types/interfaces/session.go index 565eae84..b2f4340b 100644 --- a/internal/types/interfaces/session.go +++ b/internal/types/interfaces/session.go @@ -49,8 +49,6 @@ type SessionService interface { SearchKnowledge(ctx context.Context, knowledgeBaseIDs []string, knowledgeIDs []string, query string) ([]*types.SearchResult, error) // AgentQA performs agent-based question answering with conversation history and streaming support. AgentQA(ctx context.Context, req *types.QARequest, eventBus *event.EventBus) error - // ClearContext clears the LLM context for a session - ClearContext(ctx context.Context, sessionID string) error } // SessionRepository defines the session repository interface