Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompt/vscode-node/settingsEditorSearchServiceImpl.ts
13399 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
import { CancellationToken, Progress, SettingsSearchProviderOptions, SettingsSearchResult, SettingsSearchResultKind } from 'vscode';
6
import { IAuthenticationService } from '../../../platform/authentication/common/authentication';
7
import { Embeddings, EmbeddingType, IEmbeddingsComputer } from '../../../platform/embeddings/common/embeddingsComputer';
8
import { ICombinedEmbeddingIndex, SettingListItem } from '../../../platform/embeddings/common/vscodeIndex';
9
import { ChatEndpointFamily, IEndpointProvider } from '../../../platform/endpoint/common/endpointProvider';
10
import { ISettingsEditorSearchService } from '../../../platform/settingsEditor/common/settingsEditorSearchService';
11
import { TelemetryCorrelationId } from '../../../util/common/telemetryCorrelationId';
12
import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';
13
import { SettingsEditorSearchResultsSelector } from '../node/settingsEditorSearchResultsSelector';
14
15
export class SettingsEditorSearchServiceImpl implements ISettingsEditorSearchService {
16
declare readonly _serviceBrand: undefined;
17
18
constructor(
19
@IAuthenticationService private readonly authenticationService: IAuthenticationService,
20
@IEndpointProvider private readonly endpointProvider: IEndpointProvider,
21
@ICombinedEmbeddingIndex private readonly embeddingIndex: ICombinedEmbeddingIndex,
22
@IEmbeddingsComputer private readonly embeddingsComputer: IEmbeddingsComputer,
23
@IInstantiationService private readonly instantiationService: IInstantiationService
24
) {
25
}
26
27
async provideSettingsSearchResults(query: string, options: SettingsSearchProviderOptions, progress: Progress<SettingsSearchResult>, token: CancellationToken): Promise<void> {
28
if (!query || options.limit <= 0) {
29
return;
30
}
31
32
// Start searching for embedding results.
33
let embeddingResult: Embeddings;
34
try {
35
embeddingResult = await this.embeddingsComputer.computeEmbeddings(EmbeddingType.text3small_512, [query], {}, new TelemetryCorrelationId('SettingsEditorSearchServiceImpl::provideSettingsSearchResults'), token);
36
} catch {
37
this.reportEmptyEmbeddingsResult(query, progress);
38
if (!options.embeddingsOnly) {
39
this.reportEmptyLLMRankedResult(query, progress);
40
}
41
return;
42
}
43
44
if (token.isCancellationRequested || !embeddingResult || embeddingResult.values.length === 0) {
45
this.reportEmptyEmbeddingsResult(query, progress);
46
if (!options.embeddingsOnly) {
47
this.reportEmptyLLMRankedResult(query, progress);
48
}
49
return;
50
}
51
52
await this.embeddingIndex.loadIndexes();
53
const embeddingSettings: SettingListItem[] = this.embeddingIndex.settingsIndex.nClosestValues(embeddingResult.values[0], 25);
54
if (token.isCancellationRequested) {
55
this.reportEmptyEmbeddingsResult(query, progress);
56
if (!options.embeddingsOnly) {
57
this.reportEmptyLLMRankedResult(query, progress);
58
}
59
return;
60
}
61
62
// Report final embedding results.
63
progress.report({
64
query,
65
kind: SettingsSearchResultKind.EMBEDDED,
66
settings: embeddingSettings.map(setting => setting.key)
67
});
68
69
if (options.embeddingsOnly) {
70
return;
71
}
72
73
// Start searching LLM-ranked results.
74
const copilotToken = await this.authenticationService.getCopilotToken();
75
if (embeddingSettings.length === 0 || copilotToken.isFreeUser || copilotToken.isNoAuthUser) {
76
this.reportEmptyLLMRankedResult(query, progress);
77
return;
78
}
79
80
const endpointName: ChatEndpointFamily = 'copilot-base';
81
const endpoint = await this.endpointProvider.getChatEndpoint(endpointName);
82
const generator = this.instantiationService.createInstance(SettingsEditorSearchResultsSelector);
83
const llmSearchSuggestions = await generator.selectTopSearchResults(endpoint, query, embeddingSettings, token);
84
if (token.isCancellationRequested) {
85
this.reportEmptyLLMRankedResult(query, progress);
86
return;
87
}
88
89
// Report final LLM-ranked results.
90
progress.report({
91
query,
92
kind: SettingsSearchResultKind.LLM_RANKED,
93
settings: llmSearchSuggestions
94
});
95
}
96
97
private reportEmptyEmbeddingsResult(query: string, progress: Progress<SettingsSearchResult>): void {
98
progress.report({
99
query,
100
kind: SettingsSearchResultKind.EMBEDDED,
101
settings: []
102
});
103
}
104
105
private reportEmptyLLMRankedResult(query: string, progress: Progress<SettingsSearchResult>): void {
106
progress.report({
107
query,
108
kind: SettingsSearchResultKind.LLM_RANKED,
109
settings: []
110
});
111
}
112
}
113