Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/base/cachingChunksEndpointClient.ts
13388 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 { FileChunkWithEmbedding, FileChunkWithOptionalEmbedding } from '../../src/platform/chunking/common/chunk';
6
import { ChunkableContent, ComputeBatchInfo, EmbeddingsComputeQos, IChunkingEndpointClient } from '../../src/platform/chunking/common/chunkingEndpointClient';
7
import { ChunkingEndpointClientImpl } from '../../src/platform/chunking/common/chunkingEndpointClientImpl';
8
import { EmbeddingType } from '../../src/platform/embeddings/common/embeddingsComputer';
9
import { createSha256Hash } from '../../src/util/common/crypto';
10
import { CallTracker } from '../../src/util/common/telemetryCorrelationId';
11
import { CancellationToken } from '../../src/util/vs/base/common/cancellation';
12
import { URI } from '../../src/util/vs/base/common/uri';
13
import { Range } from '../../src/util/vs/editor/common/core/range';
14
import { IInstantiationService } from '../../src/util/vs/platform/instantiation/common/instantiation';
15
import { CHUNKING_ENDPOINT_CACHE_SALT } from '../cacheSalt';
16
import { SQLiteCache } from './cache';
17
import { CurrentTestRunInfo } from './simulationContext';
18
19
class CacheableChunkingEndpointClientRequest {
20
21
static async create(content: ChunkableContent) {
22
const hash = await createSha256Hash(CHUNKING_ENDPOINT_CACHE_SALT + await content.getText());
23
return new CacheableChunkingEndpointClientRequest(hash, content);
24
}
25
26
private constructor(
27
readonly hash: string,
28
readonly content: ChunkableContent,
29
) { }
30
}
31
32
interface IChunkingEndpointClientCache {
33
get(req: CacheableChunkingEndpointClientRequest): Promise<FileChunkWithEmbedding[] | undefined>;
34
set(req: CacheableChunkingEndpointClientRequest, cachedResponse: readonly FileChunkWithEmbedding[]): Promise<void>;
35
}
36
37
export class ChunkingEndpointClientSQLiteCache extends SQLiteCache<CacheableChunkingEndpointClientRequest, FileChunkWithEmbedding[]> implements IChunkingEndpointClientCache {
38
39
constructor(salt: string, currentTestRunInfo: CurrentTestRunInfo) {
40
super('chunks-endpoint', salt, currentTestRunInfo);
41
}
42
43
override async get(req: CacheableChunkingEndpointClientRequest): Promise<FileChunkWithEmbedding[] | undefined> {
44
const result = await super.get(req);
45
46
// Revive objects from cache
47
return result?.map(cachedResponse => {
48
const chunk: FileChunkWithEmbedding = {
49
chunk: {
50
file: URI.from(cachedResponse.chunk.file),
51
range: new Range(cachedResponse.chunk.range.startLineNumber, cachedResponse.chunk.range.startColumn, cachedResponse.chunk.range.endLineNumber, cachedResponse.chunk.range.endColumn),
52
isFullFile: cachedResponse.chunk.isFullFile,
53
text: cachedResponse.chunk.text,
54
rawText: cachedResponse.chunk.rawText,
55
},
56
chunkHash: cachedResponse.chunkHash,
57
embedding: cachedResponse.embedding,
58
};
59
60
return chunk;
61
});
62
}
63
}
64
65
export class CachingChunkingEndpointClient implements IChunkingEndpointClient {
66
declare readonly _serviceBrand: undefined;
67
private readonly _chunkingEndpointClient: IChunkingEndpointClient;
68
69
constructor(
70
private readonly _cache: IChunkingEndpointClientCache,
71
@IInstantiationService instantiationService: IInstantiationService,
72
) {
73
this._chunkingEndpointClient = instantiationService.createInstance(ChunkingEndpointClientImpl);
74
}
75
76
async 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> {
77
const req = await CacheableChunkingEndpointClientRequest.create(content);
78
const cacheValue = await this._cache.get(req);
79
if (cacheValue) {
80
return cacheValue;
81
}
82
83
const result = await this._chunkingEndpointClient.computeChunksAndEmbeddings(authToken, embeddingType, content, batchInfo, qos, cache, telemetryInfo, token);
84
if (result) {
85
await this._cache.set(req, result);
86
}
87
88
return result;
89
}
90
91
computeChunks(authToken: string, embeddingType: EmbeddingType, content: ChunkableContent, batchInfo: ComputeBatchInfo, qos: EmbeddingsComputeQos, cache: ReadonlyMap<string, FileChunkWithEmbedding> | undefined, telemetryInfo: CallTracker, token: CancellationToken): Promise<readonly FileChunkWithOptionalEmbedding[] | undefined> {
92
return this.computeChunksAndEmbeddings(authToken, embeddingType, content, batchInfo, qos, cache, telemetryInfo, token);
93
}
94
}
95