Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompts/node/inline/inlineChatGenerateCodePrompt.tsx
13404 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 { ConfigKey, IConfigurationService } from '../../../../platform/configuration/common/configurationService';
8
import { IIgnoreService } from '../../../../platform/ignore/common/ignoreService';
9
import { KnownSources } from '../../../../platform/languageServer/common/languageContextService';
10
import { IParserService } from '../../../../platform/parser/node/parserService';
11
import { IExperimentationService } from '../../../../platform/telemetry/common/nullExperimentationService';
12
import { isNotebookCellOrNotebookChatInput } from '../../../../util/common/notebooks';
13
import { illegalArgument } from '../../../../util/vs/base/common/errors';
14
import { GenericInlinePromptProps } from '../../../context/node/resolvers/genericInlineIntentInvocation';
15
import { SelectionSplitKind, SummarizedDocumentData, SummarizedDocumentWithSelection } from '../../../intents/node/testIntent/summarizedDocumentWithSelection';
16
import { EarlyStopping, LeadingMarkdownStreaming } from '../../../prompt/node/intents';
17
import { TextPieceClassifiers } from '../../../prompt/node/streamingEdits';
18
import { InstructionMessage } from '../base/instructionMessage';
19
import { LegacySafetyRules } from '../base/safetyRules';
20
import { Tag } from '../base/tag';
21
import { ChatToolReferences, ChatVariables, UserQuery } from '../panel/chatVariables';
22
import { HistoryWithInstructions } from '../panel/conversationHistory';
23
import { CustomInstructions } from '../panel/customInstructions';
24
import { ProjectLabels } from '../panel/projectLabels';
25
import { LanguageServerContextPrompt } from './languageServerContextPrompt';
26
import { SummarizedDocumentSplit } from './promptingSummarizedDocument';
27
28
export interface InlineChatGenerateCodePromptProps extends GenericInlinePromptProps {
29
}
30
31
export class InlineChatGenerateCodePrompt extends PromptElement<InlineChatGenerateCodePromptProps> {
32
33
constructor(
34
props: InlineChatGenerateCodePromptProps,
35
@IIgnoreService private readonly _ignoreService: IIgnoreService,
36
@IParserService private readonly _parserService: IParserService,
37
@IExperimentationService private readonly _experimentationService: IExperimentationService,
38
@IConfigurationService private readonly _configurationService: IConfigurationService
39
) {
40
super(props);
41
}
42
43
async render(state: void, sizing: PromptSizing) {
44
const context = this.props.documentContext;
45
const document = context.document;
46
const languageId = document.languageId;
47
48
if (isNotebookCellOrNotebookChatInput(document.uri)) {
49
throw illegalArgument('InlineChatGenerateCodePrompt should not be used with a notebook!');
50
}
51
52
if (languageId === 'markdown') {
53
throw illegalArgument('InlineChatGenerateCodePrompt should not be used with a markdown document!');
54
}
55
56
const isIgnored = await this._ignoreService.isCopilotIgnored(document.uri);
57
if (isIgnored) {
58
return <ignoredFiles value={[document.uri]} />;
59
}
60
61
const { query, history, chatVariables, } = this.props.promptContext;
62
63
const useProjectLabels = this._configurationService.getExperimentBasedConfig(ConfigKey.Advanced.ProjectLabelsInline, this._experimentationService);
64
65
const data = await SummarizedDocumentData.create(this._parserService, document, context.fileIndentInfo, context.wholeRange, SelectionSplitKind.OriginalEnd);
66
67
const replyInterpreterFn = (splitDoc: SummarizedDocumentSplit) => splitDoc.createReplyInterpreter(
68
LeadingMarkdownStreaming.Mute,
69
EarlyStopping.StopAfterFirstCodeBlock,
70
splitDoc.insertStreaming,
71
TextPieceClassifiers.createCodeBlockClassifier(),
72
line => line.value.trim() !== data.placeholderText
73
);
74
75
return (
76
<>
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 expert in programming, and especially good at {languageId}.<br />
81
<LegacySafetyRules />
82
</SystemMessage >
83
<HistoryWithInstructions inline={true} historyPriority={700} history={history} passPriority>
84
<InstructionMessage priority={1000}>
85
Source code is always contained in ``` blocks.<br />
86
The user needs help to write some new code.<br />
87
{data.hasContent && <>The user includes existing code 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 code from the user in your reply.<br /></>}
90
{!data.hasContent && <>Your must generate a code block surrounded with ``` that will be used in a new file<br /></>}
91
</InstructionMessage>
92
</HistoryWithInstructions>
93
{useProjectLabels && <ProjectLabels priority={600} embeddedInsideUserMessage={false} />}
94
<UserMessage priority={725}>
95
<CustomInstructions languageId={languageId} chatVariables={chatVariables} />
96
<LanguageServerContextPrompt priority={700} document={document} position={context.selection.start} requestId={this.props.promptContext.requestId} source={KnownSources.chat} />
97
</UserMessage>
98
<ChatToolReferences priority={750} promptContext={this.props.promptContext} flexGrow={1} embeddedInsideUserMessage={false} />
99
<ChatVariables priority={750} chatVariables={chatVariables} embeddedInsideUserMessage={false} />
100
<UserMessage priority={900} flexGrow={2} flexReserve={sizing.endpoint.modelMaxPromptTokens / 3}>
101
<SummarizedDocumentWithSelection
102
flexGrow={1}
103
tokenBudget={'usePromptSizingBudget'}
104
documentData={data}
105
createReplyInterpreter={replyInterpreterFn}
106
/>
107
<Tag name='userPrompt'>
108
<UserQuery chatVariables={chatVariables} query={query} /><br />
109
</Tag>
110
{data.hasContent && <>The code that would fit at {data.placeholderText} with ``` is:</>}
111
</UserMessage>
112
</>
113
);
114
}
115
}
116
117