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:
@@ -1,78 +1,18 @@
|
||||
package llmcontext
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
const (
|
||||
// Context manager types
|
||||
ContextManagerTypeMemory = "memory"
|
||||
ContextManagerTypeRedis = "redis"
|
||||
|
||||
// Default values
|
||||
DefaultMaxTokens = 128 * 1024 // 128K tokens
|
||||
DefaultRecentMessageCount = 20
|
||||
DefaultSummarizeThreshold = 5
|
||||
DefaultCompressionStrategy = "sliding_window"
|
||||
)
|
||||
|
||||
// NewContextManagerFromConfig creates a ContextManager based on configuration
|
||||
// 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(
|
||||
contextCfg *types.ContextConfig,
|
||||
storage ContextStorage,
|
||||
chatModel chat.Chat,
|
||||
messageRepo interfaces.MessageRepository,
|
||||
) interfaces.ContextManager {
|
||||
// Use default values if config is nil
|
||||
if contextCfg == nil {
|
||||
logger.Info(context.TODO(), "ContextManager config not found, using default memory-based context manager")
|
||||
strategy := NewSlidingWindowStrategy(DefaultRecentMessageCount)
|
||||
storage := NewMemoryStorage()
|
||||
return NewContextManager(storage, strategy, DefaultMaxTokens)
|
||||
if storage == nil {
|
||||
storage = NewMemoryStorage()
|
||||
}
|
||||
|
||||
// Set default values if not specified
|
||||
maxTokens := contextCfg.MaxTokens
|
||||
if maxTokens == 0 {
|
||||
maxTokens = DefaultMaxTokens
|
||||
}
|
||||
|
||||
recentMessageCount := contextCfg.RecentMessageCount
|
||||
if recentMessageCount == 0 {
|
||||
recentMessageCount = DefaultRecentMessageCount
|
||||
}
|
||||
|
||||
summarizeThreshold := contextCfg.SummarizeThreshold
|
||||
if summarizeThreshold == 0 {
|
||||
summarizeThreshold = DefaultSummarizeThreshold
|
||||
}
|
||||
|
||||
compressionStrategy := contextCfg.CompressionStrategy
|
||||
if compressionStrategy == "" {
|
||||
compressionStrategy = DefaultCompressionStrategy
|
||||
}
|
||||
|
||||
// Create compression strategy
|
||||
var strategy interfaces.CompressionStrategy
|
||||
switch compressionStrategy {
|
||||
case "sliding_window":
|
||||
strategy = NewSlidingWindowStrategy(recentMessageCount)
|
||||
case "smart":
|
||||
if chatModel != nil {
|
||||
strategy = NewSmartCompressionStrategy(recentMessageCount, chatModel, summarizeThreshold)
|
||||
} else {
|
||||
logger.Warn(context.TODO(), "Smart compression requested but no chat model provided, falling back to sliding window")
|
||||
strategy = NewSlidingWindowStrategy(recentMessageCount)
|
||||
}
|
||||
default:
|
||||
logger.Warnf(context.TODO(), "Unknown compression strategy '%s', using sliding window", compressionStrategy)
|
||||
strategy = NewSlidingWindowStrategy(recentMessageCount)
|
||||
}
|
||||
|
||||
// Create context manager with storage and strategy
|
||||
return NewContextManager(storage, strategy, maxTokens)
|
||||
return NewContextManager(storage, messageRepo)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user