-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: add timeout in searching text in files #1233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
f7b7f47
b176a12
e1d6814
a4b3dde
86bdb0e
7f0cd35
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -11,6 +11,7 @@ import { URI } from '../../../util/vs/base/common/uri'; | |||||
import * as l10n from '@vscode/l10n'; | ||||||
import { ISearchService } from '../../../platform/search/common/searchService'; | ||||||
import { IWorkspaceService } from '../../../platform/workspace/common/workspaceService'; | ||||||
import { raceCancellation, raceTimeout } from '../../../util/vs/base/common/async'; | ||||||
import { CancellationToken } from '../../../util/vs/base/common/cancellation'; | ||||||
import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation'; | ||||||
import { ExtendedLanguageModelToolResult, LanguageModelPromptTsxPart, MarkdownString } from '../../../vscodeTypes'; | ||||||
|
@@ -40,14 +41,38 @@ export class FindFilesTool implements ICopilotTool<IFindFilesToolParams> { | |||||
// The input _should_ be a pattern matching inside a workspace, folder, but sometimes we get absolute paths, so try to resolve them | ||||||
const pattern = inputGlobToPattern(options.input.query, this.workspaceService); | ||||||
|
||||||
const results = await this.searchService.findFiles(pattern, undefined, token); | ||||||
// try find files with a timeout of 10s | ||||||
// TODO: consider making the timeout configurable | ||||||
const timeoutInMs = 10_000; | ||||||
const results = await raceTimeout( | ||||||
raceCancellation( | ||||||
Promise.resolve(this.searchService.findFiles(pattern, undefined, token)), | ||||||
|
Promise.resolve(this.searchService.findFiles(pattern, undefined, token)), | |
this.searchService.findFiles(pattern, undefined, token), |
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately the findFiles
returns a Thenable<>
which doesn't fit the required Promise<>
type, and fails compilation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would make sense for the search service to return a Promise instead (this comes from our extension API but our searchService could do the Promise.resolve
. You can do that if it makes things cleaner, you don't have to
bryanchen-d marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
bryanchen-d marked this conversation as resolved.
Show resolved
Hide resolved
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import { IPromptPathRepresentationService } from '../../../platform/prompts/comm | |
import { ISearchService } from '../../../platform/search/common/searchService'; | ||
import { IWorkspaceService } from '../../../platform/workspace/common/workspaceService'; | ||
import { asArray } from '../../../util/vs/base/common/arrays'; | ||
import { raceCancellation, raceTimeout } from '../../../util/vs/base/common/async'; | ||
import { CancellationToken } from '../../../util/vs/base/common/cancellation'; | ||
import { count } from '../../../util/vs/base/common/strings'; | ||
import { URI } from '../../../util/vs/base/common/uri'; | ||
|
@@ -51,14 +52,45 @@ export class FindTextInFilesTool implements ICopilotTool<IFindTextInFilesToolPar | |
const maxResults = Math.min(options.input.maxResults ?? 20, MaxResultsCap); | ||
const isRegExp = options.input.isRegexp ?? true; | ||
const queryIsValidRegex = this.isValidRegex(options.input.query); | ||
let results = await this.searchAndCollectResults(options.input.query, isRegExp, patterns, maxResults, token); | ||
if (!results.length && queryIsValidRegex) { | ||
results = await this.searchAndCollectResults(options.input.query, !isRegExp, patterns, maxResults, token); | ||
|
||
// try find text with a timeout of 10s | ||
// TODO: consider making the timeout configurable | ||
const timeoutInMs = 10_000; | ||
let results = await raceTimeout( | ||
raceCancellation( | ||
this.searchAndCollectResults(options.input.query, isRegExp, patterns, maxResults, token), | ||
token | ||
), | ||
timeoutInMs | ||
); | ||
|
||
if (results && !results.length && queryIsValidRegex) { | ||
results = await raceTimeout( | ||
raceCancellation( | ||
this.searchAndCollectResults(options.input.query, !isRegExp, patterns, maxResults, token), | ||
token | ||
), | ||
timeoutInMs | ||
); | ||
} | ||
|
||
if (results === undefined) { | ||
throw new Error('Timeout in searching text in files'); | ||
} | ||
|
||
const prompt = await raceTimeout( | ||
raceCancellation( | ||
renderPromptElementJSON(this.instantiationService, FindTextInFilesResult, { textResults: results, maxResults, askedForTooManyResults: Boolean(askedForTooManyResults) }, options.tokenizationOptions, token), | ||
token | ||
), | ||
timeoutInMs | ||
); | ||
|
||
bryanchen-d marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (prompt === undefined) { | ||
throw new Error('Timeout in rendering prompt'); | ||
} | ||
|
||
const result = new ExtendedLanguageModelToolResult([ | ||
new LanguageModelPromptTsxPart( | ||
await renderPromptElementJSON(this.instantiationService, FindTextInFilesResult, { textResults: results, maxResults, askedForTooManyResults: Boolean(askedForTooManyResults) }, options.tokenizationOptions, token))]); | ||
const result = new ExtendedLanguageModelToolResult([new LanguageModelPromptTsxPart(prompt)]); | ||
const textMatches = results.flatMap(r => { | ||
if ('ranges' in r) { | ||
return asArray(r.ranges).map(rangeInfo => new Location(r.uri, rangeInfo.sourceRange)); | ||
|
@@ -99,6 +131,7 @@ export class FindTextInFilesTool implements ICopilotTool<IFindTextInFilesToolPar | |
token); | ||
const results: vscode.TextSearchResult2[] = []; | ||
for await (const item of searchResult.results) { | ||
await new Promise(r => setTimeout(r, 5000)); // yield to allow cancellation to be processed | ||
|
||
checkCancellation(token); | ||
results.push(item); | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.