Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/base/completionsCache.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 { FetchResponse } from '../../src/platform/nesFetch/node/completionsFetchServiceImpl';
6
import { getRequestId } from '../../src/platform/networking/common/fetch';
7
import { IHeaders, ReportFetchEvent, Response } from '../../src/platform/networking/common/fetcherService';
8
import { AsyncIterableObject } from '../../src/util/vs/base/common/async';
9
import { SQLiteSlottedCache } from './cache';
10
import { CachedResponseMetadata } from './cachingChatMLFetcher';
11
import { CacheableCompletionRequest } from './cachingCompletionsFetchService';
12
import { CurrentTestRunInfo } from './simulationContext';
13
14
export interface ICacheableCompletionsResponse {
15
readonly requestId: string;
16
readonly cacheMetadata: CachedResponseMetadata;
17
readonly status: number;
18
readonly statusText: string;
19
readonly body: string;
20
}
21
22
export namespace ICacheableCompletionsResponse {
23
24
export function create(requestId: string, cacheMetadata: CachedResponseMetadata, status: number, statusText: string, body: string): ICacheableCompletionsResponse {
25
return { requestId, cacheMetadata, status, statusText, body };
26
}
27
28
export function isICacheableResponse(obj: unknown): obj is ICacheableCompletionsResponse {
29
return (
30
typeof obj === 'object' &&
31
obj !== null &&
32
'requestId' in obj &&
33
typeof (obj as any).requestId === 'string' &&
34
'cacheMetadata' in obj &&
35
CachedResponseMetadata.isCachedResponseMetadata((obj as any).cacheMetadata) &&
36
'status' in obj &&
37
typeof (obj as any).status === 'number' &&
38
'statusText' in obj &&
39
typeof (obj as any).statusText === 'string' &&
40
'body' in obj &&
41
typeof (obj as any).body === 'string'
42
);
43
}
44
45
export function toFetchResponse(v: ICacheableCompletionsResponse): FetchResponse {
46
// @ulugbekna: currently, if we don't chunk up, the streaming logic errors out if the stream eventually errored (eg "response too long"),
47
// but we want to be able to capture edits proposed before the error
48
const bodyStream = stringToChunkedStream(v.body, 512 /* arbitrary chunk size to hit fast/correct balance */);
49
50
const headers = new Headers(); // @ulugbekna: we don't use headers, so this should be ok for now
51
52
const response = emptyFetcherResponse(headers);
53
54
return {
55
status: v.status,
56
statusText: v.statusText,
57
body: bodyStream,
58
headers,
59
requestId: getRequestId(headers),
60
response,
61
};
62
}
63
64
function stringToChunkedStream(str: string, chunkSize: number) {
65
return new AsyncIterableObject<string>(emitter => {
66
for (let i = 0; i < str.length; i += chunkSize) {
67
emitter.emitOne(str.slice(i, i + chunkSize));
68
}
69
});
70
}
71
}
72
73
export interface ICompletionsCache {
74
get(req: CacheableCompletionRequest, cacheSlot: number): Promise<ICacheableCompletionsResponse | undefined>;
75
set(req: CacheableCompletionRequest, cacheSlot: number, cachedResponse: ICacheableCompletionsResponse): Promise<void>;
76
}
77
78
export class CompletionsSQLiteCache extends SQLiteSlottedCache<CacheableCompletionRequest, ICacheableCompletionsResponse> implements ICompletionsCache {
79
constructor(salt: string, info: CurrentTestRunInfo) {
80
super('completions', salt, info);
81
}
82
}
83
84
export function emptyFetcherResponse(headers: IHeaders, reportEvent: ReportFetchEvent = () => { }): Response {
85
return new Response(
86
200,
87
'',
88
headers,
89
null,
90
'electron-fetch',
91
reportEvent,
92
'test',
93
'test',
94
);
95
}
96
97