Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompts/node/panel/terminal.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 } from '@vscode/prompt-tsx';
8
import { IChatEndpoint } from '../../../../platform/networking/common/networking';
9
import { IBuildPromptContext } from '../../../prompt/common/intents';
10
import { CopilotIdentityRules } from '../base/copilotIdentity';
11
import { InstructionMessage } from '../base/instructionMessage';
12
import { ResponseTranslationRules } from '../base/responseTranslationRules';
13
import { LegacySafetyRules } from '../base/safetyRules';
14
import { ChatToolReferences, ChatVariablesAndQuery } from './chatVariables';
15
import { HistoryWithInstructions } from './conversationHistory';
16
import { CustomInstructions } from './customInstructions';
17
import { EditorIntegrationRules } from './editorIntegrationRules';
18
import { TerminalLastCommand } from './terminalLastCommand';
19
20
export interface TerminalPromptProps extends BasePromptElementProps {
21
promptContext: IBuildPromptContext;
22
osName: string;
23
shellType: string;
24
endpoint: IChatEndpoint;
25
}
26
27
export interface TerminalPromptState {
28
}
29
30
const enum ShellExamples {
31
Sh = `
32
User: How do I print all files recursively within a directory?
33
Assistant:
34
\`\`\`sh
35
ls -lR
36
\`\`\``,
37
38
Pwsh = `User: go to the foo dir
39
Assistant:
40
\`\`\`pwsh
41
cd .\\foo\\
42
\`\`\`
43
44
User: How do I delete a directory?
45
Assistant:
46
\`\`\`pwsh
47
Remove-Item {dir_name}
48
\`\`\`
49
50
User: create a file called foo
51
Assistant:
52
\`\`\`pwsh
53
New-Item -ItemType File -Name foo
54
\`\`\``,
55
56
Bash = `User: Print all files starting with "pre"
57
\`\`\`bash
58
find . -type f -name 'pre*'
59
\`\`\``,
60
61
/**
62
* Example answers that are the relevant across all shells.
63
*/
64
Generic = `User: How do I revert a specific commit?
65
Assistant:
66
\`\`\`sh
67
git revert {commit_id}
68
\`\`\`
69
70
User: How do I commit in git?
71
Assistant:
72
\`\`\`sh
73
git commit -m "{message}"
74
\`\`\``,
75
76
GenericNonPwsh = `User: go to the foo dir
77
Assistant:
78
\`\`\`sh
79
cd foo
80
\`\`\``
81
}
82
83
export class TerminalPrompt extends PromptElement<TerminalPromptProps, TerminalPromptState> {
84
85
override render(state: TerminalPromptState): PromptPiece<any, any> | undefined {
86
const { query, history, chatVariables, } = this.props.promptContext;
87
return (
88
<>
89
<SystemMessage priority={1000}>
90
You are a programmer who specializes in using the command line. Your task is to help the Developer craft a command to run on the command line.<br />
91
<CopilotIdentityRules />
92
<LegacySafetyRules />
93
</SystemMessage>
94
<HistoryWithInstructions flexGrow={1} historyPriority={600} passPriority history={history}>
95
<InstructionMessage priority={1000}>
96
<EditorIntegrationRules />
97
<ResponseTranslationRules />
98
<br />
99
Additional Rules<br />
100
{`Think step by step:`}
101
{`
102
1. Read the provided relevant workspace information (file names, project files in the project root) to understand the user's workspace.`}
103
{`
104
2. Generate a response that clearly and accurately answers the user's question. In your response, follow the following:
105
- Prefer single line commands.
106
- Omit an explanation unless the suggestion is complex, if an explanation is included then be concise.
107
- Provide the command suggestions using the active shell and operating system.
108
- 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.
109
- Say "I'm not quite sure how to do that." when you aren't confident in your explanation`}
110
111
{isPowerShell(this.props.shellType)
112
? `
113
- Prefer idiomatic PowerShell over aliases for other shells or system utilities. For example use \`Stop-Process\` or \`Get-NetTCPConnection\` instead of \`kill\` or \`lsof\` respectively.
114
- Only use unix utilities when there is no PowerShell equivalent.
115
- Prefer cross-platform PowerShell scripting that works on any operating system.`
116
: `
117
- Only use a tool like python or perl when it is not possible with the shell.`}
118
119
{`
120
3. At the end of the response, list all text that needs to be replaced with associated descriptions in the form of a markdown list
121
`.trim()}<br />
122
</InstructionMessage>
123
<InstructionMessage priority={700}>
124
Examples:<br />
125
{getShellExamples(this.props.shellType)}
126
</InstructionMessage>
127
</HistoryWithInstructions>
128
<UserMessage flexGrow={1} priority={750}>
129
<CustomInstructions languageId={isPowerShell(this.props.shellType) ? 'ps1' : 'bash'} chatVariables={chatVariables} />
130
</UserMessage>
131
<UserMessage flexGrow={1} priority={800}>
132
The active terminal's shell type is:<br />
133
{this.props.shellType}
134
</UserMessage >
135
<UserMessage flexGrow={1} priority={800}>
136
The active operating system is:<br />
137
{this.props.osName}
138
</UserMessage >
139
<TerminalLastCommand priority={801} />
140
<ChatToolReferences priority={899} flexGrow={2} promptContext={this.props.promptContext} embeddedInsideUserMessage={false} />
141
<ChatVariablesAndQuery flexGrow={2} priority={900} chatVariables={chatVariables} query={query} embeddedInsideUserMessage={false} />
142
</>
143
);
144
}
145
}
146
147
function getShellExamples(shellType: string) {
148
const examples: string[] = [
149
ShellExamples.Generic
150
];
151
// Generic
152
if (!isPowerShell(shellType)) {
153
examples.push(ShellExamples.GenericNonPwsh);
154
}
155
// Shell-specific
156
switch (shellType) {
157
case 'ps1':
158
case 'pwsh':
159
case 'powershell': {
160
examples.push(ShellExamples.Pwsh);
161
break;
162
}
163
case 'bash': {
164
examples.push(ShellExamples.Bash);
165
break;
166
}
167
default: {
168
examples.push(ShellExamples.Sh);
169
break;
170
}
171
}
172
return examples.join('\n\n');
173
}
174
175
function isPowerShell(shellType: string) {
176
return shellType === 'ps1' || shellType === 'pwsh' || shellType === 'powershell';
177
}
178
179