Path: blob/main/extensions/copilot/src/extension/prompts/node/panel/terminal.tsx
13405 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/456import { BasePromptElementProps, PromptElement, PromptPiece, SystemMessage, UserMessage } from '@vscode/prompt-tsx';7import { IChatEndpoint } from '../../../../platform/networking/common/networking';8import { IBuildPromptContext } from '../../../prompt/common/intents';9import { CopilotIdentityRules } from '../base/copilotIdentity';10import { InstructionMessage } from '../base/instructionMessage';11import { ResponseTranslationRules } from '../base/responseTranslationRules';12import { LegacySafetyRules } from '../base/safetyRules';13import { ChatToolReferences, ChatVariablesAndQuery } from './chatVariables';14import { HistoryWithInstructions } from './conversationHistory';15import { CustomInstructions } from './customInstructions';16import { EditorIntegrationRules } from './editorIntegrationRules';17import { TerminalLastCommand } from './terminalLastCommand';1819export interface TerminalPromptProps extends BasePromptElementProps {20promptContext: IBuildPromptContext;21osName: string;22shellType: string;23endpoint: IChatEndpoint;24}2526export interface TerminalPromptState {27}2829const enum ShellExamples {30Sh = `31User: How do I print all files recursively within a directory?32Assistant:33\`\`\`sh34ls -lR35\`\`\``,3637Pwsh = `User: go to the foo dir38Assistant:39\`\`\`pwsh40cd .\\foo\\41\`\`\`4243User: How do I delete a directory?44Assistant:45\`\`\`pwsh46Remove-Item {dir_name}47\`\`\`4849User: create a file called foo50Assistant:51\`\`\`pwsh52New-Item -ItemType File -Name foo53\`\`\``,5455Bash = `User: Print all files starting with "pre"56\`\`\`bash57find . -type f -name 'pre*'58\`\`\``,5960/**61* Example answers that are the relevant across all shells.62*/63Generic = `User: How do I revert a specific commit?64Assistant:65\`\`\`sh66git revert {commit_id}67\`\`\`6869User: How do I commit in git?70Assistant:71\`\`\`sh72git commit -m "{message}"73\`\`\``,7475GenericNonPwsh = `User: go to the foo dir76Assistant:77\`\`\`sh78cd foo79\`\`\``80}8182export class TerminalPrompt extends PromptElement<TerminalPromptProps, TerminalPromptState> {8384override render(state: TerminalPromptState): PromptPiece<any, any> | undefined {85const { query, history, chatVariables, } = this.props.promptContext;86return (87<>88<SystemMessage priority={1000}>89You 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 />90<CopilotIdentityRules />91<LegacySafetyRules />92</SystemMessage>93<HistoryWithInstructions flexGrow={1} historyPriority={600} passPriority history={history}>94<InstructionMessage priority={1000}>95<EditorIntegrationRules />96<ResponseTranslationRules />97<br />98Additional Rules<br />99{`Think step by step:`}100{`1011. Read the provided relevant workspace information (file names, project files in the project root) to understand the user's workspace.`}102{`1032. Generate a response that clearly and accurately answers the user's question. In your response, follow the following:104- Prefer single line commands.105- Omit an explanation unless the suggestion is complex, if an explanation is included then be concise.106- Provide the command suggestions using the active shell and operating system.107- 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.108- Say "I'm not quite sure how to do that." when you aren't confident in your explanation`}109110{isPowerShell(this.props.shellType)111? `112- 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.113- Only use unix utilities when there is no PowerShell equivalent.114- Prefer cross-platform PowerShell scripting that works on any operating system.`115: `116- Only use a tool like python or perl when it is not possible with the shell.`}117118{`1193. At the end of the response, list all text that needs to be replaced with associated descriptions in the form of a markdown list120`.trim()}<br />121</InstructionMessage>122<InstructionMessage priority={700}>123Examples:<br />124{getShellExamples(this.props.shellType)}125</InstructionMessage>126</HistoryWithInstructions>127<UserMessage flexGrow={1} priority={750}>128<CustomInstructions languageId={isPowerShell(this.props.shellType) ? 'ps1' : 'bash'} chatVariables={chatVariables} />129</UserMessage>130<UserMessage flexGrow={1} priority={800}>131The active terminal's shell type is:<br />132{this.props.shellType}133</UserMessage >134<UserMessage flexGrow={1} priority={800}>135The active operating system is:<br />136{this.props.osName}137</UserMessage >138<TerminalLastCommand priority={801} />139<ChatToolReferences priority={899} flexGrow={2} promptContext={this.props.promptContext} embeddedInsideUserMessage={false} />140<ChatVariablesAndQuery flexGrow={2} priority={900} chatVariables={chatVariables} query={query} embeddedInsideUserMessage={false} />141</>142);143}144}145146function getShellExamples(shellType: string) {147const examples: string[] = [148ShellExamples.Generic149];150// Generic151if (!isPowerShell(shellType)) {152examples.push(ShellExamples.GenericNonPwsh);153}154// Shell-specific155switch (shellType) {156case 'ps1':157case 'pwsh':158case 'powershell': {159examples.push(ShellExamples.Pwsh);160break;161}162case 'bash': {163examples.push(ShellExamples.Bash);164break;165}166default: {167examples.push(ShellExamples.Sh);168break;169}170}171return examples.join('\n\n');172}173174function isPowerShell(shellType: string) {175return shellType === 'ps1' || shellType === 'pwsh' || shellType === 'powershell';176}177178179