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
74 lines • 2.28 kB
TypeScript
/**
* Rate Limiter Pattern - Control request throughput
*/
import { AsyncFunction, Logger } from "../types/common";
import { RateLimitStrategy, RateLimiterOptions, RateLimitResult } from "../types/rate-limiter";
/**
* Internal options (without execute field)
*/
interface RateLimiterInternalOptions {
maxRequests?: number;
windowMs?: number;
strategy?: RateLimitStrategy;
refillRate?: number;
logger?: Logger;
onLimitReached?: (retryAfter: number) => void;
}
/**
* Rate Limiter - Controls request throughput
*/
export declare class RateLimiter<TResult = any, TArgs extends any[] = any[]> {
private readonly fn;
private readonly options;
private limiter;
constructor(fn: AsyncFunction<TResult, TArgs>, options?: RateLimiterInternalOptions);
/**
* Execute function with rate limiting
*/
execute(...args: TArgs): Promise<RateLimitResult<TResult>>;
/**
* Execute with automatic wait if rate limit reached
*/
executeWithWait(...args: TArgs): Promise<RateLimitResult<TResult>>;
/**
* Get remaining requests
*/
getRemaining(): number;
/**
* Reset rate limiter
*/
reset(): void;
}
/**
* Callable rate limiter type (Vercel-style)
*/
export interface CallableRateLimiter<TResult = any, TArgs extends any[] = any[]> {
(...args: TArgs): Promise<RateLimitResult<TResult>>;
wait(...args: TArgs): Promise<RateLimitResult<TResult>>;
getRemaining(): number;
reset(): void;
}
/**
* Define a rate limiter with Vercel-style callable API
*
* @example
* ```typescript
* const limiter = defineRateLimiter({
* execute: async () => callAPI(),
* maxRequests: 60,
* windowMs: 60000
* });
*
* const result = await limiter(); // Direct call
* console.log(limiter.getRemaining()); // Check remaining
* await limiter.wait(); // Wait if needed
* ```
*/
export declare function defineRateLimiter<TResult = any, TArgs extends any[] = any[]>(options: RateLimiterOptions<TResult>): CallableRateLimiter<TResult, TArgs>;
/**
* @deprecated Use `defineRateLimiter` instead for better alignment with Vercel AI SDK patterns
* @see defineRateLimiter
*/
export declare const rateLimiter: typeof defineRateLimiter;
export {};
//# sourceMappingURL=rate-limiter.d.ts.map