|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +import Logger from '../common/logger'; |
| 8 | +import { IssueModel } from '../github/issueModel'; |
| 9 | + |
| 10 | +export interface ComplexityScore { |
| 11 | + score: number; |
| 12 | + reasoning?: string; |
| 13 | +} |
| 14 | + |
| 15 | +export class ComplexityService { |
| 16 | + private static readonly ID = 'ComplexityService'; |
| 17 | + private _cache = new Map<string, ComplexityScore>(); |
| 18 | + |
| 19 | + /** |
| 20 | + * Calculate complexity score for an issue using VS Code's LM API |
| 21 | + * @param issue The issue to calculate complexity for |
| 22 | + * @returns A complexity score from 1-100 (1 = simple, 100 = very complex) |
| 23 | + */ |
| 24 | + async calculateComplexity(issue: IssueModel): Promise<ComplexityScore> { |
| 25 | + const cacheKey = `${issue.number}-${issue.updatedAt}`; |
| 26 | + |
| 27 | + // Check cache first |
| 28 | + if (this._cache.has(cacheKey)) { |
| 29 | + return this._cache.get(cacheKey)!; |
| 30 | + } |
| 31 | + |
| 32 | + try { |
| 33 | + const models = await vscode.lm.selectChatModels({ vendor: 'copilot' }); |
| 34 | + if (models.length === 0) { |
| 35 | + Logger.debug('No language model available for complexity calculation', ComplexityService.ID); |
| 36 | + return { score: 50 }; // Default to medium complexity |
| 37 | + } |
| 38 | + |
| 39 | + const model = models[0]; |
| 40 | + const prompt = this.createComplexityPrompt(issue); |
| 41 | + |
| 42 | + const messages = [ |
| 43 | + vscode.LanguageModelChatMessage.User(prompt) |
| 44 | + ]; |
| 45 | + |
| 46 | + const request = await model.sendRequest(messages, { |
| 47 | + justification: 'Calculating issue complexity to help prioritize developer work' |
| 48 | + }); |
| 49 | + |
| 50 | + let response = ''; |
| 51 | + for await (const fragment of request.text) { |
| 52 | + response += fragment; |
| 53 | + } |
| 54 | + |
| 55 | + const complexityScore = this.parseComplexityResponse(response); |
| 56 | + |
| 57 | + // Cache the result |
| 58 | + this._cache.set(cacheKey, complexityScore); |
| 59 | + |
| 60 | + return complexityScore; |
| 61 | + } catch (error) { |
| 62 | + Logger.error(`Failed to calculate complexity for issue #${issue.number}: ${error}`, ComplexityService.ID); |
| 63 | + return { score: 50 }; // Default to medium complexity on error |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Create a prompt for the language model to analyze issue complexity |
| 69 | + */ |
| 70 | + private createComplexityPrompt(issue: IssueModel): string { |
| 71 | + const labels = issue.item.labels?.map(label => label.name).join(', ') || 'None'; |
| 72 | + const assignees = issue.assignees?.map(a => a.login).join(', ') || 'None'; |
| 73 | + |
| 74 | + return `Analyze the complexity of this GitHub issue and provide a score from 1-100 where: |
| 75 | +- 1-20: Very simple (typo fixes, minor documentation updates) |
| 76 | +- 21-40: Simple (small feature additions, simple bug fixes) |
| 77 | +- 41-60: Medium (moderate features, complex bug fixes) |
| 78 | +- 61-80: Complex (large features, architectural changes) |
| 79 | +- 81-100: Very complex (major system overhauls, complex integrations) |
| 80 | +
|
| 81 | +Issue Details: |
| 82 | +Title: ${issue.title} |
| 83 | +Description: ${issue.body || 'No description provided'} |
| 84 | +Labels: ${labels} |
| 85 | +Assignees: ${assignees} |
| 86 | +State: ${issue.state} |
| 87 | +Milestone: ${issue.milestone?.title || 'None'} |
| 88 | +
|
| 89 | +Please respond with ONLY a JSON object in this format: |
| 90 | +{ |
| 91 | + "score": <number between 1-100>, |
| 92 | + "reasoning": "<brief explanation of why this score was chosen>" |
| 93 | +}`; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Parse the language model response to extract complexity score |
| 98 | + */ |
| 99 | + private parseComplexityResponse(response: string): ComplexityScore { |
| 100 | + try { |
| 101 | + // Try to extract JSON from the response |
| 102 | + const jsonMatch = response.match(/\{[\s\S]*\}/); |
| 103 | + if (jsonMatch) { |
| 104 | + const parsed = JSON.parse(jsonMatch[0]); |
| 105 | + if (typeof parsed.score === 'number' && parsed.score >= 1 && parsed.score <= 100) { |
| 106 | + return { |
| 107 | + score: Math.round(parsed.score), |
| 108 | + reasoning: parsed.reasoning || undefined |
| 109 | + }; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Fallback: look for just a number in the response |
| 114 | + const numberMatch = response.match(/\b(\d{1,3})\b/); |
| 115 | + if (numberMatch) { |
| 116 | + const score = parseInt(numberMatch[1], 10); |
| 117 | + if (score >= 1 && score <= 100) { |
| 118 | + return { score }; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + Logger.debug(`Could not parse complexity response: ${response}`, ComplexityService.ID); |
| 123 | + return { score: 50 }; |
| 124 | + } catch (error) { |
| 125 | + Logger.error(`Error parsing complexity response: ${error}`, ComplexityService.ID); |
| 126 | + return { score: 50 }; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Clear the cache (useful for testing or when issues are updated) |
| 132 | + */ |
| 133 | + clearCache(): void { |
| 134 | + this._cache.clear(); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Get cached complexity score if available |
| 139 | + */ |
| 140 | + getCachedComplexity(issue: IssueModel): ComplexityScore | undefined { |
| 141 | + const cacheKey = `${issue.number}-${issue.updatedAt}`; |
| 142 | + return this._cache.get(cacheKey); |
| 143 | + } |
| 144 | +} |
0 commit comments