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