fix(agent): avoid consecutive system messages in compression
Append emergency compression note to the original system prompt instead of creating a separate system message. Some APIs like Zhipu reject two consecutive system messages.
This commit is contained in:
parent
58b5e21d90
commit
1e96733435
1 changed files with 9 additions and 19 deletions
|
|
@ -779,31 +779,21 @@ func (al *AgentLoop) forceCompression(sessionKey string) {
|
||||||
mid := len(conversation) / 2
|
mid := len(conversation) / 2
|
||||||
|
|
||||||
// New history structure:
|
// New history structure:
|
||||||
// 1. System Prompt
|
// 1. System Prompt (with compression note appended)
|
||||||
// 2. [Summary of dropped part] - synthesized
|
// 2. Second half of conversation
|
||||||
// 3. Second half of conversation
|
// 3. Last message
|
||||||
// 4. Last message
|
|
||||||
|
|
||||||
// Simplified approach for emergency: Drop first half of conversation
|
|
||||||
// and rely on existing summary if present, or create a placeholder.
|
|
||||||
|
|
||||||
droppedCount := mid
|
droppedCount := mid
|
||||||
keptConversation := conversation[mid:]
|
keptConversation := conversation[mid:]
|
||||||
|
|
||||||
newHistory := make([]providers.Message, 0)
|
newHistory := make([]providers.Message, 0)
|
||||||
newHistory = append(newHistory, history[0]) // System prompt
|
|
||||||
|
|
||||||
// Add a note about compression
|
// Append compression note to the original system prompt instead of adding a new system message
|
||||||
compressionNote := fmt.Sprintf("[System: Emergency compression dropped %d oldest messages due to context limit]", droppedCount)
|
// This avoids having two consecutive system messages which some APIs (like Zhipu) reject
|
||||||
// If there was an existing summary, we might lose it if it was in the dropped part (which is just messages).
|
compressionNote := fmt.Sprintf("\n\n[System Note: Emergency compression dropped %d oldest messages due to context limit]", droppedCount)
|
||||||
// The summary is stored separately in session.Summary, so it persists!
|
enhancedSystemPrompt := history[0]
|
||||||
// We just need to ensure the user knows there's a gap.
|
enhancedSystemPrompt.Content = enhancedSystemPrompt.Content + compressionNote
|
||||||
|
newHistory = append(newHistory, enhancedSystemPrompt)
|
||||||
// We only modify the messages list here
|
|
||||||
newHistory = append(newHistory, providers.Message{
|
|
||||||
Role: "system",
|
|
||||||
Content: compressionNote,
|
|
||||||
})
|
|
||||||
|
|
||||||
newHistory = append(newHistory, keptConversation...)
|
newHistory = append(newHistory, keptConversation...)
|
||||||
newHistory = append(newHistory, history[len(history)-1]) // Last message
|
newHistory = append(newHistory, history[len(history)-1]) // Last message
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue