]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/blob
df91d5cae038dfb52298b140669b438d3a2169ac
[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 { CODE_BLOCK, FILE_PATH_SEPARATOR_REGEX } from '$lib/constants';
8 import { BuiltInTool } from '$lib/enums';
9 import { type AgenticSection, getFileTypeByExtension, tryParseToolResultObject } from '$lib/utils';
10
11 export type WriteFileMeta = {
12 fileName: string;
13 filePath: string;
14 language: string;
15 content: string;
16 bytesWritten?: number;
17 resultMessage?: string;
18 errorMessage?: string;
19 };
20
21 export function parseWriteFileMeta(section: AgenticSection): WriteFileMeta | null {
22 const args = parseToolArgs(BuiltInTool.WRITE_FILE, section, { partial: true });
23
24 if (!args) return null;
25
26 // Tool contracts drifted over time: some models emit `path`,
27 // others `file_path` / `filePath`. Accept all three.
28 const rawPath = args.path ?? args.file_path ?? args.filePath;
29
30 if (typeof rawPath !== 'string' || !rawPath) return null;
31
32 const fileName = rawPath.split(FILE_PATH_SEPARATOR_REGEX).pop() || rawPath;
33 const content = typeof args.content === 'string' ? args.content : '';
34 const language =
35 getFileTypeByExtension(rawPath)?.replace(CODE_BLOCK.TEXT_LANGUAGE_PREFIX_REGEX, '') ??
36 CODE_BLOCK.DEFAULT_LANGUAGE;
37 const resultObj = tryParseToolResultObject(section.toolResult);
38 const bytesWritten =
39 resultObj && Number.isFinite(Number(resultObj.bytes)) ? Number(resultObj.bytes) : undefined;
40 const resultMessage = typeof resultObj?.result === 'string' ? resultObj.result : undefined;
41 const errorMessage = typeof resultObj?.error === 'string' ? resultObj.error : undefined;
42
43 return {
44 bytesWritten,
45 content,
46 errorMessage,
47 fileName,
48 filePath: rawPath,
49 language,
50 resultMessage
51 };
52 }