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