-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcpServer.ts
More file actions
592 lines (496 loc) · 23 KB
/
mcpServer.ts
File metadata and controls
592 lines (496 loc) · 23 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
import * as vscode from 'vscode';
import { AIModelRouter, WorkspaceContext } from './aiModelRouter';
import * as path from 'path';
import * as fs from 'fs';
export class MCPServer {
private router: AIModelRouter;
private contextCache: Map<string, WorkspaceContext>;
constructor() {
this.router = new AIModelRouter();
this.contextCache = new Map();
}
async analyzeWorkspace(workspaceUri: vscode.Uri): Promise<any> {
const startTime = performance.now();
const workspaceKey = workspaceUri.toString();
try {
// Check cache first to improve performance
const cachedContext = this.contextCache.get(workspaceKey);
if (cachedContext && this.isCacheValid(cachedContext)) {
vscode.window.showInformationMessage('Using cached workspace analysis...');
return await this.router.routeRequest(cachedContext);
}
// Progress notification
const progressOptions = {
location: vscode.ProgressLocation.Notification,
title: 'Analyzing Workspace',
cancellable: true
};
return await vscode.window.withProgress(progressOptions, async (progress, token) => {
if (token.isCancellationRequested) {
return null;
}
progress.report({ increment: 10, message: 'Scanning workspace...' });
const files = await this.scanWorkspace(workspaceUri);
progress.report({ increment: 40, message: 'Building context...' });
const context = await this.buildContext(workspaceUri);
// Enhanced caching with timestamp
context.analysisTimestamp = Date.now();
this.contextCache.set(workspaceKey, context);
progress.report({ increment: 40, message: 'Routing AI request...' });
const result = await this.router.routeRequest(context);
const endTime = performance.now();
console.log(`Workspace analysis completed in ${endTime - startTime}ms`);
return result;
});
} catch (error: any) {
// Advanced error logging
console.error('Workspace Analysis Error:', {
message: error.message,
stack: error.stack,
workspaceUri: workspaceKey
});
// Provide more detailed error feedback
vscode.window.showErrorMessage(
`Workspace Analysis Failed: ${error.message}.
Check console for detailed error information.`,
'View Logs'
).then(selection => {
if (selection === 'View Logs') {
vscode.commands.executeCommand('workbench.action.toggleDevTools');
}
});
throw error;
}
}
// New method to validate cache
private isCacheValid(context: WorkspaceContext, maxAgeMinutes: number = 30): boolean {
if (!context.analysisTimestamp) return false;
const cacheAgeMinutes = (Date.now() - context.analysisTimestamp) / (1000 * 60);
return cacheAgeMinutes < maxAgeMinutes;
}
async analyzeFile(fileUri: vscode.Uri): Promise<any> {
try {
const document = await vscode.workspace.openTextDocument(fileUri);
const content = document.getText();
const fileName = path.basename(fileUri.fsPath);
const extension = path.extname(fileUri.fsPath);
// Build context for the file
const context: WorkspaceContext = {
files: [fileUri.fsPath],
language: new Map([[extension, 1]]),
dependencies: {},
projectType: '',
fileContents: new Map([[fileUri.fsPath, content]]),
folderStructure: {},
symbols: new Map(),
references: new Map(),
imports: new Map()
};
// Extract symbols and references
const symbols = await this.extractFileSymbols(document);
if (symbols.length > 0) {
context.symbols?.set(fileUri.fsPath, symbols);
}
const references = await this.findReferences(document);
if (references.length > 0) {
context.references?.set(fileUri.fsPath, references);
}
// Extract imports
context.imports?.set(fileUri.fsPath, this.extractImports(content, extension));
// Route to the appropriate AI model
return await this.router.routeRequest(context);
} catch (error: any) {
throw new Error(`File analysis failed: ${error.message}`);
}
}
async analyzeSelection(
selectedText: string,
document: vscode.TextDocument,
selection: vscode.Selection,
modelType?: string
): Promise<any> {
try {
const fileUri = document.uri;
const fileName = path.basename(fileUri.fsPath);
const extension = path.extname(fileUri.fsPath);
const content = document.getText();
// Build context for the selection
const context: WorkspaceContext = {
files: [fileUri.fsPath],
language: new Map([[extension, 1]]),
dependencies: {},
projectType: '',
fileContents: new Map([[fileUri.fsPath, content]]),
folderStructure: {},
symbols: new Map(),
references: new Map(),
imports: new Map(),
selectionMetadata: {
selectedText,
fileName,
language: document.languageId,
startLine: selection.start.line,
endLine: selection.end.line,
startCharacter: selection.start.character,
endCharacter: selection.end.character,
surroundingText: this.getSurroundingText(document, selection),
containingSymbol: await this.findContainingSymbol(document, selection)
}
};
// Extract symbols and imports for better context
const symbols = await this.extractFileSymbols(document);
if (symbols.length > 0) {
context.symbols?.set(fileUri.fsPath, symbols);
}
// Extract imports
context.imports?.set(fileUri.fsPath, this.extractImports(content, extension));
// Route to the appropriate AI model (or use specified model)
if (modelType && this.router.getAvailableModelTypes().includes(modelType)) {
return await this.router.routeRequestToModel(context, modelType);
} else {
return await this.router.routeRequest(context);
}
} catch (error: any) {
throw new Error(`Selection analysis failed: ${error.message}`);
}
}
async updateFileInContext(document: vscode.TextDocument) {
const fileUri = document.uri;
const workspaceFolder = vscode.workspace.getWorkspaceFolder(fileUri);
if (!workspaceFolder) {
return;
}
const workspaceKey = workspaceFolder.uri.toString();
const cachedContext = this.contextCache.get(workspaceKey);
if (!cachedContext) {
return;
}
// Update the file content in the cached context
const content = document.getText();
cachedContext.fileContents.set(fileUri.fsPath, content);
// Update symbols and imports
const symbols = await this.extractFileSymbols(document);
if (symbols.length > 0) {
cachedContext.symbols?.set(fileUri.fsPath, symbols);
}
const extension = path.extname(fileUri.fsPath);
const imports = this.extractImports(content, extension);
cachedContext.imports?.set(fileUri.fsPath, imports);
// Update references
const references = await this.findReferences(document);
if (references.length > 0) {
cachedContext.references?.set(fileUri.fsPath, references);
}
// Update semantic relationships if they exist
if (cachedContext.semanticRelationships) {
this.buildSemanticRelationships(cachedContext);
}
// Update the cache
this.contextCache.set(workspaceKey, cachedContext);
}
getAvailableModelTypes(): string[] {
return this.router.getAvailableModelTypes();
}
private async scanWorkspace(uri: vscode.Uri): Promise<vscode.Uri[]> {
const pattern = new vscode.RelativePattern(uri, '**/*');
const excludePattern = '{**/node_modules/**,**/dist/**,**/build/**,**/.git/**}';
const files = await vscode.workspace.findFiles(pattern, excludePattern);
return files;
}
private async buildContext(uri: vscode.Uri): Promise<WorkspaceContext> {
// Scan the workspace for files
const files = await this.scanWorkspace(uri);
// Initialize context
const context: WorkspaceContext = {
files: files.map(f => f.fsPath),
language: new Map<string, number>(),
dependencies: {},
projectType: '',
fileContents: new Map<string, string>(),
folderStructure: {},
symbols: new Map<string, any[]>(),
references: new Map<string, any[]>(),
imports: new Map<string, any[]>()
};
// First pass: gather file contents, language stats, and imports
for (const file of files) {
try {
const filePath = file.fsPath;
const extension = path.extname(filePath);
// Update language stats
const count = context.language.get(extension) || 0;
context.language.set(extension, count + 1);
// Read file content
try {
const document = await vscode.workspace.openTextDocument(file);
const content = document.getText();
context.fileContents.set(filePath, content);
// Special handling for package.json
if (filePath.endsWith('package.json')) {
try {
const packageJson = JSON.parse(content);
context.dependencies = {
...packageJson.dependencies || {},
...packageJson.devDependencies || {}
};
context.projectType = this.determineProjectType(packageJson);
} catch (e) {
console.error('Error parsing package.json:', e);
}
}
// Extract imports for this file
if (context.imports) {
context.imports.set(filePath, this.extractImports(content, extension));
}
} catch (error) {
console.error(`Error processing file ${filePath}:`, error);
}
} catch (error) {
console.error(`Error processing file ${file.fsPath}:`, error);
}
}
// Second pass: extract symbols and references
for (const file of files) {
try {
const document = await vscode.workspace.openTextDocument(file);
// Extract symbols (classes, functions, etc.)
const symbols = await this.extractFileSymbols(document);
if (symbols.length > 0 && context.symbols) {
context.symbols.set(file.fsPath, symbols);
}
// Find references to other files/symbols
const references = await this.findReferences(document);
if (references.length > 0 && context.references) {
context.references.set(file.fsPath, references);
}
} catch (error) {
console.error(`Error extracting symbols from ${file.fsPath}:`, error);
}
}
// Build semantic relationships between files
this.buildSemanticRelationships(context);
// Cache the context for future use
const workspaceKey = uri.toString();
this.contextCache.set(workspaceKey, context);
return context;
}
public async extractFileSymbols(document: vscode.TextDocument): Promise<any[]> {
try {
const symbols = await vscode.commands.executeCommand<vscode.DocumentSymbol[]>(
'vscode.executeDocumentSymbolProvider',
document.uri
) || [];
return this.flattenSymbols(symbols);
} catch (error: any) {
console.error('Error extracting symbols:', error);
return [];
}
}
private flattenSymbols(symbols: vscode.DocumentSymbol[], parent: string = '') {
let result: any[] = [];
for (const symbol of symbols) {
const fullName = parent ? `${parent}.${symbol.name}` : symbol.name;
const flatSymbol = {
name: symbol.name,
fullName,
kind: symbol.kind,
range: {
start: symbol.range.start,
end: symbol.range.end
},
detail: symbol.detail
};
result.push(flatSymbol);
if (symbol.children && symbol.children.length > 0) {
result = result.concat(this.flattenSymbols(symbol.children, fullName));
}
}
return result;
}
private async findReferences(document: vscode.TextDocument): Promise<any[]> {
const references: any[] = [];
try {
// This is a simplified approach. In a real implementation,
// you would need to iterate through symbols and find references for each
const text = document.getText();
const importMatches = text.match(/import\s+.*?from\s+['"](.+?)['"]/g) || [];
for (const match of importMatches) {
const importPath = match.match(/from\s+['"](.+?)['"]/)?.[1];
if (importPath) {
references.push({
type: 'import',
path: importPath
});
}
}
return references;
} catch (error: any) {
console.error('Error finding references:', error);
return references;
}
}
private extractImports(content: string, extension: string) {
const imports: any[] = [];
try {
if (['.js', '.ts', '.jsx', '.tsx'].includes(extension)) {
// JavaScript/TypeScript imports
const importRegex = /import\s+(?:{([^}]+)}|([^{}\s;]+))\s+from\s+['"]([^'"]+)['"]/g;
let match;
while ((match = importRegex.exec(content)) !== null) {
const namedImports = match[1] ? match[1].split(',').map(s => s.trim()) : [];
const defaultImport = match[2] ? match[2].trim() : null;
const source = match[3];
imports.push({
source,
defaultImport,
namedImports
});
}
} else if (extension === '.py') {
// Python imports
const importRegex = /(?:from\s+([^\s]+)\s+)?import\s+([^;\n]+)/g;
let match;
while ((match = importRegex.exec(content)) !== null) {
const fromModule = match[1] || null;
const importedItems = match[2].split(',').map(s => s.trim());
imports.push({
fromModule,
importedItems
});
}
}
} catch (error: any) {
console.error('Error extracting imports:', error);
}
return imports;
}
private buildSemanticRelationships(context: WorkspaceContext) {
// Initialize semantic relationships map if it doesn't exist
if (!context.semanticRelationships) {
context.semanticRelationships = new Map();
}
// Analyze imports to build relationships
if (context.imports) {
context.imports.forEach((imports, filePath) => {
const relationships: string[] = [];
imports.forEach(importPath => {
// Handle relative imports
if (importPath.startsWith('.')) {
const resolved = path.resolve(path.dirname(filePath), importPath);
// Find the matching file in our context
const matchingFile = context.files.find(f =>
f === resolved ||
f === resolved + '.js' ||
f === resolved + '.ts' ||
f === resolved + '.jsx' ||
f === resolved + '.tsx'
);
if (matchingFile) {
relationships.push(matchingFile);
}
}
});
if (relationships.length > 0 && context.semanticRelationships) {
context.semanticRelationships.set(filePath, relationships);
}
});
}
}
private addToFolderStructure(structure: any, pathParts: string[], content: string) {
let current = structure;
pathParts.forEach((part, index) => {
if (!current[part]) {
if (index === pathParts.length - 1) {
// It's a file
current[part] = {
type: 'file',
size: content.length,
extension: path.extname(part),
lastModified: fs.existsSync(path.join(...pathParts.slice(0, index + 1))) ?
fs.statSync(path.join(...pathParts.slice(0, index + 1))).mtime : null
};
} else {
// It's a directory
current[part] = {};
}
}
if (index < pathParts.length - 1) {
current = current[part];
}
});
}
private determineProjectType(packageJson: any): string {
const deps = { ...packageJson.dependencies, ...packageJson.devDependencies };
// Check for VS Code extension
if (deps['@types/vscode'] || packageJson.engines?.vscode) return 'vscode-extension';
// Check for web frameworks
if (deps['next']) return 'next-js';
if (deps['react']) return 'react';
if (deps['vue']) return 'vue';
if (deps['angular'] || deps['@angular/core']) return 'angular';
// Check for backend frameworks
if (deps['express']) return 'node-express';
if (deps['koa']) return 'node-koa';
if (deps['fastify']) return 'node-fastify';
if (deps['nestjs'] || deps['@nestjs/core']) return 'nestjs';
if (deps['django']) return 'django';
if (deps['flask']) return 'flask';
// Check for mobile
if (deps['react-native']) return 'react-native';
if (deps['ionic']) return 'ionic';
// Check for desktop
if (deps['electron']) return 'electron';
return packageJson.name || 'unknown';
}
private detectFileType(filePath: string, content: string): string {
const extension = path.extname(filePath).toLowerCase();
const fileName = path.basename(filePath).toLowerCase();
if (fileName === 'package.json') return 'npm-package';
if (fileName === 'tsconfig.json') return 'typescript-config';
if (fileName === 'webpack.config.js') return 'webpack-config';
if (fileName.includes('dockerfile')) return 'docker';
if (extension === '.ts' || extension === '.js') {
if (content.includes('vscode')) return 'vscode-extension';
if (content.includes('react')) return 'react';
if (content.includes('vue')) return 'vue';
if (content.includes('angular')) return 'angular';
if (content.includes('express')) return 'node-express';
}
if (extension === '.py') {
if (content.includes('django')) return 'django';
if (content.includes('flask')) return 'flask';
}
if (extension === '.md') return 'documentation';
if (extension === '.json') return 'json-data';
if (extension === '.css' || extension === '.scss' || extension === '.less') return 'styles';
if (extension === '.html') return 'html';
if (fileName.includes('test') || fileName.includes('spec')) return 'test';
return 'unknown';
}
async findContainingSymbol(document: vscode.TextDocument, selection: vscode.Selection): Promise<any | undefined> {
try {
const symbols = await this.extractFileSymbols(document);
// Find the symbol that contains the selection
return symbols.find(symbol => {
const symbolRange = new vscode.Range(
new vscode.Position(symbol.range.start.line, symbol.range.start.character),
new vscode.Position(symbol.range.end.line, symbol.range.end.character)
);
return symbolRange.contains(selection);
});
} catch (error) {
console.error('Error finding containing symbol:', error);
return undefined;
}
}
getSurroundingText(document: vscode.TextDocument, selection: vscode.Selection, contextLines: number = 5): string {
const startLine = Math.max(0, selection.start.line - contextLines);
const endLine = Math.min(document.lineCount - 1, selection.end.line + contextLines);
let surroundingText = '';
for (let i = startLine; i <= endLine; i++) {
const line = document.lineAt(i);
surroundingText += line.text + '\n';
}
return surroundingText;
}
}