Skip to content

Commit 63dac8c

Browse files
committed
Fix getReadFileToolDescription for native files
1 parent 225aa45 commit 63dac8c

File tree

2 files changed

+57
-24
lines changed

2 files changed

+57
-24
lines changed

src/core/tools/kilocode.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,53 @@ export async function blockFileReadWhenTooLarge(task: Task, relPath: string, con
4949
xmlContent: `<file><path>${relPath}</path><error>${errorMsg}</error></file>`,
5050
}
5151
}
52+
53+
type FileEntry = {
54+
path?: string
55+
lineRanges?: {
56+
start: number
57+
end: number
58+
}[]
59+
}
60+
61+
export function parseNativeFiles(nativeFiles: { path?: string; line_ranges?: string[] }[]) {
62+
const fileEntries = new Array<FileEntry>()
63+
for (const file of nativeFiles) {
64+
if (!file.path) continue
65+
66+
const fileEntry: FileEntry = {
67+
path: file.path,
68+
lineRanges: [],
69+
}
70+
71+
// Handle line_ranges array from native format
72+
if (file.line_ranges && Array.isArray(file.line_ranges)) {
73+
for (const range of file.line_ranges) {
74+
const match = String(range).match(/(\d+)-(\d+)/)
75+
if (match) {
76+
const [, start, end] = match.map(Number)
77+
if (!isNaN(start) && !isNaN(end)) {
78+
fileEntry.lineRanges?.push({ start, end })
79+
}
80+
}
81+
}
82+
}
83+
fileEntries.push(fileEntry)
84+
}
85+
return fileEntries
86+
}
87+
88+
export function getNativeReadFileToolDescription(blockName: string, files: FileEntry[]) {
89+
const paths = files.map((file) => file.path)
90+
if (paths.length === 0) {
91+
return `[${blockName} with no valid paths]`
92+
} else if (paths.length === 1) {
93+
// Modified part for single file
94+
return `[${blockName} for '${paths[0]}'. Reading multiple files at once is more efficient for the LLM. If other files are relevant to your current task, please read them simultaneously.]`
95+
} else if (paths.length <= 3) {
96+
const pathList = paths.map((p) => `'${p}'`).join(", ")
97+
return `[${blockName} for ${pathList}]`
98+
} else {
99+
return `[${blockName} for ${paths.length} files]`
100+
}
101+
}

src/core/tools/readFileTool.ts

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { readLines } from "../../integrations/misc/read-lines"
1414
import { extractTextFromFile, addLineNumbers, getSupportedBinaryFormats } from "../../integrations/misc/extract-text"
1515
import { parseSourceCodeDefinitionsForFile } from "../../services/tree-sitter"
1616
import { parseXml } from "../../utils/xml"
17-
import { blockFileReadWhenTooLarge } from "./kilocode"
17+
import { blockFileReadWhenTooLarge, getNativeReadFileToolDescription, parseNativeFiles } from "./kilocode"
1818
import {
1919
DEFAULT_MAX_IMAGE_FILE_SIZE_MB,
2020
DEFAULT_MAX_TOTAL_IMAGE_SIZE_MB,
@@ -26,7 +26,11 @@ import {
2626

2727
export function getReadFileToolDescription(blockName: string, blockParams: any): string {
2828
// Handle both single path and multiple files via args
29-
if (blockParams.args) {
29+
// kilocode_change start
30+
if (blockParams.files && Array.isArray(blockParams.files)) {
31+
return getNativeReadFileToolDescription(blockName, parseNativeFiles(blockParams.files))
32+
// kilocode_change end
33+
} else if (blockParams.args) {
3034
try {
3135
const parsed = parseXml(blockParams.args) as any
3236
const files = Array.isArray(parsed.file) ? parsed.file : [parsed.file].filter(Boolean)
@@ -131,28 +135,7 @@ export async function readFileTool(
131135
// kilocode_change start
132136
// Handle native JSON format first (from OpenAI-style tool calls)
133137
if (nativeFiles && Array.isArray(nativeFiles)) {
134-
for (const file of nativeFiles) {
135-
if (!file.path) continue
136-
137-
const fileEntry: FileEntry = {
138-
path: file.path,
139-
lineRanges: [],
140-
}
141-
142-
// Handle line_ranges array from native format
143-
if (file.line_ranges && Array.isArray(file.line_ranges)) {
144-
for (const range of file.line_ranges) {
145-
const match = String(range).match(/(\d+)-(\d+)/)
146-
if (match) {
147-
const [, start, end] = match.map(Number)
148-
if (!isNaN(start) && !isNaN(end)) {
149-
fileEntry.lineRanges?.push({ start, end })
150-
}
151-
}
152-
}
153-
}
154-
fileEntries.push(fileEntry)
155-
}
138+
fileEntries.push(...parseNativeFiles(nativeFiles))
156139
// kilocode_change end
157140
} else if (argsXmlTag) {
158141
// Parse file entries from XML (new multi-file format)

0 commit comments

Comments
 (0)