]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/blob
bbd50ce89172bbd080981bafa6e0e25fdb1e66f1
[pkg/ggml/sources/llama.cpp] /
1 // Meta parser for `file_glob_search` tool calls. Reads the path,
2 // include pattern, and optional exclude from the args (strict parsing)
3 // and the matches from the result blob. Like grep_search, the result
4 // parser keeps the original raw-text fallback for MCP servers that
5 // emit unparseable output.
6
7 import { parseToolArgs } from './_shared';
8 import { BuiltInTool } from '$lib/enums';
9 import { type AgenticSection, splitSearchSummaryList } from '$lib/utils';
10
11 export type FileGlobSearchMeta = {
12 path: string;
13 include: string;
14 exclude?: string;
15 matches: string[];
16 totalMatches?: number;
17 errorMessage?: string;
18 };
19
20 export function parseFileGlobSearchMeta(section: AgenticSection): FileGlobSearchMeta | null {
21 const args = parseToolArgs(BuiltInTool.FILE_GLOB_SEARCH, section);
22
23 if (!args) return null;
24
25 const path = typeof args.path === 'string' ? args.path : '';
26 const include = typeof args.include === 'string' && args.include ? args.include : '**';
27 const exclude = typeof args.exclude === 'string' && args.exclude ? args.exclude : undefined;
28
29 if (!path) return null;
30
31 let matches: string[] = [];
32 let totalMatches: number | undefined;
33 let errorMessage: string | undefined;
34
35 const toolResultString = section.toolResult;
36
37 if (toolResultString) {
38 try {
39 const parsed: unknown = JSON.parse(toolResultString);
40
41 if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
42 const obj = parsed as Record<string, unknown>;
43
44 if (typeof obj.error === 'string') {
45 errorMessage = obj.error;
46 } else if (typeof obj.plain_text_response === 'string') {
47 const split = splitSearchSummaryList(obj.plain_text_response, (total) => {
48 totalMatches = total;
49 });
50
51 matches = split.lines;
52 }
53 }
54 } catch {
55 // See grep-search.ts: same fallback used there.
56 const split = splitSearchSummaryList(toolResultString, (total) => {
57 totalMatches = total;
58 });
59
60 matches = split.lines;
61 }
62 }
63
64 return { errorMessage, exclude, include, matches, path, totalMatches };
65 }