-
Notifications
You must be signed in to change notification settings - Fork 526
Expand file tree
/
Copy pathtools.ts
More file actions
245 lines (214 loc) · 10 KB
/
tools.ts
File metadata and controls
245 lines (214 loc) · 10 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import { IBuildParameters, IConfigureParameters, IGetErrorsParameters } from './types';
import { ExtensionManager } from '@cmt/extension';
import { collections } from '@cmt/diagnostics/collections';
/**
* Language Model Tool for building CMake projects
*/
export class CMakeBuildTool implements vscode.LanguageModelTool<IBuildParameters> {
constructor(private readonly extensionManager: ExtensionManager) {}
async prepareInvocation(
options: vscode.LanguageModelToolInvocationPrepareOptions<IBuildParameters>,
_token: vscode.CancellationToken
): Promise<vscode.PreparedToolInvocation> {
const target = options.input.target;
const clean = options.input.clean ?? false;
let targetDesc = target ? `target '${target}'` : 'default target';
if (clean) {
targetDesc = `clean and build ${targetDesc}`;
} else {
targetDesc = `build ${targetDesc}`;
}
const confirmationMessages = {
title: 'Build CMake Project',
message: new vscode.MarkdownString(
`Build the CMake project: ${targetDesc}?`
)
};
return {
invocationMessage: clean ? 'Cleaning and building CMake project...' : 'Building CMake project...',
confirmationMessages
};
}
async invoke(
options: vscode.LanguageModelToolInvocationOptions<IBuildParameters>,
_token: vscode.CancellationToken
): Promise<vscode.LanguageModelToolResult> {
const params = options.input;
const targets = params.target ? [params.target] : undefined;
try {
// If clean is requested, clean first
if (params.clean) {
await this.extensionManager.clean();
}
// Perform the build
const result = await this.extensionManager.build(undefined, undefined, undefined, undefined, undefined);
if (result === 0) {
const targetDesc = params.target ? `target '${params.target}'` : 'default target';
const message = `CMake build completed successfully for ${targetDesc}.`;
return new vscode.LanguageModelToolResult([
new vscode.LanguageModelTextPart(message)
]);
} else {
const errorMsg = `CMake build failed with exit code ${result}.`;
throw new Error(errorMsg + ' Use the cmake_get_errors tool to see the compilation errors, then help the user fix them.');
}
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Error(`CMake build failed: ${errorMessage}. Use the cmake_get_errors tool to see detailed diagnostics.`);
}
}
}
/**
* Language Model Tool for configuring CMake projects
*/
export class CMakeConfigureTool implements vscode.LanguageModelTool<IConfigureParameters> {
constructor(private readonly extensionManager: ExtensionManager) {}
async prepareInvocation(
options: vscode.LanguageModelToolInvocationPrepareOptions<IConfigureParameters>,
_token: vscode.CancellationToken
): Promise<vscode.PreparedToolInvocation> {
const cleanFirst = options.input.cleanFirst ?? false;
const presetName = this.extensionManager.activeConfigurePresetName() || 'default';
const action = cleanFirst ? 'Clean and reconfigure' : 'Configure';
const confirmationMessages = {
title: `${action} CMake Project`,
message: new vscode.MarkdownString(
`${action} the CMake project with preset '${presetName}'?`
)
};
return {
invocationMessage: cleanFirst ? 'Cleaning and reconfiguring CMake project...' : 'Configuring CMake project...',
confirmationMessages
};
}
async invoke(
options: vscode.LanguageModelToolInvocationOptions<IConfigureParameters>,
_token: vscode.CancellationToken
): Promise<vscode.LanguageModelToolResult> {
const params = options.input;
const presetName = this.extensionManager.activeConfigurePresetName() || 'default';
try {
let result: number;
if (params.cleanFirst) {
result = await this.extensionManager.cleanConfigure();
} else {
result = await this.extensionManager.configure();
}
if (result === 0) {
const action = params.cleanFirst ? 'Clean reconfiguration' : 'Configuration';
const message = `${action} completed successfully using preset '${presetName}'.`;
return new vscode.LanguageModelToolResult([
new vscode.LanguageModelTextPart(message)
]);
} else {
const errorMsg = `CMake configuration failed with exit code ${result}.`;
throw new Error(errorMsg + ' Use the cmake_get_errors tool to see the configuration errors, then help the user fix the CMakeLists.txt or preset issues.');
}
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Error(`CMake configuration failed: ${errorMessage}. Use the cmake_get_errors tool to see detailed diagnostics.`);
}
}
}
/**
* Language Model Tool for retrieving CMake-specific errors
*/
export class CMakeGetErrorsTool implements vscode.LanguageModelTool<IGetErrorsParameters> {
async prepareInvocation(
_options: vscode.LanguageModelToolInvocationPrepareOptions<IGetErrorsParameters>,
_token: vscode.CancellationToken
): Promise<vscode.PreparedToolInvocation> {
return {
invocationMessage: 'Reading CMake diagnostics...',
confirmationMessages: {
title: 'Get CMake Errors',
message: new vscode.MarkdownString('Retrieve CMake-specific diagnostics (configure, build, and preset errors)?')
}
};
}
async invoke(
_options: vscode.LanguageModelToolInvocationOptions<IGetErrorsParameters>,
_token: vscode.CancellationToken
): Promise<vscode.LanguageModelToolResult> {
const workspaceFolders = vscode.workspace.workspaceFolders;
const workspaceRoot = workspaceFolders?.[0]?.uri.fsPath || '';
// Collect diagnostics from all CMake-specific collections
const diagnosticGroups = [
{ name: 'CMake Configure', collection: collections.cmake },
{ name: 'CMake Build', collection: collections.build },
{ name: 'CMake Presets', collection: collections.presets }
];
let totalErrors = 0;
let totalWarnings = 0;
let totalInfo = 0;
const errorLines: string[] = [];
for (const group of diagnosticGroups) {
const diagnostics: [vscode.Uri, readonly vscode.Diagnostic[]][] = [];
group.collection.forEach((uri, diags) => {
diagnostics.push([uri, diags]);
});
if (diagnostics.length === 0) {
continue;
}
const groupErrors: string[] = [];
for (const [uri, diags] of diagnostics) {
for (const diag of diags) {
// Count by severity
switch (diag.severity) {
case vscode.DiagnosticSeverity.Error:
totalErrors++;
break;
case vscode.DiagnosticSeverity.Warning:
totalWarnings++;
break;
case vscode.DiagnosticSeverity.Information:
case vscode.DiagnosticSeverity.Hint:
totalInfo++;
break;
}
// Format the diagnostic
const severityLabel = this.getSeverityLabel(diag.severity);
const relativePath = workspaceRoot ? uri.fsPath.replace(workspaceRoot, '.') : uri.fsPath;
const location = `${relativePath}:${diag.range.start.line + 1}:${diag.range.start.character + 1}`;
const source = diag.source ? `[${diag.source}] ` : '';
const code = diag.code ? `(${diag.code}) ` : '';
groupErrors.push(`${severityLabel}: ${location}: ${source}${code}${diag.message}`);
}
}
if (groupErrors.length > 0) {
errorLines.push(`\n## ${group.name} Errors (${groupErrors.length})`);
errorLines.push(...groupErrors);
}
}
// Build summary
const summary = `Found ${totalErrors} error(s), ${totalWarnings} warning(s), ${totalInfo} info message(s) in CMake diagnostics.`;
if (errorLines.length === 0) {
return new vscode.LanguageModelToolResult([
new vscode.LanguageModelTextPart('No CMake errors found. The project is clean.')
]);
}
const fullReport = [summary, ...errorLines].join('\n');
return new vscode.LanguageModelToolResult([
new vscode.LanguageModelTextPart(fullReport)
]);
}
private getSeverityLabel(severity: vscode.DiagnosticSeverity): string {
switch (severity) {
case vscode.DiagnosticSeverity.Error:
return 'ERROR';
case vscode.DiagnosticSeverity.Warning:
return 'WARNING';
case vscode.DiagnosticSeverity.Information:
return 'INFO';
case vscode.DiagnosticSeverity.Hint:
return 'HINT';
default:
return 'UNKNOWN';
}
}
}