UNPKG

@juspay/neurolink

Version:

The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002

72 lines (71 loc) • 2.4 kB
/** * Retry and resilience utilities for NeuroLink * Part of Sub-phase 3.3.3 - Edge Case Handling */ import type { RetryOptions } from "../types/index.js"; /** * Calculate exponential backoff delay with jitter * @param attempt - Current attempt number (1-based) * @param initialDelay - Initial delay in milliseconds * @param multiplier - Backoff multiplier for exponential growth * @param maxDelay - Maximum delay cap in milliseconds * @param addJitter - Whether to add random jitter to prevent thundering herd * @returns Calculated delay in milliseconds */ export declare function calculateBackoffDelay(attempt: number, initialDelay?: number, multiplier?: number, maxDelay?: number, addJitter?: boolean): number; /** * Default retry configuration */ export declare const DEFAULT_RETRY_CONFIG: Required<RetryOptions>; /** * Execute an operation with retry logic */ export declare function withRetry<T>(operation: () => Promise<T>, options?: RetryOptions): Promise<T>; /** * Enhanced timeout with retry for network operations */ export declare function withTimeoutAndRetry<T>(operation: () => Promise<T>, timeoutMs: number, retryOptions?: RetryOptions): Promise<T>; /** * Circuit breaker pattern for preventing cascading failures */ export declare class CircuitBreaker { private threshold; private timeout; private monitorWindow; private failures; private lastFailureTime; private state; constructor(threshold?: number, timeout?: number, // 1 minute monitorWindow?: number); execute<T>(operation: () => Promise<T>): Promise<T>; private onSuccess; private onFailure; getState(): string; reset(): void; } /** * Rate limiter to prevent overwhelming APIs */ export declare class RateLimiter { private maxRequests; private windowMs; private requests; constructor(maxRequests: number, windowMs: number); acquire(): Promise<void>; } /** * Utility for graceful shutdown handling */ export declare class GracefulShutdown { private operations; private shutdownPromise; track<T>(operation: Promise<T>): Promise<T>; shutdown(timeoutMs?: number): Promise<void>; private performShutdown; } /** * Global instances for convenience */ export declare const globalShutdown: GracefulShutdown; export declare const providerCircuitBreaker: CircuitBreaker; export declare const apiRateLimiter: RateLimiter;