mirror of
https://github.com/pacnpal/Roo-Code.git
synced 2025-12-22 05:11:06 -05:00
* Chore: Pretier for consistant formatting - TODO: This PR needs to be updated by Saoud after he runs `npm install` & `npm format:fix` and commits the results of the prettier changes. * Revert prettier config * Run npm install * Fix prettier config and ignore package lock * Run format --------- Co-authored-by: Saoud Rizwan <7799382+saoudrizwan@users.noreply.github.com>
27 lines
1.3 KiB
TypeScript
27 lines
1.3 KiB
TypeScript
import { Anthropic } from "@anthropic-ai/sdk"
|
|
|
|
/*
|
|
We can't implement a dynamically updating sliding window as it would break prompt cache
|
|
every time. To maintain the benefits of caching, we need to keep conversation history
|
|
static. This operation should be performed as infrequently as possible. If a user reaches
|
|
a 200k context, we can assume that the first half is likely irrelevant to their current task.
|
|
Therefore, this function should only be called when absolutely necessary to fit within
|
|
context limits, not as a continuous process.
|
|
*/
|
|
export function truncateHalfConversation(
|
|
messages: Anthropic.Messages.MessageParam[],
|
|
): Anthropic.Messages.MessageParam[] {
|
|
// 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.
|
|
|
|
// Always keep the first Task message (this includes the project's file structure in environment_details)
|
|
const truncatedMessages = [messages[0]]
|
|
|
|
// Remove half of user-assistant pairs
|
|
const messagesToRemove = Math.floor(messages.length / 4) * 2 // has to be even number
|
|
|
|
const remainingMessages = messages.slice(messagesToRemove + 1) // has to start with assistant message since tool result cannot follow assistant message with no tool use
|
|
truncatedMessages.push(...remainingMessages)
|
|
|
|
return truncatedMessages
|
|
}
|