forked from microsoft/vscode-copilot-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscmChangesTool.ts
More file actions
140 lines (115 loc) · 6.16 KB
/
scmChangesTool.ts
File metadata and controls
140 lines (115 loc) · 6.16 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
/*---------------------------------------------------------------------------------------------
* 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 type * as vscode from 'vscode';
import { Diff, IGitDiffService } from '../../../platform/git/common/gitDiffService';
import { IGitService } from '../../../platform/git/common/gitService';
import { Change } from '../../../platform/git/vscode/git';
import { ILogService } from '../../../platform/log/common/logService';
import { IPromptPathRepresentationService } from '../../../platform/prompts/common/promptPathRepresentationService';
import { CancellationToken } from '../../../util/vs/base/common/cancellation';
import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';
import { LanguageModelPromptTsxPart, LanguageModelTextPart, LanguageModelToolResult, MarkdownString } from '../../../vscodeTypes';
import { renderPromptElementJSON } from '../../prompts/node/base/promptRenderer';
import { GitChanges } from '../../prompts/node/git/gitChanges';
import { ToolName } from '../common/toolNames';
import { ICopilotTool, ToolRegistry } from '../common/toolsRegistry';
import { checkCancellation, formatUriForFileWidget } from './toolUtils';
interface IGetScmChangesToolParams {
repositoryPath?: string;
sourceControlState?: ('unstaged' | 'staged' | 'merge-conflicts')[];
}
class GetScmChangesTool implements ICopilotTool<IGetScmChangesToolParams> {
public static readonly toolName = ToolName.GetScmChanges;
constructor(
@IInstantiationService private readonly instantiationService: IInstantiationService,
@IGitService private readonly gitService: IGitService,
@IGitDiffService private readonly gitDiffService: IGitDiffService,
@ILogService private readonly logService: ILogService,
@IPromptPathRepresentationService private readonly promptPathRepresentationService: IPromptPathRepresentationService,
) { }
async invoke(options: vscode.LanguageModelToolInvocationOptions<IGetScmChangesToolParams>, token: CancellationToken): Promise<vscode.LanguageModelToolResult | null | undefined> {
checkCancellation(token);
await this.gitService.initialize();
this.logService.logger.trace(`[GetScmChangesTool][invoke] Options: ${JSON.stringify(options)}`);
const diffs: Diff[] = [];
const changedFiles: Change[] = [];
const uri = options.input.repositoryPath
? this.promptPathRepresentationService.resolveFilePath(options.input.repositoryPath)
: undefined;
let repository = uri ? await this.gitService.getRepository(uri) : undefined;
repository = repository ?? this.gitService.activeRepository.get();
if (!repository) {
this.logService.logger.warn(`[GetScmChangesTool][invoke] Unable to resolve the repository using repositoryPath: ${options.input.repositoryPath}`);
this.logService.logger.warn(`[GetScmChangesTool][invoke] Unable to resolve the active repository: ${this.gitService.activeRepository.get()?.rootUri.toString()}`);
return new LanguageModelToolResult([new LanguageModelTextPart('The workspace does not contain a git repository')]);
}
this.logService.logger.trace(`[GetScmChangesTool][invoke] Uri: ${uri?.toString()}`);
this.logService.logger.trace(`[GetScmChangesTool][invoke] Repository: ${repository.rootUri.toString()}`);
const changes = repository?.changes;
if (changes) {
try {
if (options.input.sourceControlState) {
for (const state of options.input.sourceControlState) {
switch (state) {
case 'staged':
changedFiles.push(...changes.indexChanges);
break;
case 'unstaged':
changedFiles.push(
...changes.workingTree,
...changes.untrackedChanges);
break;
case 'merge-conflicts':
changedFiles.push(...changes.mergeChanges);
break;
}
}
} else {
changedFiles.push(
...changes.workingTree,
...changes.indexChanges,
...changes.mergeChanges,
...changes.untrackedChanges);
}
diffs.push(...await this.gitDiffService.getChangeDiffs(repository.rootUri, changedFiles));
} catch { }
} else {
this.logService.logger.warn(`[GetScmChangesTool][invoke] Unable to retrieve changes because there is no active repository`);
}
checkCancellation(token);
return new LanguageModelToolResult(
[diffs.length
? new LanguageModelPromptTsxPart(await renderPromptElementJSON(this.instantiationService, GitChanges, { diffs }, options.tokenizationOptions, token))
: new LanguageModelTextPart('No changed files found')]
);
}
prepareInvocation?(options: vscode.LanguageModelToolInvocationPrepareOptions<IGetScmChangesToolParams>, token: vscode.CancellationToken): vscode.ProviderResult<vscode.PreparedToolInvocation> {
checkCancellation(token);
const uri = options.input.repositoryPath
? this.promptPathRepresentationService.resolveFilePath(options.input.repositoryPath)
: undefined;
this.logService.logger.trace(`[GetScmChangesTool][prepareInvocation] Options: ${JSON.stringify(options)}`);
this.logService.logger.trace(`[GetScmChangesTool][prepareInvocation] Uri: ${uri?.toString()}`);
return uri
? {
invocationMessage: new MarkdownString(l10n.t`Reading changed files in ${formatUriForFileWidget(uri)}`),
pastTenseMessage: new MarkdownString(l10n.t`Read changed files in ${formatUriForFileWidget(uri)}`),
}
: {
invocationMessage: new MarkdownString(l10n.t`Reading changed files in the active git repository`),
pastTenseMessage: new MarkdownString(l10n.t`Read changed files in the active git repository`),
};
}
async provideInput(): Promise<IGetScmChangesToolParams | undefined> {
await this.gitService.initialize();
this.logService.logger.trace(`[GetScmChangesTool][provideInput] Active repository: ${this.gitService.activeRepository.get()?.rootUri.toString()}`);
return Promise.resolve({
repositoryPath: this.gitService.activeRepository.get()?.rootUri.toString(),
sourceControlState: ['unstaged', 'staged'],
});
}
}
ToolRegistry.registerTool(GetScmChangesTool);