@rocketnew/llm-sdk
Version:
Universal LLM SDK for JavaScript/TypeScript - OpenAI, Anthropic, Gemini, Perplexity and more
1,408 lines (1,399 loc) • 1.83 MB
JavaScript
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __typeError = (msg) => {
throw TypeError(msg);
};
var __esm = (fn, res) => function __init() {
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
};
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
// src/types/utils.ts
function generateId() {
return "chatcmpl-" + (0, import_crypto.randomUUID)();
}
var import_crypto, Usage, RocketLLMProviders;
var init_utils = __esm({
"src/types/utils.ts"() {
"use strict";
import_crypto = require("crypto");
Usage = class {
constructor(params) {
// Private attributes for caching (Anthropic compatibility)
this._cache_creation_input_tokens = 0;
this._cache_read_input_tokens = 0;
let _completionTokensDetails = void 0;
if (params.completion_tokens_details) {
_completionTokensDetails = { ...params.completion_tokens_details };
}
let effectiveReasoningTokens = params.reasoning_tokens;
if (effectiveReasoningTokens === void 0) {
if (_completionTokensDetails?.reasoning_tokens !== void 0 && _completionTokensDetails.reasoning_tokens > 0) {
effectiveReasoningTokens = _completionTokensDetails.reasoning_tokens;
}
}
if (effectiveReasoningTokens) {
if (_completionTokensDetails === void 0) {
_completionTokensDetails = {};
}
if (_completionTokensDetails.reasoning_tokens === void 0) {
_completionTokensDetails.reasoning_tokens = effectiveReasoningTokens;
}
if (_completionTokensDetails.text_tokens === void 0 && params.completion_tokens !== void 0) {
let calculatedTextTokens = params.completion_tokens - effectiveReasoningTokens;
if (_completionTokensDetails.image_tokens) {
calculatedTextTokens -= _completionTokensDetails.image_tokens;
}
if (_completionTokensDetails.audio_tokens) {
calculatedTextTokens -= _completionTokensDetails.audio_tokens;
}
_completionTokensDetails.text_tokens = Math.max(0, calculatedTextTokens);
}
}
let _promptTokensDetails = void 0;
if (params.prompt_tokens_details) {
_promptTokensDetails = { ...params.prompt_tokens_details };
}
if (_promptTokensDetails && _promptTokensDetails.text_tokens === void 0) {
const cachedTokens = _promptTokensDetails.cached_tokens || 0;
const audioTokens = _promptTokensDetails.audio_tokens || 0;
if (cachedTokens > 0 || audioTokens > 0) {
const calculatedTextTokens = (params.prompt_tokens || 0) - cachedTokens - audioTokens;
_promptTokensDetails.text_tokens = Math.max(0, calculatedTextTokens);
}
}
if (params.prompt_cache_hit_tokens !== void 0 && typeof params.prompt_cache_hit_tokens === "number") {
if (_promptTokensDetails === void 0) {
_promptTokensDetails = { cached_tokens: params.prompt_cache_hit_tokens };
} else {
_promptTokensDetails.cached_tokens = params.prompt_cache_hit_tokens;
}
}
if (params.cache_read_input_tokens !== void 0 && typeof params.cache_read_input_tokens === "number") {
if (_promptTokensDetails === void 0) {
_promptTokensDetails = { cached_tokens: params.cache_read_input_tokens };
} else {
_promptTokensDetails.cached_tokens = params.cache_read_input_tokens;
}
}
if (params.cache_creation_input_tokens !== void 0 && typeof params.cache_creation_input_tokens === "number") {
if (_promptTokensDetails === void 0) {
_promptTokensDetails = { cache_creation_tokens: params.cache_creation_input_tokens };
} else {
_promptTokensDetails.cache_creation_tokens = params.cache_creation_input_tokens;
}
}
this.prompt_tokens = params.prompt_tokens || 0;
this.completion_tokens = params.completion_tokens || 0;
this.total_tokens = params.total_tokens || 0;
if (_completionTokensDetails !== void 0) {
this.completion_tokens_details = _completionTokensDetails;
}
if (_promptTokensDetails !== void 0) {
this.prompt_tokens_details = _promptTokensDetails;
}
if (params.server_tool_use !== void 0 && params.server_tool_use !== null) {
this.server_tool_use = params.server_tool_use;
}
if (params.cost !== void 0 && params.cost !== null) {
this.cost = params.cost;
}
if (params.cache_creation_input_tokens !== void 0 && typeof params.cache_creation_input_tokens === "number") {
this._cache_creation_input_tokens = params.cache_creation_input_tokens;
}
if (params.cache_read_input_tokens !== void 0 && typeof params.cache_read_input_tokens === "number") {
this._cache_read_input_tokens = params.cache_read_input_tokens;
}
if (params.prompt_cache_hit_tokens !== void 0 && typeof params.prompt_cache_hit_tokens === "number") {
this._cache_read_input_tokens = params.prompt_cache_hit_tokens;
}
for (const [key, value] of Object.entries(params)) {
if (![
"prompt_tokens",
"completion_tokens",
"total_tokens",
"reasoning_tokens",
"prompt_tokens_details",
"completion_tokens_details",
"server_tool_use",
"cost",
"cache_creation_input_tokens",
"cache_read_input_tokens",
"prompt_cache_hit_tokens"
].includes(key)) {
this[key] = value;
}
}
}
/**
* Check if a key exists (for 'in' operator compatibility)
*/
has(key) {
return key in this;
}
/**
* Get a value with default (for .get() compatibility)
*/
get(key, defaultValue) {
return this[key] ?? defaultValue;
}
/**
* Convert to plain object for JSON serialization
*/
toJSON() {
const result = {
prompt_tokens: this.prompt_tokens,
completion_tokens: this.completion_tokens,
total_tokens: this.total_tokens
};
if (this.prompt_tokens_details !== void 0) {
result.prompt_tokens_details = this.prompt_tokens_details;
}
if (this.completion_tokens_details !== void 0) {
result.completion_tokens_details = this.completion_tokens_details;
}
if (this.server_tool_use !== void 0) {
result.server_tool_use = this.server_tool_use;
}
if (this.cost !== void 0) {
result.cost = this.cost;
}
return result;
}
};
RocketLLMProviders = /* @__PURE__ */ ((RocketLLMProviders2) => {
RocketLLMProviders2["OPENAI"] = "openai";
RocketLLMProviders2["ANTHROPIC"] = "anthropic";
RocketLLMProviders2["GEMINI"] = "gemini";
RocketLLMProviders2["PERPLEXITY"] = "perplexity";
RocketLLMProviders2["ELEVENLABS"] = "elevenlabs";
RocketLLMProviders2["ROCKETLLM_PROXY"] = "rocketllm_proxy";
return RocketLLMProviders2;
})(RocketLLMProviders || {});
}
});
// 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/rocketllm_core_utils/prompt_templates/image_handling.ts
async function convertUrlToBase64(url) {
const cached = inMemoryCache.get(url);
if (cached) {
return cached;
}
for (let attempt = 0; attempt < 3; attempt++) {
try {
const response = await fetch(url, {
method: "GET",
redirect: "follow"
});
if (!response.ok) {
throw new ImageFetchError(
`Error: Unable to fetch image from URL. Status code: ${response.status}, url=${url}`
);
}
const bytes = await response.arrayBuffer();
const base64 = Buffer.from(bytes).toString("base64");
let contentType = response.headers.get("Content-Type");
if (!contentType) {
const ext = url.split(".").pop()?.toLowerCase();
const typeMap = {
"jpg": "image/jpeg",
"jpeg": "image/jpeg",
"png": "image/png",
"gif": "image/gif",
"webp": "image/webp",
"pdf": "application/pdf"
};
contentType = typeMap[ext || ""] || "application/octet-stream";
}
const result = `data:${contentType};base64,${base64}`;
inMemoryCache.set(url, result);
return result;
} catch (error) {
if (error instanceof ImageFetchError) {
if (attempt === 2) {
throw error;
}
continue;
}
if (attempt === 2) {
throw new ImageFetchError(
`Error: Unable to fetch image from URL after 3 attempts. url=${url}`
);
}
}
}
throw new ImageFetchError(
`Error: Unable to fetch image from URL after 3 attempts. url=${url}`
);
}
var InMemoryCache, inMemoryCache;
var init_image_handling = __esm({
"src/rocketllm_core_utils/prompt_templates/image_handling.ts"() {
"use strict";
init_exceptions();
InMemoryCache = class {
constructor(maxSize = 10) {
this.cache = /* @__PURE__ */ new Map();
this.maxSize = maxSize;
}
get(key) {
return this.cache.get(key) || null;
}
set(key, value) {
if (this.cache.size >= this.maxSize) {
const firstKey = this.cache.keys().next().value;
if (firstKey !== void 0) {
this.cache.delete(firstKey);
}
}
this.cache.set(key, value);
}
};
inMemoryCache = new InMemoryCache(10);
}
});
// src/rocketllm_core_utils/prompt_templates/common_utils.ts
function getCompletionMessages(messages, assistantContinueMessage, userContinueMessage, ensureAlternatingRoles = false) {
if (!ensureAlternatingRoles) {
return deepCopyMessages(messages);
}
let result = insertUserContinueMessage(
messages,
userContinueMessage,
ensureAlternatingRoles
);
result = insertAssistantContinueMessage(
result,
assistantContinueMessage,
ensureAlternatingRoles
);
return result;
}
function insertUserContinueMessage(messages, userContinueMessage, ensureAlternatingRoles = false) {
if (!messages || messages.length === 0) {
return messages;
}
const resultMessages = deepCopyMessages(messages);
const continueMessage = userContinueMessage || DEFAULT_USER_CONTINUE_MESSAGE;
if (resultMessages[0].role === "assistant") {
resultMessages.unshift(continueMessage);
}
let i = 1;
while (i < resultMessages.length) {
const currMessage = resultMessages[i];
const prevMessage = resultMessages[i - 1];
if (currMessage.role === "assistant" && prevMessage.role === "assistant") {
resultMessages.splice(i, 0, continueMessage);
i += 2;
} else {
i += 1;
}
}
if (resultMessages[resultMessages.length - 1].role === "assistant" && ensureAlternatingRoles) {
resultMessages.push(continueMessage);
}
return resultMessages;
}
function insertAssistantContinueMessage(messages, assistantContinueMessage, ensureAlternatingRoles = true) {
if (!ensureAlternatingRoles || messages.length <= 1) {
return messages;
}
const modifiedMessages = [];
const continueMessage = assistantContinueMessage || DEFAULT_ASSISTANT_CONTINUE_MESSAGE;
for (let i = 0; i < messages.length; i++) {
const message = messages[i];
modifiedMessages.push(message);
if (i < messages.length - 1 && message.role === "user" && messages[i + 1].role === "user") {
modifiedMessages.push(continueMessage);
}
}
return modifiedMessages;
}
function deepCopyMessages(messages) {
return JSON.parse(JSON.stringify(messages));
}
function filterValueFromDict(obj, key, depth = 0) {
if (depth > 10) return obj;
if (Array.isArray(obj)) {
return obj.map(
(item) => typeof item === "object" ? filterValueFromDict(item, key, depth + 1) : item
);
} else if (obj && typeof obj === "object") {
const result = {};
for (const [k, v] of Object.entries(obj)) {
if (k !== key) {
result[k] = typeof v === "object" ? filterValueFromDict(v, key, depth + 1) : v;
}
}
return result;
}
return obj;
}
function unpackDefs(schema, defs, refChain = /* @__PURE__ */ new Set()) {
if (!schema || typeof schema !== "object") return;
for (const key of Object.keys(schema)) {
const value = schema[key];
if (!value || typeof value !== "object") continue;
if (Array.isArray(value)) {
for (const item of value) {
if (item && typeof item === "object") {
unpackDefs(item, defs, refChain);
}
}
continue;
}
const valueRecord = value;
const ref = valueRecord.$ref;
if (typeof ref === "string") {
const refName = ref.split("/").pop();
if (!refName || refChain.has(refName)) {
delete valueRecord.$ref;
continue;
}
const target = defs[refName];
if (target && typeof target === "object") {
const resolved = JSON.parse(JSON.stringify(target));
const nextChain = new Set(refChain);
nextChain.add(refName);
unpackDefs(resolved, defs, nextChain);
delete valueRecord.$ref;
for (const [rk, rv] of Object.entries(resolved)) {
if (!(rk in valueRecord)) {
valueRecord[rk] = rv;
}
}
} else {
delete valueRecord.$ref;
}
} else {
unpackDefs(valueRecord, defs, refChain);
}
}
}
var DEFAULT_USER_CONTINUE_MESSAGE, DEFAULT_USER_CONTINUE_MESSAGE_TYPED, DEFAULT_ASSISTANT_CONTINUE_MESSAGE;
var init_common_utils = __esm({
"src/rocketllm_core_utils/prompt_templates/common_utils.ts"() {
"use strict";
DEFAULT_USER_CONTINUE_MESSAGE = {
role: "user",
content: "continue"
};
DEFAULT_USER_CONTINUE_MESSAGE_TYPED = {
role: "user",
content: "Please continue."
};
DEFAULT_ASSISTANT_CONTINUE_MESSAGE = {
role: "assistant",
content: ""
};
}
});
// src/constants.ts
var RESPONSE_FORMAT_TOOL_NAME, DEFAULT_ANTHROPIC_CHAT_MAX_TOKENS, DEFAULT_MAX_TOKENS, DEFAULT_MAX_RETRIES, INITIAL_RETRY_DELAY, MAX_RETRY_DELAY, JITTER, REQUEST_TIMEOUT, DEFAULT_REASONING_EFFORT_DISABLE_THINKING_BUDGET, DEFAULT_REASONING_EFFORT_MINIMAL_THINKING_BUDGET, DEFAULT_REASONING_EFFORT_LOW_THINKING_BUDGET, DEFAULT_REASONING_EFFORT_MEDIUM_THINKING_BUDGET, DEFAULT_REASONING_EFFORT_HIGH_THINKING_BUDGET, ANTHROPIC_WEB_SEARCH_TOOL_MAX_USES, ANTHROPIC_HOSTED_TOOLS, ANTHROPIC_BETA_HEADER_VALUES, ANTHROPIC_TOOL_SEARCH_BETA_HEADER, ANTHROPIC_EFFORT_BETA_HEADER, ANTHROPIC_SKILLS_API_BETA_VERSION, ANTHROPIC_ADVISOR_TOOL_TYPE, COMPUTER_TOOL_BETA_MAPPING, OPENAI_CHAT_COMPLETION_PARAMS, OPENAI_COMPATIBLE_PROVIDERS, all_rocketllm_params, OPENAI_TRANSCRIPTION_PARAMS;
var init_constants = __esm({
"src/constants.ts"() {
"use strict";
RESPONSE_FORMAT_TOOL_NAME = "json_tool_call";
DEFAULT_ANTHROPIC_CHAT_MAX_TOKENS = parseInt(
process.env.DEFAULT_ANTHROPIC_CHAT_MAX_TOKENS || "4096"
);
DEFAULT_MAX_TOKENS = parseInt(
process.env.DEFAULT_MAX_TOKENS || "4096"
);
DEFAULT_MAX_RETRIES = parseInt(
process.env.DEFAULT_MAX_RETRIES || "2"
);
INITIAL_RETRY_DELAY = parseFloat(
process.env.INITIAL_RETRY_DELAY || "0.5"
);
MAX_RETRY_DELAY = parseFloat(
process.env.MAX_RETRY_DELAY || "8.0"
);
JITTER = parseFloat(
process.env.JITTER || "0.75"
);
REQUEST_TIMEOUT = parseFloat(
process.env.REQUEST_TIMEOUT || "6000"
);
DEFAULT_REASONING_EFFORT_DISABLE_THINKING_BUDGET = parseInt(
process.env.DEFAULT_REASONING_EFFORT_DISABLE_THINKING_BUDGET || "0"
);
DEFAULT_REASONING_EFFORT_MINIMAL_THINKING_BUDGET = parseInt(
process.env.DEFAULT_REASONING_EFFORT_MINIMAL_THINKING_BUDGET || "128"
);
DEFAULT_REASONING_EFFORT_LOW_THINKING_BUDGET = parseInt(
process.env.DEFAULT_REASONING_EFFORT_LOW_THINKING_BUDGET || "1024"
);
DEFAULT_REASONING_EFFORT_MEDIUM_THINKING_BUDGET = parseInt(
process.env.DEFAULT_REASONING_EFFORT_MEDIUM_THINKING_BUDGET || "2048"
);
DEFAULT_REASONING_EFFORT_HIGH_THINKING_BUDGET = parseInt(
process.env.DEFAULT_REASONING_EFFORT_HIGH_THINKING_BUDGET || "4096"
);
ANTHROPIC_WEB_SEARCH_TOOL_MAX_USES = {
low: 1,
medium: 5,
high: 10
};
ANTHROPIC_HOSTED_TOOLS = {
WEB_SEARCH: "web_search",
BASH: "bash",
TEXT_EDITOR: "text_editor",
CODE_EXECUTION: "code_execution",
WEB_FETCH: "web_fetch",
MEMORY: "memory",
TOOL_SEARCH_TOOL: "tool_search_tool"
};
ANTHROPIC_BETA_HEADER_VALUES = {
WEB_FETCH_2025_09_10: "web-fetch-2025-09-10",
WEB_SEARCH_2025_03_05: "web-search-2025-03-05",
CONTEXT_MANAGEMENT_2025_06_27: "context-management-2025-06-27",
COMPACT_2026_01_12: "compact-2026-01-12",
STRUCTURED_OUTPUT_2025_09_25: "structured-outputs-2025-11-13",
ADVANCED_TOOL_USE_2025_11_20: "advanced-tool-use-2025-11-20",
FAST_MODE_2026_02_01: "fast-mode-2026-02-01",
ADVISOR_TOOL_2026_03_01: "advisor-tool-2026-03-01"
};
ANTHROPIC_TOOL_SEARCH_BETA_HEADER = "advanced-tool-use-2025-11-20";
ANTHROPIC_EFFORT_BETA_HEADER = "effort-2025-11-24";
ANTHROPIC_SKILLS_API_BETA_VERSION = "skills-2025-10-02";
ANTHROPIC_ADVISOR_TOOL_TYPE = "advisor_20260301";
COMPUTER_TOOL_BETA_MAPPING = {
"computer_20250124": "computer-use-2025-01-24",
"computer_20241022": "computer-use-2024-10-22"
};
OPENAI_CHAT_COMPLETION_PARAMS = [
"functions",
"function_call",
"temperature",
"top_p",
"n",
"stream",
"stream_options",
"stop",
"max_completion_tokens",
"modalities",
"prediction",
"audio",
"max_tokens",
"presence_penalty",
"frequency_penalty",
"logit_bias",
"user",
"request_timeout",
"api_base",
"api_version",
"api_key",
"deployment_id",
"organization",
"base_url",
"default_headers",
"timeout",
"response_format",
"seed",
"tools",
"tool_choice",
"max_retries",
"parallel_tool_calls",
"logprobs",
"top_logprobs",
"reasoning_effort",
"extra_headers",
"thinking",
"web_search_options",
"service_tier"
];
OPENAI_COMPATIBLE_PROVIDERS = [
"perplexity",
"rocketllm_proxy"
// Future providers(uncomment when adding support)
// 'anyscale',
// 'groq',
// 'nvidia_nim',
// 'cerebras',
// 'baseten',
// 'sambanova',
// 'ai21_chat',
// 'ai21',
// 'volcengine',
// 'codestral',
// 'deepseek',
// 'deepinfra',
// 'xinference',
// 'xai',
// 'zai',
// 'together_ai',
// 'fireworks_ai',
// 'empower',
// 'friendliai',
// 'azure_ai',
// 'github',
// 'hosted_vllm',
// 'llamafile',
// 'lm_studio',
// 'galadriel',
// 'github_copilot',
// 'novita',
// 'meta_llama',
];
all_rocketllm_params = [
// Framework / config passthrough
"api_key",
"api_base",
"base_url",
"api_version",
"timeout",
"request_timeout",
"stream_timeout",
"max_retries",
"custom_llm_provider",
"organization",
"headers",
"extra_headers",
"provider_specific_header",
"drop_params",
"additional_drop_params",
// Message-shape helpers (consumed by completion())
"thinking",
"ensure_alternating_roles",
"user_continue_message",
"assistant_continue_message",
"supports_system_message"
];
OPENAI_TRANSCRIPTION_PARAMS = [
"language",
"response_format",
"timestamp_granularities"
];
}
});
// src/llms/base_llm/base_utils.ts
function updateRefs(schema, refTemplate) {
const stack = [[schema, []]];
const visited = /* @__PURE__ */ new Set();
while (stack.length > 0) {
const [obj, path] = stack.pop();
if (visited.has(obj)) {
continue;
}
visited.add(obj);
if (obj && typeof obj === "object") {
if ("$ref" in obj) {
const refPath = obj["$ref"];
const modelName = refPath.split("/").pop();
obj["$ref"] = refTemplate.replace("{model}", modelName || "");
}
if (Array.isArray(obj)) {
for (let i = 0; i < obj.length; i++) {
if (obj[i] && typeof obj[i] === "object") {
stack.push([obj[i], [...path, i]]);
}
}
} else {
for (const [k, v] of Object.entries(obj)) {
if (v && typeof v === "object") {
stack.push([v, [...path, k]]);
}
}
}
}
}
}
function dictToResponseFormatHelper(responseFormat, refTemplate) {
if (refTemplate && responseFormat.type === "json_schema") {
const modifiedFormat = JSON.parse(JSON.stringify(responseFormat));
const schema = modifiedFormat.json_schema?.schema;
if (schema) {
updateRefs(schema, refTemplate);
}
return modifiedFormat;
}
return responseFormat;
}
function typeToResponseFormatParam(responseFormat, refTemplate) {
if (responseFormat === null || responseFormat === void 0) {
return null;
}
if (typeof responseFormat === "object") {
return dictToResponseFormatHelper(responseFormat, refTemplate);
}
throw new TypeError(`Unsupported response_format type - ${typeof responseFormat}`);
}
function mapDeveloperRoleToSystemRole(messages) {
const newMessages = [];
for (const m of messages) {
if (m.role === "developer") {
newMessages.push({
role: "system",
content: m.content
});
} else {
newMessages.push(m);
}
}
return newMessages;
}
var init_base_utils = __esm({
"src/llms/base_llm/base_utils.ts"() {
"use strict";
}
});
// src/llms/base_llm/chat/transformation.ts
var transformation_exports = {};
__export(transformation_exports, {
BaseConfig: () => BaseConfig,
BaseLLMException: () => BaseLLMException
});
var BaseLLMException, BaseConfig;
var init_transformation = __esm({
"src/llms/base_llm/chat/transformation.ts"() {
"use strict";
init_constants();
init_base_utils();
BaseLLMException = class extends Error {
constructor(options) {
super(options.message);
this.name = "BaseLLMException";
this.statusCode = options.statusCode;
this.message = options.message;
this.headers = options.headers;
this.request = options.request;
this.response = options.response;
this.body = options.body;
}
};
BaseConfig = class {
constructor() {
}
/**
* Get configuration as a dictionary
*
* Returns all non-private, non-function class attributes.
*/
static getConfig() {
const config2 = {};
for (const [key, value] of Object.entries(this)) {
if (!key.startsWith("_") && typeof value !== "function" && value !== null && value !== void 0) {
config2[key] = value;
}
}
return config2;
}
/**
* Get JSON schema from response format
*
* Converts a response format object to the appropriate JSON schema format.
*/
getJsonSchemaFromResponseFormat(responseFormat) {
return typeToResponseFormatParam(responseFormat);
}
/**
* Check if thinking/reasoning is enabled in the parameters
*/
isThinkingEnabled(nonDefaultParams) {
return nonDefaultParams.thinking?.type === "enabled" || nonDefaultParams.reasoning_effort !== void 0 && nonDefaultParams.reasoning_effort !== null;
}
/**
* Check if max_tokens is specified in the request
*
* OpenAI spec allows max_tokens or max_completion_tokens to be specified.
*/
isMaxTokensInRequest(nonDefaultParams) {
return "max_tokens" in nonDefaultParams || "max_completion_tokens" in nonDefaultParams;
}
/**
* 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, optionalParams) {
const isThinkingEnabled = this.isThinkingEnabled(optionalParams);
if (isThinkingEnabled && !("max_tokens" in nonDefaultParams)) {
const thinkingTokenBudget = optionalParams.thinking?.budget_tokens;
if (thinkingTokenBudget !== null && thinkingTokenBudget !== void 0) {
optionalParams.max_tokens = thinkingTokenBudget + DEFAULT_MAX_TOKENS;
}
}
}
/**
* 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, stream, customLLMProvider) {
return false;
}
/**
* Helper util to add tools to optional_params
*/
addToolsToOptionalParams(optionalParams, tools) {
if (!("tools" in optionalParams)) {
optionalParams.tools = tools;
} else {
optionalParams.tools = [...optionalParams.tools, ...tools];
}
return optionalParams;
}
/**
* Translate `developer` role to `system` role for non-OpenAI providers.
*
* Overridden by OpenAI/Azure to preserve the developer role.
*/
translateDeveloperRoleToSystemRole(messages) {
return mapDeveloperRoleToSystemRole(messages);
}
/**
* 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, rocketllmParams) {
return false;
}
/**
* Transform the request data on UnprocessableEntityError
*/
transformRequestOnUnprocessableEntityError(error, requestData) {
return requestData;
}
/**
* Returns the max retry count for UnprocessableEntityError
*
* Used if `shouldRetryLLMApiInsideLLMTranslationOnHttpError` returns true.
*/
get maxRetryOnUnprocessableEntityError() {
return 0;
}
/**
* 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.
*/
addResponseFormatToTools(optionalParams, value, isResponseFormatSupported, enforceToolChoice = true) {
let jsonSchema = null;
if ("response_schema" in value) {
jsonSchema = value.response_schema;
} else if ("json_schema" in value) {
jsonSchema = value.json_schema?.schema;
}
if (jsonSchema && !isResponseFormatSupported) {
const toolChoice = {
type: "function",
function: {
name: RESPONSE_FORMAT_TOOL_NAME
}
};
const tool = {
type: "function",
function: {
name: RESPONSE_FORMAT_TOOL_NAME,
parameters: jsonSchema
}
};
if (!optionalParams.tools) {
optionalParams.tools = [];
}
optionalParams.tools.push(tool);
if (enforceToolChoice) {
optionalParams.tool_choice = toolChoice;
}
optionalParams.json_mode = true;
} else if (isResponseFormatSupported) {
optionalParams.response_format = value;
}
return optionalParams;
}
/**
* 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, optionalParams, requestData, apiBase, apiKey, model, stream, fakeStream) {
return [headers, void 0];
}
/**
* Get the complete URL for the request
*
* Some providers need `model` in `api_base`.
*/
getCompleteUrl(apiBase, apiKey, model, optionalParams, rocketllmParams, stream) {
if (apiBase === null) {
throw new Error("api_base is required");
}
return apiBase;
}
/**
* Async version of transform request
*
* Override to allow for http requests on async calls - e.g. converting url to base64.
*/
async asyncTransformRequest(model, messages, optionalParams, rocketllmParams, headers) {
return this.transformRequest(
model,
messages,
optionalParams,
rocketllmParams,
headers
);
}
/**
* Get model response iterator for streaming
*
* Returns an iterator that yields streaming chunks from the response.
*/
getModelResponseIterator(streamingResponse, syncStream, jsonMode) {
return null;
}
/**
* Custom LLM provider identifier
*/
get customLLMProvider() {
return null;
}
/**
* Whether this provider has a custom stream wrapper
*/
get hasCustomStreamWrapper() {
return false;
}
/**
* 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() {
return true;
}
};
}
});
// src/llms/openai/common_utils.ts
function getOpenaiApiBase(apiBase) {
return apiBase || process.env.OPENAI_BASE_URL || process.env.OPENAI_API_BASE || OPENAI_DEFAULT_API_BASE;
}
function getOpenaiApiKey(apiKey) {
return apiKey || process.env.OPENAI_API_KEY;
}
function getOpenaiOrganization(organization) {
return organization || process.env.OPENAI_ORGANIZATION;
}
function dropParamsFromUnprocessableEntityError(error, data) {
const invalidParams = [];
let errorBody;
if (error.response) {
try {
const parsed = typeof error.response === "string" ? JSON.parse(error.response) : error.response;
errorBody = parsed && typeof parsed === "object" ? parsed.error ?? parsed : parsed;
} catch {
errorBody = { message: error.message };
}
} else {
errorBody = error.body || { message: error.message };
}
if (errorBody && typeof errorBody === "object" && errorBody.message) {
let message = errorBody.message;
if (typeof message === "string") {
try {
message = JSON.parse(message);
} catch {
message = { detail: message };
}
}
const detail = message?.detail;
if (Array.isArray(detail) && detail.length > 0) {
for (const errorDict of detail) {
if (errorDict && typeof errorDict === "object" && errorDict.loc && Array.isArray(errorDict.loc) && errorDict.loc.length === 2) {
invalidParams.push(errorDict.loc[1]);
}
}
}
}
const newData = {};
for (const [key, value] of Object.entries(data)) {
if (!invalidParams.includes(key)) {
newData[key] = value;
}
}
return newData;
}
var OPENAI_DEFAULT_API_BASE, OpenAIError;
var init_common_utils2 = __esm({
"src/llms/openai/common_utils.ts"() {
"use strict";
init_transformation();
OPENAI_DEFAULT_API_BASE = "https://api.openai.com/v1";
OpenAIError = class extends BaseLLMException {
constructor(statusCode, message, headers, body) {
const defaultRequest = { method: "POST", url: "https://api.openai.com/v1" };
super({
statusCode,
message,
headers,
request: defaultRequest,
response: void 0,
body
});
this.name = "OpenAIError";
}
};
}
});
// model_prices_and_context_window.json
var model_prices_and_context_window_default;
var init_model_prices_and_context_window = __esm({
"model_prices_and_context_window.json"() {
model_prices_and_context_window_default = {
sample_spec: {
code_interpreter_cost_per_session: 0,
computer_use_input_cost_per_1k_tokens: 0,
computer_use_output_cost_per_1k_tokens: 0,
deprecation_date: "date when the model becomes deprecated in the format YYYY-MM-DD",
file_search_cost_per_1k_calls: 0,
file_search_cost_per_gb_per_day: 0,
input_cost_per_audio_token: 0,
input_cost_per_token: 0,
litellm_provider: "one of https://docs.litellm.ai/docs/providers",
max_input_tokens: "max input tokens, if the provider specifies it. if not default to max_tokens",
max_output_tokens: "max output tokens, if the provider specifies it. if not default to max_tokens",
max_tokens: "LEGACY parameter. set to max_output_tokens if provider specifies it. IF not set to max_input_tokens, if provider specifies it.",
mode: "one of: chat, embedding, completion, image_generation, audio_transcription, audio_speech, image_generation, moderation, rerank, search",
output_cost_per_reasoning_token: 0,
output_cost_per_token: 0,
search_context_cost_per_query: {
search_context_size_high: 0,
search_context_size_low: 0,
search_context_size_medium: 0
},
supported_regions: [
"global",
"us-west-2",
"eu-west-1",
"ap-southeast-1",
"ap-northeast-1"
],
supports_audio_input: true,
supports_audio_output: true,
supports_function_calling: true,
supports_p