Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompts/node/inline/inlineChatGenerateMarkdownPrompt.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 { PromptElement, PromptElementProps, PromptReference, PromptSizing, SystemMessage, TextChunk, UserMessage } from '@vscode/prompt-tsx';
7
import type * as vscode from 'vscode';
8
import { IIgnoreService } from '../../../../platform/ignore/common/ignoreService';
9
import { isNotebookCellOrNotebookChatInput } from '../../../../util/common/notebooks';
10
import { illegalArgument } from '../../../../util/vs/base/common/errors';
11
import { IInstantiationService } from '../../../../util/vs/platform/instantiation/common/instantiation';
12
import { GenericInlinePromptProps } from '../../../context/node/resolvers/genericInlineIntentInvocation';
13
import { SelectionSplitKind, SummarizedDocumentData, SummarizedDocumentWithSelection } from '../../../intents/node/testIntent/summarizedDocumentWithSelection';
14
import { EarlyStopping, LeadingMarkdownStreaming } from '../../../prompt/node/intents';
15
import { TextPieceClassifiers } from '../../../prompt/node/streamingEdits';
16
import { InstructionMessage } from '../base/instructionMessage';
17
import { LegacySafetyRules } from '../base/safetyRules';
18
import { Tag } from '../base/tag';
19
import { ChatToolReferences, ChatVariables, UserQuery } from '../panel/chatVariables';
20
import { HistoryWithInstructions } from '../panel/conversationHistory';
21
import { CustomInstructions } from '../panel/customInstructions';
22
import { SafePromptElement } from '../panel/safeElements';
23
import { SummarizedDocumentSplit } from './promptingSummarizedDocument';
24
25
export interface InlineChatGenerateMarkdownPromptProps extends GenericInlinePromptProps {
26
}
27
28
export class InlineChatGenerateMarkdownPrompt extends PromptElement<InlineChatGenerateMarkdownPromptProps> {
29
30
constructor(
31
props: InlineChatGenerateMarkdownPromptProps,
32
@IIgnoreService private readonly _ignoreService: IIgnoreService,
33
@IInstantiationService private readonly _instantiationService: IInstantiationService,
34
) {
35
super(props);
36
}
37
38
async render(state: void, sizing: PromptSizing) {
39
const context = this.props.documentContext;
40
const document = context.document;
41
const languageId = document.languageId;
42
43
if (isNotebookCellOrNotebookChatInput(this.props.documentContext.document.uri)) {
44
throw illegalArgument('InlineChatGenerateMarkdownPrompt should not be used with a notebook!');
45
}
46
47
if (languageId !== 'markdown') {
48
throw illegalArgument('InlineChatGenerateMarkdownPrompt should only be used with markdown documents!');
49
}
50
51
const isIgnored = await this._ignoreService.isCopilotIgnored(context.document.uri);
52
if (isIgnored) {
53
return <ignoredFiles value={[this.props.documentContext.document.uri]} />;
54
}
55
56
const { query, history, chatVariables, } = this.props.promptContext;
57
58
const data = await this._instantiationService.invokeFunction(
59
SummarizedDocumentData.create,
60
context.document,
61
context.fileIndentInfo,
62
context.wholeRange,
63
SelectionSplitKind.OriginalEnd
64
);
65
66
const replyInterpreterFn = (splitDoc: SummarizedDocumentSplit) => splitDoc.createReplyInterpreter(
67
LeadingMarkdownStreaming.Mute,
68
EarlyStopping.None,
69
splitDoc.insertStreaming,
70
TextPieceClassifiers.createFencedBlockClassifier(MarkdownBlock.FenceSequence),
71
line => line.value.trim() !== data.placeholderText
72
);
73
74
return (
75
<>
76
{/* <meta value={new ReplyInterpreterMetaData(replyInterpreter)} /> */}
77
<SystemMessage priority={1000}>
78
You are an AI programming assistant.<br />
79
When asked for your name, you must respond with "GitHub Copilot".<br />
80
You are a world class markdown editor, very well versed in programming.<br />
81
<LegacySafetyRules />
82
</SystemMessage>
83
<HistoryWithInstructions inline={true} historyPriority={700} passPriority history={history}>
84
<InstructionMessage priority={1000}>
85
The user needs help to write some new markdown.<br />
86
The markdown is always delimited by {MarkdownBlock.FenceSequence}.<br />
87
{data.hasContent && <>The user includes existing markdown and marks with {data.placeholderText} where the new code should go.<br /></>}
88
{data.hasContent && <>DO NOT include the text "{data.placeholderText}" in your reply.<br /></>}
89
{data.hasContent && <>DO NOT repeat any markdown from the user in your reply.<br /></>}
90
{!data.hasContent && <>Your answer must begin and end with {MarkdownBlock.FenceSequence}<br /></>}
91
</InstructionMessage>
92
</HistoryWithInstructions>
93
<UserMessage priority={725}>
94
<CustomInstructions languageId={languageId} chatVariables={chatVariables} />
95
</UserMessage>
96
<ChatToolReferences priority={750} promptContext={this.props.promptContext} flexGrow={1} embeddedInsideUserMessage={false} />
97
<ChatVariables priority={750} chatVariables={chatVariables} embeddedInsideUserMessage={false} />
98
<UserMessage priority={900} flexGrow={2} flexReserve={sizing.endpoint.modelMaxPromptTokens / 3}>
99
<SummarizedDocumentWithSelection
100
flexGrow={1}
101
tokenBudget={'usePromptSizingBudget'}
102
documentData={data}
103
createReplyInterpreter={replyInterpreterFn}
104
/>
105
<Tag name='userPrompt'>
106
<UserQuery chatVariables={chatVariables} query={query} /><br />
107
</Tag>
108
{data.hasContent && <>Remember to start and end your answer with {MarkdownBlock.FenceSequence}. The markdown that would fit at {data.placeholderText} is:</>}
109
</UserMessage>
110
</>
111
);
112
}
113
}
114
115
export type MarkdownBlockProps = PromptElementProps<{
116
uri: vscode.Uri | null;
117
code: string;
118
references?: PromptReference[];
119
}>;
120
121
export class MarkdownBlock extends SafePromptElement<MarkdownBlockProps> {
122
123
public static FenceSequence = `-+-+-+-+-+`;
124
125
async render(state: void) {
126
const isIgnored = this.props.uri ? await this._ignoreService.isCopilotIgnored(this.props.uri) : false;
127
if (isIgnored) {
128
return this._handleFoulPrompt();
129
}
130
const fence = MarkdownBlock.FenceSequence;
131
const code = `${fence}\n${this.props.code}\n${fence}`;
132
return <TextChunk>{code}</TextChunk>;
133
}
134
}
135
136