Path: blob/main/extensions/copilot/src/extension/prompts/node/inline/inlineChatGenerateCodePrompt.tsx
13404 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 { PromptElement, PromptSizing, SystemMessage, UserMessage } from '@vscode/prompt-tsx';6import { ConfigKey, IConfigurationService } from '../../../../platform/configuration/common/configurationService';7import { IIgnoreService } from '../../../../platform/ignore/common/ignoreService';8import { KnownSources } from '../../../../platform/languageServer/common/languageContextService';9import { IParserService } from '../../../../platform/parser/node/parserService';10import { IExperimentationService } from '../../../../platform/telemetry/common/nullExperimentationService';11import { isNotebookCellOrNotebookChatInput } from '../../../../util/common/notebooks';12import { illegalArgument } from '../../../../util/vs/base/common/errors';13import { GenericInlinePromptProps } from '../../../context/node/resolvers/genericInlineIntentInvocation';14import { SelectionSplitKind, SummarizedDocumentData, SummarizedDocumentWithSelection } from '../../../intents/node/testIntent/summarizedDocumentWithSelection';15import { EarlyStopping, LeadingMarkdownStreaming } from '../../../prompt/node/intents';16import { TextPieceClassifiers } from '../../../prompt/node/streamingEdits';17import { InstructionMessage } from '../base/instructionMessage';18import { LegacySafetyRules } from '../base/safetyRules';19import { Tag } from '../base/tag';20import { ChatToolReferences, ChatVariables, UserQuery } from '../panel/chatVariables';21import { HistoryWithInstructions } from '../panel/conversationHistory';22import { CustomInstructions } from '../panel/customInstructions';23import { ProjectLabels } from '../panel/projectLabels';24import { LanguageServerContextPrompt } from './languageServerContextPrompt';25import { SummarizedDocumentSplit } from './promptingSummarizedDocument';2627export interface InlineChatGenerateCodePromptProps extends GenericInlinePromptProps {28}2930export class InlineChatGenerateCodePrompt extends PromptElement<InlineChatGenerateCodePromptProps> {3132constructor(33props: InlineChatGenerateCodePromptProps,34@IIgnoreService private readonly _ignoreService: IIgnoreService,35@IParserService private readonly _parserService: IParserService,36@IExperimentationService private readonly _experimentationService: IExperimentationService,37@IConfigurationService private readonly _configurationService: IConfigurationService38) {39super(props);40}4142async render(state: void, sizing: PromptSizing) {43const context = this.props.documentContext;44const document = context.document;45const languageId = document.languageId;4647if (isNotebookCellOrNotebookChatInput(document.uri)) {48throw illegalArgument('InlineChatGenerateCodePrompt should not be used with a notebook!');49}5051if (languageId === 'markdown') {52throw illegalArgument('InlineChatGenerateCodePrompt should not be used with a markdown document!');53}5455const isIgnored = await this._ignoreService.isCopilotIgnored(document.uri);56if (isIgnored) {57return <ignoredFiles value={[document.uri]} />;58}5960const { query, history, chatVariables, } = this.props.promptContext;6162const useProjectLabels = this._configurationService.getExperimentBasedConfig(ConfigKey.Advanced.ProjectLabelsInline, this._experimentationService);6364const data = await SummarizedDocumentData.create(this._parserService, document, context.fileIndentInfo, context.wholeRange, SelectionSplitKind.OriginalEnd);6566const replyInterpreterFn = (splitDoc: SummarizedDocumentSplit) => splitDoc.createReplyInterpreter(67LeadingMarkdownStreaming.Mute,68EarlyStopping.StopAfterFirstCodeBlock,69splitDoc.insertStreaming,70TextPieceClassifiers.createCodeBlockClassifier(),71line => line.value.trim() !== data.placeholderText72);7374return (75<>76<SystemMessage priority={1000}>77You are an AI programming assistant.<br />78When asked for your name, you must respond with "GitHub Copilot".<br />79You are a world class expert in programming, and especially good at {languageId}.<br />80<LegacySafetyRules />81</SystemMessage >82<HistoryWithInstructions inline={true} historyPriority={700} history={history} passPriority>83<InstructionMessage priority={1000}>84Source code is always contained in ``` blocks.<br />85The user needs help to write some new code.<br />86{data.hasContent && <>The user includes existing code and marks with {data.placeholderText} where the new code should go.<br /></>}87{data.hasContent && <>DO NOT include the text "{data.placeholderText}" in your reply.<br /></>}88{data.hasContent && <>DO NOT repeat any code from the user in your reply.<br /></>}89{!data.hasContent && <>Your must generate a code block surrounded with ``` that will be used in a new file<br /></>}90</InstructionMessage>91</HistoryWithInstructions>92{useProjectLabels && <ProjectLabels priority={600} embeddedInsideUserMessage={false} />}93<UserMessage priority={725}>94<CustomInstructions languageId={languageId} chatVariables={chatVariables} />95<LanguageServerContextPrompt priority={700} document={document} position={context.selection.start} requestId={this.props.promptContext.requestId} source={KnownSources.chat} />96</UserMessage>97<ChatToolReferences priority={750} promptContext={this.props.promptContext} flexGrow={1} embeddedInsideUserMessage={false} />98<ChatVariables priority={750} chatVariables={chatVariables} embeddedInsideUserMessage={false} />99<UserMessage priority={900} flexGrow={2} flexReserve={sizing.endpoint.modelMaxPromptTokens / 3}>100<SummarizedDocumentWithSelection101flexGrow={1}102tokenBudget={'usePromptSizingBudget'}103documentData={data}104createReplyInterpreter={replyInterpreterFn}105/>106<Tag name='userPrompt'>107<UserQuery chatVariables={chatVariables} query={query} /><br />108</Tag>109{data.hasContent && <>The code that would fit at {data.placeholderText} with ``` is:</>}110</UserMessage>111</>112);113}114}115116117