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.
7 import { BuiltInTool } from '$lib/enums';
8 import { splitSearchSummaryList, type AgenticSection } from '$lib/utils';
9 import { parseToolArgs } from './_shared';
11 export type FileGlobSearchMeta = {
16 totalMatches?: number;
17 errorMessage?: string;
20 export function parseFileGlobSearchMeta(section: AgenticSection): FileGlobSearchMeta | null {
21 const args = parseToolArgs(BuiltInTool.FILE_GLOB_SEARCH, section);
22 if (!args) return null;
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;
29 let matches: string[] = [];
30 let totalMatches: number | undefined;
31 let errorMessage: string | undefined;
33 const toolResultString = section.toolResult;
34 if (toolResultString) {
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) => {
45 matches = split.lines;
49 // See grep-search.ts: same fallback used there.
50 const split = splitSearchSummaryList(toolResultString, (total) => {
53 matches = split.lines;
57 return { path, include, exclude, matches, totalMatches, errorMessage };