mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-02-18 00:17:39 +01:00
fix(ai): skip orphaned tool calls when pruning messages
When pruning older messages to fit context limits, we may cut off a user message that preceded an assistant message with tool calls. This leaves an orphaned tool call sequence at the start. Extend pruneMessagesForModel to: - Skip leading assistant messages with tool calls - Also skip their following tool results - Ensures clean message sequence for all providers
This commit is contained in:
@@ -1251,10 +1251,23 @@ func pruneMessagesForModel(messages []Message) []Message {
|
||||
|
||||
start := len(messages) - MaxContextMessagesLimit
|
||||
pruned := messages[start:]
|
||||
|
||||
// Skip leading tool results (orphaned from pruned tool calls)
|
||||
for len(pruned) > 0 && pruned[0].ToolResult != nil {
|
||||
pruned = pruned[1:]
|
||||
}
|
||||
|
||||
// If we start with an assistant message that has tool calls,
|
||||
// skip it and its following tool results — we've pruned the
|
||||
// user message that preceded it, so the sequence is broken.
|
||||
for len(pruned) > 0 && pruned[0].Role == "assistant" && len(pruned[0].ToolCalls) > 0 {
|
||||
pruned = pruned[1:]
|
||||
// Also skip the tool results that followed
|
||||
for len(pruned) > 0 && pruned[0].ToolResult != nil {
|
||||
pruned = pruned[1:]
|
||||
}
|
||||
}
|
||||
|
||||
return pruned
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user