Path: blob/main/extensions/copilot/src/extension/prompts/node/panel/explain.tsx
13405 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 { BasePromptElementProps, PromptElement, PromptPiece, PromptSizing, SystemMessage, UserMessage } from '@vscode/prompt-tsx';6import type * as vscode from 'vscode';7import { TextDocumentSnapshot } from '../../../../platform/editing/common/textDocumentSnapshot';8import { ILanguageFeaturesService } from '../../../../platform/languages/common/languageFeaturesService';9import { IChatEndpoint } from '../../../../platform/networking/common/networking';10import { DiagnosticSeverity } from '../../../../vscodeTypes';11import { IBuildPromptContext } from '../../../prompt/common/intents';12import { CopilotIdentityRules } from '../base/copilotIdentity';13import { InstructionMessage } from '../base/instructionMessage';14import { ResponseTranslationRules } from '../base/responseTranslationRules';15import { LegacySafetyRules } from '../base/safetyRules';16import { ChatToolReferences, ChatVariablesAndQuery } from './chatVariables';17import { CodeBlockFormattingRules } from './codeBlockFormattingRules';18import { HistoryWithInstructions } from './conversationHistory';19import { CurrentSelection } from './currentSelection';20import { CustomInstructions } from './customInstructions';21import { EditorIntegrationRules } from './editorIntegrationRules';22import { ProjectLabels } from './projectLabels';23import { SymbolAtCursor } from './symbolAtCursor';24import { SymbolDefinitions } from './symbolDefinitions';2526export interface ExplainPromptProps extends BasePromptElementProps {27promptContext: IBuildPromptContext;28endpoint: IChatEndpoint;2930// We want these upfront if possible because these could change during async prompt rendering31document?: TextDocumentSnapshot;32selection?: vscode.Selection;33isInlineChat?: boolean;34}3536export interface ExplainPromptState {37explainingDiagnostic: boolean;38}3940export class ExplainPrompt extends PromptElement<ExplainPromptProps, ExplainPromptState> {4142constructor(43props: ExplainPromptProps,44@ILanguageFeaturesService private readonly languageService: ILanguageFeaturesService,45) {46super(props);47}4849override async prepare() {50let explainingDiagnostic = false;51const { document, selection } = this.props;52if (document?.uri && selection) {53const severeDiagnostics = this.languageService.getDiagnostics(document.uri);54const diagnosticsInSelection = severeDiagnostics.filter(d => !!d.range.intersection(selection));55const filteredDiagnostics = diagnosticsInSelection.filter(d => d.severity <= DiagnosticSeverity.Warning);56explainingDiagnostic = filteredDiagnostics.length > 0;57}58return { explainingDiagnostic };59}6061override render(state: ExplainPromptState, sizing: PromptSizing): PromptPiece<any, any> | undefined {62let { query, history, chatVariables, } = this.props.promptContext;63chatVariables = chatVariables.filter(v => !v.reference.id.startsWith('vscode.implicit'));64return (65<>66<SystemMessage priority={1000}>67You 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 />68<CopilotIdentityRules />69<LegacySafetyRules />70</SystemMessage>71<HistoryWithInstructions inline={this.props.isInlineChat} historyPriority={600} passPriority history={history}>72<InstructionMessage priority={1000}>73<EditorIntegrationRules />74<ResponseTranslationRules />75<br />76Additional Rules<br />77Think step by step:<br />781. Examine the provided code selection and any other context like user question, related errors, project details, class definitions, etc.<br />792. If you are unsure about the code, concepts, or the user's question, ask clarifying questions.<br />803. 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 />814. Provide suggestions if you see opportunities to improve code readability, performance, etc.<br />82<br />83Focus on being clear, helpful, and thorough without assuming extensive prior knowledge.<br />84Use developer-friendly terms and analogies in your explanations.<br />85Identify 'gotchas' or less obvious parts of the code that might trip up someone new.<br />86Provide clear and relevant examples aligned with any provided context.<br />87Use Markdown formatting in your answers.<br />88<CodeBlockFormattingRules />89</InstructionMessage>90</HistoryWithInstructions>91<ProjectLabels priority={700} embeddedInsideUserMessage={false} />92<UserMessage priority={750}>93<CustomInstructions languageId={undefined} chatVariables={chatVariables} />94</UserMessage>95<CurrentSelection document={this.props.document} range={this.props.selection} priority={900} />96<SymbolDefinitions document={this.props.document} range={this.props.selection} priority={800} embeddedInsideUserMessage={false} />97{!state.explainingDiagnostic && <SymbolAtCursor document={this.props.document} selection={this.props.selection} priority={800} />}98<ChatToolReferences priority={899} flexGrow={2} promptContext={this.props.promptContext} embeddedInsideUserMessage={false} />99<ChatVariablesAndQuery priority={900} chatVariables={chatVariables} query={query} embeddedInsideUserMessage={false} />100</>101);102}103}104105106