Path: blob/main/extensions/copilot/src/extension/prompts/node/inline/inlineChatEditMarkdownPrompt.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 { PromptElement, PromptSizing, SystemMessage, UserMessage } from '@vscode/prompt-tsx';6import { IIgnoreService } from '../../../../platform/ignore/common/ignoreService';7import { isNotebookCellOrNotebookChatInput } from '../../../../util/common/notebooks';8import { illegalArgument } from '../../../../util/vs/base/common/errors';9import { IInstantiationService } from '../../../../util/vs/platform/instantiation/common/instantiation';10import { GenericInlinePromptProps } from '../../../context/node/resolvers/genericInlineIntentInvocation';11import { SelectionSplitKind, SummarizedDocumentData, SummarizedDocumentWithSelection } from '../../../intents/node/testIntent/summarizedDocumentWithSelection';12import { EarlyStopping, LeadingMarkdownStreaming } from '../../../prompt/node/intents';13import { TextPieceClassifiers } from '../../../prompt/node/streamingEdits';14import { InstructionMessage } from '../base/instructionMessage';15import { LegacySafetyRules } from '../base/safetyRules';16import { Tag } from '../base/tag';17import { ChatToolReferences, ChatVariables, UserQuery } from '../panel/chatVariables';18import { HistoryWithInstructions } from '../panel/conversationHistory';19import { CustomInstructions } from '../panel/customInstructions';20import { MarkdownBlock } from './inlineChatGenerateMarkdownPrompt';21import { SummarizedDocumentSplit } from './promptingSummarizedDocument';2223export interface InlineChatEditMarkdownPromptProps extends GenericInlinePromptProps {24}2526export class InlineChatEditMarkdownPrompt extends PromptElement<InlineChatEditMarkdownPromptProps> {2728constructor(29props: InlineChatEditMarkdownPromptProps,30@IIgnoreService private readonly _ignoreService: IIgnoreService,31@IInstantiationService private readonly _instantiationService: IInstantiationService32) {33super(props);34}3536async render(state: void, sizing: PromptSizing) {37const context = this.props.documentContext;38const document = context.document;39const languageId = document.languageId;4041if (isNotebookCellOrNotebookChatInput(this.props.documentContext.document.uri)) {42throw illegalArgument('InlineChatEditMarkdownPrompt should not be used with a notebook!');43}4445if (languageId !== 'markdown') {46throw illegalArgument('InlineChatEditMarkdownPrompt should only be used with markdown documents!');47}4849const isIgnored = await this._ignoreService.isCopilotIgnored(context.document.uri);50if (isIgnored) {51return <ignoredFiles value={[this.props.documentContext.document.uri]} />;52}5354const data = await this._instantiationService.invokeFunction(SummarizedDocumentData.create,55document,56context.fileIndentInfo,57context.wholeRange,58SelectionSplitKind.Adjusted59);6061const { query, history, chatVariables, } = this.props.promptContext;6263// const summarizedDocument = await createPromptingSummarizedDocument(64// this._parserService,65// context.document,66// context.fileIndentInfo,67// context.document.validateRange(context.wholeRange),68// sizing.endpoint.modelMaxPromptTokens / 3 // consume one 3rd of the model window69// );7071// const splitDoc = summarizedDocument.splitAroundAdjustedSelection();72// const { codeAbove, codeSelected, codeBelow, hasCodeWithoutSelection } = splitDoc;73// const placeHolder = '$SELECTION_PLACEHOLDER$';74// const codeWithoutSelection = `${codeAbove}${placeHolder}${codeBelow}`;75const replyInterpreterFn = (splitDoc: SummarizedDocumentSplit) => splitDoc.createReplyInterpreter(76LeadingMarkdownStreaming.Mute,77EarlyStopping.None,78splitDoc.replaceSelectionStreaming,79TextPieceClassifiers.createFencedBlockClassifier(MarkdownBlock.FenceSequence),80line => line.value.trim() !== data.placeholderText81);8283return (84<>85{/* <meta value={new ReplyInterpreterMetaData(replyInterpreter)} /> */}86<SystemMessage priority={1000}>87You are an AI programming assistant.<br />88When asked for your name, you must respond with "GitHub Copilot".<br />89You are a world class markdown editor, very well versed in programming.<br />90The user needs help to modify some markdown content.<br />91<LegacySafetyRules />92</SystemMessage>93<HistoryWithInstructions inline={true} historyPriority={700} passPriority history={history}>94<InstructionMessage priority={1000}>95The markdown is always delimited by {MarkdownBlock.FenceSequence}.<br />96Your answer must begin and end with {MarkdownBlock.FenceSequence}.<br />97</InstructionMessage>98</HistoryWithInstructions>99<UserMessage priority={725}>100<CustomInstructions languageId={languageId} chatVariables={chatVariables} />101</UserMessage>102<ChatToolReferences priority={750} promptContext={this.props.promptContext} flexGrow={1} embeddedInsideUserMessage={false} />103<ChatVariables priority={750} chatVariables={chatVariables} embeddedInsideUserMessage={false} />104<UserMessage priority={900}105flexGrow={2}106flexReserve={sizing.endpoint.modelMaxPromptTokens / 3}>107<SummarizedDocumentWithSelection108documentData={data}109createReplyInterpreter={replyInterpreterFn}110tokenBudget={'usePromptSizingBudget'}111/>112<Tag name='userPrompt'>113<UserQuery chatVariables={chatVariables} query={query} /><br />114The rewritten markdown content that would fit at {data.placeholderText} wrapped with {MarkdownBlock.FenceSequence} is:115</Tag>116</UserMessage>117</>118);119}120}121122123