Refactor out of utils

This commit is contained in:
Saoud Rizwan
2024-09-24 11:45:12 -04:00
parent 7c21a4c833
commit 40f7942801
8 changed files with 7 additions and 11 deletions

View File

@@ -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

View 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
}