]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/blob
ab1a4d6aa01bd1670c0a43d420927e6f4543b968
[pkg/ggml/sources/llama.cpp] /
1 import { FILE_PATH_SEPARATOR_REGEX, NEWLINE } from '$lib/constants/code';
2 import {
3 PREFIX_FILE,
4 PREFIX_MIME,
5 PREFIX_SIZE,
6 READ_MEDIA_SIZE_REGEX
7 } from '$lib/constants/read-media';
8 import type { AgenticSection } from '$lib/utils';
9
10 export interface ReadMediaMeta {
11 fileName: string;
12 path: string;
13 sizeBytes?: number;
14 mimeType?: string;
15 }
16
17 /**
18 * Parse read_media tool result to extract metadata.
19 * Expected format (after extractBase64Attachments processing):
20 * File: /path/to/file.png
21 * Size: 12345 bytes
22 * MIME: image/png
23 * [Attachment saved: mcp-attachment-xxx.png]
24 *
25 * The data URI line is replaced by the attachment marker by
26 * agenticStore.extractBase64Attachments before storage.
27 */
28 export function parseReadMediaMeta(section: AgenticSection): ReadMediaMeta | null {
29 if (!section.toolResult) return null;
30
31 const lines = section.toolResult.split(NEWLINE);
32
33 let fileName = '';
34 let path = '';
35 let sizeBytes: number | undefined;
36 let mimeType: string | undefined;
37
38 for (const line of lines) {
39 const trimmed = line.trim();
40
41 if (trimmed.startsWith(PREFIX_FILE)) {
42 path = trimmed.slice(PREFIX_FILE.length).trim();
43 fileName = path.split(FILE_PATH_SEPARATOR_REGEX).pop() ?? path;
44 } else if (trimmed.startsWith(PREFIX_SIZE)) {
45 const match = trimmed.match(READ_MEDIA_SIZE_REGEX);
46
47 if (match) sizeBytes = Number(match[1]);
48 } else if (trimmed.startsWith(PREFIX_MIME)) {
49 mimeType = trimmed.slice(PREFIX_MIME.length).trim();
50 }
51 }
52
53 if (!path) return null;
54
55 return { fileName, mimeType, path, sizeBytes };
56 }