forked from microsoft/vscode-copilot-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocTool.tsx
More file actions
103 lines (83 loc) · 3.71 KB
/
Copy pathdocTool.tsx
File metadata and controls
103 lines (83 loc) · 3.71 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type * as vscode from 'vscode';
import { IPromptPathRepresentationService } from '../../../platform/prompts/common/promptPathRepresentationService';
import { IWorkspaceService } from '../../../platform/workspace/common/workspaceService';
import { isLocation } from '../../../util/common/types';
import { URI } from '../../../util/vs/base/common/uri';
import { LanguageModelTextPart, LanguageModelToolResult } from '../../../vscodeTypes';
import { IBuildPromptContext } from '../../prompt/common/intents';
import { ToolName } from '../common/toolNames';
import { ToolRegistry } from '../common/toolsRegistry';
import { checkCancellation } from './toolUtils';
interface IDocInfoTool {
readonly filePaths: string[];
}
class DocInfoTool implements vscode.LanguageModelTool<IDocInfoTool> {
static readonly toolName = ToolName.DocInfo;
private static _docTypeNames = new Map<string, string>([
['typescript', 'TSDoc comment'],
['typescriptreact', 'TSDoc comment'],
['javascript', 'JSDoc comment'],
['javascriptreact', 'JSDoc comment'],
['python', 'docstring'],
]);
constructor(
@IWorkspaceService private readonly workspaceService: IWorkspaceService,
@IPromptPathRepresentationService private readonly _promptPathRepresentationService: IPromptPathRepresentationService,
) { }
async invoke(options: vscode.LanguageModelToolInvocationOptions<IDocInfoTool>, token: vscode.CancellationToken) {
const docNames = new Set<string>();
for (const filePath of options.input.filePaths) {
const uri = this._promptPathRepresentationService.resolveFilePath(filePath);
if (!uri) {
continue;
}
const doc = await this.workspaceService.openTextDocumentAndSnapshot(uri);
const docName: string = DocInfoTool._docTypeNames.get(doc.languageId) || 'documentation comment';
docNames.add(docName);
}
checkCancellation(token);
return new LanguageModelToolResult([
new LanguageModelTextPart(`Please generate ${Array.from(docNames).join(', ')} for the respective files. ONLY add documentation and do not change the code.`)
]);
}
async prepareInvocation(options: vscode.LanguageModelToolInvocationPrepareOptions<IDocInfoTool>, token: vscode.CancellationToken): Promise<vscode.PreparedToolInvocation> {
return {
presentation: 'hidden',
};
}
async provideInput(promptContext: IBuildPromptContext): Promise<IDocInfoTool | 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) {
// no context variables or working set
}
return {
filePaths,
};
}
}
ToolRegistry.registerTool(DocInfoTool);