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
+4
View File
@@ -293,7 +293,11 @@ func (e *AgentEngine) executeLoop(
// the API-reported usage from the previous round plus a BPE delta
// for newly appended messages (assistant reply + tool results).
currentTokens := e.estimateCurrentTokens(messages)
beforeLen := len(messages)
messages = e.manageContextWindow(ctx, messages, state.CurrentRound+1, currentTokens)
if len(messages) < beforeLen {
currentTokens = e.tokenEstimator.EstimateMessages(messages)
}
logger.Infof(ctx, "[Agent][Round-%d/%d] Starting: %d messages, %d tools, est_tokens=%d",
state.CurrentRound+1, e.config.MaxIterations, len(messages), len(tools), currentTokens)