Skip to content

Commit f7b7f47

Browse files
committed
feat: add timeout handling for file and text search operations
1 parent dccb635 commit f7b7f47

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/extension/tools/node/findFilesTool.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,25 @@ export class FindFilesTool implements ICopilotTool<IFindFilesToolParams> {
4040
// The input _should_ be a pattern matching inside a workspace, folder, but sometimes we get absolute paths, so try to resolve them
4141
const pattern = inputGlobToPattern(options.input.query, this.workspaceService);
4242

43-
const results = await this.searchService.findFiles(pattern, undefined, token);
43+
// try find files with a timeout of 10s
44+
// TODO: consider making the timeout configurable
45+
const timeoutInMs = 10_000;
46+
const results = await Promise.race([
47+
this.searchService.findFiles(pattern, undefined, token),
48+
new Promise<never>((_, reject) => setTimeout(() => reject(new Error('Timeout in searching files')), timeoutInMs))
49+
]);
50+
4451
checkCancellation(token);
4552

4653
const maxResults = options.input.maxResults ?? 20;
4754
const resultsToShow = results.slice(0, maxResults);
48-
const result = new ExtendedLanguageModelToolResult([
49-
new LanguageModelPromptTsxPart(
50-
await renderPromptElementJSON(this.instantiationService, FindFilesResult, { fileResults: resultsToShow, totalResults: results.length }, options.tokenizationOptions, token))]);
55+
// Render the prompt element with a timeout
56+
const prompt = await Promise.race([
57+
renderPromptElementJSON(this.instantiationService, FindFilesResult, { fileResults: resultsToShow, totalResults: results.length }, options.tokenizationOptions, token),
58+
new Promise<never>((_, reject) => setTimeout(() => reject(new Error('Timeout in rendering prompt element')), timeoutInMs))
59+
]);
60+
61+
const result = new ExtendedLanguageModelToolResult([new LanguageModelPromptTsxPart(prompt)]);
5162
const query = `\`${options.input.query}\``;
5263
result.toolResultMessage = resultsToShow.length === 0 ?
5364
new MarkdownString(l10n.t`Searched for files matching ${query}, no matches`) :

0 commit comments

Comments
 (0)