Path: blob/main/extensions/copilot/src/platform/chat/test/common/staticChatMLFetcher.ts
13405 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*--------------------------------------------------------------------------------------------*/45import { Event } from '../../../../util/vs/base/common/event';6import { IResponseDelta } from '../../../networking/common/fetch';7import { IChatMLFetcher, IFetchMLOptions } from '../../common/chatMLFetcher';8import { ChatFetchResponseType, ChatResponse, ChatResponses } from '../../common/commonTypes';910export type StaticChatMLFetcherInput = string | (string | IResponseDelta[])[];1112export class StaticChatMLFetcher implements IChatMLFetcher {13_serviceBrand: undefined;14onDidMakeChatMLRequest = Event.None;15private reqs = 0;16public resolvedModel = '';1718constructor(public readonly value: StaticChatMLFetcherInput) { }1920async fetchOne({ finishedCb }: IFetchMLOptions): Promise<ChatResponse> {21// chunk up22const value = typeof this.value === 'string'23? this.value24: (this.value.at(this.reqs++) || this.value.at(-1)!);2526const chunks: IResponseDelta[] = (Array.isArray(value) ? value : [value]).flatMap(value => {27if (typeof value === 'string') {28const chunks: IResponseDelta[] = [];29for (let i = 0; i < value.length; i += 4) {30const chunk = value.slice(i, i + 4);31chunks.push({ text: chunk });32}33return chunks;34} else {35return value;36}37});3839// stream through finishedCb40let responseSoFar = '';41for (let i = 0; i < chunks.length; i++) {42finishedCb?.(responseSoFar, i, chunks[i]);43responseSoFar += chunks[i].text;44}4546return { 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 };47}4849async fetchMany(): Promise<ChatResponses> {50throw new Error('Method not implemented.');51}52}535455