|
| 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 | +'use strict'; |
| 6 | + |
| 7 | +import * as vscode from 'vscode'; |
| 8 | +import { PullRequestModel } from '../../github/pullRequestModel'; |
| 9 | +import { RepositoriesManager } from '../../github/repositoriesManager'; |
| 10 | + |
| 11 | +interface ResolveReviewThreadToolParameters { |
| 12 | + threadId: string; |
| 13 | +} |
| 14 | + |
| 15 | +export class ResolveReviewThreadTool implements vscode.LanguageModelTool<ResolveReviewThreadToolParameters> { |
| 16 | + public static readonly toolId = 'github-pull-request_resolveReviewThread'; |
| 17 | + |
| 18 | + constructor(private readonly folderManagers: RepositoriesManager) { } |
| 19 | + |
| 20 | + private _findActivePullRequest(): PullRequestModel | undefined { |
| 21 | + const folderManager = this.folderManagers.folderManagers.find((manager) => manager.activePullRequest); |
| 22 | + return folderManager?.activePullRequest; |
| 23 | + } |
| 24 | + |
| 25 | + async prepareInvocation(options: vscode.LanguageModelToolInvocationPrepareOptions<ResolveReviewThreadToolParameters>): Promise<vscode.PreparedToolInvocation> { |
| 26 | + const pullRequest = this._findActivePullRequest(); |
| 27 | + const threadId = options.input?.threadId; |
| 28 | + |
| 29 | + if (!pullRequest) { |
| 30 | + return { |
| 31 | + invocationMessage: vscode.l10n.t('Resolving review thread'), |
| 32 | + }; |
| 33 | + } |
| 34 | + |
| 35 | + const thread = pullRequest.reviewThreadsCache.find(t => t.id === threadId); |
| 36 | + const file = thread?.path ? ` in \`${thread.path}\`` : ''; |
| 37 | + const firstComment = thread?.comments[0]?.body; |
| 38 | + const snippet = firstComment ? `: "${firstComment.length > 60 ? firstComment.slice(0, 57) + '...' : firstComment}"` : ''; |
| 39 | + |
| 40 | + return { |
| 41 | + invocationMessage: vscode.l10n.t('Resolving review thread{0}{1}', file, snippet), |
| 42 | + }; |
| 43 | + } |
| 44 | + |
| 45 | + async invoke(options: vscode.LanguageModelToolInvocationOptions<ResolveReviewThreadToolParameters>, _token: vscode.CancellationToken): Promise<vscode.LanguageModelToolResult> { |
| 46 | + const pullRequest = this._findActivePullRequest(); |
| 47 | + if (!pullRequest) { |
| 48 | + return new vscode.LanguageModelToolResult([new vscode.LanguageModelTextPart('There is no active pull request.')]); |
| 49 | + } |
| 50 | + |
| 51 | + const { threadId } = options.input; |
| 52 | + if (!threadId) { |
| 53 | + return new vscode.LanguageModelToolResult([new vscode.LanguageModelTextPart('No threadId provided.')]); |
| 54 | + } |
| 55 | + |
| 56 | + const thread = pullRequest.reviewThreadsCache.find(t => t.id === threadId); |
| 57 | + if (!thread) { |
| 58 | + return new vscode.LanguageModelToolResult([new vscode.LanguageModelTextPart(`Review thread with id "${threadId}" not found on the active pull request.`)]); |
| 59 | + } |
| 60 | + |
| 61 | + if (thread.isResolved) { |
| 62 | + return new vscode.LanguageModelToolResult([new vscode.LanguageModelTextPart(`Review thread "${threadId}" is already resolved.`)]); |
| 63 | + } |
| 64 | + |
| 65 | + if (!thread.viewerCanResolve) { |
| 66 | + return new vscode.LanguageModelToolResult([new vscode.LanguageModelTextPart(`You do not have permission to resolve review thread "${threadId}".`)]); |
| 67 | + } |
| 68 | + |
| 69 | + await pullRequest.resolveReviewThread(threadId); |
| 70 | + return new vscode.LanguageModelToolResult([new vscode.LanguageModelTextPart(`Review thread "${threadId}" resolved successfully.`)]); |
| 71 | + } |
| 72 | +} |
0 commit comments