feat(agent): enhance message consolidation and context management

- Improved the message consolidation logic to preserve the current turn's user query and all subsequent assistant/tool messages, ensuring better context retention.
- Updated the CompressContext function to reflect the new consolidation strategy, maintaining the system prompt and relevant recent messages.
- Refactored the context manager to support optional message repository for improved context rebuilding from persistent storage.
- Added comprehensive tests to validate the new consolidation behavior and ensure correct message handling across various scenarios.
This commit is contained in:
wizardchen
2026-03-24 17:32:24 +08:00
committed by lyingbug
parent b980d4619d
commit a8924e301e
11 changed files with 662 additions and 535 deletions
@@ -111,7 +111,7 @@ func (s *sessionService) AgentQA(
}
// Get or create contextManager for this session
contextManager := s.getContextManagerForSession(ctx, req.Session, summaryModel)
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
@@ -265,6 +265,10 @@ func (s *sessionService) buildAgentConfig(
agentConfig.SearchTargets = searchTargets
logger.Infof(ctx, "Agent search targets built: %d targets", len(searchTargets))
if agentConfig.MaxContextTokens <= 0 {
agentConfig.MaxContextTokens = types.DefaultMaxContextTokens
}
return agentConfig, nil
}
@@ -322,32 +326,9 @@ func (s *sessionService) configureSkillsFromAgent(
}
// getContextManagerForSession creates a context manager for the session based on configuration
// Returns the configured context manager (tenant-level or session-level) or default
func (s *sessionService) getContextManagerForSession(
ctx context.Context,
session *types.Session,
chatModel chat.Chat,
) interfaces.ContextManager {
// Get tenant to access global context configuration
tenant, _ := types.TenantInfoFromContext(ctx)
// Determine which context config to use: tenant-level or default
var contextConfig *types.ContextConfig
if tenant != nil && tenant.ContextConfig != nil {
// Use tenant-level configuration
contextConfig = tenant.ContextConfig
logger.Infof(ctx, "Using tenant-level context config for session %s", session.ID)
} else {
// Use service's default context manager
logger.Debugf(ctx, "Using default context manager for session %s", session.ID)
contextConfig = &types.ContextConfig{
MaxTokens: llmcontext.DefaultMaxTokens,
CompressionStrategy: llmcontext.DefaultCompressionStrategy,
RecentMessageCount: llmcontext.DefaultRecentMessageCount,
SummarizeThreshold: llmcontext.DefaultSummarizeThreshold,
}
}
return llmcontext.NewContextManagerFromConfig(contextConfig, s.sessionStorage, chatModel)
// 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