multi-llm
Version:
A unified TypeScript/JavaScript package to use LLMs across ALL platforms with support for 17 major providers, streaming, MCP tools, and intelligent response parsing
50 lines • 1.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT_RETRY_CONFIG = void 0;
exports.getRetryConfig = getRetryConfig;
exports.sleep = sleep;
exports.executeWithRetry = executeWithRetry;
exports.DEFAULT_RETRY_CONFIG = {
retries: 1,
retryInterval: 1000,
retryBackoff: 2
};
function getRetryConfig(options) {
return {
retries: options.retries ?? exports.DEFAULT_RETRY_CONFIG.retries,
retryInterval: options.retryInterval ?? exports.DEFAULT_RETRY_CONFIG.retryInterval,
retryBackoff: options.retryBackoff ?? exports.DEFAULT_RETRY_CONFIG.retryBackoff
};
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function executeWithRetry(operation, config, context) {
let lastError;
let attempt = 0;
const maxAttempts = config.retries + 1; // retries + initial attempt
while (attempt < maxAttempts) {
try {
return await operation();
}
catch (error) {
lastError = error;
attempt++;
// If this was the last attempt, throw the error
if (attempt >= maxAttempts) {
const contextMsg = context ? ` (${context})` : '';
throw new Error(`Failed after ${config.retries} retries${contextMsg}: ${lastError.message}`);
}
// Calculate delay with exponential backoff
const delay = config.retryInterval * Math.pow(config.retryBackoff, attempt - 1);
if (process.env.NODE_ENV !== 'test') {
console.warn(`Retry ${attempt}/${config.retries} after ${delay}ms${context ? ` (${context})` : ''}: ${lastError.message}`);
}
// Wait before retrying
await sleep(delay);
}
}
// This should never be reached, but TypeScript requires it
throw lastError;
}
//# sourceMappingURL=retry.js.map