Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompts/node/inline/inlineChatEditCodePrompt.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
29
export interface InlineChatEditCodePromptProps extends GenericInlinePromptProps {
30
readonly ignoreCustomInstructions?: boolean;
31
}
32
33
export class InlineChatEditCodePrompt extends PromptElement<InlineChatEditCodePromptProps> {
34
35
constructor(
36
props: InlineChatEditCodePromptProps,
37
@IIgnoreService private readonly _ignoreService: IIgnoreService,
38
@IParserService private readonly _parserService: IParserService,
39
@IExperimentationService private readonly _experimentationService: IExperimentationService,
40
@IConfigurationService private readonly _configurationService: IConfigurationService,
41
) {
42
super(props);
43
}
44
45
async render(state: void, sizing: PromptSizing) {
46
const context = this.props.documentContext;
47
const document = context.document;
48
const languageId = document.languageId;
49
50
if (isNotebookCellOrNotebookChatInput(document.uri)) {
51
throw illegalArgument('InlineChatEditCodePrompt should not be used with a notebook!');
52
}
53
54
if (languageId === 'markdown') {
55
throw illegalArgument('InlineChatEditCodePrompt should not be used with a markdown document!');
56
}
57
58
const isIgnored = await this._ignoreService.isCopilotIgnored(document.uri);
59
if (isIgnored) {
60
return <ignoredFiles value={[document.uri]} />;
61
}
62
const { query, history, chatVariables, } = this.props.promptContext;
63
64
const useProjectLabels = this._configurationService.getExperimentBasedConfig(ConfigKey.Advanced.ProjectLabelsInline, this._experimentationService);
65
66
const data = await SummarizedDocumentData.create(this._parserService, document, context.fileIndentInfo, context.wholeRange, SelectionSplitKind.Adjusted);
67
68
const replyInterpreterFactory = (splitDoc: SummarizedDocumentSplit) => splitDoc.createReplyInterpreter(
69
LeadingMarkdownStreaming.Mute,
70
EarlyStopping.StopAfterFirstCodeBlock,
71
splitDoc.replaceSelectionStreaming,
72
TextPieceClassifiers.createCodeBlockClassifier(),
73
line => line.value.trim() !== data.placeholderText,
74
);
75
76
return (
77
<>
78
<SystemMessage priority={1000}>
79
You are an AI programming assistant.<br />
80
When asked for your name, you must respond with "GitHub Copilot".<br />
81
You are a world class expert in programming, and especially good at {languageId}.<br />
82
<LegacySafetyRules />
83
</SystemMessage>
84
<HistoryWithInstructions inline={true} historyPriority={700} passPriority history={history}>
85
<InstructionMessage priority={1000}>
86
Source code is always contained in ``` blocks.<br />
87
The user needs help to modify some code.<br />
88
{data.hasCodeWithoutSelection && <>The user includes existing code and marks with {data.placeholderText} where the selected code should go.<br /></>}
89
</InstructionMessage>
90
</HistoryWithInstructions>
91
{useProjectLabels && <ProjectLabels priority={600} embeddedInsideUserMessage={false} />}
92
<UserMessage>
93
{!this.props.ignoreCustomInstructions && <CustomInstructions priority={725} chatVariables={chatVariables} languageId={languageId} />}
94
<LanguageServerContextPrompt priority={700} document={document} position={context.selection.start} requestId={this.props.promptContext.requestId} source={KnownSources.chat} />
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={replyInterpreterFactory}
104
/>
105
<Tag name='userPrompt'>
106
<UserQuery chatVariables={chatVariables} query={query} /><br />
107
</Tag>
108
{data.hasCodeWithoutSelection && <>The modified {data.placeholderText} code with ``` is:</>}
109
</UserMessage>
110
</>
111
);
112
}
113
}
114
115