]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/blob
0b275f9732e4da83540cecf48c7dd11d0db423cc
[pkg/ggml/sources/llama.cpp] /
1 // Meta parser for `read_file` tool calls. Reads the file path and an
2 // optional line range (either `start_line`+`end_line` or
3 // `start_line`+`line_count`). Args are parsed partially so a header
4 // can render incrementally as the file path streams in.
5
6 import { parseToolArgs } from './_shared';
7 import { CODE_BLOCK, FILE_PATH_SEPARATOR_REGEX } from '$lib/constants';
8 import { BuiltInTool } from '$lib/enums';
9 import { type AgenticSection, getFileTypeByExtension } from '$lib/utils';
10
11 export type ReadFileMeta = {
12 fileName: string;
13 lineRange: { start: number; end: number } | null;
14 language: string;
15 };
16
17 export function parseReadFileMeta(section: AgenticSection): ReadFileMeta | null {
18 const args = parseToolArgs(BuiltInTool.READ_FILE, section, { partial: true });
19
20 if (!args) return null;
21
22 const rawPath = args.path ?? args.file_path ?? args.filePath;
23
24 if (typeof rawPath !== 'string' || !rawPath) return null;
25
26 const fileName = rawPath.split(FILE_PATH_SEPARATOR_REGEX).pop() || rawPath;
27 // Models emit range arguments under several aliases. Accept all to
28 // stay forgiving across prompt variations.
29 const startRaw = args.start_line ?? args.line_start ?? args.startLine ?? args.from_line;
30 const endRaw = args.end_line ?? args.line_end ?? args.endLine ?? args.to_line;
31 const countRaw = args.line_count ?? args.count ?? args.num_lines;
32
33 let lineRange: { start: number; end: number } | null = null;
34
35 const sNum = Number(startRaw);
36 const eNum = Number(endRaw);
37
38 if (startRaw != null && endRaw != null && Number.isFinite(sNum) && Number.isFinite(eNum)) {
39 lineRange = { end: eNum, start: sNum };
40 } else if (startRaw != null && countRaw != null) {
41 const cNum = Number(countRaw);
42
43 if (Number.isFinite(sNum) && Number.isFinite(cNum)) {
44 lineRange = { end: sNum + cNum - 1, start: sNum };
45 }
46 }
47
48 const fileType = getFileTypeByExtension(fileName);
49 const language = fileType
50 ? fileType.replace(CODE_BLOCK.TEXT_LANGUAGE_PREFIX_REGEX, '')
51 : CODE_BLOCK.DEFAULT_LANGUAGE;
52
53 return { fileName, language, lineRange };
54 }