refactor: Improve file search tool descriptions and reliability

- Updates the description for the regex_search_files tool to dynamically indicate the regex syntax used based on the user's settings (ECMAScript or Rust).
- Refactors the core plugin match search to wait for the search view to load before attempting to access results, addressing a timing issue.
- Adds new settings for regexSearchBackend and matchSearchBackend in settings.test.ts to reflect the refactored backend options for file searching.
This commit is contained in:
travertexg 2025-06-10 04:21:40 +00:00
parent 250a0e1bde
commit a00b640dad
3 changed files with 31 additions and 8 deletions

View File

@ -1,4 +1,5 @@
import { ToolArgs } from "./types"
import { useSettings } from '../../../contexts/SettingsContext'
export function getSearchFilesDescription(args: ToolArgs): string {
if (args.searchTool === 'match') {
@ -17,7 +18,7 @@ export function getMatchSearchFilesDescription(args: ToolArgs): string {
Description: Request to perform a match/fuzzy search across files in a specified directory, providing context-rich results. This tool searches for specific content across multiple files, displaying each match with encapsulating context.
Parameters:
- path: (required) The path of the directory to search in (relative to the current working directory ${args.cwd}). This directory will be recursively searched.
- query: (required) The keyword, phrase to search for. The system will find documents with similar keywords/phrases.
- query: (required) The keyword/phrase to search for. The system will find documents with similar keywords/phrases.
Usage:
<match_search_files>
@ -33,11 +34,24 @@ Example: Requesting to search for all Markdown files containing 'test' in the cu
}
export function getRegexSearchFilesDescription(args: ToolArgs): string {
const { settings } = useSettings()
let regex_syntax: string;
switch (settings.regexSearchBackend) {
case 'coreplugin':
regex_syntax = "ECMAScript (JavaScript)";
break;
case 'ripgrep':
regex_syntax = "Rust";
break;
default:
regex_syntax = "ECMAScript (JavaScript)";
}
return `## regex_search_files
Description: Request to perform a regex search across files in a specified directory, providing context-rich results. This tool searches for patterns or specific content across multiple files, displaying each match with encapsulating context.
Parameters:
- path: (required) The path of the directory to search in (relative to the current working directory ${args.cwd}). This directory will be recursively searched.
- regex: (required) The regular expression pattern to search for. Uses Rust regex syntax, **but should not include word boundaries (\b)**.
- regex: (required) The regular expression pattern to search for. Uses ${regex_syntax} regex syntax, **but should not include word boundaries (\b)**.
Usage:
<regex_search_files>

View File

@ -28,18 +28,23 @@ export async function matchSearchUsingCorePlugin(
// It does not return the results directly.
searchPlugin.openGlobalSearch(query);
// We must wait for the search to execute and the UI to update.
await new Promise(resolve => setTimeout(resolve, 500));
const searchLeaf = app.workspace.getLeavesOfType('search')[0];
if (!searchLeaf) {
throw new Error("No active search pane found after triggering search.");
}
// @ts-ignore
const searchResultsMap = (searchLeaf.view as any).dom.resultDomLookup;
// Ensure the view is fully loaded before we try to access its properties.
const view = await searchLeaf.open(searchLeaf.view);
const searchResultsMap = await new Promise<Map<TFile, any>>(resolve => {
setTimeout(() => {
// @ts-ignore
const results = (view as any).dom?.resultDomLookup;
resolve(results || new Map());
}, 5000)
});
if (!searchResultsMap || searchResultsMap.size === 0) {
console.error("No results found.");
console.error("No results found or search results map is not available.");
return "No results found."
}

View File

@ -13,6 +13,8 @@ describe('parseSmartCopilotSettings', () => {
openAIApiKey: '',
anthropicApiKey: '',
filesSearchMethod: 'auto',
regexSearchBackend: 'ripgrep',
matchSearchBackend: 'coreplugin',
fuzzyMatchThreshold: 0.85,
geminiApiKey: '',
groqApiKey: '',
@ -196,6 +198,8 @@ describe('settings migration', () => {
openAIApiKey: '',
anthropicApiKey: '',
filesSearchMethod: 'auto',
regexSearchBackend: 'ripgrep',
matchSearchBackend: 'coreplugin',
fuzzyMatchThreshold: 0.85,
geminiApiKey: '',
groqApiKey: '',