Path: blob/main/extensions/copilot/src/extension/prompts/node/panel/searchPanelPrompt.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*--------------------------------------------------------------------------------------------*/45import { BasePromptElementProps, PromptElement, PromptPiece, PromptSizing, SystemMessage, TextChunk, UserMessage } from '@vscode/prompt-tsx';6import { FileChunk } from '../../../../platform/chunking/common/chunk';7import { IChatEndpoint } from '../../../../platform/networking/common/networking';8import { getWorkspaceFileDisplayPath, IWorkspaceService } from '../../../../platform/workspace/common/workspaceService';9import { createFencedCodeBlock, getLanguageId } from '../../../../util/common/markdown';10import { IBuildPromptContext } from '../../../prompt/common/intents';11import { CopilotIdentityRules } from '../base/copilotIdentity';12import { InstructionMessage } from '../base/instructionMessage';13import { ResponseTranslationRules } from '../base/responseTranslationRules';14import { SafetyRules } from '../base/safetyRules';15import { ChatToolReferences, ChatVariablesAndQuery } from './chatVariables';16import { HistoryWithInstructions } from './conversationHistory';17import { EditorIntegrationRules } from './editorIntegrationRules';1819export interface ISearchPanelPromptProps extends BasePromptElementProps {20promptContext: ISearchPanelPromptContext;21endpoint: IChatEndpoint;22}2324export interface ISearchPanelPromptContext extends IBuildPromptContext {25chunkResults: FileChunk[];26}2728export interface ISearchChunkResultProps extends BasePromptElementProps {29chunkResults: FileChunk[];30}3132export class SearchChunkResult extends PromptElement<ISearchChunkResultProps> {33constructor(props: ISearchChunkResultProps,34@IWorkspaceService private readonly workspaceService: IWorkspaceService,35) {36super(props);37}3839override render(): PromptPiece<any, any> | undefined {40if (!this.props.chunkResults.length) {41return;42}4344return <>45{this.props.chunkResults46.map((chunk, i) => {47// Give chunks a scaled priority from `X` to `X + 1` with the earliest chunks having the highest priority48const priority = typeof this.props.priority !== 'undefined'49? this.props.priority + (1 - ((i + 1) / this.props.chunkResults.length))50: undefined;5152return { chunk, priority };53})54// Send chunks in reverse order with most relevant chunks last55.reverse()56.filter(x => x.chunk.text)57.map(({ chunk, priority }) => {58const fileLabel = getWorkspaceFileDisplayPath(this.workspaceService, chunk.file);59return <TextChunk priority={priority}>60{chunk.isFullFile61? `Here is the full text of \`${fileLabel}\`:`62: `Here is a potentially relevant text excerpt in \`${fileLabel}\` starting at line ${chunk.range.startLineNumber}:`}<br />63{createFencedCodeBlock(getLanguageId(chunk.file), chunk.text)}<br /><br />64</TextChunk>;65})}66</>;67}68}6970export class SearchPanelPrompt extends PromptElement<ISearchPanelPromptProps> {71private base64Code = `72\`\`\`json73[74{75"file": "/src/encoders/base64.ts",76"query": "/src/encoders/base64.ts:private async decodeFunction()"77}78]79\`\`\`80`;81private npmCode = `82\`\`\`json83[84{85"file": "/package.json",86"query": "npm run test"87},88{89"file": "/src/second-package/package.json",90"query": "npm run production"91}92]93\`\`\`94`;95override render(state: void, sizing: PromptSizing): PromptPiece<any, any> | undefined {96const { query, history, chatVariables } = this.props.promptContext;97return <>98<SystemMessage priority={1000}>99You are a software engineer with expert knowledge of the codebase the user has open in their workspace.<br />100You will be provided with a few code excerpts, file names, and symbols from the user's that have been extracted as important to the user's query.<br />101Your job is to understand what the user is searching for and find the relevant piece of code.<br />102That piece of code will be searched for using grep in the user's workspace.<br />103<br />104<CopilotIdentityRules />105<SafetyRules />106</SystemMessage>107<HistoryWithInstructions flexGrow={2} historyPriority={400} history={history} passPriority>108<InstructionMessage priority={1000}>109<EditorIntegrationRules />110<ResponseTranslationRules />111# Additional Rules<br />112Think step by step:<br />1131. Read the provided relevant workspace information (code excerpts, file names, and symbols) to understand the user's workspace.<br />1142. Select ONLY from the provided code excerpts, file names, and symbols any code snippets that are relevant to the user's query.<br />1153. Provide ONE query FOR EACH code excerpt the user should search for in order to find the relevant wrapping code, prioritizing the most meaningful code, class names, functions, definitions, etc.<br />116<br />117You MUST ONLY consider the included code excerpts, file names and symbols to provide your answer.<br />118You MUST only return the file path and the query or phrase to search for using grep<br />119You MUST avoid returning queries that are too short and too generic that would return a lot of noisy results<br />120You MUST return one query per code excerpt provided<br />121<br />122# Examples<br />123Question:<br />124base64 encoding<br />125<br />126Response:<br />127{this.base64Code}128<br />129<br />130Question:<br />131npm scripts<br />132<br />133Response:<br />134{this.npmCode}135</InstructionMessage>136</HistoryWithInstructions>137<UserMessage>138<SearchChunkResult priority={898} chunkResults={this.props.promptContext.chunkResults} />139<ChatToolReferences priority={899} flexGrow={3} promptContext={this.props.promptContext} />140<ChatVariablesAndQuery flexGrow={3} chatVariables={chatVariables} priority={900} query={`Here is the user query: ${query}`} />141</UserMessage>142</>;143}144}145146147