Skip to content

Commit 68f7a09

Browse files
committed
refactor: extract convertFileEntries helper function
Extracted the duplicated line_ranges to lineRanges conversion logic into a reusable private helper function convertFileEntries() for better maintainability.
1 parent e5e6dbe commit 68f7a09

File tree

1 file changed

+27
-34
lines changed

1 file changed

+27
-34
lines changed

src/core/assistant-message/NativeToolCallParser.ts

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,31 @@ export class NativeToolCallParser {
286286
return finalToolUse
287287
}
288288

289+
/**
290+
* Convert raw file entries from API (with line_ranges strings) to FileEntry objects
291+
* (with lineRanges objects).
292+
*
293+
* API sends: { path: string, line_ranges: ["1-50", "100-200"] }
294+
* Returns: { path: string, lineRanges: [{ start: 1, end: 50 }, { start: 100, end: 200 }] }
295+
*/
296+
private static convertFileEntries(files: any[]): FileEntry[] {
297+
return files.map((file: any) => {
298+
const entry: FileEntry = { path: file.path }
299+
if (file.line_ranges && Array.isArray(file.line_ranges)) {
300+
entry.lineRanges = file.line_ranges
301+
.map((range: string) => {
302+
const match = String(range).match(/^(\d+)-(\d+)$/)
303+
if (match) {
304+
return { start: parseInt(match[1], 10), end: parseInt(match[2], 10) }
305+
}
306+
return null
307+
})
308+
.filter(Boolean)
309+
}
310+
return entry
311+
})
312+
}
313+
289314
/**
290315
* Create a partial ToolUse from currently parsed arguments.
291316
* Used during streaming to show progress.
@@ -313,23 +338,7 @@ export class NativeToolCallParser {
313338
switch (name) {
314339
case "read_file":
315340
if (partialArgs.files && Array.isArray(partialArgs.files)) {
316-
// Convert line_ranges strings to lineRanges objects
317-
const convertedFiles = partialArgs.files.map((file: any) => {
318-
const entry: any = { path: file.path }
319-
if (file.line_ranges && Array.isArray(file.line_ranges)) {
320-
entry.lineRanges = file.line_ranges
321-
.map((range: string) => {
322-
const match = String(range).match(/^(\d+)-(\d+)$/)
323-
if (match) {
324-
return { start: parseInt(match[1], 10), end: parseInt(match[2], 10) }
325-
}
326-
return null
327-
})
328-
.filter(Boolean)
329-
}
330-
return entry
331-
})
332-
nativeArgs = { files: convertedFiles }
341+
nativeArgs = { files: this.convertFileEntries(partialArgs.files) }
333342
}
334343
break
335344

@@ -574,23 +583,7 @@ export class NativeToolCallParser {
574583
switch (toolCall.name) {
575584
case "read_file":
576585
if (args.files && Array.isArray(args.files)) {
577-
// Convert line_ranges strings to lineRanges objects
578-
const convertedFiles = args.files.map((file: any) => {
579-
const entry: FileEntry = { path: file.path }
580-
if (file.line_ranges && Array.isArray(file.line_ranges)) {
581-
entry.lineRanges = file.line_ranges
582-
.map((range: string) => {
583-
const match = String(range).match(/^(\d+)-(\d+)$/)
584-
if (match) {
585-
return { start: parseInt(match[1], 10), end: parseInt(match[2], 10) }
586-
}
587-
return null
588-
})
589-
.filter(Boolean)
590-
}
591-
return entry
592-
})
593-
nativeArgs = { files: convertedFiles } as NativeArgsFor<TName>
586+
nativeArgs = { files: this.convertFileEntries(args.files) } as NativeArgsFor<TName>
594587
}
595588
break
596589

0 commit comments

Comments
 (0)