/** * Enhanced Rate Limiter and Quota Manager for MCP * * Implements sophisticated rate limiting and quota management for LLM interactions */ import { EventEmitter } from 'events'; import { LLMRequest, UsageInfo } from '../types'; /** * Rate limit window types */ export declare enum RateLimitWindow { SECOND = 1000, MINUTE = 60000, HOUR = 3600000, DAY = 86400000, WEEK = 604800000, MONTH = 2592000000 } /** * Rate limit rule */ export interface RateLimitRule { id: string; name: string; description?: string; window: RateLimitWindow; limit: number; resource?: string; action?: string; scope: 'global' | 'agent' | 'provider' | 'model'; priority: number; burst?: number; cooldown?: number; } /** * Quota definition */ export interface Quota { id: string; name: string; agentDID?: string; provider?: string; model?: string; window: RateLimitWindow; limits: { requests?: number; tokens?: number; cost?: number; }; usage: { requests: number; tokens: number; cost: number; }; resetAt: Date; warningThreshold?: number; hardLimit: boolean; } /** * Rate limit result */ export interface RateLimitResult { allowed: boolean; limit?: number; remaining?: number; resetAt?: Date; retryAfter?: number; reason?: string; violatedRules?: RateLimitRule[]; } /** * Quota status */ export interface QuotaStatus { quota: Quota; percentageUsed: number; remaining: { requests?: number; tokens?: number; cost?: number; }; willReset: Date; isWarning: boolean; isExceeded: boolean; } /** * Enhanced Rate Limiter and Quota Manager */ export declare class RateLimiterManager extends EventEmitter { private rules; private quotas; private tokenBuckets; private usageHistory; private slidingWindows; private cooldowns; private cleanupInterval?; constructor(); /** * Initialize default rate limit rules */ private initializeDefaultRules; /** * Check rate limit */ checkRateLimit(request: LLMRequest, provider?: string, model?: string): Promise; /** * Check quota */ checkQuota(agentDID: string, usage: UsageInfo, provider?: string, model?: string): Promise; /** * Record usage */ recordUsage(agentDID: string, usage: UsageInfo, provider?: string, model?: string): Promise; /** * Add rate limit rule */ addRule(rule: RateLimitRule): void; /** * Remove rate limit rule */ removeRule(ruleId: string): void; /** * Add quota */ addQuota(quota: Quota): void; /** * Remove quota */ removeQuota(quotaId: string): void; /** * Get quota status */ getQuotaStatus(quotaId: string): QuotaStatus | null; /** * Get all quota statuses for agent */ getAgentQuotaStatuses(agentDID: string): QuotaStatus[]; /** * Get usage statistics */ getUsageStatistics(window: RateLimitWindow, agentDID?: string, provider?: string): { requests: number; tokens: number; cost: number; averageTokensPerRequest: number; peakRequestsPerMinute: number; }; /** * Get applicable rules */ private getApplicableRules; /** * Get applicable quotas */ private getApplicableQuotas; /** * Get rate limit key */ private getRateLimitKey; /** * Check rule using sliding window */ private checkRule; /** * Check token bucket */ private checkTokenBucket; /** * Consume token */ private consumeToken; /** * Calculate quota percentage used */ private calculateQuotaPercentage; /** * Check if quota would be exceeded */ private wouldExceedQuota; /** * Check if quota is exceeded */ private isQuotaExceeded; /** * Schedule quota reset */ private scheduleQuotaReset; /** * Reset quota */ private resetQuota; /** * Start cleanup timer */ private startCleanupTimer; /** * Clean up old data */ private cleanup; /** * Export rate limit configuration */ exportConfiguration(): { rules: RateLimitRule[]; quotas: Quota[]; }; /** * Import rate limit configuration */ importConfiguration(config: { rules?: RateLimitRule[]; quotas?: Quota[]; }): void; /** * Shutdown rate limiter */ shutdown(): void; } export default RateLimiterManager; //# sourceMappingURL=rate-limiter.d.ts.map