/** * Base Provider Implementation * * Abstract base class that implements common functionality for all LLM providers. * Handles authentication headers, error handling, timeout management, and * provides the interface that concrete providers must implement. */ import { type Provider, type ProviderConfig, type CompletionRequest, type CompletionResponse, type SessionDelegationResponse, type StreamChunk } from './types.js'; /** * Options for making HTTP requests to provider APIs. */ export interface RequestOptions { /** HTTP method */ method: 'GET' | 'POST' | 'PUT' | 'DELETE'; /** Request URL */ url: string; /** Request headers */ headers: Record; /** Request body (will be JSON stringified) */ body?: unknown; /** Timeout in milliseconds */ timeoutMs?: number; /** Whether this is a streaming request */ stream?: boolean; } /** * Error response structure from provider APIs. */ export interface ProviderErrorResponse { error?: { message?: string; type?: string; code?: string; }; message?: string; } /** * Abstract base class for LLM providers. * * Provides common functionality including: * - Authentication header management * - HTTP request helpers with timeout support * - Standardized error handling * - Response parsing utilities * * Concrete providers must implement: * - complete(): Execute a completion request * - completeStream(): Execute a streaming completion request * - healthCheck(): Verify provider connectivity */ export declare abstract class BaseProvider implements Provider { /** Provider name identifier */ abstract readonly name: string; /** Provider configuration */ protected readonly config: ProviderConfig; /** Default timeout in milliseconds (3 minutes for complex LLM operations) */ protected readonly defaultTimeoutMs: number; /** * Create a new provider instance. * * @param config - Provider-specific configuration (API keys, URLs, etc.) */ constructor(config: ProviderConfig); /** * Execute a completion request and return the full response. * Must be implemented by concrete providers. * * For providers that support session delegation (e.g., Anthropic in 'session' mode), * this may return a SessionDelegationResponse instead of making an API call. * * @param request - The completion request * @returns Promise resolving to the completion response or session delegation */ abstract complete(request: CompletionRequest): Promise; /** * Execute a streaming completion request. * Must be implemented by concrete providers. * * @param request - The completion request * @returns Async iterable of stream chunks */ abstract completeStream(request: CompletionRequest): AsyncIterable; /** * Check if the provider is available and configured correctly. * Must be implemented by concrete providers. * * @throws ProviderError if the health check fails */ abstract healthCheck(): Promise; /** * Build common HTTP headers for provider requests. * Includes authentication and content-type headers. * * @returns Headers object for HTTP requests */ protected buildHeaders(): Record; /** * Make an HTTP request to the provider API with timeout support. * * @param options - Request options * @returns Promise resolving to the parsed JSON response * @throws ProviderError for API errors * @throws TimeoutError if the request times out */ protected makeRequest(options: RequestOptions): Promise; /** * Make a streaming HTTP request to the provider API. * * @param options - Request options * @returns Response object for streaming * @throws ProviderError for API errors * @throws TimeoutError if the connection times out */ protected makeStreamingRequest(options: RequestOptions): Promise; /** * Handle HTTP error responses from provider APIs. * Throws appropriate error types based on status code. * * @param response - The HTTP response object * @throws RateLimitError for 429 responses * @throws AuthenticationError for 401 responses * @throws ProviderError for other error responses */ protected handleErrorResponse(response: Response): Promise; /** * Handle request-level errors (network, timeout, etc.). * * @param error - The caught error * @param timeoutMs - The timeout that was configured * @throws TimeoutError for abort errors * @throws ProviderError for other errors */ protected handleRequestError(error: unknown, timeoutMs: number): never; /** * Get the base URL for API requests. * Uses configured base_url or returns the default for the provider. * * @param defaultUrl - Default URL if not configured * @returns The base URL to use for requests */ protected getBaseUrl(defaultUrl: string): string; /** * Create an AbortController with a timeout. * Useful for streaming requests where we need to track the controller. * * @param timeoutMs - Timeout in milliseconds * @returns Object containing the controller and a cleanup function */ protected createTimeoutController(timeoutMs: number): { controller: AbortController; cleanup: () => void; }; } //# sourceMappingURL=base.d.ts.map