From 8313fa099c19845aa2bcbdbd1ff69719172ce440 Mon Sep 17 00:00:00 2001 From: ocasta181 Date: Tue, 25 Feb 2025 12:14:15 -0800 Subject: [PATCH] reduce complexity, get working --- src/core/Cline.ts | 42 +++++--------------- src/integrations/misc/extract-text.test.ts | 6 ++- src/integrations/misc/extract-text.ts | 13 ++++-- src/integrations/terminal/TerminalProcess.ts | 23 +++++++---- src/utils/content-size.test.ts | 30 ++++++++++++-- src/utils/content-size.ts | 30 ++++++++++++-- 6 files changed, 91 insertions(+), 53 deletions(-) diff --git a/src/core/Cline.ts b/src/core/Cline.ts index 9fc08e92..44b7e3fc 100644 --- a/src/core/Cline.ts +++ b/src/core/Cline.ts @@ -1166,10 +1166,8 @@ export class Cline { // Tools - async executeCommandTool(command: string): Promise<[boolean, ToolResponse]> { - const contextWindow = this.api.getModel().info.contextWindow || 128_000 - const maxAllowedSize = getMaxAllowedSize(contextWindow) - const usedContext = this.apiConversationHistory.reduce((total, msg) => { + private calculateUsedContext(): number { + return this.apiConversationHistory.reduce((total, msg) => { if (Array.isArray(msg.content)) { return ( total + @@ -1183,6 +1181,12 @@ export class Cline { } return total + (typeof msg.content === "string" ? msg.content.length / 4 : 0) }, 0) + } + + async executeCommandTool(command: string): Promise<[boolean, ToolResponse]> { + const contextWindow = this.api.getModel().info.contextWindow || 128_000 + const maxAllowedSize = getMaxAllowedSize(contextWindow) + const usedContext = this.calculateUsedContext() const terminalInfo = await this.terminalManager.getOrCreateTerminal(cwd) terminalInfo.terminal.show() // weird visual bug when creating new terminals (even manually) where there's an empty space at the top. @@ -1359,20 +1363,7 @@ export class Cline { if (this.api instanceof OpenAiHandler && this.api.getModel().id.toLowerCase().includes("deepseek")) { contextWindow = 64_000 } - let maxAllowedSize: number - switch (contextWindow) { - case 64_000: // deepseek models - maxAllowedSize = contextWindow - 27_000 - break - case 128_000: // most models - maxAllowedSize = contextWindow - 30_000 - break - case 200_000: // claude models - maxAllowedSize = contextWindow - 40_000 - break - default: - maxAllowedSize = Math.max(contextWindow - 40_000, contextWindow * 0.8) // for deepseek, 80% of 64k meant only ~10k buffer which was too small and resulted in users getting context window errors. - } + const maxAllowedSize = getMaxAllowedSize(contextWindow) // This is the most reliable way to know when we're close to hitting the context window. if (totalTokens >= maxAllowedSize) { @@ -2017,20 +2008,7 @@ export class Cline { const maxAllowedSize = getMaxAllowedSize(contextWindow) // Calculate used context from current conversation - const usedContext = this.apiConversationHistory.reduce((total, msg) => { - if (Array.isArray(msg.content)) { - return ( - total + - msg.content.reduce((acc, block) => { - if (block.type === "text") { - return acc + block.text.length / 4 // Rough estimate of tokens - } - return acc - }, 0) - ) - } - return total + (typeof msg.content === "string" ? msg.content.length / 4 : 0) - }, 0) + const usedContext = this.calculateUsedContext() // now execute the tool like normal const content = await extractTextFromFile(absolutePath, maxAllowedSize, usedContext) diff --git a/src/integrations/misc/extract-text.test.ts b/src/integrations/misc/extract-text.test.ts index f3798cb6..9cf94aac 100644 --- a/src/integrations/misc/extract-text.test.ts +++ b/src/integrations/misc/extract-text.test.ts @@ -4,6 +4,7 @@ import fs from "fs/promises" import path from "path" import os from "os" import { ContentTooLargeError } from "../../shared/errors" +import { calculateMaxAllowedSize } from "../../utils/content-size" const CONTEXT_LIMIT = 1000 const USED_CONTEXT = 200 @@ -29,8 +30,9 @@ describe("extract-text", () => { } }) - it("throws ContentTooLargeError when file would exceed limit", async () => { - const largeContent = "x".repeat(3000) // 3000 bytes = ~750 tokens + it("throws ContentTooLargeError when file would exceed half of context limit", async () => { + const halfContextLimit = calculateMaxAllowedSize(CONTEXT_LIMIT) // 500 tokens + const largeContent = "x".repeat(halfContextLimit * 4 + 4) // Just over half context limit in tokens await fs.writeFile(tempFilePath, largeContent) try { diff --git a/src/integrations/misc/extract-text.ts b/src/integrations/misc/extract-text.ts index 81b66ca8..807e7e7c 100644 --- a/src/integrations/misc/extract-text.ts +++ b/src/integrations/misc/extract-text.ts @@ -4,7 +4,7 @@ import pdf from "pdf-parse/lib/pdf-parse" import mammoth from "mammoth" import fs from "fs/promises" import { isBinaryFile } from "isbinaryfile" -import { estimateFileSize } from "../../utils/content-size" +import { estimateFileSize, wouldExceedSizeLimit } from "../../utils/content-size" import { ContentTooLargeError } from "../../shared/errors" export async function extractTextFromFile(filePath: string, contextLimit: number, usedContext: number = 0): Promise { @@ -14,9 +14,14 @@ export async function extractTextFromFile(filePath: string, contextLimit: number throw new Error(`File not found: ${filePath}`) } - // Check file size before attempting to read - const sizeEstimate = await estimateFileSize(filePath, contextLimit, usedContext) - if (sizeEstimate.wouldExceedLimit) { + // Get file stats to check size + const stats = await fs.stat(filePath) + + // Check if file size would exceed limit before attempting to read + // This is more efficient than creating a full SizeEstimate object when we just need a boolean check + if (wouldExceedSizeLimit(stats.size, contextLimit)) { + // Only create the full size estimate when we need it for the error + const sizeEstimate = await estimateFileSize(filePath, contextLimit, usedContext) throw new ContentTooLargeError({ type: "file", path: filePath, diff --git a/src/integrations/terminal/TerminalProcess.ts b/src/integrations/terminal/TerminalProcess.ts index fa2997ae..b40fab48 100644 --- a/src/integrations/terminal/TerminalProcess.ts +++ b/src/integrations/terminal/TerminalProcess.ts @@ -1,8 +1,8 @@ import { EventEmitter } from "events" -import * as stripAnsi from "strip-ansi" +import stripAnsi from "strip-ansi" import * as vscode from "vscode" import { ContentTooLargeError } from "../../shared/errors" -import { estimateContentSize } from "../../utils/content-size" +import { estimateContentSize, wouldExceedSizeLimit } from "../../utils/content-size" export interface TerminalProcessEvents { line: [line: string] @@ -52,9 +52,15 @@ export class TerminalProcess extends EventEmitter { const dataBytes = Buffer.from(data).length this.totalBytes += dataBytes - // Check total accumulated size - const sizeEstimate = estimateContentSize(Buffer.alloc(this.totalBytes), this.contextLimit, this.usedContext) - if (sizeEstimate.wouldExceedLimit) { + // Check total accumulated size against half of context limit + // Use wouldExceedSizeLimit to avoid creating unnecessary buffer + if (wouldExceedSizeLimit(this.totalBytes, this.contextLimit)) { + // Create size estimate only when needed for error details + const sizeEstimate = estimateContentSize( + Buffer.alloc(0, this.totalBytes), + this.contextLimit, + this.usedContext, + ) this.emit( "error", new ContentTooLargeError({ @@ -215,10 +221,11 @@ export class TerminalProcess extends EventEmitter { // Inspired by https://github.com/sindresorhus/execa/blob/main/lib/transform/split.js private emitIfEol(chunk: string) { - // Check size before adding to buffer + // Check size before adding to buffer against half of context limit const newBufferSize = this.buffer.length + chunk.length - const sizeEstimate = estimateContentSize(Buffer.alloc(newBufferSize), this.contextLimit, this.usedContext) - if (sizeEstimate.wouldExceedLimit) { + if (wouldExceedSizeLimit(newBufferSize, this.contextLimit)) { + // Create size estimate only when needed for error details + const sizeEstimate = estimateContentSize(Buffer.alloc(0, newBufferSize), this.contextLimit, this.usedContext) this.emit( "error", new ContentTooLargeError({ diff --git a/src/utils/content-size.test.ts b/src/utils/content-size.test.ts index d59510a6..5795f0c5 100644 --- a/src/utils/content-size.test.ts +++ b/src/utils/content-size.test.ts @@ -1,5 +1,11 @@ import { expect } from "chai" -import { estimateContentSize, estimateFileSize, estimateTokens } from "./content-size" +import { + estimateContentSize, + estimateFileSize, + estimateTokens, + calculateMaxAllowedSize, + wouldExceedSizeLimit, +} from "./content-size" import fs from "fs/promises" import path from "path" import os from "os" @@ -8,6 +14,13 @@ const CONTEXT_LIMIT = 1000 const USED_CONTEXT = 200 describe("content-size", () => { + describe("calculateMaxAllowedSize", () => { + it("calculates half of the context limit", () => { + expect(calculateMaxAllowedSize(1000)).to.equal(500) + expect(calculateMaxAllowedSize(128000)).to.equal(64000) + }) + }) + describe("estimateTokens", () => { it("estimates tokens based on byte count", () => { expect(estimateTokens(100)).to.equal(25) // 100 bytes / 4 chars per token = 25 tokens @@ -15,6 +28,14 @@ describe("content-size", () => { }) }) + describe("wouldExceedSizeLimit", () => { + it("checks if byte count would exceed half of context limit", () => { + expect(wouldExceedSizeLimit(100, 1000)).to.equal(false) // 25 tokens < 500 tokens + expect(wouldExceedSizeLimit(2000, 1000)).to.equal(true) // 500 tokens = 500 tokens (equal is considered exceeding) + expect(wouldExceedSizeLimit(2004, 1000)).to.equal(true) // 501 tokens > 500 tokens + }) + }) + describe("estimateContentSize", () => { it("estimates size for string content", () => { const content = "Hello world" // 11 bytes @@ -36,12 +57,13 @@ describe("content-size", () => { expect(result.wouldExceedLimit).to.equal(false) }) - it("detects when content would exceed limit", () => { - const largeContent = "x".repeat(3000) // 3000 bytes = ~750 tokens + it("detects when content would exceed half of context limit", () => { + const halfContextLimit = calculateMaxAllowedSize(CONTEXT_LIMIT) // 500 tokens + const largeContent = "x".repeat(halfContextLimit * 4 + 4) // Just over half context limit in tokens const result = estimateContentSize(largeContent, CONTEXT_LIMIT, USED_CONTEXT) expect(result.wouldExceedLimit).to.equal(true) - expect(result.remainingContextSize).to.equal(800) + expect(result.remainingContextSize).to.equal(800) // This is still contextLimit - usedContext }) }) diff --git a/src/utils/content-size.ts b/src/utils/content-size.ts index f420045d..f07925a2 100644 --- a/src/utils/content-size.ts +++ b/src/utils/content-size.ts @@ -1,4 +1,3 @@ -import fs from "fs/promises" import { stat } from "fs/promises" // Rough approximation: 1 token ≈ 4 characters for English text @@ -11,6 +10,14 @@ export interface SizeEstimate { remainingContextSize: number } +/** + * Calculates the maximum allowed size for a single content item (file or terminal output) + * We limit to half the context window to ensure no single item can consume too much context + */ +export function calculateMaxAllowedSize(contextLimit: number): number { + return Math.floor(contextLimit / 2) +} + /** * Estimates tokens from byte count using a simple character ratio * This is a rough approximation - actual token count may vary @@ -19,6 +26,16 @@ export function estimateTokens(bytes: number): number { return Math.ceil(bytes / CHARS_PER_TOKEN) } +/** + * Checks if the given byte count would exceed the size limit + * More efficient than creating a buffer just to check size + */ +export function wouldExceedSizeLimit(byteCount: number, contextLimit: number): boolean { + const estimatedTokenCount = estimateTokens(byteCount) + const maxAllowedSize = calculateMaxAllowedSize(contextLimit) + return estimatedTokenCount >= maxAllowedSize +} + /** * Estimates size metrics for a string or buffer without loading entire content */ @@ -26,11 +43,12 @@ export function estimateContentSize(content: string | Buffer, contextLimit: numb const bytes = Buffer.isBuffer(content) ? content.length : Buffer.from(content).length const estimatedTokenCount = estimateTokens(bytes) const remainingContext = contextLimit - usedContext + const maxAllowedSize = calculateMaxAllowedSize(contextLimit) return { bytes, estimatedTokens: estimatedTokenCount, - wouldExceedLimit: estimatedTokenCount > remainingContext, + wouldExceedLimit: estimatedTokenCount >= maxAllowedSize, remainingContextSize: remainingContext, } } @@ -43,15 +61,21 @@ export async function estimateFileSize(filePath: string, contextLimit: number, u const bytes = stats.size const estimatedTokenCount = estimateTokens(bytes) const remainingContext = contextLimit - usedContext + const maxAllowedSize = calculateMaxAllowedSize(contextLimit) return { bytes, estimatedTokens: estimatedTokenCount, - wouldExceedLimit: estimatedTokenCount > remainingContext, + wouldExceedLimit: estimatedTokenCount >= maxAllowedSize, remainingContextSize: remainingContext, } } +/** + * Gets the maximum allowed size for the API context window + * This is different from calculateMaxAllowedSize as it's for the entire context window + * rather than a single content item + */ export function getMaxAllowedSize(contextWindow: number): number { // Get context window and used context from API model let maxAllowedSize: number