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
+5 -2
View File
@@ -7,6 +7,9 @@ import (
"time"
)
// DefaultMaxContextTokens is the default context window budget for agent conversations (200k).
const DefaultMaxContextTokens = 200000
// AgentConfig represents the full agent configuration (used at tenant level and runtime)
// This includes all configuration parameters for agent execution
type AgentConfig struct {
@@ -47,8 +50,8 @@ type AgentConfig struct {
// Outputs exceeding this limit are truncated with head + tail preservation.
MaxToolOutputChars int `json:"max_tool_output_chars,omitempty"`
// Maximum context window tokens for the agent (default: 0 = disabled).
// When set, the agent compresses older messages to stay within this limit,
// Maximum context window tokens for the agent (default: 200000).
// The agent compresses older messages to stay within this limit,
// preserving tool_call/tool_result pairs.
MaxContextTokens int `json:"max_context_tokens,omitempty"`
}
@@ -42,12 +42,3 @@ type ContextStats struct {
OriginalMessageCount int `json:"original_message_count"`
}
// CompressionStrategy defines how context should be compressed
type CompressionStrategy interface {
// Compress compresses messages when context exceeds limits
// Returns compressed messages that fit within the limit
Compress(ctx context.Context, messages []chat.Message, maxTokens int) ([]chat.Message, error)
// EstimateTokens estimates token count for messages
EstimateTokens(messages []chat.Message) int
}