import { Raw } from '@vscode/prompt-tsx';
import type { CancellationToken } from 'vscode';
import { IAuthenticationService } from '../../../platform/authentication/common/authentication';
import { IChatMLFetcher, IFetchMLOptions, Source } from '../../../platform/chat/common/chatMLFetcher';
import { IChatQuotaService } from '../../../platform/chat/common/chatQuotaService';
import { ChatLocation, ChatResponse, ChatResponses } from '../../../platform/chat/common/commonTypes';
import { IConversationOptions } from '../../../platform/chat/common/conversationOptions';
import { IInteractionService } from '../../../platform/chat/common/interactionService';
import { IConfigurationService } from '../../../platform/configuration/common/configurationService';
import { ICAPIClientService } from '../../../platform/endpoint/common/capiClient';
import { ILogService } from '../../../platform/log/common/logService';
import { OptionalChatRequestParams } from '../../../platform/networking/common/fetch';
import { IFetcherService } from '../../../platform/networking/common/fetcherService';
import { IChatEndpoint } from '../../../platform/networking/common/networking';
import { IChatWebSocketManager } from '../../../platform/networking/node/chatWebSocketManager';
import { IOTelService } from '../../../platform/otel/common/otelService';
import { IRequestLogger } from '../../../platform/requestLogger/common/requestLogger';
import { IExperimentationService } from '../../../platform/telemetry/common/nullExperimentationService';
import { ITelemetryService } from '../../../platform/telemetry/common/telemetry';
import { TelemetryData } from '../../../platform/telemetry/common/telemetryData';
import { Emitter } from '../../../util/vs/base/common/event';
import { Disposable } from '../../../util/vs/base/common/lifecycle';
import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';
import { IPowerService } from '../../power/common/powerService';
export interface IMadeChatRequestEvent {
    readonly messages: Raw.ChatMessage[];
    readonly model: string;
    readonly source?: Source;
    readonly tokenCount?: number;
}
export declare abstract class AbstractChatMLFetcher extends Disposable implements IChatMLFetcher {
    protected readonly options: IConversationOptions;
    _serviceBrand: undefined;
    constructor(options: IConversationOptions);
    protected preparePostOptions(requestOptions: OptionalChatRequestParams): OptionalChatRequestParams;
    protected readonly _onDidMakeChatMLRequest: Emitter<IMadeChatRequestEvent>;
    readonly onDidMakeChatMLRequest: import("../../../util/vs/base/common/event").Event<IMadeChatRequestEvent>;
    fetchOne(opts: IFetchMLOptions, token: CancellationToken): Promise<ChatResponse>;
    /**
     * Note: the returned array of strings may be less than `n` (e.g., in case there were errors during streaming)
     */
    abstract fetchMany(opts: IFetchMLOptions, token: CancellationToken): Promise<ChatResponses>;
}
export declare class ChatMLFetcherImpl extends AbstractChatMLFetcher {
    private readonly _fetcherService;
    private readonly _telemetryService;
    private readonly _requestLogger;
    private readonly _logService;
    private readonly _authenticationService;
    private readonly _interactionService;
    private readonly _chatQuotaService;
    private readonly _capiClientService;
    private readonly _configurationService;
    private readonly _experimentationService;
    private readonly _powerService;
    private readonly _instantiationService;
    private readonly _webSocketManager;
    private readonly _otelService;
    private static readonly _maxConsecutiveWebSocketFallbacks;
    /**
     * Delays (in ms) between connectivity check attempts before retrying a failed request.
     * Configurable for testing purposes.
     */
    connectivityCheckDelays: number[];
    /**
     * Tracks consecutive WebSocket request failures where the HTTP retry succeeded.
     * After {@link _maxConsecutiveWebSocketFallbacks} such failures, WebSocket requests are disabled entirely.
     */
    private _consecutiveWebSocketRetryFallbacks;
    constructor(_fetcherService: IFetcherService, _telemetryService: ITelemetryService, _requestLogger: IRequestLogger, _logService: ILogService, _authenticationService: IAuthenticationService, _interactionService: IInteractionService, _chatQuotaService: IChatQuotaService, _capiClientService: ICAPIClientService, options: IConversationOptions, _configurationService: IConfigurationService, _experimentationService: IExperimentationService, _powerService: IPowerService, _instantiationService: IInstantiationService, _webSocketManager: IChatWebSocketManager, _otelService: IOTelService);
    /**
     * Note: the returned array of strings may be less than `n` (e.g., in case there were errors during streaming)
     */
    fetchMany(opts: IFetchMLOptions, token: CancellationToken): Promise<ChatResponses>;
    private _checkNetworkConnectivity;
    private _getAuthHeaders;
    private _retryAfterError;
    private _fetchAndStreamChat;
    private _doFetchAndStreamChat;
    /**
     * Sends a chat request via a persistent WebSocket connection instead of HTTP POST.
     * Events are the same Responses API streaming events, processed by OpenAIResponsesProcessor.
     */
    private _doFetchViaWebSocket;
    private _doFetchViaHttp;
    private _fetchWithInstrumentation;
    private _handleError;
    private processSuccessfulResponse;
    private filterImageMessages;
    private isRepetitive;
    /**
     * Check for repetition in partial response deltas from a cancelled request.
     *
     * This method performs the same repetition detection as the `isRepetitive` method,
     * but operates on partial response data collected before the request was cancelled.
     *
     * Key differences from completed requests:
     * - Text is reconstructed from delta.text values instead of message.content
     * - Tokens are approximated by splitting text on whitespace instead of using
     *   the actual token array (which is only available in completed responses)
     * - Enhanced telemetry won't include RequestId fields since we only have the
     *   headerRequestId string, not the full RequestId object
     * - The finishReason is marked as 'canceled' to distinguish from server-generated
     *   finish reasons
     */
    private checkRepetitionInDeltas;
    private processCanceledResponse;
    private processFailedResponse;
    private processError;
    private _handleWebSocketCAPIError;
    private _handleWebSocketError;
    private scrubErrorDetail;
}
export declare function createTelemetryData(chatEndpointInfo: IChatEndpoint, location: ChatLocation, headerRequestId: string): TelemetryData;
/**
 * WARNING: The value that is returned from this function drives the disablement of RAI for full-file rewrite requests
 * in Copilot Edits, Copilot Chat, Agent Mode, and Inline Chat.
 * If your chat location generates full-file rewrite requests and you are unsure if changing something here will cause problems, please talk to @roblourens
 */
export declare function locationToIntent(location: ChatLocation): string;
//# sourceMappingURL=chatMLFetcher.d.ts.map