Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/projectTemplatesIndex/common/projectTemplatesIndex.ts
13401 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 { createServiceIdentifier } from '../../../util/common/services';
7
import { sanitizeVSCodeVersion } from '../../../util/common/vscodeVersion';
8
import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';
9
import { Embedding, EmbeddingType, EmbeddingVector, rankEmbeddings } from '../../embeddings/common/embeddingsComputer';
10
import { EmbeddingCacheType, IEmbeddingsCache, LocalEmbeddingsCache, RemoteCacheType, RemoteEmbeddingsCache } from '../../embeddings/common/embeddingsIndex';
11
import { IEnvService } from '../../env/common/envService';
12
13
export type ProjectTemplateItem = {
14
key: string;
15
embedding?: EmbeddingVector;
16
};
17
18
export interface IProjectTemplatesIndex {
19
readonly _serviceBrand: undefined;
20
updateIndex(): Promise<void>;
21
nClosestValues(embedding: Embedding, n: number): Promise<string[]>;
22
}
23
24
export const IProjectTemplatesIndex = createServiceIdentifier<IProjectTemplatesIndex>('IProjectTemplatesIndex');
25
26
export class ProjectTemplatesIndex implements IProjectTemplatesIndex {
27
declare _serviceBrand: undefined;
28
29
private readonly embeddingsCache: IEmbeddingsCache;
30
private _embeddings: ProjectTemplateItem[] | undefined;
31
private _isIndexLoaded = false;
32
33
constructor(
34
useRemoteCache: boolean = true,
35
@IEnvService envService: IEnvService,
36
@IInstantiationService instantiationService: IInstantiationService
37
) {
38
const cacheVersion = sanitizeVSCodeVersion(envService.getEditorInfo().version);
39
this.embeddingsCache = useRemoteCache ?
40
instantiationService.createInstance(RemoteEmbeddingsCache, EmbeddingCacheType.GLOBAL, 'projectTemplateEmbeddings', cacheVersion, EmbeddingType.text3small_512, RemoteCacheType.ProjectTemplates)
41
: instantiationService.createInstance(LocalEmbeddingsCache, EmbeddingCacheType.GLOBAL, 'projectTemplateEmbeddings', cacheVersion, EmbeddingType.text3small_512);
42
}
43
44
async updateIndex(): Promise<void> {
45
if (this._isIndexLoaded) {
46
return;
47
}
48
this._isIndexLoaded = true;
49
this._embeddings = await this.embeddingsCache.getCache();
50
}
51
52
public async nClosestValues(embedding: Embedding, n: number): Promise<string[]> {
53
await this.updateIndex();
54
if (!this._embeddings) {
55
return [];
56
}
57
58
return rankEmbeddings(embedding, this._embeddings.filter(x => x.embedding).map(item => [`${item.key} `, { type: this.embeddingsCache.embeddingType, value: item.embedding! } satisfies Embedding] as const), n)
59
.map(x => x.value);
60
}
61
}
62
63