Path: blob/main/extensions/copilot/src/extension/intents/node/explainIntent.ts
13399 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as l10n from '@vscode/l10n';6import type * as vscode from 'vscode';7import { IResponsePart } from '../../../platform/chat/common/chatMLFetcher';8import { ChatLocation } from '../../../platform/chat/common/commonTypes';9import { TextDocumentSnapshot } from '../../../platform/editing/common/textDocumentSnapshot';10import { IEndpointProvider } from '../../../platform/endpoint/common/endpointProvider';11import { IChatEndpoint } from '../../../platform/networking/common/networking';12import { ITabsAndEditorsService } from '../../../platform/tabs/common/tabsAndEditorsService';13import { CancellationToken } from '../../../util/vs/base/common/cancellation';14import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';15import { Intent } from '../../common/constants';16import { IBuildPromptContext } from '../../prompt/common/intents';17import { IIntent, IIntentInvocation, IIntentInvocationContext, IIntentSlashCommandInfo, IResponseProcessorContext, StreamingMarkdownReplyInterpreter } from '../../prompt/node/intents';18import { PromptRenderer, RendererIntentInvocation } from '../../prompts/node/base/promptRenderer';19import { ExplainPrompt } from '../../prompts/node/panel/explain';202122export const explainIntentPromptSnippet = 'Write an explanation for the active selection as paragraphs of text.';2324class ExplainIntentInvocation extends RendererIntentInvocation implements IIntentInvocation {2526protected readonly defaultQuery: string = 'Write an explanation for the code above as paragraphs of text.';2728constructor(29intent: IIntent,30location: ChatLocation,31endpoint: IChatEndpoint,32@ITabsAndEditorsService private readonly tabsAndEditorsService: ITabsAndEditorsService,33@IInstantiationService private readonly instantiationService: IInstantiationService,34) {35super(intent, location, endpoint);36}3738override async buildPrompt(promptParams: IBuildPromptContext, progress: vscode.Progress<vscode.ChatResponseProgressPart | vscode.ChatResponseReferencePart>, token: vscode.CancellationToken) {39if (promptParams.query === '') {40promptParams = { ...promptParams, query: this.defaultQuery };41}42return super.buildPrompt(promptParams, progress, token);43}4445createRenderer(promptContext: IBuildPromptContext, endpoint: IChatEndpoint, progress: vscode.Progress<vscode.ChatResponseProgressPart | vscode.ChatResponseReferencePart>, token: vscode.CancellationToken) {46const editor = this.tabsAndEditorsService.activeTextEditor;47return PromptRenderer.create(this.instantiationService, endpoint, ExplainPrompt, {48promptContext,49document: editor ? TextDocumentSnapshot.create(editor?.document) : undefined,50selection: editor?.selection,51isInlineChat: this.location === ChatLocation.Editor,52endpoint53});54}55}5657class InlineExplainIntentInvocation extends ExplainIntentInvocation implements IIntentInvocation {5859protected override readonly defaultQuery = explainIntentPromptSnippet;6061processResponse(context: IResponseProcessorContext, inputStream: AsyncIterable<IResponsePart>, outputStream: vscode.ChatResponseStream, token: CancellationToken): Promise<void> {62const replyInterpreter = new StreamingMarkdownReplyInterpreter();63return replyInterpreter.processResponse(context, inputStream, outputStream, token);64}65}6667export class ExplainIntent implements IIntent {6869static readonly ID = Intent.Explain;70readonly id: string = Intent.Explain;71readonly locations = [ChatLocation.Panel, ChatLocation.Editor, ChatLocation.Notebook];72readonly description: string = l10n.t('Explain how the code in your active editor works');7374readonly commandInfo: IIntentSlashCommandInfo | undefined;7576constructor(77@IEndpointProvider private readonly endpointProvider: IEndpointProvider,78@IInstantiationService private readonly instantiationService: IInstantiationService,79) { }8081async invoke(invocationContext: IIntentInvocationContext): Promise<IIntentInvocation> {82const location = invocationContext.location;83const endpoint = await this.endpointProvider.getChatEndpoint(invocationContext.request);84if (location === ChatLocation.Editor) {85return this.instantiationService.createInstance(InlineExplainIntentInvocation, this, location, endpoint);86}87return this.instantiationService.createInstance(ExplainIntentInvocation, this, location, endpoint);88}89}909192