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 { parseToolArgs } from './_shared';
8 import { BuiltInTool } from '$lib/enums';
9 import { type AgenticSection, splitSearchSummaryList } from '$lib/utils';
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);
23 if (!args) return null;
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;
29 if (!path) return null;
31 let matches: string[] = [];
32 let totalMatches: number | undefined;
33 let errorMessage: string | undefined;
35 const toolResultString = section.toolResult;
37 if (toolResultString) {
39 const parsed: unknown = JSON.parse(toolResultString);
41 if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
42 const obj = parsed as Record<string, unknown>;
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) => {
51 matches = split.lines;
55 // See grep-search.ts: same fallback used there.
56 const split = splitSearchSummaryList(toolResultString, (total) => {
60 matches = split.lines;
64 return { errorMessage, exclude, include, matches, path, totalMatches };