Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/base/cachingCodeSearchClient.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 type { CancellationToken } from 'vscode';
6
import { ICodeOrDocsSearchBaseScopingQuery, ICodeOrDocsSearchItem, ICodeOrDocsSearchMultiRepoScopingQuery, ICodeOrDocsSearchOptions, ICodeOrDocsSearchResult, ICodeOrDocsSearchSingleRepoScopingQuery, IDocsSearchClient } from '../../src/platform/remoteSearch/common/codeOrDocsSearchClient';
7
import { SyncDescriptor } from '../../src/util/vs/platform/instantiation/common/descriptors';
8
import { IInstantiationService } from '../../src/util/vs/platform/instantiation/common/instantiation';
9
import { CODE_SEARCH_CACHE_SALT } from '../cacheSalt';
10
import { SQLiteCache } from './cache';
11
import { computeSHA256 } from './hash';
12
import { CurrentTestRunInfo } from './simulationContext';
13
14
class CacheableCodeOrDocSearchRequest {
15
16
readonly hash: string;
17
readonly obj: unknown;
18
19
constructor(
20
readonly query: string,
21
readonly scopingQuery: ICodeOrDocsSearchBaseScopingQuery,
22
readonly requestOptions: ICodeOrDocsSearchOptions,
23
) {
24
this.obj = { query, scopingQuery, requestOptions };
25
this.hash = computeSHA256(CODE_SEARCH_CACHE_SALT + JSON.stringify(this.obj));
26
}
27
28
toJSON() {
29
return this.obj;
30
}
31
}
32
33
interface ICodeOrDocSearchCache {
34
get(req: CacheableCodeOrDocSearchRequest): Promise<ICodeOrDocsSearchItem[] | ICodeOrDocsSearchResult | undefined>;
35
set(req: CacheableCodeOrDocSearchRequest, cachedResponse: ICodeOrDocsSearchItem[] | ICodeOrDocsSearchResult): Promise<void>;
36
}
37
38
export class CodeOrDocSearchSQLiteCache extends SQLiteCache<CacheableCodeOrDocSearchRequest, ICodeOrDocsSearchItem[] | ICodeOrDocsSearchResult> implements ICodeOrDocSearchCache {
39
40
constructor(salt: string, currentTestRunInfo: CurrentTestRunInfo) {
41
super('docs-search', salt, currentTestRunInfo);
42
}
43
}
44
45
export class CachingCodeOrDocSearchClient implements IDocsSearchClient {
46
declare readonly _serviceBrand: undefined;
47
private readonly searchClient: IDocsSearchClient;
48
49
constructor(
50
searchClientDesc: SyncDescriptor<IDocsSearchClient>,
51
private readonly cache: ICodeOrDocSearchCache,
52
@IInstantiationService instantiationService: IInstantiationService,
53
) {
54
this.searchClient = instantiationService.createInstance(searchClientDesc);
55
}
56
57
search(query: string, scopingQuery: ICodeOrDocsSearchSingleRepoScopingQuery, options?: ICodeOrDocsSearchOptions, cancellationToken?: CancellationToken | undefined): Promise<ICodeOrDocsSearchItem[]>;
58
search(query: string, scopingQuery: ICodeOrDocsSearchMultiRepoScopingQuery, options?: ICodeOrDocsSearchOptions, cancellationToken?: CancellationToken | undefined): Promise<ICodeOrDocsSearchResult>;
59
async search(query: string,
60
scopingQuery: ICodeOrDocsSearchSingleRepoScopingQuery | ICodeOrDocsSearchMultiRepoScopingQuery,
61
options: ICodeOrDocsSearchOptions = {},
62
cancellationToken?: CancellationToken
63
): Promise<ICodeOrDocsSearchItem[] | ICodeOrDocsSearchResult> {
64
options.limit ??= 6;
65
options.similarity ??= 0.766;
66
67
const req = new CacheableCodeOrDocSearchRequest(query, scopingQuery, options);
68
const cacheValue = await this.cache.get(req);
69
if (cacheValue) {
70
return cacheValue;
71
}
72
73
let result: ICodeOrDocsSearchItem[] | ICodeOrDocsSearchResult;
74
if (Array.isArray(scopingQuery.repo)) {
75
result = await this.searchClient.search(
76
query,
77
scopingQuery as ICodeOrDocsSearchMultiRepoScopingQuery,
78
options,
79
cancellationToken
80
);
81
} else {
82
result = await this.searchClient.search(
83
query,
84
scopingQuery as ICodeOrDocsSearchSingleRepoScopingQuery,
85
options,
86
cancellationToken
87
);
88
}
89
await this.cache.set(req, result);
90
return result;
91
}
92
}
93
94