]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/blob
95edc3d95eafac787896f61a7801b1983a87f3be
[pkg/ggml/sources/llama.cpp] /
1 // Meta parser for `write_file` tool calls. Reads the path/content from
2 // the streamed args (partial JSON so we can render before the call
3 // finishes) and surfaces `bytes`, `result`, and `error` from the
4 // result blob.
5
6 import { BuiltInTool } from '$lib/enums';
7 import {
8 DEFAULT_LANGUAGE,
9 FILE_PATH_SEPARATOR_REGEX,
10 TEXT_LANGUAGE_PREFIX_REGEX
11 } from '$lib/constants';
12 import { getFileTypeByExtension, tryParseToolResultObject, type AgenticSection } from '$lib/utils';
13 import { parseToolArgs } from './_shared';
14
15 export type WriteFileMeta = {
16 fileName: string;
17 filePath: string;
18 language: string;
19 content: string;
20 bytesWritten?: number;
21 resultMessage?: string;
22 errorMessage?: string;
23 };
24
25 export function parseWriteFileMeta(section: AgenticSection): WriteFileMeta | null {
26 const args = parseToolArgs(BuiltInTool.WRITE_FILE, section, { partial: true });
27 if (!args) return null;
28
29 // Tool contracts drifted over time: some models emit `path`,
30 // others `file_path` / `filePath`. Accept all three.
31 const rawPath = args.path ?? args.file_path ?? args.filePath;
32 if (typeof rawPath !== 'string' || !rawPath) return null;
33
34 const fileName = rawPath.split(FILE_PATH_SEPARATOR_REGEX).pop() || rawPath;
35 const content = typeof args.content === 'string' ? args.content : '';
36 const language =
37 getFileTypeByExtension(rawPath)?.replace(TEXT_LANGUAGE_PREFIX_REGEX, '') ?? DEFAULT_LANGUAGE;
38
39 const resultObj = tryParseToolResultObject(section.toolResult);
40 const bytesWritten =
41 resultObj && Number.isFinite(Number(resultObj.bytes)) ? Number(resultObj.bytes) : undefined;
42 const resultMessage = typeof resultObj?.result === 'string' ? resultObj.result : undefined;
43 const errorMessage = typeof resultObj?.error === 'string' ? resultObj.error : undefined;
44
45 return {
46 fileName,
47 filePath: rawPath,
48 language,
49 content,
50 bytesWritten,
51 resultMessage,
52 errorMessage
53 };
54 }