Skip to content

Commit 9dec97e

Browse files
committed
feat: add timeout handling for search and rendering operations in FindTextInFilesTool
1 parent 11808ff commit 9dec97e

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/extension/tools/node/findTextInFilesTool.tsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,28 @@ export class FindTextInFilesTool implements ICopilotTool<IFindTextInFilesToolPar
5151
const maxResults = Math.min(options.input.maxResults ?? 20, MaxResultsCap);
5252
const isRegExp = options.input.isRegexp ?? true;
5353
const queryIsValidRegex = this.isValidRegex(options.input.query);
54-
let results = await this.searchAndCollectResults(options.input.query, isRegExp, patterns, maxResults, token);
54+
55+
// try find text with a timeout of 10s
56+
// TODO: consider making the timeout configurable
57+
const timeoutInMs = 10_000;
58+
let results = await Promise.race([
59+
this.searchAndCollectResults(options.input.query, isRegExp, patterns, maxResults, token),
60+
new Promise<never>((_, reject) => setTimeout(() => reject(new Error("Timeout in searching text in files")), timeoutInMs))]);
61+
62+
checkCancellation(token);
5563
if (!results.length && queryIsValidRegex) {
56-
results = await this.searchAndCollectResults(options.input.query, !isRegExp, patterns, maxResults, token);
64+
results = await Promise.race([
65+
this.searchAndCollectResults(options.input.query, !isRegExp, patterns, maxResults, token),
66+
new Promise<never>((_, reject) => setTimeout(() => reject(new Error("Timeout in searching text in files")), timeoutInMs))]);
5767
}
5868

59-
const result = new ExtendedLanguageModelToolResult([
60-
new LanguageModelPromptTsxPart(
61-
await renderPromptElementJSON(this.instantiationService, FindTextInFilesResult, { textResults: results, maxResults, askedForTooManyResults: Boolean(askedForTooManyResults) }, options.tokenizationOptions, token))]);
69+
checkCancellation(token);
70+
const prompt = await Promise.race([
71+
renderPromptElementJSON(this.instantiationService, FindTextInFilesResult, { textResults: results, maxResults, askedForTooManyResults: Boolean(askedForTooManyResults) }, options.tokenizationOptions, token),
72+
new Promise<never>((_, reject) => setTimeout(() => reject(new Error('Timeout in rendering prompt element')), timeoutInMs))
73+
]);
74+
75+
const result = new ExtendedLanguageModelToolResult([new LanguageModelPromptTsxPart(prompt)]);
6276
const textMatches = results.flatMap(r => {
6377
if ('ranges' in r) {
6478
return asArray(r.ranges).map(rangeInfo => new Location(r.uri, rangeInfo.sourceRange));

0 commit comments

Comments
 (0)