@autobe/agent
Version:
AI backend server code generator
134 lines • 5.99 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.randomBackoffRetry = randomBackoffRetry;
exports.randomBackoffStrategy = randomBackoffStrategy;
/**
* Retries failed LLM API calls with exponential backoff and jitter.
*
* Automatically retries transient failures (rate limits, server errors, network
* issues) while immediately failing on permanent errors (quota exceeded,
* invalid requests). Uses exponential backoff with randomized jitter to avoid
* thundering herd problems when multiple concurrent requests fail
* simultaneously.
*
* The retry logic is critical for production reliability: LLM APIs frequently
* return temporary 429/5xx errors under heavy load, and network timeouts are
* common. Without retry, these transient failures would cascade into
* user-visible errors despite being automatically recoverable.
*
* @author Kakasoo
* @param fn Async function to execute with retry logic
* @param options Retry configuration (maxRetries, delays, error handler)
* @returns Promise resolving to function result if successful
* @throws Last encountered error if all retry attempts exhausted
*/
function randomBackoffRetry(fn_1) {
return __awaiter(this, arguments, void 0, function* (fn, options = {}) {
const { maxRetries = 5, baseDelay = 4000, maxDelay = 60000, jitter = 0.8, handleError = isRetryError, } = options;
let lastError;
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
return yield fn();
}
catch (err) {
lastError = err;
if (attempt === maxRetries - 1)
throw err;
if (!handleError(err))
throw err;
const tempDelay = Math.min(baseDelay * 2 ** attempt, maxDelay);
const delay = tempDelay * (1 + Math.random() * jitter);
yield new Promise((res) => setTimeout(res, delay));
}
}
throw lastError;
});
}
/**
* Calculates retry delay using exponential backoff with jitter.
*
* Throws immediately for non-retryable errors or when retry count exceeds 5.
* Used by orchestrators that need explicit delay calculation without automatic
* retry loop execution.
*
* @param props Retry count and error to evaluate
* @returns Calculated delay in milliseconds
* @throws Original error if non-retryable or max retries exceeded
*/
function randomBackoffStrategy(props) {
const { count, error } = props;
if (count > 5) {
throw error;
}
if (isRetryError(error) === false) {
throw error;
}
const baseDelay = 4000;
const maxDelay = 60000;
const jitter = 0.8;
const tempDelay = Math.min(baseDelay * 2 ** count, maxDelay);
const delay = tempDelay * (1 + Math.random() * jitter);
return delay;
}
/**
* Determines if an error represents a transient failure that should trigger
* retry.
*
* Returns `true` for retryable errors (rate limits, server errors, network
* issues) and `false` for permanent failures (quota exceeded, invalid
* requests). This classification prevents wasting resources retrying
* unrecoverable errors.
*
* @param error Error object from LLM API or network layer
* @returns `true` if error is retryable, `false` otherwise
*/
function isRetryError(
// biome-ignore lint: @todo SunRabbit
error) {
var _a, _b, _c, _d;
// 1) Quota exceeded → No retry
if ((error === null || error === void 0 ? void 0 : error.code) === "insufficient_quota" ||
((_a = error === null || error === void 0 ? void 0 : error.error) === null || _a === void 0 ? void 0 : _a.type) === "insufficient_quota") {
return false;
}
// 2) 5xx / server_error → Retry
// Streaming errors from OpenRouter may have status: undefined but code: 502
if ((typeof (error === null || error === void 0 ? void 0 : error.status) === "number" && error.status >= 500) ||
(typeof (error === null || error === void 0 ? void 0 : error.code) === "number" && error.code >= 500) ||
((_b = error === null || error === void 0 ? void 0 : error.error) === null || _b === void 0 ? void 0 : _b.type) === "server_error") {
return true;
}
// 3) HTTP 429
if ((error === null || error === void 0 ? void 0 : error.status) === 429) {
return true;
}
// 4) undici / network related
const code = (error === null || error === void 0 ? void 0 : error.code) || ((_c = error === null || error === void 0 ? void 0 : error.cause) === null || _c === void 0 ? void 0 : _c.code);
if ([
"UND_ERR_SOCKET",
"UND_ERR_CONNECT_TIMEOUT",
"ETIMEDOUT",
"ECONNRESET",
"EPIPE",
].includes(code)) {
return true;
}
// 5) fetch abort
if ((error === null || error === void 0 ? void 0 : error.message) === "terminated" || (error === null || error === void 0 ? void 0 : error.name) === "AbortError") {
return true;
}
if ((_d = error === null || error === void 0 ? void 0 : error.message) === null || _d === void 0 ? void 0 : _d.startsWith(`SyntaxError: Expected ',' or '}' after property value in JSON at position`)) {
return true;
}
return false;
}
//# sourceMappingURL=backoffRetry.js.map