Path: blob/main/extensions/copilot/src/extension/prompts/node/panel/terminalQuickFix.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, type PromptElementProps } from '@vscode/prompt-tsx';7import { IEnvService } from '../../../../platform/env/common/envService';8import { ITerminalService } from '../../../../platform/terminal/common/terminalService';9import { basename, join } from '../../../../util/vs/base/common/path';10import { URI } from '../../../../util/vs/base/common/uri';11import { ResponseTranslationRules } from '../base/responseTranslationRules';12import { SafetyRules } from '../base/safetyRules';13import { FileVariable } from './fileVariable';14import { TerminalLastCommand } from './terminalLastCommand';1516export interface TerminalQuickFixFileContextPromptProps extends BasePromptElementProps {17readonly commandLine: string;18readonly output: string[];19}2021export class TerminalQuickFixFileContextPrompt extends PromptElement<TerminalQuickFixFileContextPromptProps, any> {2223constructor(24props: PromptElementProps<TerminalQuickFixFileContextPromptProps>,25@ITerminalService private readonly _terminalService: ITerminalService,26) {27super(props);28}2930override render(): PromptPiece<any, any> | undefined {31const cwd = this._terminalService.terminalLastCommand?.cwd32? typeof this._terminalService.terminalLastCommand.cwd === 'string'33? this._terminalService.terminalLastCommand.cwd34: this._terminalService.terminalLastCommand.cwd.path35: undefined;36return (37<>38<SystemMessage priority={1000}>39You 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 />40<SafetyRules />41<ResponseTranslationRules />42{`43You MUST respond ONLY with a JSON array in the format:4445\`\`\`json46[47{48fileName: string49},50...51]52\`\`\`5354Follow these rules in your response:5556- Use an absolute path if you know the exact location of the file.57- Do NOT include any introduction, description or prose. Only include the paths.58`}59</SystemMessage>60<SystemMessage priority={1000}>61{`Examples:6263User: npm startt64Assistant:65- \`${cwd ? join(cwd, '.bin/startt') : '.bin/startt'}\`66- \`${cwd ? join(cwd, 'package.json') : 'package.json'}\`67`}68</SystemMessage>69<TerminalShellType priority={800} />70<OperatingSystem priority={600} />71<UserMessage priority={1100}>72{!this._terminalService.terminalLastCommand73? `The following command just failed when run in the terminal \`${this.props.commandLine}\`.7475Here is the output of the command:76${(this.props.output ?? []).join()}`77: ''}78</UserMessage>79<TerminalLastCommand priority={800} />80</>81);82}83}84export interface TerminalQuickFixPromptProps extends BasePromptElementProps {85readonly commandLine: string;86readonly output: string[];87readonly verifiedContextUris: URI[];88readonly verifiedContextDirectoryUris: URI[];89readonly nonExistentContextUris: URI[];90}9192export interface TerminalQuickFixPromptState {93readonly additionalContext: UserMessage[];94}9596export class TerminalQuickFixPrompt extends PromptElement<TerminalQuickFixPromptProps, TerminalQuickFixPromptState> {9798constructor(99props: PromptElementProps<TerminalQuickFixPromptProps>,100@ITerminalService private readonly _terminalService: ITerminalService,101) {102super(props);103}104105override render(state: TerminalQuickFixPromptState): PromptPiece<any, any> | undefined {106// A low priority is used here as file references could be very large107const fileVariables = this.props.verifiedContextUris.map(uri => {108return <FileVariable variableName={basename(uri.path)} variableValue={uri}></FileVariable>;109});110return (111<>112<SystemMessage priority={1000}>113You 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 />114<SafetyRules />115<ResponseTranslationRules />116{`117You MUST respond ONLY with a JSON array containing HIGHLY RELEVANT command suggestions in the format:118119\`\`\`json120[121{122command: string,123description: string,124relevance: 'low' | 'medium' | 'high'125},126...127]128\`\`\`129130Follow these rules in your response:131132- You MUST NOT suggest commands that use non-existent files.133- Under no circumstance will you include an summary, description or any prose whatsoever.134- Do NOT repeat the command and/or output.135- Provide a maximum of 10 suggestions, starting with the most relevant.136- 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.137- Avoid providing suggestions that do exactly the same thing like aliases.138- Only provide suggestions for the active shell and avoid shelling out where possible.139- The suggestions must be relevant. For example, if the command is a build command, the suggestions must look like build commands, not test commands.140- If the command is related to a particular programming language, do not include suggestions for different languages.141- NEVER suggest to change directory to the current working directory.142`}143</SystemMessage>144<SystemMessage priority={700}>145{`Examples:146147User: lss148Assistant:149- \`ls\`150151User: clone git152Assistant:153- \`git clone {repository}\`154155User: .venv/bin/activate156Context: .venv/bin/activate DOES NOT exist157Assistant:158- \`python -m venv .venv\`159160User: .venv/bin/activate161Context: .venv/bin/activate exists162Assistant:163- \`source .venv/bin/activate\`164`}165</SystemMessage>166<TerminalShellType priority={800} />167<OperatingSystem priority={800} />168<PythonModuleError priority={600} />169<UserMessage priority={1100}>170{!this._terminalService.terminalLastCommand171? `The following command just failed when run in the terminal \`${this.props.commandLine}\`.172173Here is the output of the command:174${(this.props.output ?? []).join()}`175: ''}176</UserMessage>177<TerminalLastCommand priority={800} />178<UserMessage priority={700}>179{`${this.props.verifiedContextDirectoryUris.length > 0 ?180`The following directories exist:\n\n${this.props.verifiedContextDirectoryUris.map(uri => `- ${uri.path}`).join('\n')}`181: ''}`}182</UserMessage>183<UserMessage priority={700}>184{`${this.props.nonExistentContextUris.length > 0 ?185`The following files DO NOT exist and cannot be used in the suggestion:\n\n${this.props.nonExistentContextUris.map(uri => `- ${uri.path}`).join('\n')}`186: ''}`}187</UserMessage>188{...fileVariables}189</>190);191}192}193194class PythonModuleError extends PromptElement {195render() {196return (197<>198<SystemMessage priority={this.props.priority}>199{`200Follow these guidelines for python:201- NEVER recommend using "pip install" directly, always recommend "python -m pip install"202- The following are pypi modules: ruff, pylint, black, autopep8, etc203- If the error is module not found, recommend installing the module using "python -m pip install" command.204- If activate is not available create an environment using "python -m venv .venv".205`}206</SystemMessage >207</>208);209}210}211212class TerminalShellType extends PromptElement {213214constructor(215props: BasePromptElementProps,216@ITerminalService private readonly _terminalService: ITerminalService,217) {218super(props);219}220221render() {222return (223<>224<UserMessage priority={this.props.priority}>225The active terminal's shell type is: {this._terminalService.terminalShellType}226</UserMessage >227</>228);229}230}231232class OperatingSystem extends PromptElement {233234constructor(235props: BasePromptElementProps,236@IEnvService private readonly _envService: IEnvService,237) {238super(props);239}240241render() {242return (243<>244<UserMessage priority={this.props.priority}>245The active operating system is: {this._envService.OS}246</UserMessage >247</>248);249}250}251252253