You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
refactor: use file size instead of line count for large file detection
- Replace line count threshold with file size threshold (100KB)
- Files larger than 100KB trigger token count check
- Files larger than 1MB automatically apply safeguard if token counting fails
- Update tests to reflect new file size-based approach
- This better handles files with large amounts of content on single lines
As requested by @cte in PR comment
safeguardNotice=`<notice>This file contains ${totalLines} lines and approximately ${tokenCount.toLocaleString()} tokens, which could consume a significant portion of the context window. Showing only the first ${FALLBACK_MAX_LINES} lines to preserve context space. Use line_range if you need to read specific sections.</notice>\n`
545
-
}
546
-
}catch(error){
547
-
// If token counting fails, apply safeguard based on line count alone
548
-
console.warn(`Failed to count tokens for large file ${relPath}:`,error)
549
-
if(totalLines>LARGE_FILE_LINE_THRESHOLD*5){
550
-
// For very large files (>50000 lines), apply safeguard anyway
551
-
shouldApplySafeguard=true
552
-
linesToRead=FALLBACK_MAX_LINES
553
-
safeguardNotice=`<notice>This file contains ${totalLines} lines, which could consume a significant portion of the context window. Showing only the first ${FALLBACK_MAX_LINES} lines to preserve context space. Use line_range if you need to read specific sections.</notice>\n`
536
+
if(maxReadFileLine===-1){
537
+
// Get file size
538
+
constfileStats=awaitstat(fullPath)
539
+
constfileSizeKB=Math.round(fileStats.size/1024)
540
+
541
+
if(fileStats.size>LARGE_FILE_SIZE_THRESHOLD){
542
+
// File is large enough to warrant token count check
safeguardNotice=`<notice>This file is ${fileSizeKB}KB and contains approximately ${tokenCount.toLocaleString()} tokens, which could consume a significant portion of the context window. Showing only the first ${FALLBACK_MAX_LINES} lines to preserve context space. Use line_range if you need to read specific sections.</notice>\n`
551
+
}
552
+
}catch(error){
553
+
// If token counting fails, apply safeguard based on file size alone
554
+
console.warn(`Failed to count tokens for large file ${relPath}:`,error)
555
+
if(fileStats.size>VERY_LARGE_FILE_SIZE){
556
+
// For very large files (>1MB), apply safeguard anyway
557
+
shouldApplySafeguard=true
558
+
linesToRead=FALLBACK_MAX_LINES
559
+
safeguardNotice=`<notice>This file is ${fileSizeKB}KB, which could consume a significant portion of the context window. Showing only the first ${FALLBACK_MAX_LINES} lines to preserve context space. Use line_range if you need to read specific sections.</notice>\n`
0 commit comments