You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// If the previous API request's total token usage is close to the context window, truncate the conversation history to free up space for the new request
// API expects messages to be in user-assistant order, and tool use messages must be followed by tool results. We need to maintain this structure while truncating.
15
-
16
-
// Always keep the first Task message (this includes the project's file structure in environment_details)
17
22
consttruncatedMessages=[messages[0]]
18
-
19
-
// Remove half of user-assistant pairs
20
-
constmessagesToRemove=Math.floor(messages.length/4)*2// has to be even number
21
-
22
-
constremainingMessages=messages.slice(messagesToRemove+1)// has to start with assistant message since tool result cannot follow assistant message with no tool use
0 commit comments