1- import fs from "fs/promises"
21import { stat } from "fs/promises"
32
43// Rough approximation: 1 token ≈ 4 characters for English text
@@ -11,6 +10,14 @@ export interface SizeEstimate {
1110 remainingContextSize : number
1211}
1312
13+ /**
14+ * Calculates the maximum allowed size for a single content item (file or terminal output)
15+ * We limit to half the context window to ensure no single item can consume too much context
16+ */
17+ export function calculateMaxAllowedSize ( contextLimit : number ) : number {
18+ return Math . floor ( contextLimit / 2 )
19+ }
20+
1421/**
1522 * Estimates tokens from byte count using a simple character ratio
1623 * This is a rough approximation - actual token count may vary
@@ -19,18 +26,29 @@ export function estimateTokens(bytes: number): number {
1926 return Math . ceil ( bytes / CHARS_PER_TOKEN )
2027}
2128
29+ /**
30+ * Checks if the given byte count would exceed the size limit
31+ * More efficient than creating a buffer just to check size
32+ */
33+ export function wouldExceedSizeLimit ( byteCount : number , contextLimit : number ) : boolean {
34+ const estimatedTokenCount = estimateTokens ( byteCount )
35+ const maxAllowedSize = calculateMaxAllowedSize ( contextLimit )
36+ return estimatedTokenCount >= maxAllowedSize
37+ }
38+
2239/**
2340 * Estimates size metrics for a string or buffer without loading entire content
2441 */
2542export function estimateContentSize ( content : string | Buffer , contextLimit : number , usedContext : number = 0 ) : SizeEstimate {
2643 const bytes = Buffer . isBuffer ( content ) ? content . length : Buffer . from ( content ) . length
2744 const estimatedTokenCount = estimateTokens ( bytes )
2845 const remainingContext = contextLimit - usedContext
46+ const maxAllowedSize = calculateMaxAllowedSize ( contextLimit )
2947
3048 return {
3149 bytes,
3250 estimatedTokens : estimatedTokenCount ,
33- wouldExceedLimit : estimatedTokenCount > remainingContext ,
51+ wouldExceedLimit : estimatedTokenCount >= maxAllowedSize ,
3452 remainingContextSize : remainingContext ,
3553 }
3654}
@@ -43,15 +61,21 @@ export async function estimateFileSize(filePath: string, contextLimit: number, u
4361 const bytes = stats . size
4462 const estimatedTokenCount = estimateTokens ( bytes )
4563 const remainingContext = contextLimit - usedContext
64+ const maxAllowedSize = calculateMaxAllowedSize ( contextLimit )
4665
4766 return {
4867 bytes,
4968 estimatedTokens : estimatedTokenCount ,
50- wouldExceedLimit : estimatedTokenCount > remainingContext ,
69+ wouldExceedLimit : estimatedTokenCount >= maxAllowedSize ,
5170 remainingContextSize : remainingContext ,
5271 }
5372}
5473
74+ /**
75+ * Gets the maximum allowed size for the API context window
76+ * This is different from calculateMaxAllowedSize as it's for the entire context window
77+ * rather than a single content item
78+ */
5579export function getMaxAllowedSize ( contextWindow : number ) : number {
5680 // Get context window and used context from API model
5781 let maxAllowedSize : number
0 commit comments