feat(agent): enhance token management and context handling in AgentEngine

- Added token usage tracking to the AgentEngine, allowing for better estimation of current context token counts.
- Implemented a new method to estimate current tokens based on previous usage and newly appended messages, improving context management.
- Updated context window management to utilize estimated token counts for more efficient message consolidation.
- Enhanced streaming methods to include token usage information, providing better insights into LLM interactions.
- Refactored the token estimator to support BPE tokenization, ensuring accurate token counts for messages.
- Added unit tests to validate new token estimation and context management functionalities.
This commit is contained in:
wizardchen
2026-03-22 23:15:33 +08:00
committed by lyingbug
parent 0d4ceb6c96
commit 0bb1a581de
16 changed files with 247 additions and 239 deletions
+5 -5
View File
@@ -58,14 +58,14 @@ func NewConsolidator(
}
}
// ShouldConsolidate checks if the messages need consolidation based on token count.
func (c *Consolidator) ShouldConsolidate(messages []chat.Message) bool {
if c.maxTokens <= 0 || len(messages) <= 3 {
// ShouldConsolidate checks if consolidation is needed based on the caller-provided
// token estimate. The estimate should come from the model API's Usage when available.
func (c *Consolidator) ShouldConsolidate(currentTokens int) bool {
if c.maxTokens <= 0 {
return false
}
tokenCount := c.estimator.EstimateMessages(messages)
triggerAt := int(float64(c.maxTokens) * c.threshold)
return tokenCount > triggerAt
return currentTokens > triggerAt
}
// Consolidate summarizes older messages and returns a compressed message array.
+13 -19
View File
@@ -9,7 +9,8 @@ import (
)
func TestConsolidator_ShouldConsolidate(t *testing.T) {
est := token.NewEstimator(0)
est, err := token.NewEstimator()
assert.NoError(t, err)
t.Run("below threshold returns false", func(t *testing.T) {
c := NewConsolidator(nil, est, 100000, 0)
@@ -19,27 +20,18 @@ func TestConsolidator_ShouldConsolidate(t *testing.T) {
{Role: "assistant", Content: "hi"},
{Role: "user", Content: "current"},
}
assert.False(t, c.ShouldConsolidate(messages))
tokens := est.EstimateMessages(messages)
assert.False(t, c.ShouldConsolidate(tokens))
})
t.Run("too few messages returns false", func(t *testing.T) {
c := NewConsolidator(nil, est, 10, 0)
messages := []chat.Message{
{Role: "system", Content: "system"},
{Role: "user", Content: "hello"},
}
assert.False(t, c.ShouldConsolidate(messages))
t.Run("over threshold returns true", func(t *testing.T) {
c := NewConsolidator(nil, est, 10, 0) // very low threshold
assert.True(t, c.ShouldConsolidate(100))
})
t.Run("disabled returns false", func(t *testing.T) {
c := NewConsolidator(nil, est, 0, 0) // disabled
messages := []chat.Message{
{Role: "system", Content: "system"},
{Role: "user", Content: "hello"},
{Role: "assistant", Content: "hi"},
{Role: "user", Content: "current"},
}
assert.False(t, c.ShouldConsolidate(messages))
c := NewConsolidator(nil, est, 0, 0)
assert.False(t, c.ShouldConsolidate(99999))
})
}
@@ -61,7 +53,8 @@ func TestTruncateForPrompt(t *testing.T) {
}
func TestRawArchive(t *testing.T) {
est := token.NewEstimator(0)
est, err := token.NewEstimator()
assert.NoError(t, err)
c := NewConsolidator(nil, est, 100000, 0)
messages := []chat.Message{
@@ -83,7 +76,8 @@ func TestRawArchive(t *testing.T) {
}
func TestBuildConsolidationPrompt(t *testing.T) {
est := token.NewEstimator(0)
est, err := token.NewEstimator()
assert.NoError(t, err)
c := NewConsolidator(nil, est, 100000, 0)
messages := []chat.Message{