-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathgetErrorsTool.tsx
More file actions
359 lines (310 loc) · 13.8 KB
/
getErrorsTool.tsx
File metadata and controls
359 lines (310 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as l10n from '@vscode/l10n';
import { BasePromptElementProps, PromptElement, PromptElementProps } from '@vscode/prompt-tsx';
import type * as vscode from 'vscode';
import { ILanguageDiagnosticsService } from '../../../platform/languages/common/languageDiagnosticsService';
import { ILogService } from '../../../platform/log/common/logService';
import { INotebookService } from '../../../platform/notebook/common/notebookService';
import { IPromptPathRepresentationService } from '../../../platform/prompts/common/promptPathRepresentationService';
import { IWorkspaceService } from '../../../platform/workspace/common/workspaceService';
import { getLanguage } from '../../../util/common/languages';
import { findNotebook } from '../../../util/common/notebooks';
import { isLocation } from '../../../util/common/types';
import { coalesce } from '../../../util/vs/base/common/arrays';
import { CancellationToken } from '../../../util/vs/base/common/cancellation';
import { Disposable } from '../../../util/vs/base/common/lifecycle';
import { ResourceSet } from '../../../util/vs/base/common/map';
import { isEqualOrParent } from '../../../util/vs/base/common/resources';
import { URI } from '../../../util/vs/base/common/uri';
import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';
import { DiagnosticSeverity, ExtendedLanguageModelToolResult, LanguageModelPromptTsxPart, MarkdownString, Range } from '../../../vscodeTypes';
import { IBuildPromptContext } from '../../prompt/common/intents';
import { renderPromptElementJSON } from '../../prompts/node/base/promptRenderer';
import { Tag } from '../../prompts/node/base/tag';
import { DiagnosticContext, Diagnostics } from '../../prompts/node/inline/diagnosticsContext';
import { ToolName } from '../common/toolNames';
import { ICopilotTool, ToolRegistry } from '../common/toolsRegistry';
import { formatUriForFileWidget } from '../common/toolUtils';
import { checkCancellation, resolveToolInputPath } from './toolUtils';
interface IGetErrorsParams {
// Note that empty array is not the same as absence; empty array
// will not return any errors. Absence returns all errors.
filePaths?: string[];
// sparse array of ranges, as numbers because it goes through JSON
// ignored if filePaths is missing / null.
ranges?: ([a: number, b: number, c: number, d: number] | undefined)[];
}
export class GetErrorsTool extends Disposable implements ICopilotTool<IGetErrorsParams> {
public static readonly toolName = ToolName.GetErrors;
public static readonly nonDeferred = true;
constructor(
@IInstantiationService private readonly instantiationService: IInstantiationService,
@ILanguageDiagnosticsService private readonly languageDiagnosticsService: ILanguageDiagnosticsService,
@IWorkspaceService private readonly workspaceService: IWorkspaceService,
@IPromptPathRepresentationService private readonly promptPathRepresentationService: IPromptPathRepresentationService,
@INotebookService private readonly notebookService: INotebookService,
@ILogService private readonly logService: ILogService
) {
super();
}
/**
* Get diagnostics for the given paths and optional ranges.
* Note - This is made public for testing purposes only.
*/
public getDiagnostics(paths: { uri: URI; range: Range | undefined }[]): Array<{ uri: URI; diagnostics: vscode.Diagnostic[]; inputUri?: URI }> {
const results: Array<{ uri: URI; diagnostics: vscode.Diagnostic[]; inputUri?: URI }> = [];
// for notebooks, we need to find the cell matching the range and get diagnostics for that cell
const nonNotebookPaths = paths.filter(p => {
const isNotebook = this.notebookService.hasSupportedNotebooks(p.uri);
if (isNotebook) {
const diagnostics = this.getNotebookCellDiagnostics(p.uri);
results.push({ uri: p.uri, diagnostics });
}
return !isNotebook;
});
if (nonNotebookPaths.length === 0) {
return results;
}
const pendingMatchPaths = new Set(nonNotebookPaths.map(p => p.uri));
// for non-notebooks, we get all diagnostics and filter down
for (const [resource, entries] of this.languageDiagnosticsService.getAllDiagnostics()) {
const pendingDiagnostics = entries.filter(d => d.severity <= DiagnosticSeverity.Warning);
if (pendingDiagnostics.length === 0) {
continue;
}
// find all path&range pairs and collect the ranges to further filter diagnostics
// if any path matches the resource without a range, take all diagnostics for that file
// otherwise, filter diagnostics to those intersecting one of the provided ranges
const ranges: Range[] = [];
let shouldTakeAll = false;
let foundMatch = false;
let inputUri: URI | undefined;
let matchedExactPath = false;
for (const path of nonNotebookPaths) {
// we support file or folder paths
if (isEqualOrParent(resource, path.uri)) {
foundMatch = true;
// Track the input URI that matched - prefer exact matches, otherwise use the folder
const isExactMatch = resource.toString() === path.uri.toString();
if (isExactMatch) {
// Exact match - this is the file itself, no input folder
inputUri = undefined;
matchedExactPath = true;
} else if (!matchedExactPath) {
// Folder match - only set if we haven't found an exact match or a previous folder match
if (inputUri === undefined) {
inputUri = path.uri;
}
}
if (pendingMatchPaths.has(path.uri)) {
pendingMatchPaths.delete(path.uri);
}
if (path.range) {
ranges.push(path.range);
} else {
// no range, so all diagnostics for this file
shouldTakeAll = true;
break;
}
}
}
if (shouldTakeAll) {
results.push({ uri: resource, diagnostics: pendingDiagnostics, inputUri });
continue;
}
if (foundMatch && ranges.length > 0) {
const diagnostics = pendingDiagnostics.filter(d => ranges.some(range => d.range.intersection(range)));
results.push({ uri: resource, diagnostics, inputUri });
}
}
// for any given paths that didn't match any files, return empty diagnostics for each of them
for (const uri of pendingMatchPaths) {
results.push({ uri, diagnostics: [] });
}
return results;
}
async invoke(options: vscode.LanguageModelToolInvocationOptions<IGetErrorsParams>, token: CancellationToken) {
const getAll = () => this.languageDiagnosticsService.getAllDiagnostics()
.map(d => ({ uri: d[0], diagnostics: d[1].filter(e => e.severity <= DiagnosticSeverity.Warning), inputUri: undefined }))
// filter any documents w/o warnings or errors
.filter(d => d.diagnostics.length > 0);
const getSome = (filePaths: string[]) =>
this.getDiagnostics(filePaths.map((filePath, i) => {
const uri = resolveToolInputPath(filePath, this.promptPathRepresentationService);
const range = options.input.ranges?.[i];
if (!uri) {
throw new Error(`Invalid input path ${filePath}`);
}
return { uri, range: range ? new Range(...range) : undefined };
}));
const ds = options.input.filePaths?.length ? getSome(options.input.filePaths) : getAll();
const diagnostics = coalesce(await Promise.all(ds.map((async ({ uri, diagnostics, inputUri }) => {
try {
const document = await this.workspaceService.openTextDocumentAndSnapshot(uri);
checkCancellation(token);
return {
uri,
diagnostics,
context: { document, language: getLanguage(document) },
inputUri
};
} catch (e) {
this.logService.error(e, 'get_errors failed to open doc with diagnostics');
return undefined;
}
}))));
checkCancellation(token);
const result = new ExtendedLanguageModelToolResult([
new LanguageModelPromptTsxPart(
await renderPromptElementJSON(this.instantiationService, DiagnosticToolOutput, { diagnosticsGroups: diagnostics, maxDiagnostics: 50 }, options.tokenizationOptions, token)
)
]);
const numDiagnostics = diagnostics.reduce((acc, { diagnostics }) => acc + diagnostics.length, 0);
// For display message, use inputUri if available (indicating file was found via folder input), otherwise use the file uri
// Deduplicate URIs since multiple files may have the same inputUri
const displayUriSet = new ResourceSet();
for (const d of diagnostics) {
const displayUri = d.inputUri ?? d.uri;
displayUriSet.add(displayUri);
}
const formattedURIs = this.formatURIs(Array.from(displayUriSet));
if (options.input.filePaths?.length) {
result.toolResultMessage = numDiagnostics === 0 ?
new MarkdownString(l10n.t`Checked ${formattedURIs}, no problems found`) :
numDiagnostics === 1 ?
new MarkdownString(l10n.t`Checked ${formattedURIs}, 1 problem found`) :
new MarkdownString(l10n.t`Checked ${formattedURIs}, ${numDiagnostics} problems found`);
} else {
result.toolResultMessage = numDiagnostics === 0 ?
new MarkdownString(l10n.t`Checked workspace, no problems found`) :
numDiagnostics === 1 ?
new MarkdownString(l10n.t`Checked workspace, 1 problem found in ${formattedURIs}`) :
new MarkdownString(l10n.t`Checked workspace, ${numDiagnostics} problems found in ${formattedURIs}`);
}
return result;
}
prepareInvocation(options: vscode.LanguageModelToolInvocationPrepareOptions<IGetErrorsParams>, token: vscode.CancellationToken): vscode.ProviderResult<vscode.PreparedToolInvocation> {
if (!options.input.filePaths?.length) {
// When no file paths provided, check all files with diagnostics
return {
invocationMessage: new MarkdownString(l10n.t`Checking workspace for problems`),
};
}
else {
const uris = options.input.filePaths.map(filePath => resolveToolInputPath(filePath, this.promptPathRepresentationService));
if (uris.some(uri => uri === undefined)) {
throw new Error('Invalid file path provided');
}
return {
invocationMessage: new MarkdownString(l10n.t`Checking ${this.formatURIs(uris)}`),
};
}
}
private formatURIs(uris: URI[]): string {
return uris.map(uri => formatUriForFileWidget(uri)).join(', ');
}
private getNotebookCellDiagnostics(uri: URI) {
const notebook = findNotebook(uri, this.workspaceService.notebookDocuments);
if (!notebook) {
this.logService.error(`Notebook not found: ${uri.toString()}, could not retrieve diagnostics`);
return [];
}
return notebook.getCells()
.flatMap((cell) => {
const uri = cell.document.uri;
return this.languageDiagnosticsService.getDiagnostics(uri);
});
}
async provideInput(promptContext: IBuildPromptContext): Promise<IGetErrorsParams | undefined> {
const seen = new Set<string>();
const filePaths: string[] = [];
const ranges: ([a: number, b: number, c: number, d: number] | undefined)[] = [];
function addPath(path: string, range: vscode.Range | undefined) {
if (!seen.has(path)) {
seen.add(path);
filePaths.push(path);
ranges.push(range && [range.start.line, range.start.character, range.end.line, range.end.character]);
}
}
for (const ref of promptContext.chatVariables) {
if (URI.isUri(ref.value)) {
addPath(this.promptPathRepresentationService.getFilePath(ref.value), undefined);
} else if (isLocation(ref.value)) {
addPath(this.promptPathRepresentationService.getFilePath(ref.value.uri), ref.value.range);
}
}
if (promptContext.workingSet) {
for (const file of promptContext.workingSet) {
addPath(this.promptPathRepresentationService.getFilePath(file.document.uri), file.range);
}
}
if (!filePaths.length) {
for (const [uri, diags] of this.languageDiagnosticsService.getAllDiagnostics()) {
const path = this.promptPathRepresentationService.getFilePath(uri);
if (diags.length) {
let range = diags[0].range;
for (let i = 1; i < diags.length; i++) {
range = range.union(diags[i].range);
}
addPath(path, range);
}
}
}
return {
filePaths,
ranges
};
}
}
ToolRegistry.registerTool(GetErrorsTool);
interface IDiagnosticToolOutputProps extends BasePromptElementProps {
diagnosticsGroups: { context: DiagnosticContext; uri: URI; diagnostics: vscode.Diagnostic[] }[];
maxDiagnostics?: number;
}
export class DiagnosticToolOutput extends PromptElement<IDiagnosticToolOutputProps> {
constructor(
props: PromptElementProps<IDiagnosticToolOutputProps>,
@IPromptPathRepresentationService private readonly promptPathRepresentationService: IPromptPathRepresentationService,
) {
super(props);
}
render() {
if (!this.props.diagnosticsGroups.length) {
return <>No errors found.</>;
}
let diagnosticsGroups = this.props.diagnosticsGroups;
let limitMsg;
if (typeof this.props.maxDiagnostics === 'number') {
let remaining = this.props.maxDiagnostics;
diagnosticsGroups = this.props.diagnosticsGroups.map(group => {
if (remaining <= 0) {
return { ...group, diagnostics: [] };
}
const take = Math.min(group.diagnostics.length, remaining);
remaining -= take;
return { ...group, diagnostics: group.diagnostics.slice(0, take) };
});
const totalDiagnostics = this.props.diagnosticsGroups.reduce((acc, group) => acc + group.diagnostics.length, 0);
limitMsg = totalDiagnostics > this.props.maxDiagnostics
? <>Showing first {this.props.maxDiagnostics} results out of {totalDiagnostics}<br /></>
: undefined;
}
return <>
{limitMsg}
{diagnosticsGroups.map(d =>
<Tag name='errors' attrs={{ path: this.promptPathRepresentationService.getFilePath(d.uri) }}>
{d.diagnostics.length
? <Diagnostics
documentContext={d.context}
diagnostics={d.diagnostics}
includeRelatedInfos={false} // avoid blowing up the prompt #12655
/>
: 'No errors found'}
</Tag>
)}
</>;
}
}