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:
parent
250a0e1bde
commit
a00b640dad
@ -1,4 +1,5 @@
|
|||||||
import { ToolArgs } from "./types"
|
import { ToolArgs } from "./types"
|
||||||
|
import { useSettings } from '../../../contexts/SettingsContext'
|
||||||
|
|
||||||
export function getSearchFilesDescription(args: ToolArgs): string {
|
export function getSearchFilesDescription(args: ToolArgs): string {
|
||||||
if (args.searchTool === 'match') {
|
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.
|
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:
|
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.
|
- 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:
|
Usage:
|
||||||
<match_search_files>
|
<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 {
|
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
|
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.
|
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:
|
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.
|
- 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:
|
Usage:
|
||||||
<regex_search_files>
|
<regex_search_files>
|
||||||
|
|||||||
@ -28,18 +28,23 @@ export async function matchSearchUsingCorePlugin(
|
|||||||
// It does not return the results directly.
|
// It does not return the results directly.
|
||||||
searchPlugin.openGlobalSearch(query);
|
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];
|
const searchLeaf = app.workspace.getLeavesOfType('search')[0];
|
||||||
if (!searchLeaf) {
|
if (!searchLeaf) {
|
||||||
throw new Error("No active search pane found after triggering search.");
|
throw new Error("No active search pane found after triggering search.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// @ts-ignore
|
// Ensure the view is fully loaded before we try to access its properties.
|
||||||
const searchResultsMap = (searchLeaf.view as any).dom.resultDomLookup;
|
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) {
|
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."
|
return "No results found."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -13,6 +13,8 @@ describe('parseSmartCopilotSettings', () => {
|
|||||||
openAIApiKey: '',
|
openAIApiKey: '',
|
||||||
anthropicApiKey: '',
|
anthropicApiKey: '',
|
||||||
filesSearchMethod: 'auto',
|
filesSearchMethod: 'auto',
|
||||||
|
regexSearchBackend: 'ripgrep',
|
||||||
|
matchSearchBackend: 'coreplugin',
|
||||||
fuzzyMatchThreshold: 0.85,
|
fuzzyMatchThreshold: 0.85,
|
||||||
geminiApiKey: '',
|
geminiApiKey: '',
|
||||||
groqApiKey: '',
|
groqApiKey: '',
|
||||||
@ -196,6 +198,8 @@ describe('settings migration', () => {
|
|||||||
openAIApiKey: '',
|
openAIApiKey: '',
|
||||||
anthropicApiKey: '',
|
anthropicApiKey: '',
|
||||||
filesSearchMethod: 'auto',
|
filesSearchMethod: 'auto',
|
||||||
|
regexSearchBackend: 'ripgrep',
|
||||||
|
matchSearchBackend: 'coreplugin',
|
||||||
fuzzyMatchThreshold: 0.85,
|
fuzzyMatchThreshold: 0.85,
|
||||||
geminiApiKey: '',
|
geminiApiKey: '',
|
||||||
groqApiKey: '',
|
groqApiKey: '',
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user