UNPKG

ai-patterns

Version:

Production-ready TypeScript patterns to build solid and robust AI applications. Retry logic, circuit breakers, rate limiting, human-in-the-loop escalation, prompt versioning, response validation, context window management, and more—all with complete type

68 lines 1.88 kB
"use strict"; /** * Types for Retry Pattern */ Object.defineProperty(exports, "__esModule", { value: true }); exports.RetryPredicates = exports.BackoffStrategy = void 0; /** * Backoff strategy for retry delays */ var BackoffStrategy; (function (BackoffStrategy) { /** * Same delay between each attempt */ BackoffStrategy["CONSTANT"] = "CONSTANT"; /** * Delay increases linearly (initialDelay * attempt) */ BackoffStrategy["LINEAR"] = "LINEAR"; /** * Delay increases exponentially (initialDelay * 2^attempt) */ BackoffStrategy["EXPONENTIAL"] = "EXPONENTIAL"; })(BackoffStrategy || (exports.BackoffStrategy = BackoffStrategy = {})); /** * Common retry predicates for different error scenarios */ exports.RetryPredicates = { /** * Retry only on network errors */ networkErrors: (error) => { const message = error.message.toLowerCase(); return (message.includes("network") || message.includes("timeout") || message.includes("econnrefused") || message.includes("enotfound")); }, /** * Retry only on specific HTTP status codes */ httpStatusCodes: (retryCodes) => { return (error) => { return (error.response !== undefined && error.response.status !== undefined && retryCodes.includes(error.response.status)); }; }, /** * Retry on rate limit errors (429) with exponential backoff */ rateLimitErrors: (error) => { return error.response !== undefined && error.response.status === 429; }, /** * Never retry (useful for testing) */ never: () => { return () => false; }, /** * Always retry (default behavior) */ always: () => { return () => true; }, }; //# sourceMappingURL=retry.js.map