Path: blob/main/extensions/copilot/test/base/cachingChunksEndpointClient.ts
13388 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*--------------------------------------------------------------------------------------------*/4import { FileChunkWithEmbedding, FileChunkWithOptionalEmbedding } from '../../src/platform/chunking/common/chunk';5import { ChunkableContent, ComputeBatchInfo, EmbeddingsComputeQos, IChunkingEndpointClient } from '../../src/platform/chunking/common/chunkingEndpointClient';6import { ChunkingEndpointClientImpl } from '../../src/platform/chunking/common/chunkingEndpointClientImpl';7import { EmbeddingType } from '../../src/platform/embeddings/common/embeddingsComputer';8import { createSha256Hash } from '../../src/util/common/crypto';9import { CallTracker } from '../../src/util/common/telemetryCorrelationId';10import { CancellationToken } from '../../src/util/vs/base/common/cancellation';11import { URI } from '../../src/util/vs/base/common/uri';12import { Range } from '../../src/util/vs/editor/common/core/range';13import { IInstantiationService } from '../../src/util/vs/platform/instantiation/common/instantiation';14import { CHUNKING_ENDPOINT_CACHE_SALT } from '../cacheSalt';15import { SQLiteCache } from './cache';16import { CurrentTestRunInfo } from './simulationContext';1718class CacheableChunkingEndpointClientRequest {1920static async create(content: ChunkableContent) {21const hash = await createSha256Hash(CHUNKING_ENDPOINT_CACHE_SALT + await content.getText());22return new CacheableChunkingEndpointClientRequest(hash, content);23}2425private constructor(26readonly hash: string,27readonly content: ChunkableContent,28) { }29}3031interface IChunkingEndpointClientCache {32get(req: CacheableChunkingEndpointClientRequest): Promise<FileChunkWithEmbedding[] | undefined>;33set(req: CacheableChunkingEndpointClientRequest, cachedResponse: readonly FileChunkWithEmbedding[]): Promise<void>;34}3536export class ChunkingEndpointClientSQLiteCache extends SQLiteCache<CacheableChunkingEndpointClientRequest, FileChunkWithEmbedding[]> implements IChunkingEndpointClientCache {3738constructor(salt: string, currentTestRunInfo: CurrentTestRunInfo) {39super('chunks-endpoint', salt, currentTestRunInfo);40}4142override async get(req: CacheableChunkingEndpointClientRequest): Promise<FileChunkWithEmbedding[] | undefined> {43const result = await super.get(req);4445// Revive objects from cache46return result?.map(cachedResponse => {47const chunk: FileChunkWithEmbedding = {48chunk: {49file: URI.from(cachedResponse.chunk.file),50range: new Range(cachedResponse.chunk.range.startLineNumber, cachedResponse.chunk.range.startColumn, cachedResponse.chunk.range.endLineNumber, cachedResponse.chunk.range.endColumn),51isFullFile: cachedResponse.chunk.isFullFile,52text: cachedResponse.chunk.text,53rawText: cachedResponse.chunk.rawText,54},55chunkHash: cachedResponse.chunkHash,56embedding: cachedResponse.embedding,57};5859return chunk;60});61}62}6364export class CachingChunkingEndpointClient implements IChunkingEndpointClient {65declare readonly _serviceBrand: undefined;66private readonly _chunkingEndpointClient: IChunkingEndpointClient;6768constructor(69private readonly _cache: IChunkingEndpointClientCache,70@IInstantiationService instantiationService: IInstantiationService,71) {72this._chunkingEndpointClient = instantiationService.createInstance(ChunkingEndpointClientImpl);73}7475async computeChunksAndEmbeddings(authToken: string, embeddingType: EmbeddingType, content: ChunkableContent, batchInfo: ComputeBatchInfo, qos: EmbeddingsComputeQos, cache: ReadonlyMap</* hash */string, FileChunkWithEmbedding> | undefined, telemetryInfo: CallTracker, token: CancellationToken): Promise<readonly FileChunkWithEmbedding[] | undefined> {76const req = await CacheableChunkingEndpointClientRequest.create(content);77const cacheValue = await this._cache.get(req);78if (cacheValue) {79return cacheValue;80}8182const result = await this._chunkingEndpointClient.computeChunksAndEmbeddings(authToken, embeddingType, content, batchInfo, qos, cache, telemetryInfo, token);83if (result) {84await this._cache.set(req, result);85}8687return result;88}8990computeChunks(authToken: string, embeddingType: EmbeddingType, content: ChunkableContent, batchInfo: ComputeBatchInfo, qos: EmbeddingsComputeQos, cache: ReadonlyMap<string, FileChunkWithEmbedding> | undefined, telemetryInfo: CallTracker, token: CancellationToken): Promise<readonly FileChunkWithOptionalEmbedding[] | undefined> {91return this.computeChunksAndEmbeddings(authToken, embeddingType, content, batchInfo, qos, cache, telemetryInfo, token);92}93}9495