UNPKG

@ai2070/l0

Version:

L0: The Missing Reliability Substrate for AI

105 lines (104 loc) 2.81 kB
import { z } from "zod4"; import { ErrorCategory } from "../types/retry"; const ErrorTypeDelaysSchema = z.object({ connectionDropped: z.number().optional(), fetchError: z.number().optional(), econnreset: z.number().optional(), econnrefused: z.number().optional(), sseAborted: z.number().optional(), noBytes: z.number().optional(), partialChunks: z.number().optional(), runtimeKilled: z.number().optional(), backgroundThrottle: z.number().optional(), dnsError: z.number().optional(), timeout: z.number().optional(), unknown: z.number().optional() }); const RetryReasonSchema = z.enum([ "zero_output", "guardrail_violation", "drift", "unknown", "incomplete", "network_error", "timeout", "rate_limit", "server_error" ]); const BackoffStrategySchema = z.enum([ "exponential", "linear", "fixed", "full-jitter", "fixed-jitter" ]); const ErrorCategorySchema = z.nativeEnum(ErrorCategory); const RetryConfigSchema = z.object({ attempts: z.number(), maxRetries: z.number().optional(), baseDelay: z.number(), maxDelay: z.number().optional(), backoff: BackoffStrategySchema, retryOn: z.array(RetryReasonSchema), errorTypeDelays: ErrorTypeDelaysSchema.optional(), maxErrorHistory: z.number().optional() }); const CategorizedErrorSchema = z.object({ error: z.instanceof(Error), category: ErrorCategorySchema, reason: RetryReasonSchema, countsTowardLimit: z.boolean(), retryable: z.boolean(), timestamp: z.number(), statusCode: z.number().optional() }); const RetryStateSchema = z.object({ attempt: z.number(), networkRetryCount: z.number(), transientRetries: z.number(), lastError: CategorizedErrorSchema.optional(), errorHistory: z.array(CategorizedErrorSchema), totalDelay: z.number(), limitReached: z.boolean() }); const BackoffResultSchema = z.object({ delay: z.number(), cappedAtMax: z.boolean(), rawDelay: z.number() }); const RetryDecisionSchema = z.object({ shouldRetry: z.boolean(), delay: z.number(), reason: z.string(), category: ErrorCategorySchema, countsTowardLimit: z.boolean() }); const ErrorClassificationSchema = z.object({ isNetwork: z.boolean(), isRateLimit: z.boolean(), isServerError: z.boolean(), isTimeout: z.boolean(), isAuthError: z.boolean(), isClientError: z.boolean(), statusCode: z.number().optional() }); const RetryContextSchema = z.object({ state: RetryStateSchema, config: RetryConfigSchema, error: CategorizedErrorSchema, backoff: BackoffResultSchema }); export { BackoffResultSchema, BackoffStrategySchema, CategorizedErrorSchema, ErrorCategorySchema, ErrorClassificationSchema, ErrorTypeDelaysSchema, RetryConfigSchema, RetryContextSchema, RetryDecisionSchema, RetryReasonSchema, RetryStateSchema }; //# sourceMappingURL=retry.js.map