refactor(agent): rebuild multi-turn history from DB, drop llmcontext layer
The Agent ran a parallel context cache (Redis/in-memory) on top of the messages table to feed multi-turn history into the LLM. That dual-write caused subtle drift (e.g. compression diverging from DB, system-prompt swaps lost on restart) and required a separate ClearContext path on session/IM clear. Make the messages table the single source of truth: - Add service.LoadAgentHistory: rebuilds chat.Message history per turn from the persisted messages, expanding AgentSteps into proper OpenAI assistant_with_tool_calls + tool messages and replaying the canonical final answer (with <think> blocks stripped). final_answer tool calls are filtered to avoid duplicating the trailing answer. - Make AgentEngine stateless across turns: drop ContextManager / sessionID cache plumbing from the engine, agent service, and CreateAgentEngine signature. The engine only uses sessionID for logging/event emission. - Wire AgentQA to load history from DB on demand using HistoryTurns (default 5) when MultiTurnEnabled, otherwise run with empty history. - Delete the llmcontext package (ContextManager interface, Redis/memory storage, factory) and the SessionService.ClearContext API path; IM /clear and session message clear no longer need to invalidate cache. Behavior preserved: KnowledgeQA-mode replay is unchanged (turns with empty AgentSteps just produce the single canonical assistant message), and Agent-mode turns now consistently see prior tool calls and results.
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
@@ -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"`
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user