Path: blob/main/extensions/copilot/src/platform/nesFetch/common/completionsFetchService.ts
13401 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 { Result } from '../../../util/common/result';6import { createServiceIdentifier } from '../../../util/common/services';7import { CancellationToken } from '../../../util/vs/base/common/cancellation';8import { IHeaders } from '../../networking/common/fetcherService';9import { ResponseStream } from './responseStream';1011export namespace Completions {12interface BaseCompletionsParams {13prompt: string;14stop?: string[];15top_p?: number;16best_of?: number;17max_tokens?: number;18temperature?: number;19presence_penalty?: number;20frequency_penalty?: number;21// required to access certain experimental models22model?: string;23logprobs?: number;24n?: number;25stream: true;26}2728interface CodexV2Params {29suffix?: string;30extra?: { [key: string]: any };31code_annotations?: boolean;32}3334export interface ModelParams extends BaseCompletionsParams, CodexV2Params { }3536export class RequestCancelled {37readonly kind = 'cancelled' as const;38}39export class UnsuccessfulResponse {40readonly kind = 'not-200-status' as const;41constructor(42public readonly status: number,43public readonly statusText: string,44public readonly headers: IHeaders,45public readonly text: () => Promise<string>46) { }47}48export class Unexpected {49readonly kind = 'unexpected' as const;50constructor(51public readonly error: Error52) { }53}54export type CompletionsFetchFailure =55| Completions.RequestCancelled56| Completions.UnsuccessfulResponse57| Completions.Unexpected;5859export namespace Internal {60export type FetchOptions = {61requestId: string;62headers: { [name: string]: string };63body: string;64};65}66}6768export type CompletionsFetchErrorType = 'stop_content_filter' | 'stop_length' | 'unknown';6970export class CompletionsFetchError extends Error {71constructor(72readonly type: CompletionsFetchErrorType,73readonly requestId: string,74message: string75) {76super(message);77}78}7980export const ICompletionsFetchService = createServiceIdentifier<ICompletionsFetchService>('ICompletionsFetchService');8182/**83* OpenAI has completions and _chat_ completions endpoints. This's (non-chat) completions endpoint fetcher.84*/85export interface ICompletionsFetchService {86readonly _serviceBrand: undefined;8788fetch(89url: string,90secretKey: string,91params: Completions.ModelParams,92requestId: string,93ct: CancellationToken,94headerOverrides?: Record<string, string>95): Promise<Result<ResponseStream, Completions.CompletionsFetchFailure>>;9697disconnectAll(): Promise<unknown>;98}99100101