Path: blob/main/extensions/copilot/src/platform/projectTemplatesIndex/common/projectTemplatesIndex.ts
13401 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 { createServiceIdentifier } from '../../../util/common/services';6import { sanitizeVSCodeVersion } from '../../../util/common/vscodeVersion';7import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';8import { Embedding, EmbeddingType, EmbeddingVector, rankEmbeddings } from '../../embeddings/common/embeddingsComputer';9import { EmbeddingCacheType, IEmbeddingsCache, LocalEmbeddingsCache, RemoteCacheType, RemoteEmbeddingsCache } from '../../embeddings/common/embeddingsIndex';10import { IEnvService } from '../../env/common/envService';1112export type ProjectTemplateItem = {13key: string;14embedding?: EmbeddingVector;15};1617export interface IProjectTemplatesIndex {18readonly _serviceBrand: undefined;19updateIndex(): Promise<void>;20nClosestValues(embedding: Embedding, n: number): Promise<string[]>;21}2223export const IProjectTemplatesIndex = createServiceIdentifier<IProjectTemplatesIndex>('IProjectTemplatesIndex');2425export class ProjectTemplatesIndex implements IProjectTemplatesIndex {26declare _serviceBrand: undefined;2728private readonly embeddingsCache: IEmbeddingsCache;29private _embeddings: ProjectTemplateItem[] | undefined;30private _isIndexLoaded = false;3132constructor(33useRemoteCache: boolean = true,34@IEnvService envService: IEnvService,35@IInstantiationService instantiationService: IInstantiationService36) {37const cacheVersion = sanitizeVSCodeVersion(envService.getEditorInfo().version);38this.embeddingsCache = useRemoteCache ?39instantiationService.createInstance(RemoteEmbeddingsCache, EmbeddingCacheType.GLOBAL, 'projectTemplateEmbeddings', cacheVersion, EmbeddingType.text3small_512, RemoteCacheType.ProjectTemplates)40: instantiationService.createInstance(LocalEmbeddingsCache, EmbeddingCacheType.GLOBAL, 'projectTemplateEmbeddings', cacheVersion, EmbeddingType.text3small_512);41}4243async updateIndex(): Promise<void> {44if (this._isIndexLoaded) {45return;46}47this._isIndexLoaded = true;48this._embeddings = await this.embeddingsCache.getCache();49}5051public async nClosestValues(embedding: Embedding, n: number): Promise<string[]> {52await this.updateIndex();53if (!this._embeddings) {54return [];55}5657return rankEmbeddings(embedding, this._embeddings.filter(x => x.embedding).map(item => [`${item.key} `, { type: this.embeddingsCache.embeddingType, value: item.embedding! } satisfies Embedding] as const), n)58.map(x => x.value);59}60}616263