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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user