Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompts/node/inline/inlineChatWorkspaceSearch.tsx
13404 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, PromptSizing } from '@vscode/prompt-tsx';
7
import { TelemetryCorrelationId } from '../../../../util/common/telemetryCorrelationId';
8
import { Diagnostic } from '../../../../vscodeTypes';
9
import { IDocumentContext } from '../../../prompt/node/documentContext';
10
import { ChunksToolProps, WorkspaceChunks } from '../panel/workspace/workspaceContext';
11
12
interface InlineChatWorkspaceSearchProps extends BasePromptElementProps {
13
readonly documentContext: IDocumentContext;
14
readonly diagnostics: Diagnostic[];
15
readonly useWorkspaceChunksFromSelection?: boolean;
16
readonly useWorkspaceChunksFromDiagnostics?: boolean;
17
}
18
19
export class InlineChatWorkspaceSearch extends PromptElement<InlineChatWorkspaceSearchProps> {
20
21
render(state: void, sizing: PromptSizing) {
22
const { useWorkspaceChunksFromSelection, useWorkspaceChunksFromDiagnostics } = this.props;
23
24
if (!useWorkspaceChunksFromSelection && !useWorkspaceChunksFromDiagnostics) {
25
return null;
26
}
27
28
let tokenBudget = sizing.tokenBudget;
29
if (useWorkspaceChunksFromSelection && useWorkspaceChunksFromDiagnostics) {
30
tokenBudget = tokenBudget / 2;
31
}
32
return (
33
<>
34
{useWorkspaceChunksFromSelection &&
35
<WorkspaceChunks {...this.getChunkSearchPropsForSelection()} />}
36
{useWorkspaceChunksFromDiagnostics &&
37
<WorkspaceChunks {...this.getChunkSearchPropsForDiagnostics(tokenBudget)} />}
38
</>
39
);
40
}
41
42
private getChunkSearchPropsForSelection(): ChunksToolProps {
43
const { document, wholeRange } = this.props.documentContext;
44
let range = document.validateRange(wholeRange);
45
this.props.diagnostics.forEach(d => {
46
range = range.union(d.range);
47
});
48
const selectedText = document.getText(range);
49
50
const query = [
51
`Please find code that is similar to the following code block:\n`,
52
'```',
53
selectedText,
54
'```'
55
].join('\n');
56
return {
57
telemetryInfo: new TelemetryCorrelationId('InlineChatWorkspaceSearch::getChunkSearchPropsForSelection'),
58
query: {
59
queryText: query
60
},
61
// do not return matches in the current file
62
globPatterns: { exclude: [document.uri.fsPath] }, // TODO: use relativePattern once supported
63
maxResults: 3,
64
};
65
}
66
67
private getChunkSearchPropsForDiagnostics(tokenBudget: number): ChunksToolProps {
68
const document = this.props.documentContext.document;
69
const messages = this.props.diagnostics.map(d => d.message).join(' ');
70
const query = `Please find code that can help me fix the following problems: ${messages}`;
71
return {
72
telemetryInfo: new TelemetryCorrelationId('InlineChatWorkspaceSearch::getChunkSearchPropsForDiagnostics'),
73
query: {
74
queryText: query
75
},
76
// do not return matches in the current file
77
globPatterns: { exclude: [document.uri.fsPath] },
78
maxResults: 3,
79
};
80
}
81
}
82
83