Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompts/node/panel/terminalQuickFix.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
7
import { BasePromptElementProps, PromptElement, PromptPiece, SystemMessage, UserMessage, type PromptElementProps } from '@vscode/prompt-tsx';
8
import { IEnvService } from '../../../../platform/env/common/envService';
9
import { ITerminalService } from '../../../../platform/terminal/common/terminalService';
10
import { basename, join } from '../../../../util/vs/base/common/path';
11
import { URI } from '../../../../util/vs/base/common/uri';
12
import { ResponseTranslationRules } from '../base/responseTranslationRules';
13
import { SafetyRules } from '../base/safetyRules';
14
import { FileVariable } from './fileVariable';
15
import { TerminalLastCommand } from './terminalLastCommand';
16
17
export interface TerminalQuickFixFileContextPromptProps extends BasePromptElementProps {
18
readonly commandLine: string;
19
readonly output: string[];
20
}
21
22
export class TerminalQuickFixFileContextPrompt extends PromptElement<TerminalQuickFixFileContextPromptProps, any> {
23
24
constructor(
25
props: PromptElementProps<TerminalQuickFixFileContextPromptProps>,
26
@ITerminalService private readonly _terminalService: ITerminalService,
27
) {
28
super(props);
29
}
30
31
override render(): PromptPiece<any, any> | undefined {
32
const cwd = this._terminalService.terminalLastCommand?.cwd
33
? typeof this._terminalService.terminalLastCommand.cwd === 'string'
34
? this._terminalService.terminalLastCommand.cwd
35
: this._terminalService.terminalLastCommand.cwd.path
36
: undefined;
37
return (
38
<>
39
<SystemMessage priority={1000}>
40
You are a programmer who specializes in using the command line. Your task is to respond with a list of files that you need access to in order to fix the command. Carefully consider the command line, output and current working directory in your response.<br />
41
<SafetyRules />
42
<ResponseTranslationRules />
43
{`
44
You MUST respond ONLY with a JSON array in the format:
45
46
\`\`\`json
47
[
48
{
49
fileName: string
50
},
51
...
52
]
53
\`\`\`
54
55
Follow these rules in your response:
56
57
- Use an absolute path if you know the exact location of the file.
58
- Do NOT include any introduction, description or prose. Only include the paths.
59
`}
60
</SystemMessage>
61
<SystemMessage priority={1000}>
62
{`Examples:
63
64
User: npm startt
65
Assistant:
66
- \`${cwd ? join(cwd, '.bin/startt') : '.bin/startt'}\`
67
- \`${cwd ? join(cwd, 'package.json') : 'package.json'}\`
68
`}
69
</SystemMessage>
70
<TerminalShellType priority={800} />
71
<OperatingSystem priority={600} />
72
<UserMessage priority={1100}>
73
{!this._terminalService.terminalLastCommand
74
? `The following command just failed when run in the terminal \`${this.props.commandLine}\`.
75
76
Here is the output of the command:
77
${(this.props.output ?? []).join()}`
78
: ''}
79
</UserMessage>
80
<TerminalLastCommand priority={800} />
81
</>
82
);
83
}
84
}
85
export interface TerminalQuickFixPromptProps extends BasePromptElementProps {
86
readonly commandLine: string;
87
readonly output: string[];
88
readonly verifiedContextUris: URI[];
89
readonly verifiedContextDirectoryUris: URI[];
90
readonly nonExistentContextUris: URI[];
91
}
92
93
export interface TerminalQuickFixPromptState {
94
readonly additionalContext: UserMessage[];
95
}
96
97
export class TerminalQuickFixPrompt extends PromptElement<TerminalQuickFixPromptProps, TerminalQuickFixPromptState> {
98
99
constructor(
100
props: PromptElementProps<TerminalQuickFixPromptProps>,
101
@ITerminalService private readonly _terminalService: ITerminalService,
102
) {
103
super(props);
104
}
105
106
override render(state: TerminalQuickFixPromptState): PromptPiece<any, any> | undefined {
107
// A low priority is used here as file references could be very large
108
const fileVariables = this.props.verifiedContextUris.map(uri => {
109
return <FileVariable variableName={basename(uri.path)} variableValue={uri}></FileVariable>;
110
});
111
return (
112
<>
113
<SystemMessage priority={1000}>
114
You are a programmer who specializes in using the command line. Your task is to help the user fix a command that was run in the terminal by providing a list of fixed command suggestions. Carefully consider the command line, output and current working directory in your response.<br />
115
<SafetyRules />
116
<ResponseTranslationRules />
117
{`
118
You MUST respond ONLY with a JSON array containing HIGHLY RELEVANT command suggestions in the format:
119
120
\`\`\`json
121
[
122
{
123
command: string,
124
description: string,
125
relevance: 'low' | 'medium' | 'high'
126
},
127
...
128
]
129
\`\`\`
130
131
Follow these rules in your response:
132
133
- You MUST NOT suggest commands that use non-existent files.
134
- Under no circumstance will you include an summary, description or any prose whatsoever.
135
- Do NOT repeat the command and/or output.
136
- Provide a maximum of 10 suggestions, starting with the most relevant.
137
- When there is text that needs to be replaced in the suggestion, prefix the text with '{', suffix the text with '}' and use underscores instead of whitespace. Only do this when the replacement text is NOT provided.
138
- Avoid providing suggestions that do exactly the same thing like aliases.
139
- Only provide suggestions for the active shell and avoid shelling out where possible.
140
- The suggestions must be relevant. For example, if the command is a build command, the suggestions must look like build commands, not test commands.
141
- If the command is related to a particular programming language, do not include suggestions for different languages.
142
- NEVER suggest to change directory to the current working directory.
143
`}
144
</SystemMessage>
145
<SystemMessage priority={700}>
146
{`Examples:
147
148
User: lss
149
Assistant:
150
- \`ls\`
151
152
User: clone git
153
Assistant:
154
- \`git clone {repository}\`
155
156
User: .venv/bin/activate
157
Context: .venv/bin/activate DOES NOT exist
158
Assistant:
159
- \`python -m venv .venv\`
160
161
User: .venv/bin/activate
162
Context: .venv/bin/activate exists
163
Assistant:
164
- \`source .venv/bin/activate\`
165
`}
166
</SystemMessage>
167
<TerminalShellType priority={800} />
168
<OperatingSystem priority={800} />
169
<PythonModuleError priority={600} />
170
<UserMessage priority={1100}>
171
{!this._terminalService.terminalLastCommand
172
? `The following command just failed when run in the terminal \`${this.props.commandLine}\`.
173
174
Here is the output of the command:
175
${(this.props.output ?? []).join()}`
176
: ''}
177
</UserMessage>
178
<TerminalLastCommand priority={800} />
179
<UserMessage priority={700}>
180
{`${this.props.verifiedContextDirectoryUris.length > 0 ?
181
`The following directories exist:\n\n${this.props.verifiedContextDirectoryUris.map(uri => `- ${uri.path}`).join('\n')}`
182
: ''}`}
183
</UserMessage>
184
<UserMessage priority={700}>
185
{`${this.props.nonExistentContextUris.length > 0 ?
186
`The following files DO NOT exist and cannot be used in the suggestion:\n\n${this.props.nonExistentContextUris.map(uri => `- ${uri.path}`).join('\n')}`
187
: ''}`}
188
</UserMessage>
189
{...fileVariables}
190
</>
191
);
192
}
193
}
194
195
class PythonModuleError extends PromptElement {
196
render() {
197
return (
198
<>
199
<SystemMessage priority={this.props.priority}>
200
{`
201
Follow these guidelines for python:
202
- NEVER recommend using "pip install" directly, always recommend "python -m pip install"
203
- The following are pypi modules: ruff, pylint, black, autopep8, etc
204
- If the error is module not found, recommend installing the module using "python -m pip install" command.
205
- If activate is not available create an environment using "python -m venv .venv".
206
`}
207
</SystemMessage >
208
</>
209
);
210
}
211
}
212
213
class TerminalShellType extends PromptElement {
214
215
constructor(
216
props: BasePromptElementProps,
217
@ITerminalService private readonly _terminalService: ITerminalService,
218
) {
219
super(props);
220
}
221
222
render() {
223
return (
224
<>
225
<UserMessage priority={this.props.priority}>
226
The active terminal's shell type is: {this._terminalService.terminalShellType}
227
</UserMessage >
228
</>
229
);
230
}
231
}
232
233
class OperatingSystem extends PromptElement {
234
235
constructor(
236
props: BasePromptElementProps,
237
@IEnvService private readonly _envService: IEnvService,
238
) {
239
super(props);
240
}
241
242
render() {
243
return (
244
<>
245
<UserMessage priority={this.props.priority}>
246
The active operating system is: {this._envService.OS}
247
</UserMessage >
248
</>
249
);
250
}
251
}
252
253