mirror of
https://github.com/pacnpal/Roo-Code.git
synced 2025-12-21 21:01:06 -05:00
Refactor out of utils
This commit is contained in:
@@ -23,14 +23,15 @@ import { getApiMetrics } from "../shared/getApiMetrics"
|
||||
import { HistoryItem } from "../shared/HistoryItem"
|
||||
import { Tool, ToolName } from "../shared/Tool"
|
||||
import { ClaudeAskResponse } from "../shared/WebviewMessage"
|
||||
import { findLast, findLastIndex, formatContentBlockToMarkdown } from "../utils"
|
||||
import { truncateHalfConversation } from "../utils/context-management"
|
||||
import { findLast, findLastIndex } from "../utils/array"
|
||||
import { formatContentBlockToMarkdown } from "../integrations/misc/export-markdown"
|
||||
import { truncateHalfConversation } from "./sliding-window"
|
||||
import { extractTextFromFile } from "../integrations/misc/extract-text"
|
||||
import { regexSearchFiles } from "../services/ripgrep"
|
||||
import { parseMentions } from "./mentions/context-mentions"
|
||||
import { UrlContentFetcher } from "../services/browser/UrlContentFetcher"
|
||||
import { diagnosticsToProblemsString, getNewDiagnostics } from "../integrations/diagnostics"
|
||||
import { arePathsEqual } from "../utils/path-helpers"
|
||||
import { arePathsEqual } from "../utils/path"
|
||||
|
||||
const SYSTEM_PROMPT = async (
|
||||
supportsImages: boolean
|
||||
|
||||
26
src/core/sliding-window/index.ts
Normal file
26
src/core/sliding-window/index.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user