UNPKG

@rocketnew/llm-sdk

Version:

Universal LLM SDK for JavaScript/TypeScript - OpenAI, Anthropic, Gemini, Perplexity and more

1,483 lines (1,464 loc) 78 kB
/** * OpenAI-specific type definitions */ interface ChatCompletionToolCallFunctionChunk { name: string | null; arguments: string; provider_specific_fields?: Record<string, any>; } interface ChatCompletionAssistantToolCall { id: string | null; type: 'function'; function: ChatCompletionToolCallFunctionChunk; } interface ChatCompletionToolCallChunk { id: string | null; type: 'function'; function: ChatCompletionToolCallFunctionChunk; index: number; /** * Optional caller metadata for programmatic tool calling (Anthropic extension). * Opaque to most consumers; forwarded through by downstream transformation. */ caller?: Record<string, unknown>; } interface ChatCompletionCachedContent { type: 'ephemeral'; } interface ChatCompletionThinkingBlock { type: 'thinking'; thinking: string; signature?: string; cache_control?: ChatCompletionCachedContent | Record<string, any>; } interface ChatCompletionRedactedThinkingBlock { type: 'redacted_thinking'; data: string; cache_control?: ChatCompletionCachedContent | Record<string, any>; } interface ChatCompletionAnnotationURLCitation { end_index: number; start_index: number; title?: string; url: string; } interface ChatCompletionAnnotation { type: 'url_citation'; url_citation: ChatCompletionAnnotationURLCitation; } interface OpenAIChatCompletionTextObject { type: 'text'; text: string; } interface ChatCompletionTextObject extends OpenAIChatCompletionTextObject { cache_control?: ChatCompletionCachedContent; } interface ChatCompletionImageUrlObject { url: string; detail?: string; format?: string; } interface ChatCompletionImageObject { type: 'image_url'; image_url: string | ChatCompletionImageUrlObject; } interface ChatCompletionVideoUrlObject { url: string; detail?: string; } interface ChatCompletionVideoObject { type: 'video_url'; video_url: string | ChatCompletionVideoUrlObject; } interface ChatCompletionAudioObject { type: 'input_audio'; input_audio: { data: string; format: 'wav' | 'mp3'; }; } interface DocumentObject { type: 'text'; media_type: string; data: string; } interface CitationsObject { enabled: boolean; } interface ChatCompletionDocumentObject { type: 'document'; source: DocumentObject; title: string; context: string; citations?: CitationsObject; } interface ChatCompletionFileObjectFile { file_data?: string; file_id?: string; filename?: string; format?: string; } interface ChatCompletionFileObject { type: 'file'; file: ChatCompletionFileObjectFile; } type OpenAIMessageContentListBlock = ChatCompletionTextObject | ChatCompletionImageObject | ChatCompletionAudioObject | ChatCompletionDocumentObject | ChatCompletionVideoObject | ChatCompletionFileObject; type OpenAIMessageContent = string | OpenAIMessageContentListBlock[]; interface OpenAIChatCompletionUserMessage { role: 'user'; content: OpenAIMessageContent; } interface ChatCompletionUserMessage extends OpenAIChatCompletionUserMessage { cache_control?: ChatCompletionCachedContent; } interface OpenAIChatCompletionAssistantMessage { role: 'assistant'; content?: string | (ChatCompletionTextObject | ChatCompletionThinkingBlock)[] | null; name?: string; tool_calls?: ChatCompletionAssistantToolCall[]; function_call?: ChatCompletionToolCallFunctionChunk; reasoning_content?: string; } interface ChatCompletionAssistantMessage extends OpenAIChatCompletionAssistantMessage { cache_control?: ChatCompletionCachedContent; thinking_blocks?: (ChatCompletionThinkingBlock | ChatCompletionRedactedThinkingBlock)[]; } interface ChatCompletionToolMessage { role: 'tool'; content: string | ChatCompletionTextObject[]; tool_call_id: string; } interface ChatCompletionFunctionMessage { role: 'function'; content?: string | ChatCompletionTextObject[] | null; name: string; tool_call_id?: string; } interface OpenAIChatCompletionSystemMessage { role: 'system'; content: string | any[]; name?: string; } interface OpenAIChatCompletionDeveloperMessage { role: 'developer'; content: string | any[]; name?: string; } interface ChatCompletionSystemMessage extends OpenAIChatCompletionSystemMessage { cache_control?: ChatCompletionCachedContent; } interface ChatCompletionDeveloperMessage extends OpenAIChatCompletionDeveloperMessage { cache_control?: ChatCompletionCachedContent; } type AllMessageValues = ChatCompletionUserMessage | ChatCompletionAssistantMessage | ChatCompletionToolMessage | ChatCompletionSystemMessage | ChatCompletionFunctionMessage | ChatCompletionDeveloperMessage; interface ChatCompletionToolChoiceFunctionParam { name: string; } interface ChatCompletionToolChoiceObjectParam { type: 'function'; function: ChatCompletionToolChoiceFunctionParam; } type ChatCompletionToolChoiceStringValues = 'none' | 'auto' | 'required'; type ChatCompletionToolChoiceValues = ChatCompletionToolChoiceStringValues | ChatCompletionToolChoiceObjectParam; interface ChatCompletionToolParamFunctionChunk { name: string; description?: string; parameters?: Record<string, any>; strict?: boolean; } interface OpenAIChatCompletionToolParam { type: 'function' | string; function: ChatCompletionToolParamFunctionChunk; } interface ChatCompletionToolParam extends OpenAIChatCompletionToolParam { cache_control?: ChatCompletionCachedContent; } type OpenAIImageGenerationOptionalParams = 'background' | 'moderation' | 'n' | 'output_compression' | 'output_format' | 'quality' | 'response_format' | 'size' | 'style' | 'user'; /** * Completion types for chat completions * * This module contains message parameter types for chat completion requests. */ interface ChatCompletionSystemMessageParam { content: string; role: 'system'; name?: string; } interface ChatCompletionContentPartTextParam { text: string; type: 'text'; } interface ImageURL { url: string; detail?: 'auto' | 'low' | 'high'; } interface ChatCompletionContentPartImageParam { image_url: ImageURL; type: 'image_url'; } type ChatCompletionContentPartParam = ChatCompletionContentPartTextParam | ChatCompletionContentPartImageParam; interface ChatCompletionUserMessageParam { content: string | ChatCompletionContentPartParam[]; role: 'user'; name?: string; } interface FunctionCall { arguments: string; name: string; } interface Function { arguments: string; name: string; } interface ChatCompletionToolMessageParam { content: string | ChatCompletionContentPartParam[]; role: 'tool'; tool_call_id: string; } interface ChatCompletionFunctionMessageParam { content: string | ChatCompletionContentPartParam[]; name: string; role: 'function'; } interface ChatCompletionMessageToolCallParam { id: string; function: Function; type: 'function'; } interface ChatCompletionAssistantMessageParam { role: 'assistant'; content?: string | null; function_call?: FunctionCall; name?: string; tool_calls?: ChatCompletionMessageToolCallParam[]; } interface ChatCompletionDeveloperMessageParam { content: string; role: 'developer'; name?: string; } type ChatCompletionMessageParam = ChatCompletionSystemMessageParam | ChatCompletionUserMessageParam | ChatCompletionAssistantMessageParam | ChatCompletionFunctionMessageParam | ChatCompletionToolMessageParam | ChatCompletionDeveloperMessageParam; interface CompletionRequest { model: string; messages: ChatCompletionMessageParam[]; timeout?: number; temperature?: number; top_p?: number; n?: number; stream?: boolean; stop?: string | string[]; max_tokens?: number; max_completion_tokens?: number; presence_penalty?: number; frequency_penalty?: number; logit_bias?: Record<string, any>; user?: string; response_format?: Record<string, any>; seed?: number; tools?: ChatCompletionToolParam[]; tool_choice?: ChatCompletionToolChoiceValues; parallel_tool_calls?: boolean; logprobs?: boolean; top_logprobs?: number; functions?: any[]; function_call?: string | Record<string, any>; api_base?: string; api_key?: string; } /** * Message interface for SDK input * This is the message format users pass to the completion function */ interface Message { role: 'system' | 'user' | 'assistant' | 'tool' | 'function' | 'developer'; content: string | ContentBlock[]; tool_call_id?: string; name?: string; cache_control?: ChatCompletionCachedContent | Record<string, any>; tool_calls?: ChatCompletionToolCallChunk[]; thinking_blocks?: (ChatCompletionThinkingBlock | ChatCompletionRedactedThinkingBlock)[]; reasoning_content?: string; } /** * Content block types */ type ContentBlock = TextContentBlock | ImageURLContentBlock | FileContentBlock | ThinkingContentBlock | DocumentContentBlock; /** * Text content block with optional cache control */ interface TextContentBlock { type: 'text'; text: string; cache_control?: ChatCompletionCachedContent | Record<string, any>; } /** * Image URL content block with optional cache control */ interface ImageURLContentBlock { type: 'image_url'; image_url: string | { url: string; detail?: 'auto' | 'low' | 'high'; format?: string; }; cache_control?: ChatCompletionCachedContent | Record<string, any>; } /** * File content block with optional cache control */ interface FileContentBlock { type: 'file'; file: { file_id?: string; file_data?: string; filename?: string; format?: string; }; cache_control?: ChatCompletionCachedContent | Record<string, any>; } /** * Thinking content block with optional cache control */ interface ThinkingContentBlock { type: 'thinking'; thinking: string; signature?: string; cache_control?: ChatCompletionCachedContent | Record<string, any>; } /** * Document content block with optional cache control */ interface DocumentContentBlock { type: 'document'; source: { type: string; media_type: string; data: string; }; title?: string; context?: string; citations?: { enabled: boolean; }; cache_control?: ChatCompletionCachedContent | Record<string, any>; } interface ResponseFormat { type: 'json_object' | 'json_schema' | 'text'; json_schema?: { name?: string; schema: Record<string, any>; strict?: boolean; }; response_schema?: Record<string, any>; } interface ChatCompletionRequest { model: string; messages: Message[]; temperature?: number; max_tokens?: number; max_completion_tokens?: number; top_p?: number; stream?: boolean; stream_options?: Record<string, any>; stop?: string | string[]; tools?: ChatCompletionToolParam[]; tool_choice?: ChatCompletionToolChoiceValues; parallel_tool_calls?: boolean; response_format?: ResponseFormat; thinking?: { type: 'enabled' | 'disabled'; budget_tokens?: number; }; reasoning_effort?: 'minimal' | 'none' | 'low' | 'medium' | 'high' | 'xhigh' | 'default'; verbosity?: 'low' | 'medium' | 'high'; web_search_options?: { search_context_size?: 'low' | 'medium' | 'high'; user_location?: { type: 'approximate'; approximate?: { city?: string; country?: string; region?: string; timezone?: string; }; }; }; audio?: { voice?: string; format?: string; }; modalities?: string[]; seed?: number; logprobs?: boolean; top_logprobs?: number; frequency_penalty?: number; presence_penalty?: number; api_key?: string; api_base?: string; base_url?: string; api_version?: string; timeout?: number; request_timeout?: number; max_retries?: number; organization?: string; user?: string; headers?: Record<string, string>; extra_headers?: Record<string, string>; custom_llm_provider?: string; provider_specific_header?: any; functions?: any[]; function_call?: string | Record<string, any>; prediction?: any; logit_bias?: Record<string, number>; safety_identifier?: string; service_tier?: string; n?: number; ensure_alternating_roles?: boolean; user_continue_message?: ChatCompletionUserMessage; assistant_continue_message?: ChatCompletionAssistantMessage; supports_system_message?: boolean; drop_params?: boolean; allowed_openai_params?: string[]; } type ChatCompletionRequestNonStreaming = ChatCompletionRequest & { stream?: false | never; }; type ChatCompletionRequestStreaming = ChatCompletionRequest & { stream: true; }; /** * Type utility functions and centralized Usage class * * This module provides centralized usage handling for all providers. * All providers should use the Usage class for consistent usage normalization. */ /** * FileTypes — Binary file input for endpoints that accept uploads * (audio transcription, image edit, and any future multipart uploads). * * Accepted shapes: * - `Buffer` / `Uint8Array`: raw bytes. When no filename metadata is * available, endpoints fall back to a sensible default (e.g. * `audio.wav` for transcription, `image.png` for image edit) so the * multipart request is well-formed. * - `File`: carries its own `name` and MIME `type`; used as-is. * - `Blob`: used as-is when it has a `name` attached; otherwise treated * like raw bytes with the per-endpoint filename default. * - `Array<...>`: multi-file endpoints (currently: image edit with * multiple input images). Each element follows the rules above. * * Providers that drive their own multipart serialization (for example, * ElevenLabs transcription and the OpenAI image-edit path) normalize * this value internally. The OpenAI transcription path forwards the * input to the provider client and applies the default-filename * fallback just before the wire call. */ type FileTypes = Buffer | Uint8Array | File | Blob | Array<Buffer | Uint8Array | File | Blob>; /** * RequestFiles - Represents files for multipart/form-data requests * Array of [fieldName, fileData] tuples */ type RequestFiles = Array<[string, any]>; /** * GenericRocketLLMParams - Generic parameters for RocketLLM calls */ interface GenericRocketLLMParams { customLlmProvider?: string; tpm?: number; rpm?: number; timeout?: number | string; streamTimeout?: number | string; maxRetries?: number; organization?: string; rocketllmTraceId?: string; [key: string]: any; } /** * Generate a unique response ID (private helper function) */ declare function generateId(): string; /** * Cache creation token details for Anthropic prompt caching */ interface CacheCreationTokenDetails { ephemeral_5m_input_tokens?: number; ephemeral_1h_input_tokens?: number; } /** * Completion tokens details wrapper * Extended version that includes text_tokens, image_tokens, audio_tokens */ interface CompletionTokensDetailsWrapper { reasoning_tokens?: number; text_tokens?: number; image_tokens?: number; audio_tokens?: number; accepted_prediction_tokens?: number; rejected_prediction_tokens?: number; } /** * Prompt tokens details wrapper * Extended version that includes text_tokens, image_tokens, and provider-specific fields */ interface PromptTokensDetailsWrapper { cached_tokens?: number; text_tokens?: number; image_tokens?: number; audio_tokens?: number; web_search_requests?: number; cache_creation_tokens?: number; cache_creation_token_details?: CacheCreationTokenDetails; } /** * Server tool use tracking */ interface ServerToolUse { web_search_requests?: number; tool_search_requests?: number; } /** * Usage constructor parameters */ interface UsageParams { prompt_tokens?: number; completion_tokens?: number; total_tokens?: number; reasoning_tokens?: number; prompt_tokens_details?: PromptTokensDetailsWrapper | null; completion_tokens_details?: CompletionTokensDetailsWrapper | null; server_tool_use?: ServerToolUse | null; cost?: number; cache_creation_input_tokens?: number; cache_read_input_tokens?: number; prompt_cache_hit_tokens?: number; [key: string]: any; } /** * Centralized Usage class * * All providers should use this class for consistent usage handling. * Handles: * - completion_tokens_details normalization * - Auto-calculation of text_tokens when reasoning_tokens is present * - prompt_tokens_details normalization * - Provider-specific mappings (DeepSeek, Anthropic) */ declare class Usage { prompt_tokens: number; completion_tokens: number; total_tokens: number; completion_tokens_details?: CompletionTokensDetailsWrapper; prompt_tokens_details?: PromptTokensDetailsWrapper; server_tool_use?: ServerToolUse; cost?: number; private _cache_creation_input_tokens; private _cache_read_input_tokens; constructor(params: UsageParams); /** * Check if a key exists (for 'in' operator compatibility) */ has(key: string): boolean; /** * Get a value with default (for .get() compatibility) */ get<T = any>(key: string, defaultValue?: T): T | undefined; /** * Convert to plain object for JSON serialization */ toJSON(): Record<string, any>; } interface ResponseFunctionCall { name: string; arguments: string; } interface ResponseMessage { content: string | null; role: 'assistant' | 'user' | 'system' | 'tool' | 'function'; tool_calls?: ChatCompletionToolCallChunk[]; function_call?: ResponseFunctionCall; audio?: Record<string, any>; images?: Array<{ image_url: { url: string; detail?: string; }; index: number; type: 'image_url'; }>; reasoning_content?: string; thinking_blocks?: (ChatCompletionThinkingBlock | ChatCompletionRedactedThinkingBlock)[]; provider_specific_fields?: Record<string, any>; annotations?: ChatCompletionAnnotation[]; } interface Delta { content?: string | null; role?: string; tool_calls?: ChatCompletionToolCallChunk[]; function_call?: ResponseFunctionCall; audio?: Record<string, any>; images?: Array<{ image_url: { url: string; detail?: string; }; index: number; type: 'image_url'; }>; reasoning_content?: string; thinking_blocks?: (ChatCompletionThinkingBlock | ChatCompletionRedactedThinkingBlock)[]; provider_specific_fields?: Record<string, any>; annotations?: ChatCompletionAnnotation[]; } interface TopLogprob { bytes?: number[]; logprob: number; token: string; } interface ChatCompletionTokenLogprob { bytes?: number[]; logprob: number; token: string; top_logprobs?: TopLogprob[]; } interface ChoiceLogprobs { content?: ChatCompletionTokenLogprob[]; refusal?: ChatCompletionTokenLogprob[]; } interface Choices { finish_reason: string; index: number; message: ResponseMessage; logprobs?: ChoiceLogprobs; provider_specific_fields?: Record<string, any>; } interface StreamingChoices { finish_reason: string | null; index: number; delta: Delta; logprobs?: ChoiceLogprobs; } interface ModelResponseBase { id: string; created: number; model: string | null; object: string; system_fingerprint?: string; } interface ModelResponseStream extends ModelResponseBase { choices: StreamingChoices[]; usage?: Usage; provider_specific_fields?: Record<string, any>; } interface ModelResponse extends ModelResponseBase { choices: (Choices | StreamingChoices)[]; usage?: Usage; } interface GenericStreamingChunk { text?: string; is_finished?: boolean; finish_reason?: string; usage?: UsageParams; index?: number; tool_use?: { id?: string; name?: string; input?: string; }; provider_specific_fields?: Record<string, any>; } interface GenericImageParsingChunk { type: string; media_type: string; data: string; } interface ResponseFormatChunk { type?: string; json_schema?: Record<string, any>; } type LLMProvider = 'anthropic' | 'openai' | 'gemini' | 'cohere' | 'mistral' | 'perplexity' | 'rocketllm_proxy'; /** * RocketLLMProviders - Enum of all supported LLM providers * * Used by ProviderConfigManager to determine which config class to use * for request transformation. */ declare enum RocketLLMProviders { OPENAI = "openai", ANTHROPIC = "anthropic", GEMINI = "gemini", PERPLEXITY = "perplexity", ELEVENLABS = "elevenlabs", ROCKETLLM_PROXY = "rocketllm_proxy" } /** * Completion tokens details */ interface CompletionTokensDetails { reasoning_tokens?: number; text_tokens?: number; audio_tokens?: number; image_tokens?: number; accepted_prediction_tokens?: number; rejected_prediction_tokens?: number; } /** * Prompt tokens details */ interface PromptTokensDetails { cached_tokens?: number; text_tokens?: number; audio_tokens?: number; cache_creation_tokens?: number; cache_creation_token_details?: { ephemeral_5m_input_tokens?: number; ephemeral_1h_input_tokens?: number; }; web_search_requests?: number; } /** * Usage object with detailed token information */ interface UsageInfo { prompt_tokens: number; completion_tokens: number; total_tokens: number; cache_creation_input_tokens?: number; cache_read_input_tokens?: number; completion_tokens_details?: CompletionTokensDetails; prompt_tokens_details?: PromptTokensDetails; server_tool_use?: { web_search_requests?: number; tool_search_requests?: number; }; } interface ChatCompletionResponse { id: string; object: 'chat.completion'; created: number; model: string; choices: Array<{ index: number; message: { role: string; content: string | null; tool_calls?: ChatCompletionToolCallChunk[]; thinking_blocks?: (ChatCompletionThinkingBlock | ChatCompletionRedactedThinkingBlock)[]; reasoning_content?: string; }; finish_reason: string; provider_specific_fields?: Record<string, any>; }>; usage: UsageInfo; } interface StreamChunk { id: string; object: 'chat.completion.chunk'; created: number; model: string; choices: Array<{ index: number; delta: { role?: string; content?: string; tool_calls?: ChatCompletionToolCallChunk[]; provider_specific_fields?: Record<string, any>; thinking_blocks?: (ChatCompletionThinkingBlock | ChatCompletionRedactedThinkingBlock)[]; reasoning_content?: string; }; finish_reason: string | null; }>; usage?: UsageInfo; } interface RocketLLMOptions { baseURL?: string; apiKey?: string; anthropicApiKey?: string; openaiApiKey?: string; geminiApiKey?: string; cohereApiKey?: string; mistralApiKey?: string; dangerouslyAllowBrowser?: boolean; timeout?: number; defaultHeaders?: Record<string, string>; } interface ProviderSpecificHeader { custom_llm_provider: string; extra_headers: Record<string, string>; } interface TranscriptionUsageDurationObject { type: 'duration'; seconds: number; } interface TranscriptionUsageInputTokenDetailsObject { audio_tokens: number; text_tokens: number; } interface TranscriptionUsageTokensObject { type: 'tokens'; input_tokens: number; output_tokens: number; total_tokens: number; input_token_details: TranscriptionUsageInputTokenDetailsObject; } interface TranscriptionResponse { text: string | null; usage?: TranscriptionUsageDurationObject | TranscriptionUsageTokensObject; language?: string; task?: string; duration?: number; words?: any[]; segments?: any[]; _hidden_params?: Record<string, any>; } /** * Perplexity Search Configuration * Handles web search via Perplexity API */ interface SearchResult { title: string; url: string; snippet: string; date?: string; last_updated?: string; } interface SearchResponse { object: 'search'; results: SearchResult[]; } /** * Binary response wrapper for endpoints that return raw audio/video/image * bytes (TTS today; extensible to other binary-returning endpoints). * * Wraps a Fetch `Response` so callers can: * - read the raw bytes via `content()` / `read()` * - decode as text or JSON via `text()` / `json()` * - stream bytes without buffering the whole body via `iterBytes()` / * `iterText()` / `iterLines()` / `streamToFile()` * - persist the full body via `writeToFile()` * - inspect response metadata (`status`, `headers`, `encoding`) without * re-wiring the underlying HTTP response * * Method names are JS-idiomatic camelCase. `read()` is provided as an * alias for `content()` to match the naming that upstream clients of the * underlying HTTP libraries conventionally use. */ declare class BinaryResponseContent { #private; /** * Underlying Fetch Response. Public so advanced callers can drop down * to the raw response if a method surface is missing. */ readonly response: Response; /** * Free-form metadata bag used to attach attribution fields (model, * custom_llm_provider, etc.) that downstream tooling may read. Mutable * by design — callers are expected to append to it. */ hiddenParams: Record<string, unknown>; constructor(response: Response); get status(): number; get headers(): Headers; /** * Charset parsed from the `content-type` header (e.g. `utf-8` for * `text/plain; charset=utf-8`). Null if no charset is present. */ get encoding(): string | null; get charsetEncoding(): string | null; /** * Read the full body as a `Uint8Array`. Caches the result so repeat * calls do not attempt to drain an already-consumed body stream. */ content(): Promise<Uint8Array>; /** * Alias of `content()` for API symmetry with common binary-response * wrappers in other HTTP clients. */ read(): Promise<Uint8Array>; /** * Decode the full body using the response's charset (defaults to * `utf-8`). */ text(): Promise<string>; json<T = unknown>(): Promise<T>; /** * Async byte iterator. If `content()` has already been called, yields * slices of the cached buffer sized by `chunkSize`. Otherwise streams * directly from the live response body — which is drained as a side * effect, so subsequent `content()` calls on the same instance will * return an empty cache. */ iterBytes(chunkSize?: number): AsyncIterable<Uint8Array>; /** * Iterate decoded text chunks. Uses a streaming TextDecoder so * multi-byte sequences that straddle chunk boundaries are handled * correctly. */ iterText(chunkSize?: number): AsyncIterable<string>; /** * Iterate decoded lines. Splits on `\n` and strips trailing `\r` for * CR-LF inputs. The last partial line (no trailing newline) is yielded * on stream end. */ iterLines(): AsyncIterable<string>; /** * Buffer the entire body and write it to disk in one shot. */ writeToFile(path: string | URL): Promise<void>; /** * Stream body chunks to disk. Preferred over `writeToFile()` for large * outputs where materializing the full buffer in memory is undesirable. */ streamToFile(path: string | URL, opts?: { chunkSize?: number; }): Promise<void>; /** * Cancel the underlying stream. Safe to call multiple times; swallows * any error thrown by the already-consumed or already-aborted stream. */ close(): Promise<void>; } /** * Main completion function - Entry point for all LLM completion requests * Supports multiple LLM providers with a unified OpenAI-compatible interface */ /** * Perform a completion using any supported LLM provider * Parameters match OpenAI's chat completion API specification */ declare function completion(request: ChatCompletionRequestNonStreaming): Promise<ChatCompletionResponse>; declare function completion(request: ChatCompletionRequestStreaming): Promise<AsyncGenerator<StreamChunk, void, unknown>>; declare function completion(request: ChatCompletionRequest): Promise<ChatCompletionResponse | AsyncGenerator<StreamChunk, void, unknown>>; /** * Text-to-Speech: Convert text to spoken audio */ declare function speech(request: { model: string; input: string; voice?: string | Record<string, any>; api_key?: string; api_base?: string; api_version?: string; organization?: string; project?: string; max_retries?: number; timeout?: number; custom_llm_provider?: string; response_format?: string; speed?: number; instructions?: string; headers?: Record<string, string>; extra_headers?: Record<string, string>; metadata?: Record<string, any>; [key: string]: any; }): Promise<BinaryResponseContent>; /** * Speech-to-Text: Transcribe audio to text */ declare function transcription(request: { model: string; file: FileTypes; language?: string; prompt?: string; response_format?: 'json' | 'text' | 'srt' | 'verbose_json' | 'vtt'; timestamp_granularities?: Array<'word' | 'segment'>; temperature?: number; include?: string[]; user?: string; timeout?: number; api_key?: string; api_base?: string; api_version?: string; max_retries?: number; custom_llm_provider?: string; drop_params?: boolean; extra_headers?: Record<string, string>; headers?: Record<string, string>; metadata?: Record<string, any>; [key: string]: any; }): Promise<TranscriptionResponse>; /** * Web Search: Search the web using Perplexity */ declare function search(request: { query: string | string[]; search_provider: string; max_results?: number; search_domain_filter?: string[]; max_tokens_per_page?: number; country?: string; api_key?: string; api_base?: string; timeout?: number; }): Promise<SearchResponse>; /** * Image API Types * Interfaces for image generation and editing */ /** * ImageEditInput - Accepted binary input shapes for image-edit `image` / `mask`. * * Intentionally excludes `string`: the image-edits endpoint uses * multipart/form-data and requires raw bytes on the wire. Base64 / data: URIs * must be decoded to a Buffer (or Uint8Array / Blob / File) by the caller * before reaching this boundary. This mirrors Python's `FileTypes` contract. */ type ImageEditInput = Buffer | Uint8Array | Blob | File | Array<Buffer | Uint8Array | Blob | File>; /** * ImageEditOptionalRequestParams * Optional parameters supported by OpenAI's image edit API. * Params here: https://platform.openai.com/docs/api-reference/images/createEdit */ interface ImageEditOptionalRequestParams { background?: 'transparent' | 'opaque' | 'auto'; mask?: ImageEditInput; n?: number; quality?: 'high' | 'medium' | 'low' | 'standard' | 'auto'; response_format?: 'url' | 'b64_json'; size?: string; user?: string; } /** * ImageEditRequestParams * Request parameters supported by OpenAI's image edit API. * Params here: https://platform.openai.com/docs/api-reference/images/createEdit */ interface ImageEditRequestParams extends ImageEditOptionalRequestParams { image: FileTypes; prompt: string; model?: string; } /** * Image generation request parameters * For gpt-image-1, gpt-image-1-mini, gpt-image-1.5, and future gpt-image-* / chatgpt-image-* models. * * `response_format` is DALL-E-only and is intentionally omitted (DALL-E series * is out of scope). `gpt-image-1` family uses `output_format` instead. */ interface ImageGenerationRequest { prompt: string; model?: string; n?: number; quality?: 'low' | 'medium' | 'high' | 'auto' | 'standard'; size?: string; background?: string; moderation?: boolean; output_compression?: number; output_format?: string; user?: string; api_key?: string; api_base?: string; timeout?: number; max_retries?: number; organization?: string; drop_params?: boolean; extra_headers?: Record<string, string>; headers?: Record<string, string>; custom_llm_provider?: string; } /** * Image edit request parameters * For gpt-image-1 family and any future gpt-image-* / chatgpt-image-* models. * * `response_format` is kept here (unlike ImageGenerationRequest) because the * image-edits endpoint supports it on gpt-image-1. * * `image` and `mask` MUST be binary (Buffer | Uint8Array | Blob | File). * Strings (base64 / data: URIs) are rejected at the `imageEdit()` boundary * because the endpoint uses multipart/form-data and requires raw bytes — * sending a string would be uploaded as literal ASCII bytes and the provider * rejects it as an invalid image. */ interface ImageEditRequest { image: ImageEditInput; prompt: string; mask?: ImageEditInput; background?: string; input_fidelity?: string; model?: string; n?: number; quality?: 'low' | 'medium' | 'high' | 'auto' | 'standard'; size?: string; response_format?: 'url' | 'b64_json'; user?: string; api_key?: string; api_base?: string; timeout?: number; max_retries?: number; organization?: string; drop_params?: boolean; extra_headers?: Record<string, string>; headers?: Record<string, string>; custom_llm_provider?: string; } /** * Single image data in response */ interface ImageData { url?: string; b64_json?: string; revised_prompt?: string; provider_specific_fields?: Record<string, any>; } /** * Image usage input tokens details * Contains breakdown of input tokens by type */ interface ImageUsageInputTokensDetails { image_tokens: number; text_tokens: number; } /** * Image API usage information * Tracks token usage for image generation requests */ interface ImageUsage { input_tokens: number; /**The number of tokens (images and text) in the input prompt.*/ input_tokens_details: ImageUsageInputTokensDetails; /**The input tokens detailed information for the image generation.*/ output_tokens: number; /**The number of image tokens in the output image.*/ total_tokens: number; } /** * Image API response */ interface ImageResponse { created: number; data: ImageData[]; usage?: ImageUsage; size?: string; quality?: string; output_format?: string; } /** * Create an ImageResponse with proper defaults. * - `created` defaults to current Unix time when absent. * - `usage` null fields are zero-filled and `total_tokens` is computed when missing. */ declare function createImageResponse(params: { created?: number; data?: ImageData[]; usage?: ImageUsage | Record<string, any>; quality?: string; output_format?: string; size?: string; }): ImageResponse; /** * Generate images from text prompts. * Supports OpenAI (gpt-image-1 family) and Gemini image-generation models. * Dispatch resolves via `ProviderConfigManager.getProviderImageGenerationConfig` * so any registry-backed model routes to its correct config with no per-model * branching here. */ declare function imageGeneration(request: ImageGenerationRequest): Promise<ImageResponse>; /** * Edit images with text prompts. * Supports gpt-image-1 (OpenAI) and Gemini image models. * OpenAI uses multipart/form-data, Gemini uses JSON (selected via * `useMultipartFormData()` on the provider's config). */ declare function imageEdit(request: ImageEditRequest): Promise<ImageResponse>; /** * Common base config for all LLM providers * * This module defines the base classes and interfaces that all provider * transformations must inherit from. It provides a consistent contract * for request/response transformation across all LLM providers. */ /** * Base exception class for all LLM provider errors * * All provider-specific exceptions should extend this class. */ declare class BaseLLMException extends Error { statusCode: number; message: string; headers?: Record<string, string>; request?: Request; response?: Response; body?: Record<string, any>; constructor(options: { statusCode: number; message: string; headers?: Record<string, string>; request?: Request; response?: Response; body?: Record<string, any>; }); } /** * Base configuration class for all LLM providers * * This abstract class defines the contract that all provider configs must implement. * It provides default implementations for common functionality and requires * subclasses to implement provider-specific transformation logic. * * Key responsibilities: * - Transform requests from OpenAI format to provider format * - Transform responses from provider format to OpenAI format * - Validate environment (API keys, headers) * - Map OpenAI parameters to provider parameters * - Handle provider-specific error classes */ declare abstract class BaseConfig { constructor(); /** * Get configuration as a dictionary * * Returns all non-private, non-function class attributes. */ static getConfig(): Record<string, any>; /** * Get JSON schema from response format * * Converts a response format object to the appropriate JSON schema format. */ getJsonSchemaFromResponseFormat(responseFormat: Record<string, any> | null | undefined): Record<string, any> | null; /** * Check if thinking/reasoning is enabled in the parameters */ isThinkingEnabled(nonDefaultParams: Record<string, any>): boolean; /** * Check if max_tokens is specified in the request * * OpenAI spec allows max_tokens or max_completion_tokens to be specified. */ isMaxTokensInRequest(nonDefaultParams: Record<string, any>): boolean; /** * Update optional params with thinking tokens * * Handles scenario where max tokens is not specified. For anthropic models, * this requires having the max tokens being set and being greater than * the thinking token budget. * * If 'thinking' is enabled and 'max_tokens' is not specified, * set 'max_tokens' to the thinking token budget + DEFAULT_MAX_TOKENS */ updateOptionalParamsWithThinkingTokens(nonDefaultParams: Record<string, any>, optionalParams: Record<string, any>): void; /** * Returns True if the model/provider should fake stream * * Some providers don't support native streaming, so we need to * simulate it by making a non-streaming request and yielding chunks. */ shouldFakeStream(model: string | null, stream: boolean | null, customLLMProvider?: string): boolean; /** * Helper util to add tools to optional_params */ protected addToolsToOptionalParams(optionalParams: Record<string, any>, tools: any[]): Record<string, any>; /** * Translate `developer` role to `system` role for non-OpenAI providers. * * Overridden by OpenAI/Azure to preserve the developer role. */ translateDeveloperRoleToSystemRole(messages: AllMessageValues[]): AllMessageValues[]; /** * Returns True if the model/provider should retry the LLM API on HTTP error * * Overridden by providers where different models support different parameters. */ shouldRetryLLMApiInsideLLMTranslationOnHttpError(error: Error, rocketllmParams: Record<string, any>): boolean; /** * Transform the request data on UnprocessableEntityError */ transformRequestOnUnprocessableEntityError(error: Error, requestData: Record<string, any>): Record<string, any>; /** * Returns the max retry count for UnprocessableEntityError * * Used if `shouldRetryLLMApiInsideLLMTranslationOnHttpError` returns true. */ get maxRetryOnUnprocessableEntityError(): number; /** * Get supported OpenAI parameters for this provider * * Returns a list of parameter names that this provider supports. * Used for parameter validation and filtering. */ abstract getSupportedOpenAIParams(model: string): string[]; /** * Add response format to tools * * Follow similar approach to anthropic - translate to a single tool call. * This is used to translate response_format to a tool call, for models/APIs * that don't support response_format directly. */ protected addResponseFormatToTools(optionalParams: Record<string, any>, value: Record<string, any>, isResponseFormatSupported: boolean, enforceToolChoice?: boolean): Record<string, any>; /** * Map OpenAI parameters to provider-specific parameters * * Converts OpenAI-format parameters to the format expected by this provider. */ abstract mapOpenAIParams(nonDefaultParams: Record<string, any>, optionalParams: Record<string, any>, model: string, dropParams: boolean): Record<string, any>; /** * Validate environment and return headers * * Validates API keys and other environment settings, and returns * the headers to be used in the HTTP request. */ abstract validateEnvironment(headers: Record<string, string>, model: string, messages: AllMessageValues[], optionalParams: Record<string, any>, rocketllmParams: Record<string, any>, apiKey?: string, apiBase?: string | null): Record<string, string>; /** * Sign the request * * Some providers like Bedrock require signing the request. * The sign request function needs access to `requestData` and `completeUrl`. * * @returns Tuple of [signed headers, signed body (if any)] */ signRequest(headers: Record<string, string>, optionalParams: Record<string, any>, requestData: Record<string, any>, apiBase: string, apiKey?: string, model?: string, stream?: boolean, fakeStream?: boolean): [Record<string, string>, Uint8Array | undefined]; /** * Get the complete URL for the request * * Some providers need `model` in `api_base`. */ getCompleteUrl(apiBase: string | null, apiKey: string | null | undefined, model: string, optionalParams: Record<string, any>, rocketllmParams: Record<string, any>, stream?: boolean): string; /** * Transform request from OpenAI format to provider format * * This is the main transformation method that converts an OpenAI-compatible * request to the format expected by this provider. */ abstract transformRequest(model: string, messages: AllMessageValues[], optionalParams: Record<string, any>, rocketllmParams: Record<string, any>, headers: Record<string, string>): Record<string, any> | Promise<Record<string, any>>; /** * Async version of transform request * * Override to allow for http requests on async calls - e.g. converting url to base64. */ asyncTransformRequest(model: string, messages: AllMessageValues[], optionalParams: Record<string, any>, rocketllmParams: Record<string, any>, headers: Record<string, string>): Promise<Record<string, any>>; /** * Transform response from provider format to OpenAI format * * Converts the provider's response to the standard OpenAI-compatible format. */ abstract transformResponse(model: string, rawResponse: Response, modelResponse: ModelResponse, requestData: Record<string, any>, messages: AllMessageValues[], optionalParams: Record<string, any>, rocketllmParams: Record<string, any>, apiKey?: string, jsonMode?: boolean): ModelResponse | Promise<ModelResponse>; /** * Get the error class for this provider * * Returns a provider-specific exception instance for the given error. */ abstract getErrorClass(errorMessage: string, statusCode: number, headers: Record<string, string>): BaseLLMException; /** * Get model response iterator for streaming * * Returns an iterator that yields streaming chunks from the response. */ getModelResponseIterator(streamingResponse: ReadableStream<Uint8Array> | AsyncIterable<string> | ModelResponse, syncStream: boolean, jsonMode?: boolean): AsyncIterable<any> | null; /** * Custom LLM provider identifier */ get customLLMProvider(): string | null; /** * Whether this provider has a custom stream wrapper */ get hasCustomStreamWrapper(): boolean; /** * Whether this provider supports the stream parameter in the request body * * Some providers like Bedrock invoke do not support the stream parameter * in the request body. By default, this is true for almost all providers. */ get supportsStreamParamInRequestBody(): boolean; } /** * Base configuration for image generation providers * All image generation providers must extend this class */ declare abstract class BaseImageGenerationConfig { /** * Get supported OpenAI parameters for the provider */ abstract getSupportedOpenAIParams(model: string): string[]; /** * Map OpenAI parameters to provider-specific parameters */ abstract mapOpenAIParams(nonDefaultParams: Record<string, any>, optionalParams: Record<string, any>, model: string, dropParams: boolean): Record<string, any>; /** * Get complete URL for the request * Optional method - providers can override if needed */ getCompleteUrl(apiBase: string | undefined, apiKey: string | undefined, model: string, optionalParams: Record<string, any>, rocketllmParams: Record<string, any>, stream?: boolean): string; /** * Validate environment and return headers * Optional method - providers can override */ validateEnvironment(headers: Record<string, string>, model: string, optionalParams: Record<string, any>, rocketllmParams: Record<string, any>, apiKey?: string, apiBase?: string): Record<string, string>; /** * Get error class for provider errors * Returns BaseLLMException for all providers */ getErrorClass(errorMessage: string, statusCode: number, headers: Record<string, string>): BaseLLMException; /** * Transform image generation request to provider format */ transformImageGenerationRequest(model: string, prompt: string, optionalParams: Record<string, any>, rocketllmParams: Record<string, any>, headers: Record<string, string>): any; /** * Transform provider response to ImageResponse format */ transformImageGenerationResponse(model: string, rawResponse: Response, modelResponse: any, optionalParams: Record<string, any>): Promise<any>; } /** * GPT Image Generation Transformation (gpt-image-1, gpt-image-1-mini) * Handles parameter mapping and validation for OpenAI image models */ /** * Configuration for gpt-image-1 and gpt-image-1-mini image generation */ declare class GPTImageGenerationConfig extends BaseImageGenerationConfig { readonly customLLMProvider = "openai"; /** * Get supported OpenAI params for gpt-image-1 models */ getSupportedOpenAIParams(model: string): OpenAIImageGenerationOptionalParams[]; /** * Map and validate OpenAI parameters */ mapOpenAIParams(nonDefaultParams: Record<string, any>, optionalParams: Record<string, any>, model: string, dropParams: boolean): Record<string, any>; /** * Transform image generation response * Converts raw API response to ImageResponse */ transformImageGenerationResponse(model: string, rawResponse: Response, modelResponse: any, optionalParams: Record<string, any>): Promise<any>; } /** * Base Image Edit Configuration * Abstract base class for image edit transformations */ /** * Abstract base class for image edit configurations. * All provider-specific image edit configs should extend this class. */ declare abstract class BaseImageEditConfig { constructor(); /** * Get configuration as a dictionary */ static getConfig(): Record<string, any>; /** * Get supported OpenAI params for image edit */ abstract getSupportedOpenAIParams(model: string): string[]; /** * Map OpenAI params to provider-specific params */ abstract mapOpenAIParams(imageEditOptionalParams: ImageEditOptionalRequestParams, model: string, dropParams: boolean): Record<string, any>; /** * Validate environment and return headers with auth */ abstract validateEnvironment(headers: Record<string, string>, model: string, apiKey?: string): Record<string, string>; /** * Get complete URL for the request */ abstract getCompleteUrl(model: string, apiBase: string | undefined, rocketllmParams: Record<string, any>): string; /** * Transform image edit request to provider format * Returns [data, files] tuple */ abstract transformImageEditRequest(model: string, prompt: string, image: FileTypes, imageEditOptionalRequestParams: Record<string, any>, rocketllmParams: GenericRocketLLMParams, headers: Record<string, string>): [Record<string, any>, RequestFiles]; /** * Transform raw response to ImageResponse */ abstract transformImageEditResponse(model: string, rawResponse: Response): Promise<ImageResponse>; /** * Return True if the provider uses multipart/form-data for image edit requests. * Return False if the provider uses JSON requests. * Default is True for backwards co