Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/chat/test/common/staticChatMLFetcher.ts
13405 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
6
import { Event } from '../../../../util/vs/base/common/event';
7
import { IResponseDelta } from '../../../networking/common/fetch';
8
import { IChatMLFetcher, IFetchMLOptions } from '../../common/chatMLFetcher';
9
import { ChatFetchResponseType, ChatResponse, ChatResponses } from '../../common/commonTypes';
10
11
export type StaticChatMLFetcherInput = string | (string | IResponseDelta[])[];
12
13
export class StaticChatMLFetcher implements IChatMLFetcher {
14
_serviceBrand: undefined;
15
onDidMakeChatMLRequest = Event.None;
16
private reqs = 0;
17
public resolvedModel = '';
18
19
constructor(public readonly value: StaticChatMLFetcherInput) { }
20
21
async fetchOne({ finishedCb }: IFetchMLOptions): Promise<ChatResponse> {
22
// chunk up
23
const value = typeof this.value === 'string'
24
? this.value
25
: (this.value.at(this.reqs++) || this.value.at(-1)!);
26
27
const chunks: IResponseDelta[] = (Array.isArray(value) ? value : [value]).flatMap(value => {
28
if (typeof value === 'string') {
29
const chunks: IResponseDelta[] = [];
30
for (let i = 0; i < value.length; i += 4) {
31
const chunk = value.slice(i, i + 4);
32
chunks.push({ text: chunk });
33
}
34
return chunks;
35
} else {
36
return value;
37
}
38
});
39
40
// stream through finishedCb
41
let responseSoFar = '';
42
for (let i = 0; i < chunks.length; i++) {
43
finishedCb?.(responseSoFar, i, chunks[i]);
44
responseSoFar += chunks[i].text;
45
}
46
47
return { type: ChatFetchResponseType.Success, requestId: '', serverRequestId: '', usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0, prompt_tokens_details: { cached_tokens: 0 } }, value: responseSoFar, resolvedModel: this.resolvedModel };
48
}
49
50
async fetchMany(): Promise<ChatResponses> {
51
throw new Error('Method not implemented.');
52
}
53
}
54
55