UNPKG

@rocketnew/llm-sdk

Version:

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

1,169 lines (1,164 loc) 39.5 kB
import { init_transformation, transformation_exports } from "./chunk-EMCXL27T.mjs"; import { __esm, __toCommonJS } from "./chunk-DDRQKMV4.mjs"; // src/exceptions.ts var AuthenticationError, NotFoundError, BadRequestError, ImageFetchError, UnprocessableEntityError, Timeout, PermissionDeniedError, RateLimitError, ContextWindowExceededError, ContentPolicyViolationError, ServiceUnavailableError, BadGatewayError, InternalServerError, APIError, APIConnectionError, ConfigurationError, ProviderNotFoundError, UnsupportedParamsError, ROCKETLLM_EXCEPTION_TYPES; var init_exceptions = __esm({ "src/exceptions.ts"() { "use strict"; AuthenticationError = class extends Error { constructor(message, llmProvider, model, response, rocketllmDebugInfo, maxRetries, numRetries) { super(message); this.statusCode = 401; this.name = "AuthenticationError"; this.statusCode = 401; this.message = `rocketllm.AuthenticationError: ${message}`; this.llmProvider = llmProvider; this.model = model; this.response = response; this.rocketllmDebugInfo = rocketllmDebugInfo; this.maxRetries = maxRetries; this.numRetries = numRetries; } toString() { let msg = this.message; if (this.numRetries) { msg += ` RocketLLM Retried: ${this.numRetries} times`; } if (this.maxRetries) { msg += `, RocketLLM Max Retries: ${this.maxRetries}`; } return msg; } }; NotFoundError = class extends Error { constructor(message, model, llmProvider, response, rocketllmDebugInfo, maxRetries, numRetries) { super(message); this.statusCode = 404; this.name = "NotFoundError"; this.statusCode = 404; this.message = `rocketllm.NotFoundError: ${message}`; this.model = model; this.llmProvider = llmProvider; this.response = response; this.rocketllmDebugInfo = rocketllmDebugInfo; this.maxRetries = maxRetries; this.numRetries = numRetries; } toString() { let msg = this.message; if (this.numRetries) { msg += ` RocketLLM Retried: ${this.numRetries} times`; } if (this.maxRetries) { msg += `, RocketLLM Max Retries: ${this.maxRetries}`; } return msg; } }; BadRequestError = class extends Error { constructor(message, model, llmProvider, response, rocketllmDebugInfo, maxRetries, numRetries, body) { super(message); this.statusCode = 400; this.name = "BadRequestError"; this.statusCode = 400; this.message = `rocketllm.BadRequestError: ${message}`; this.model = model; this.llmProvider = llmProvider; this.response = response; this.rocketllmDebugInfo = rocketllmDebugInfo; this.maxRetries = maxRetries; this.numRetries = numRetries; this.body = body; } toString() { let msg = this.message; if (this.numRetries) { msg += ` RocketLLM Retried: ${this.numRetries} times`; } if (this.maxRetries) { msg += `, RocketLLM Max Retries: ${this.maxRetries}`; } return msg; } }; ImageFetchError = class extends BadRequestError { constructor(message, model = "", llmProvider = "", response, rocketllmDebugInfo, maxRetries, numRetries, body) { super( message, model, llmProvider, response, rocketllmDebugInfo, maxRetries, numRetries, body ); this.name = "ImageFetchError"; } }; UnprocessableEntityError = class extends Error { constructor(message, model, llmProvider, response, rocketllmDebugInfo, maxRetries, numRetries) { super(message); this.statusCode = 422; this.name = "UnprocessableEntityError"; this.statusCode = 422; this.message = `rocketllm.UnprocessableEntityError: ${message}`; this.model = model; this.llmProvider = llmProvider; this.response = response; this.rocketllmDebugInfo = rocketllmDebugInfo; this.maxRetries = maxRetries; this.numRetries = numRetries; } toString() { let msg = this.message; if (this.numRetries) { msg += ` RocketLLM Retried: ${this.numRetries} times`; } if (this.maxRetries) { msg += `, RocketLLM Max Retries: ${this.maxRetries}`; } return msg; } }; Timeout = class extends Error { constructor(message, model, llmProvider, rocketllmDebugInfo, maxRetries, numRetries, headers, exceptionStatusCode) { super(message); this.name = "Timeout"; this.statusCode = exceptionStatusCode || 408; this.message = `rocketllm.Timeout: ${message}`; this.model = model; this.llmProvider = llmProvider; this.rocketllmDebugInfo = rocketllmDebugInfo; this.maxRetries = maxRetries; this.numRetries = numRetries; this.headers = headers; } toString() { let msg = this.message; if (this.numRetries) { msg += ` RocketLLM Retried: ${this.numRetries} times`; } if (this.maxRetries) { msg += `, RocketLLM Max Retries: ${this.maxRetries}`; } return msg; } }; PermissionDeniedError = class extends Error { constructor(message, llmProvider, model, response, rocketllmDebugInfo, maxRetries, numRetries) { super(message); this.statusCode = 403; this.name = "PermissionDeniedError"; this.statusCode = 403; this.message = `rocketllm.PermissionDeniedError: ${message}`; this.llmProvider = llmProvider; this.model = model; this.response = response; this.rocketllmDebugInfo = rocketllmDebugInfo; this.maxRetries = maxRetries; this.numRetries = numRetries; } toString() { let msg = this.message; if (this.numRetries) { msg += ` RocketLLM Retried: ${this.numRetries} times`; } if (this.maxRetries) { msg += `, RocketLLM Max Retries: ${this.maxRetries}`; } return msg; } }; RateLimitError = class extends Error { constructor(message, llmProvider, model, response, rocketllmDebugInfo, maxRetries, numRetries) { super(message); this.statusCode = 429; this.name = "RateLimitError"; this.statusCode = 429; this.message = `rocketllm.RateLimitError: ${message}`; this.llmProvider = llmProvider; this.model = model; this.response = response; this.rocketllmDebugInfo = rocketllmDebugInfo; this.maxRetries = maxRetries; this.numRetries = numRetries; } toString() { let msg = this.message; if (this.numRetries) { msg += ` RocketLLM Retried: ${this.numRetries} times`; } if (this.maxRetries) { msg += `, RocketLLM Max Retries: ${this.maxRetries}`; } return msg; } }; ContextWindowExceededError = class extends BadRequestError { constructor(message, model, llmProvider, response, rocketllmDebugInfo) { super(message, model, llmProvider, response, rocketllmDebugInfo); this.name = "ContextWindowExceededError"; this.statusCode = 400; this.message = `rocketllm.ContextWindowExceededError: ${message}`; } }; ContentPolicyViolationError = class extends BadRequestError { constructor(message, model, llmProvider, response, rocketllmDebugInfo, providerSpecificFields) { super(message, model, llmProvider, response, rocketllmDebugInfo); this.name = "ContentPolicyViolationError"; this.statusCode = 400; this.message = `rocketllm.ContentPolicyViolationError: ${message}`; this.providerSpecificFields = providerSpecificFields; } }; ServiceUnavailableError = class extends Error { constructor(message, llmProvider, model, response, rocketllmDebugInfo, maxRetries, numRetries) { super(message); this.statusCode = 503; this.name = "ServiceUnavailableError"; this.statusCode = 503; this.message = `rocketllm.ServiceUnavailableError: ${message}`; this.llmProvider = llmProvider; this.model = model; this.response = response; this.rocketllmDebugInfo = rocketllmDebugInfo; this.maxRetries = maxRetries; this.numRetries = numRetries; } toString() { let msg = this.message; if (this.numRetries) { msg += ` RocketLLM Retried: ${this.numRetries} times`; } if (this.maxRetries) { msg += `, RocketLLM Max Retries: ${this.maxRetries}`; } return msg; } }; BadGatewayError = class extends Error { constructor(message, llmProvider, model, response, rocketllmDebugInfo, maxRetries, numRetries) { super(message); this.statusCode = 502; this.name = "BadGatewayError"; this.statusCode = 502; this.message = `rocketllm.BadGatewayError: ${message}`; this.llmProvider = llmProvider; this.model = model; this.response = response; this.rocketllmDebugInfo = rocketllmDebugInfo; this.maxRetries = maxRetries; this.numRetries = numRetries; } toString() { let msg = this.message; if (this.numRetries) { msg += ` RocketLLM Retried: ${this.numRetries} times`; } if (this.maxRetries) { msg += `, RocketLLM Max Retries: ${this.maxRetries}`; } return msg; } }; InternalServerError = class extends Error { constructor(message, llmProvider, model, response, rocketllmDebugInfo, maxRetries, numRetries) { super(message); this.statusCode = 500; this.name = "InternalServerError"; this.statusCode = 500; this.message = `rocketllm.InternalServerError: ${message}`; this.llmProvider = llmProvider; this.model = model; this.response = response; this.rocketllmDebugInfo = rocketllmDebugInfo; this.maxRetries = maxRetries; this.numRetries = numRetries; } toString() { let msg = this.message; if (this.numRetries) { msg += ` RocketLLM Retried: ${this.numRetries} times`; } if (this.maxRetries) { msg += `, RocketLLM Max Retries: ${this.maxRetries}`; } return msg; } }; APIError = class extends Error { constructor(statusCode, message, llmProvider, model, request, rocketllmDebugInfo, maxRetries, numRetries) { super(message); this.name = "APIError"; this.statusCode = statusCode; this.message = `rocketllm.APIError: ${message}`; this.llmProvider = llmProvider; this.model = model; this.request = request; this.rocketllmDebugInfo = rocketllmDebugInfo; this.maxRetries = maxRetries; this.numRetries = numRetries; } toString() { let msg = this.message; if (this.numRetries) { msg += ` RocketLLM Retried: ${this.numRetries} times`; } if (this.maxRetries) { msg += `, RocketLLM Max Retries: ${this.maxRetries}`; } return msg; } }; APIConnectionError = class extends Error { constructor(message, llmProvider, model, request, rocketllmDebugInfo, maxRetries, numRetries) { super(message); this.statusCode = 500; this.name = "APIConnectionError"; this.statusCode = 500; this.message = `rocketllm.APIConnectionError: ${message}`; this.llmProvider = llmProvider; this.model = model; this.request = request; this.rocketllmDebugInfo = rocketllmDebugInfo; this.maxRetries = maxRetries; this.numRetries = numRetries; } toString() { let msg = this.message; if (this.numRetries) { msg += ` RocketLLM Retried: ${this.numRetries} times`; } if (this.maxRetries) { msg += `, RocketLLM Max Retries: ${this.maxRetries}`; } return msg; } }; ConfigurationError = class extends Error { constructor(message) { super(message); this.name = "ConfigurationError"; } }; ProviderNotFoundError = class extends Error { constructor(model) { super(`Unable to detect provider for model: ${model}`); this.name = "ProviderNotFoundError"; } }; UnsupportedParamsError = class extends BadRequestError { constructor(message, llmProvider, model, statusCode = 400, response, rocketllmDebugInfo, maxRetries, numRetries) { super( message, model || "", llmProvider || "", response, rocketllmDebugInfo, maxRetries, numRetries ); this.name = "UnsupportedParamsError"; this.statusCode = 400; this.message = `rocketllm.UnsupportedParamsError: ${message}`; } }; ROCKETLLM_EXCEPTION_TYPES = [ AuthenticationError, NotFoundError, BadRequestError, UnprocessableEntityError, UnsupportedParamsError, Timeout, PermissionDeniedError, RateLimitError, ContextWindowExceededError, ContentPolicyViolationError, InternalServerError, ServiceUnavailableError, BadGatewayError, APIError, APIConnectionError ]; } }); // src/llms/custom_fetch/http_handler.ts function maskSensitiveInfo(errorMessage) { if (typeof errorMessage !== "string") { return errorMessage; } const keyIndex = errorMessage.indexOf("key="); if (keyIndex !== -1) { const nextParam = errorMessage.indexOf("&", keyIndex); if (nextParam === -1) { return errorMessage.substring(0, keyIndex + 4) + "[REDACTED_API_KEY]"; } else { return errorMessage.substring(0, keyIndex + 4) + "[REDACTED_API_KEY]" + errorMessage.substring(nextParam); } } return errorMessage; } function extractQueryParams(url) { try { const urlObj = new URL(url); const params = {}; urlObj.searchParams.forEach((value, key) => { params[key] = value; }); return params; } catch { return {}; } } var HTTPStatusError, HTTPHandler; var init_http_handler = __esm({ "src/llms/custom_fetch/http_handler.ts"() { "use strict"; init_exceptions(); HTTPStatusError = class extends Error { constructor(response, message, text) { super(message || `HTTP ${response.status}`); this.name = "HTTPStatusError"; this.statusCode = response.status; this.response = response; this.message = message || `HTTP ${response.status}`; this.text = text || message || `HTTP ${response.status}`; } }; HTTPHandler = class { constructor(options) { this.timeout = options?.timeout; } /** * Close the handler (no-op in JavaScript, kept for API compatibility) */ close() { } /** * GET request */ async get(url, options) { const params = options?.params || {}; const urlParams = extractQueryParams(url); const mergedParams = { ...urlParams, ...params }; const urlObj = new URL(url); urlObj.search = new URLSearchParams(mergedParams).toString(); const response = await fetch(urlObj.toString(), { method: "GET", headers: options?.headers, redirect: options?.followRedirects === false ? "manual" : "follow" }); return response; } /** * POST request */ async post(url, options) { try { const timeout = options?.timeout ?? this.timeout; let finalUrl = url; if (options?.params) { const urlObj = new URL(url); Object.entries(options.params).forEach(([key, value]) => { urlObj.searchParams.set(key, value); }); finalUrl = urlObj.toString(); } let body; const headers = { ...options?.headers || {} }; if (options?.files && options.files.length > 0) { const formData = new FormData(); if (options.data && typeof options.data === "object") { Object.entries(options.data).forEach(([key, value]) => { if (value !== void 0 && value !== null) { formData.append(key, String(value)); } }); } options.files.forEach(([fieldName, [filename, fileData, contentType]]) => { if (fileData instanceof Buffer || fileData instanceof Uint8Array) { const blob = new Blob([fileData.buffer], { type: contentType || "image/png" }); formData.append(fieldName, blob, filename); } else if (fileData instanceof Blob || fileData instanceof File) { formData.append(fieldName, fileData, filename); } else { throw new Error( `Unsupported file input type for multipart upload (field "${fieldName}"): ${typeof fileData}. Expected Buffer, Uint8Array, Blob, or File.` ); } }); body = formData; delete headers["Content-Type"]; delete headers["content-type"]; } else if (options?.data !== void 0) { body = options.data; } else if (options?.json !== void 0) { body = JSON.stringify(options.json); if (!headers["content-type"] && !headers["Content-Type"]) { headers["Content-Type"] = "application/json"; } } const controller = new AbortController(); let timeoutId; if (timeout !== void 0) { timeoutId = setTimeout(() => controller.abort(), timeout * 1e3); } try { const response = await fetch(finalUrl, { method: "POST", headers, body, signal: controller.signal }); if (timeoutId) clearTimeout(timeoutId); if (!response.ok) { const errorText = options?.stream ? await response.text() : maskSensitiveInfo(await response.text()); const error = new HTTPStatusError(response, errorText, errorText); throw error; } return response; } catch (error) { if (timeoutId) clearTimeout(timeoutId); if (error.name === "AbortError") { throw new Timeout( `Connection timed out after ${timeout} seconds.`, "default-model-name", "rocketllm-fetch-handler" ); } throw error; } } catch (error) { if (error instanceof HTTPStatusError) { throw error; } if (error instanceof Timeout) { throw error; } throw error; } } /** * PUT request */ async put(url, options) { return this.request({ ...options, method: "PUT", url }); } /** * PATCH request */ async patch(url, options) { return this.request({ ...options, method: "PATCH", url }); } /** * DELETE request */ async delete(url, options) { return this.request({ ...options, method: "DELETE", url }); } /** * Generic request method * * Used internally by PUT, PATCH, DELETE methods. */ async request(options) { try { const timeout = options.timeout ?? this.timeout; let finalUrl = options.url; if (options.params) { const urlObj = new URL(options.url); Object.entries(options.params).forEach(([key, value]) => { urlObj.searchParams.set(key, value); }); finalUrl = urlObj.toString(); } let body; const headers = { ...options.headers || {} }; if (options.data !== void 0) { body = options.data; } else if (options.json !== void 0) { body = JSON.stringify(options.json); if (!headers["content-type"] && !headers["Content-Type"]) { headers["Content-Type"] = "application/json"; } } const controller = new AbortController(); let timeoutId; if (timeout !== void 0) { timeoutId = setTimeout(() => controller.abort(), timeout * 1e3); } try { const response = await fetch(finalUrl, { method: options.method, headers, body, signal: controller.signal }); if (timeoutId) clearTimeout(timeoutId); if (!response.ok) { const errorText = options.stream ? await response.text() : maskSensitiveInfo(await response.text()); const error = new HTTPStatusError(response, errorText, errorText); throw error; } return response; } catch (error) { if (timeoutId) clearTimeout(timeoutId); if (error.name === "AbortError") { throw new Timeout( `Connection timed out after ${timeout} seconds.`, "default-model-name", "rocketllm-fetch-handler" ); } throw error; } } catch (error) { if (error instanceof HTTPStatusError) { throw error; } if (error instanceof Timeout) { throw error; } throw error; } } }; } }); // src/llms/custom_fetch/llm_http_handler.ts var BaseLLMHTTPHandler; var init_llm_http_handler = __esm({ "src/llms/custom_fetch/llm_http_handler.ts"() { init_http_handler(); BaseLLMHTTPHandler = class { /** * Make common HTTP call * * Common implementation across stream + non-stream calls. * Ensures consistent error-handling and retry logic. */ async makeCommonCall(httpClient, providerConfig, apiBase, headers, data, timeout, rocketllmParams, stream = false, signedJsonBody) { const maxRetryOnUnprocessableEntityError = providerConfig.maxRetryOnUnprocessableEntityError; let response = null; for (let i = 0; i < Math.max(maxRetryOnUnprocessableEntityError, 1); i++) { try { response = await httpClient.post(apiBase, { data: signedJsonBody ? signedJsonBody : JSON.stringify(data), headers, timeout, stream }); } catch (error) { if (error instanceof HTTPStatusError) { const hitMaxRetry = i + 1 === maxRetryOnUnprocessableEntityError; const shouldRetry = providerConfig.shouldRetryLLMApiInsideLLMTranslationOnHttpError( error, rocketllmParams ); if (shouldRetry && !hitMaxRetry) { data = providerConfig.transformRequestOnUnprocessableEntityError( error, data ); continue; } else { throw this.handleError(error, providerConfig); } } else { throw this.handleError(error, providerConfig); } } break; } if (response === null) { throw providerConfig.getErrorClass( "No response from the API", 422, {} ); } return response; } /** * Async completion (non-streaming) * * Makes a non-streaming completion request and transforms the response. */ async asyncCompletion(customLLMProvider, providerConfig, apiBase, headers, data, timeout, model, messages, optionalParams, rocketllmParams, apiKey, client, jsonMode = false, signedJsonBody) { const httpClient = client || new HTTPHandler({ timeout }); const response = await this.makeCommonCall( httpClient, providerConfig, apiBase, headers, data, timeout, rocketllmParams, false, signedJsonBody ); return providerConfig.transformResponse( model, response, {}, data, messages, optionalParams, rocketllmParams, apiKey, jsonMode ); } /** * Main completion method * * Orchestrates the entire completion flow: * 1. Get provider config * 2. Validate environment * 3. Get complete URL * 4. Transform request * 5. Sign request * 6. Make HTTP call * 7. Transform response */ async completion(model, messages, apiBase, customLLMProvider, optionalParams, timeout, rocketllmParams, stream = false, apiKey, headers, client, providerConfig) { const jsonMode = optionalParams.json_mode || false; delete optionalParams.json_mode; const extraBody = optionalParams.extra_body; delete optionalParams.extra_body; if (!providerConfig) { throw new Error( `Provider config not found for model: ${model} and provider: ${customLLMProvider}` ); } headers = providerConfig.validateEnvironment( headers || {}, model, messages, optionalParams, rocketllmParams, apiKey, apiBase ); apiBase = providerConfig.getCompleteUrl( apiBase, apiKey, model, optionalParams, rocketllmParams, stream ); let data = await providerConfig.asyncTransformRequest( model, messages, optionalParams, rocketllmParams, headers ); if (extraBody !== void 0) { data = { ...data, ...extraBody }; } const [signedHeaders, signedJsonBody] = providerConfig.signRequest( headers, optionalParams, data, apiBase, apiKey, model, stream, false // fakeStream ); headers = signedHeaders; if (stream === true) { data = this.addStreamParamToRequestBody( data, providerConfig, false // fakeStream ); return this.completionStreamFunction( model, messages, apiBase, headers, customLLMProvider, providerConfig, timeout, data, rocketllmParams, client, jsonMode, optionalParams, signedJsonBody ); } else { return this.asyncCompletion( customLLMProvider, providerConfig, apiBase, headers, data, timeout, model, messages, optionalParams, rocketllmParams, apiKey, client, jsonMode, signedJsonBody ); } } /** * Completion stream function * * Handles streaming completion requests. */ async completionStreamFunction(model, messages, apiBase, headers, customLLMProvider, providerConfig, timeout, data, rocketllmParams, client, jsonMode, optionalParams, signedJsonBody) { const httpClient = client || new HTTPHandler({ timeout }); const response = await this.makeCommonCall( httpClient, providerConfig, apiBase, headers, data, timeout, rocketllmParams, true, // stream signedJsonBody ); const completionStream = providerConfig.getModelResponseIterator( response.body, false, // syncStream (always false in JS) jsonMode ); if (!completionStream) { throw new Error("Provider config did not return streaming iterator"); } return completionStream; } /** * Add stream param to request body * * Some providers like Bedrock invoke do not support the stream parameter * in the request body. We only pass `stream` in the request body if the * provider supports it. */ addStreamParamToRequestBody(data, providerConfig, fakeStream) { if (fakeStream === true) { const newData = { ...data }; delete newData.stream; return newData; } if (providerConfig.supportsStreamParamInRequestBody === true) { data.stream = true; return data; } else { const newData = { ...data }; delete newData.stream; return newData; } } /** * Handle error * * Converts errors to provider-specific exception classes. */ handleError(error, providerConfig) { let statusCode = error.statusCode || error.status || 500; let errorHeaders = {}; let errorText; if (error instanceof HTTPStatusError) { errorText = error.text; statusCode = error.response.status; if (error.response.headers) { error.response.headers.forEach((value, key) => { errorHeaders[key] = value; }); } } else { errorText = error.text || error.message || String(error); } const errorResponse = error.response; if (Object.keys(errorHeaders).length === 0 && errorResponse) { if (errorResponse.headers && typeof errorResponse.headers.forEach === "function") { errorResponse.headers.forEach((value, key) => { errorHeaders[key] = value; }); } } if (errorResponse && errorResponse.text && typeof errorResponse.text === "string") { errorText = errorResponse.text; } if (!errorHeaders || Object.keys(errorHeaders).length === 0) { errorHeaders = {}; } if (!providerConfig) { const { BaseLLMException } = (init_transformation(), __toCommonJS(transformation_exports)); throw new BaseLLMException({ statusCode, message: errorText, headers: errorHeaders }); } return providerConfig.getErrorClass( errorText, statusCode, errorHeaders ); } /** * Handle image generation requests * Orchestrates image generation flow for all providers */ async imageGenerationHandler(model, prompt, imageGenerationProviderConfig, imageGenerationOptionalParams, customLLMProvider, rocketllmParams, timeout, apiKey, client, extraHeaders) { const httpHandler = client || new HTTPHandler({ timeout }); let headers = imageGenerationProviderConfig.validateEnvironment( {}, model, imageGenerationOptionalParams, rocketllmParams, apiKey, rocketllmParams.api_base ); if (extraHeaders && Object.keys(extraHeaders).length > 0) { headers = { ...headers, ...extraHeaders }; } const apiBase = imageGenerationProviderConfig.getCompleteUrl( rocketllmParams.api_base, apiKey, model, imageGenerationOptionalParams, rocketllmParams ); const data = imageGenerationProviderConfig.transformImageGenerationRequest( model, prompt, imageGenerationOptionalParams, rocketllmParams, headers ); let response; try { response = await httpHandler.post(apiBase, { data: JSON.stringify(data), headers, timeout }); } catch (error) { throw this.handleError(error, imageGenerationProviderConfig); } const modelResponse = { created: Math.floor(Date.now() / 1e3), data: [] }; const finalResponse = await imageGenerationProviderConfig.transformImageGenerationResponse( model, response, modelResponse, imageGenerationOptionalParams ); return finalResponse; } /** * Handle image edit requests * Supports both multipart/form-data (OpenAI) and JSON (Gemini) based on provider config */ async imageEditHandler(model, image, prompt, imageEditProviderConfig, imageEditOptionalRequestParams, customLLMProvider, rocketllmParams, timeout, extraHeaders, extraBody, apiKey, client) { const httpHandler = client || new HTTPHandler({ timeout }); let headers = imageEditProviderConfig.validateEnvironment( imageEditOptionalRequestParams.extra_headers || {}, model, apiKey ); if (extraHeaders) { headers = { ...headers, ...extraHeaders }; } const apiBase = imageEditProviderConfig.getCompleteUrl( model, rocketllmParams.api_base, rocketllmParams ); const [data, files] = imageEditProviderConfig.transformImageEditRequest( model, prompt, image, imageEditOptionalRequestParams, rocketllmParams, headers ); let response; try { if (imageEditProviderConfig.useMultipartFormData()) { response = await httpHandler.post(apiBase, { data, files, headers, timeout }); } else { response = await httpHandler.post(apiBase, { data: JSON.stringify(data), headers, timeout }); } } catch (error) { throw this.handleError(error, imageEditProviderConfig); } return imageEditProviderConfig.transformImageEditResponse( model, response ); } /** * Handle text-to-speech requests * Orchestrates TTS flow for providers that route through BaseLLMHTTPHandler */ async textToSpeechHandler(model, input, voice, textToSpeechProviderConfig, textToSpeechOptionalParams, customLLMProvider, rocketllmParams, timeout, extraHeaders, client) { const httpHandler = client || new HTTPHandler({ timeout }); const headers = textToSpeechProviderConfig.validateEnvironment( extraHeaders || {}, model, rocketllmParams.api_key, rocketllmParams.api_base ); if (extraHeaders) { Object.assign(headers, extraHeaders); } const apiBase = textToSpeechProviderConfig.getCompleteUrl( model, rocketllmParams.api_base, rocketllmParams ); const requestData = textToSpeechProviderConfig.transformTextToSpeechRequest( model, input, voice, textToSpeechOptionalParams, rocketllmParams, headers ); if (requestData.headers) { Object.assign(headers, requestData.headers); } let response; try { if (requestData.dictBody) { response = await httpHandler.post(apiBase, { data: JSON.stringify(requestData.dictBody), headers, timeout }); } else if (requestData.ssmlBody) { response = await httpHandler.post(apiBase, { data: requestData.ssmlBody, headers, timeout }); } else { throw new Error("No body found in request_data"); } } catch (error) { throw this.handleError(error, textToSpeechProviderConfig); } return textToSpeechProviderConfig.transformTextToSpeechResponse(model, response); } /** * Handle audio transcription requests * Orchestrates STT flow for providers that route through BaseLLMHTTPHandler */ async audioTranscriptionHandler(model, audioFile, optionalParams, rocketllmParams, timeout, maxRetries, apiKey, apiBase, customLLMProvider, providerConfig, headers, client) { const httpHandler = client || new HTTPHandler({ timeout }); const validatedHeaders = providerConfig.validateEnvironment( headers || {}, model, [], optionalParams, rocketllmParams, apiKey, apiBase ); const completeUrl = providerConfig.getCompleteUrl( apiBase || null, apiKey, model, optionalParams, rocketllmParams ); const requestData = providerConfig.transformAudioTranscriptionRequest( model, audioFile, optionalParams, rocketllmParams ); let response; try { if (requestData.files) { const filesArray = []; for (const [fieldName, fileEntry] of Object.entries(requestData.files)) { if (Array.isArray(fileEntry) && fileEntry.length >= 2) { const [filename, content, contentType] = fileEntry; filesArray.push([fieldName, [filename, content, contentType || "application/octet-stream"]]); } } response = await httpHandler.post(completeUrl, { data: requestData.data, files: filesArray, headers: validatedHeaders, timeout }); } else { response = await httpHandler.post(completeUrl, { data: JSON.stringify(requestData.data), headers: { ...validatedHeaders, "Content-Type": "application/json" }, timeout }); } } catch (error) { throw this.handleError(error, providerConfig); } return providerConfig.transformAudioTranscriptionResponse( await response.json() ); } }; } }); export { AuthenticationError, NotFoundError, BadRequestError, ImageFetchError, UnprocessableEntityError, Timeout, PermissionDeniedError, RateLimitError, ContextWindowExceededError, ContentPolicyViolationError, ServiceUnavailableError, BadGatewayError, InternalServerError, APIError, APIConnectionError, ConfigurationError, ProviderNotFoundError, UnsupportedParamsError, ROCKETLLM_EXCEPTION_TYPES, init_exceptions, HTTPHandler, init_http_handler, BaseLLMHTTPHandler, init_llm_http_handler };