]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/blob
1ad92b74cf0562c28bcac2cea2a7b3f6615463e2
[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 { BuiltInTool } from '$lib/enums';
8 import { splitSearchSummaryList, type AgenticSection } from '$lib/utils';
9 import { parseToolArgs } from './_shared';
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 if (!args) return null;
23
24 const path = typeof args.path === 'string' ? args.path : '';
25 const include = typeof args.include === 'string' && args.include ? args.include : '**';
26 const exclude = typeof args.exclude === 'string' && args.exclude ? args.exclude : undefined;
27 if (!path) return null;
28
29 let matches: string[] = [];
30 let totalMatches: number | undefined;
31 let errorMessage: string | undefined;
32
33 const toolResultString = section.toolResult;
34 if (toolResultString) {
35 try {
36 const parsed: unknown = JSON.parse(toolResultString);
37 if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
38 const obj = parsed as Record<string, unknown>;
39 if (typeof obj.error === 'string') {
40 errorMessage = obj.error;
41 } else if (typeof obj.plain_text_response === 'string') {
42 const split = splitSearchSummaryList(obj.plain_text_response, (total) => {
43 totalMatches = total;
44 });
45 matches = split.lines;
46 }
47 }
48 } catch {
49 // See grep-search.ts: same fallback used there.
50 const split = splitSearchSummaryList(toolResultString, (total) => {
51 totalMatches = total;
52 });
53 matches = split.lines;
54 }
55 }
56
57 return { path, include, exclude, matches, totalMatches, errorMessage };
58 }