]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/blob
cdec283a02433b4dce1e2f4eabe8d58327ef4bd4
[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 { 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, tryParseToolResultObject } from '$lib/utils';
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
28 if (!args) return null;
29
30 // Tool contracts drifted over time: some models emit `path`,
31 // others `file_path` / `filePath`. Accept all three.
32 const rawPath = args.path ?? args.file_path ?? args.filePath;
33
34 if (typeof rawPath !== 'string' || !rawPath) return null;
35
36 const fileName = rawPath.split(FILE_PATH_SEPARATOR_REGEX).pop() || rawPath;
37 const content = typeof args.content === 'string' ? args.content : '';
38 const language =
39 getFileTypeByExtension(rawPath)?.replace(TEXT_LANGUAGE_PREFIX_REGEX, '') ?? DEFAULT_LANGUAGE;
40 const resultObj = tryParseToolResultObject(section.toolResult);
41 const bytesWritten =
42 resultObj && Number.isFinite(Number(resultObj.bytes)) ? Number(resultObj.bytes) : undefined;
43 const resultMessage = typeof resultObj?.result === 'string' ? resultObj.result : undefined;
44 const errorMessage = typeof resultObj?.error === 'string' ? resultObj.error : undefined;
45
46 return {
47 bytesWritten,
48 content,
49 errorMessage,
50 fileName,
51 filePath: rawPath,
52 language,
53 resultMessage
54 };
55 }