]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/blob
cbde40df46c9d39947b1eaee745597fc16411f85
[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 {
8 DEFAULT_LANGUAGE,
9 FILE_PATH_SEPARATOR_REGEX,
10 TEXT_LANGUAGE_PREFIX_REGEX
11 } from '$lib/constants';
12 import { BuiltInTool } from '$lib/enums';
13 import { type AgenticSection, getFileTypeByExtension } from '$lib/utils';
14
15 export type ReadFileMeta = {
16 fileName: string;
17 lineRange: { start: number; end: number } | null;
18 language: string;
19 };
20
21 export function parseReadFileMeta(section: AgenticSection): ReadFileMeta | null {
22 const args = parseToolArgs(BuiltInTool.READ_FILE, section, { partial: true });
23
24 if (!args) return null;
25
26 const rawPath = args.path ?? args.file_path ?? args.filePath;
27
28 if (typeof rawPath !== 'string' || !rawPath) return null;
29
30 const fileName = rawPath.split(FILE_PATH_SEPARATOR_REGEX).pop() || rawPath;
31 // Models emit range arguments under several aliases. Accept all to
32 // stay forgiving across prompt variations.
33 const startRaw = args.start_line ?? args.line_start ?? args.startLine ?? args.from_line;
34 const endRaw = args.end_line ?? args.line_end ?? args.endLine ?? args.to_line;
35 const countRaw = args.line_count ?? args.count ?? args.num_lines;
36
37 let lineRange: { start: number; end: number } | null = null;
38
39 const sNum = Number(startRaw);
40 const eNum = Number(endRaw);
41
42 if (startRaw != null && endRaw != null && Number.isFinite(sNum) && Number.isFinite(eNum)) {
43 lineRange = { end: eNum, start: sNum };
44 } else if (startRaw != null && countRaw != null) {
45 const cNum = Number(countRaw);
46
47 if (Number.isFinite(sNum) && Number.isFinite(cNum)) {
48 lineRange = { end: sNum + cNum - 1, start: sNum };
49 }
50 }
51
52 const fileType = getFileTypeByExtension(fileName);
53 const language = fileType ? fileType.replace(TEXT_LANGUAGE_PREFIX_REGEX, '') : DEFAULT_LANGUAGE;
54
55 return { fileName, language, lineRange };
56 }