Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompts/node/panel/explain.tsx
13405 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 { BasePromptElementProps, PromptElement, PromptPiece, PromptSizing, SystemMessage, UserMessage } from '@vscode/prompt-tsx';
7
import type * as vscode from 'vscode';
8
import { TextDocumentSnapshot } from '../../../../platform/editing/common/textDocumentSnapshot';
9
import { ILanguageFeaturesService } from '../../../../platform/languages/common/languageFeaturesService';
10
import { IChatEndpoint } from '../../../../platform/networking/common/networking';
11
import { DiagnosticSeverity } from '../../../../vscodeTypes';
12
import { IBuildPromptContext } from '../../../prompt/common/intents';
13
import { CopilotIdentityRules } from '../base/copilotIdentity';
14
import { InstructionMessage } from '../base/instructionMessage';
15
import { ResponseTranslationRules } from '../base/responseTranslationRules';
16
import { LegacySafetyRules } from '../base/safetyRules';
17
import { ChatToolReferences, ChatVariablesAndQuery } from './chatVariables';
18
import { CodeBlockFormattingRules } from './codeBlockFormattingRules';
19
import { HistoryWithInstructions } from './conversationHistory';
20
import { CurrentSelection } from './currentSelection';
21
import { CustomInstructions } from './customInstructions';
22
import { EditorIntegrationRules } from './editorIntegrationRules';
23
import { ProjectLabels } from './projectLabels';
24
import { SymbolAtCursor } from './symbolAtCursor';
25
import { SymbolDefinitions } from './symbolDefinitions';
26
27
export interface ExplainPromptProps extends BasePromptElementProps {
28
promptContext: IBuildPromptContext;
29
endpoint: IChatEndpoint;
30
31
// We want these upfront if possible because these could change during async prompt rendering
32
document?: TextDocumentSnapshot;
33
selection?: vscode.Selection;
34
isInlineChat?: boolean;
35
}
36
37
export interface ExplainPromptState {
38
explainingDiagnostic: boolean;
39
}
40
41
export class ExplainPrompt extends PromptElement<ExplainPromptProps, ExplainPromptState> {
42
43
constructor(
44
props: ExplainPromptProps,
45
@ILanguageFeaturesService private readonly languageService: ILanguageFeaturesService,
46
) {
47
super(props);
48
}
49
50
override async prepare() {
51
let explainingDiagnostic = false;
52
const { document, selection } = this.props;
53
if (document?.uri && selection) {
54
const severeDiagnostics = this.languageService.getDiagnostics(document.uri);
55
const diagnosticsInSelection = severeDiagnostics.filter(d => !!d.range.intersection(selection));
56
const filteredDiagnostics = diagnosticsInSelection.filter(d => d.severity <= DiagnosticSeverity.Warning);
57
explainingDiagnostic = filteredDiagnostics.length > 0;
58
}
59
return { explainingDiagnostic };
60
}
61
62
override render(state: ExplainPromptState, sizing: PromptSizing): PromptPiece<any, any> | undefined {
63
let { query, history, chatVariables, } = this.props.promptContext;
64
chatVariables = chatVariables.filter(v => !v.reference.id.startsWith('vscode.implicit'));
65
return (
66
<>
67
<SystemMessage priority={1000}>
68
You are a world-class coding tutor. Your code explanations perfectly balance high-level concepts and granular details. Your approach ensures that students not only understand how to write code, but also grasp the underlying principles that guide effective programming.<br />
69
<CopilotIdentityRules />
70
<LegacySafetyRules />
71
</SystemMessage>
72
<HistoryWithInstructions inline={this.props.isInlineChat} historyPriority={600} passPriority history={history}>
73
<InstructionMessage priority={1000}>
74
<EditorIntegrationRules />
75
<ResponseTranslationRules />
76
<br />
77
Additional Rules<br />
78
Think step by step:<br />
79
1. Examine the provided code selection and any other context like user question, related errors, project details, class definitions, etc.<br />
80
2. If you are unsure about the code, concepts, or the user's question, ask clarifying questions.<br />
81
3. If the user provided a specific question or error, answer it based on the selected code and additional provided context. Otherwise focus on explaining the selected code.<br />
82
4. Provide suggestions if you see opportunities to improve code readability, performance, etc.<br />
83
<br />
84
Focus on being clear, helpful, and thorough without assuming extensive prior knowledge.<br />
85
Use developer-friendly terms and analogies in your explanations.<br />
86
Identify 'gotchas' or less obvious parts of the code that might trip up someone new.<br />
87
Provide clear and relevant examples aligned with any provided context.<br />
88
Use Markdown formatting in your answers.<br />
89
<CodeBlockFormattingRules />
90
</InstructionMessage>
91
</HistoryWithInstructions>
92
<ProjectLabels priority={700} embeddedInsideUserMessage={false} />
93
<UserMessage priority={750}>
94
<CustomInstructions languageId={undefined} chatVariables={chatVariables} />
95
</UserMessage>
96
<CurrentSelection document={this.props.document} range={this.props.selection} priority={900} />
97
<SymbolDefinitions document={this.props.document} range={this.props.selection} priority={800} embeddedInsideUserMessage={false} />
98
{!state.explainingDiagnostic && <SymbolAtCursor document={this.props.document} selection={this.props.selection} priority={800} />}
99
<ChatToolReferences priority={899} flexGrow={2} promptContext={this.props.promptContext} embeddedInsideUserMessage={false} />
100
<ChatVariablesAndQuery priority={900} chatVariables={chatVariables} query={query} embeddedInsideUserMessage={false} />
101
</>
102
);
103
}
104
}
105
106