Skip to content

Commit 5720b6b

Browse files
authored
Tweaks to file read auto-truncate (#1934)
* Tweaks to file read auto-truncate * Change to use a text input * Changeset
1 parent 29d7846 commit 5720b6b

File tree

21 files changed

+228
-56
lines changed

21 files changed

+228
-56
lines changed

.changeset/old-llamas-fold.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"roo-cline": patch
3+
---
4+
5+
Auto-truncate

src/core/Cline.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,6 +2326,7 @@ export class Cline extends EventEmitter<ClineEvents> {
23262326
let sourceCodeDef = ""
23272327

23282328
const isBinary = await isBinaryFile(absolutePath).catch(() => false)
2329+
const autoTruncate = block.params.auto_truncate === "true"
23292330

23302331
if (isRangeRead) {
23312332
if (startLine === undefined) {
@@ -2336,7 +2337,7 @@ export class Cline extends EventEmitter<ClineEvents> {
23362337
startLine + 1,
23372338
)
23382339
}
2339-
} else if (!isBinary && totalLines > maxReadFileLine) {
2340+
} else if (autoTruncate && !isBinary && totalLines > maxReadFileLine) {
23402341
// If file is too large, only read the first maxReadFileLine lines
23412342
isFileTruncated = true
23422343

@@ -2357,7 +2358,7 @@ export class Cline extends EventEmitter<ClineEvents> {
23572358

23582359
// Add truncation notice if applicable
23592360
if (isFileTruncated) {
2360-
content += `\n\n[File truncated: showing ${maxReadFileLine} of ${totalLines} total lines. Use start_line and end_line if you need to read more.].${sourceCodeDef}`
2361+
content += `\n\n[File truncated: showing ${maxReadFileLine} of ${totalLines} total lines. Use start_line and end_line or set auto_truncate to false if you need to read more.].${sourceCodeDef}`
23612362
}
23622363

23632364
pushToolResult(content)

src/core/assistant-message/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export const toolParamNames = [
5151
"diff",
5252
"start_line",
5353
"end_line",
54+
"auto_truncate",
5455
"mode_slug",
5556
"reason",
5657
"operations",

src/core/prompts/__tests__/__snapshots__/system.test.ts.snap

Lines changed: 150 additions & 15 deletions
Large diffs are not rendered by default.

src/core/prompts/tools/read-file.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ Parameters:
77
- path: (required) The path of the file to read (relative to the current working directory ${args.cwd})
88
- start_line: (optional) The starting line number to read from (1-based). If not provided, it starts from the beginning of the file.
99
- end_line: (optional) The ending line number to read to (1-based, inclusive). If not provided, it reads to the end of the file.
10+
- auto_truncate: (optional) Whether to automatically truncate large files when start_line and end_line are not specified. If true and the file exceeds a certain line threshold, it will: a) return only a subset of lines to save tokens, b) include information about the total line count, and c) provide a summary of method definitions with their line ranges. You should set this to true unless you've been explicitly asked to read an entire large file at once, as this prevents context bloat that can lead to truncated responses. For backwards compatibility, it defaults to false when omitted.
1011
Usage:
1112
<read_file>
1213
<path>File path here</path>
1314
<start_line>Starting line number (optional)</start_line>
1415
<end_line>Ending line number (optional)</end_line>
16+
<auto_truncate>true or false (optional)</auto_truncate>
1517
</read_file>
1618
1719
Examples:
@@ -40,6 +42,13 @@ Examples:
4042
<start_line>46</start_line>
4143
<end_line>68</end_line>
4244
</read_file>
45+
Note: When both start_line and end_line are provided, this tool efficiently streams only the requested lines, making it suitable for processing large files like logs, CSV files, and other large datasets without memory issues.
4346
44-
Note: When both start_line and end_line are provided, this tool efficiently streams only the requested lines, making it suitable for processing large files like logs, CSV files, and other large datasets without memory issues.`
47+
5. Reading a large file with automatic truncation:
48+
<read_file>
49+
<path>src/large-module.ts</path>
50+
<auto_truncate>true</auto_truncate>
51+
</read_file>
52+
53+
This will return a truncated version of the file with information about total line count and method definitions, helping to prevent context size issues with very large files.`
4554
}

webview-ui/src/components/settings/ContextManagementSettings.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,24 @@ export const ContextManagementSettings = ({
9696
<div className="flex flex-col gap-2">
9797
<span className="font-medium">{t("settings:contextManagement.maxReadFile.label")}</span>
9898
<div className="flex items-center gap-2">
99-
<Slider
99+
<input
100+
type="number"
101+
className="w-24 bg-vscode-input-background text-vscode-input-foreground border border-vscode-input-border px-2 py-1 rounded text-right [appearance:textfield] [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none"
102+
value={maxReadFileLine ?? 500}
100103
min={0}
101-
max={2000}
102-
step={10}
103-
value={[maxReadFileLine ?? 450]}
104-
onValueChange={([value]) => setCachedStateField("maxReadFileLine", value)}
105-
data-testid="max-read-file-line-slider"
104+
onChange={(e) => {
105+
const newValue = parseInt(e.target.value, 10)
106+
if (!isNaN(newValue) && newValue >= 0) {
107+
setCachedStateField("maxReadFileLine", newValue)
108+
}
109+
}}
110+
onClick={(e) => e.currentTarget.select()}
111+
data-testid="max-read-file-line-input"
106112
/>
107-
<span className="w-10">{maxReadFileLine ?? 450}</span>
113+
<span>{t("settings:contextManagement.maxReadFile.lines")}</span>
108114
</div>
109115
</div>
110-
<div className="text-vscode-descriptionForeground text-sm mt-0">
116+
<div className="text-vscode-descriptionForeground text-sm mt-2">
111117
{t("settings:contextManagement.maxReadFile.description")}
112118
</div>
113119
</div>

webview-ui/src/i18n/locales/ca/settings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,9 @@
267267
"description": "Quan està habilitat, els fitxers que coincideixen amb els patrons a .rooignore es mostraran en llistes amb un símbol de cadenat. Quan està deshabilitat, aquests fitxers s'ocultaran completament de les llistes de fitxers i cerques."
268268
},
269269
"maxReadFile": {
270-
"label": "Nombre màxim de línies per llegir d'un fitxer",
271-
"description": "Nombre màxim de línies per llegir d'un fitxer a la vegada. Valors més baixos redueixen l'ús de context/recursos però poden requerir més lectures per a fitxers grans."
270+
"label": "Llindar d'auto-truncament de lectura de fitxers",
271+
"description": "El nombre predeterminat de línies per llegir d'un fitxer en un lot. Valors més baixos redueixen l'ús de context/recursos però poden requerir més lectures per a fitxers grans.",
272+
"lines": "línies"
272273
}
273274
},
274275
"terminal": {

webview-ui/src/i18n/locales/de/settings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,9 @@
267267
"description": "Wenn aktiviert, werden Dateien, die mit Mustern in .rooignore übereinstimmen, in Listen mit einem Schlosssymbol angezeigt. Wenn deaktiviert, werden diese Dateien vollständig aus Dateilisten und Suchen ausgeblendet."
268268
},
269269
"maxReadFile": {
270-
"label": "Maximale Anzahl an Zeilen, die aus einer Datei gelesen werden",
271-
"description": "Maximale Anzahl an Zeilen, die auf einmal aus einer Datei gelesen werden. Niedrigere Werte reduzieren den Kontext-/Ressourcenverbrauch, können aber mehr Lesevorgänge für große Dateien erfordern."
270+
"label": "Schwellenwert für automatische Dateilesekürzung",
271+
"description": "Die Standardanzahl an Zeilen, die in einem Durchgang aus einer Datei gelesen werden. Niedrigere Werte reduzieren den Kontext-/Ressourcenverbrauch, können aber mehr Lesevorgänge für große Dateien erfordern.",
272+
"lines": "Zeilen"
272273
}
273274
},
274275
"terminal": {

webview-ui/src/i18n/locales/en/settings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,9 @@
267267
"description": "When enabled, files matching patterns in .rooignore will be shown in lists with a lock symbol. When disabled, these files will be completely hidden from file lists and searches."
268268
},
269269
"maxReadFile": {
270-
"label": "Maximum lines to read from a file",
271-
"description": "Maximum number of lines to read from a file at once. Lower values reduce context/resource usage but may require more reads for large files."
270+
"label": "File read auto-truncate threshold",
271+
"description": "The default number of lines to read from a file in one batch. Lower values reduce context/resource usage but may require more reads for large files.",
272+
"lines": "lines"
272273
}
273274
},
274275
"terminal": {

webview-ui/src/i18n/locales/es/settings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,9 @@
267267
"description": "Cuando está habilitado, los archivos que coinciden con los patrones en .rooignore se mostrarán en listas con un símbolo de candado. Cuando está deshabilitado, estos archivos se ocultarán completamente de las listas de archivos y búsquedas."
268268
},
269269
"maxReadFile": {
270-
"label": "Número máximo de líneas para leer de un archivo",
271-
"description": "Número máximo de líneas para leer de un archivo a la vez. Valores más bajos reducen el uso de contexto/recursos pero pueden requerir más lecturas para archivos grandes."
270+
"label": "Umbral de auto-truncado de lectura de archivos",
271+
"description": "El número predeterminado de líneas para leer de un archivo en un lote. Valores más bajos reducen el uso de contexto/recursos pero pueden requerir más lecturas para archivos grandes.",
272+
"lines": "líneas"
272273
}
273274
},
274275
"terminal": {

0 commit comments

Comments
 (0)