Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/intents/node/explainIntent.ts
13399 views
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 l10n from '@vscode/l10n';
7
import type * as vscode from 'vscode';
8
import { IResponsePart } from '../../../platform/chat/common/chatMLFetcher';
9
import { ChatLocation } from '../../../platform/chat/common/commonTypes';
10
import { TextDocumentSnapshot } from '../../../platform/editing/common/textDocumentSnapshot';
11
import { IEndpointProvider } from '../../../platform/endpoint/common/endpointProvider';
12
import { IChatEndpoint } from '../../../platform/networking/common/networking';
13
import { ITabsAndEditorsService } from '../../../platform/tabs/common/tabsAndEditorsService';
14
import { CancellationToken } from '../../../util/vs/base/common/cancellation';
15
import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';
16
import { Intent } from '../../common/constants';
17
import { IBuildPromptContext } from '../../prompt/common/intents';
18
import { IIntent, IIntentInvocation, IIntentInvocationContext, IIntentSlashCommandInfo, IResponseProcessorContext, StreamingMarkdownReplyInterpreter } from '../../prompt/node/intents';
19
import { PromptRenderer, RendererIntentInvocation } from '../../prompts/node/base/promptRenderer';
20
import { ExplainPrompt } from '../../prompts/node/panel/explain';
21
22
23
export const explainIntentPromptSnippet = 'Write an explanation for the active selection as paragraphs of text.';
24
25
class ExplainIntentInvocation extends RendererIntentInvocation implements IIntentInvocation {
26
27
protected readonly defaultQuery: string = 'Write an explanation for the code above as paragraphs of text.';
28
29
constructor(
30
intent: IIntent,
31
location: ChatLocation,
32
endpoint: IChatEndpoint,
33
@ITabsAndEditorsService private readonly tabsAndEditorsService: ITabsAndEditorsService,
34
@IInstantiationService private readonly instantiationService: IInstantiationService,
35
) {
36
super(intent, location, endpoint);
37
}
38
39
override async buildPrompt(promptParams: IBuildPromptContext, progress: vscode.Progress<vscode.ChatResponseProgressPart | vscode.ChatResponseReferencePart>, token: vscode.CancellationToken) {
40
if (promptParams.query === '') {
41
promptParams = { ...promptParams, query: this.defaultQuery };
42
}
43
return super.buildPrompt(promptParams, progress, token);
44
}
45
46
createRenderer(promptContext: IBuildPromptContext, endpoint: IChatEndpoint, progress: vscode.Progress<vscode.ChatResponseProgressPart | vscode.ChatResponseReferencePart>, token: vscode.CancellationToken) {
47
const editor = this.tabsAndEditorsService.activeTextEditor;
48
return PromptRenderer.create(this.instantiationService, endpoint, ExplainPrompt, {
49
promptContext,
50
document: editor ? TextDocumentSnapshot.create(editor?.document) : undefined,
51
selection: editor?.selection,
52
isInlineChat: this.location === ChatLocation.Editor,
53
endpoint
54
});
55
}
56
}
57
58
class InlineExplainIntentInvocation extends ExplainIntentInvocation implements IIntentInvocation {
59
60
protected override readonly defaultQuery = explainIntentPromptSnippet;
61
62
processResponse(context: IResponseProcessorContext, inputStream: AsyncIterable<IResponsePart>, outputStream: vscode.ChatResponseStream, token: CancellationToken): Promise<void> {
63
const replyInterpreter = new StreamingMarkdownReplyInterpreter();
64
return replyInterpreter.processResponse(context, inputStream, outputStream, token);
65
}
66
}
67
68
export class ExplainIntent implements IIntent {
69
70
static readonly ID = Intent.Explain;
71
readonly id: string = Intent.Explain;
72
readonly locations = [ChatLocation.Panel, ChatLocation.Editor, ChatLocation.Notebook];
73
readonly description: string = l10n.t('Explain how the code in your active editor works');
74
75
readonly commandInfo: IIntentSlashCommandInfo | undefined;
76
77
constructor(
78
@IEndpointProvider private readonly endpointProvider: IEndpointProvider,
79
@IInstantiationService private readonly instantiationService: IInstantiationService,
80
) { }
81
82
async invoke(invocationContext: IIntentInvocationContext): Promise<IIntentInvocation> {
83
const location = invocationContext.location;
84
const endpoint = await this.endpointProvider.getChatEndpoint(invocationContext.request);
85
if (location === ChatLocation.Editor) {
86
return this.instantiationService.createInstance(InlineExplainIntentInvocation, this, location, endpoint);
87
}
88
return this.instantiationService.createInstance(ExplainIntentInvocation, this, location, endpoint);
89
}
90
}
91
92