@imgly/plugin-ai-generation-web
Version:
AI generation plugin for the CE.SDK editor
54 lines (53 loc) • 2.14 kB
TypeScript
import { GenerationOptions, Output } from '../core/provider';
import { Middleware } from './middleware';
export interface RateLimitOptions<I> {
/**
* Maximum number of requests allowed in the time window
*/
maxRequests: number;
/**
* Time window in milliseconds
*/
timeWindowMs: number;
/**
* Optional key function or string to create different rate limits for different inputs
* - If not provided, all requests share the same 'global' rate limit
* - If a string is provided, that string is used as a static key
* - If a function is provided, it generates a key based on input and options
*/
keyFn?: string | ((input: I, options: GenerationOptions) => string);
/**
* Callback function that is called when rate limit is exceeded
* Return value determines if the operation should be rejected (throw error) or allowed
* If true is returned, the operation will proceed despite exceeding the rate limit
* If false or undefined is returned, the operation will be rejected with a default error
*/
onRateLimitExceeded?: (input: I, options: GenerationOptions, rateLimitInfo: {
key: string;
currentCount: number;
maxRequests: number;
timeWindowMs: number;
remainingTimeMs: number;
}) => boolean | Promise<boolean> | void;
/**
* Optional database name for the IndexedDB store
* If not provided, a default name will be used
*/
dbName?: string;
/**
* Disable the rate limit middleware
*/
disable?: boolean | (() => boolean);
}
interface RequestTracker {
timestamps: number[];
lastCleanup: number;
}
export declare const inMemoryStores: Map<symbol | string, Record<string, RequestTracker>>;
/**
* Middleware that implements rate limiting for AI generation requests
* Uses IndexedDB for storage when available, with in-memory fallback
* Each middleware instance has its own isolated set of rate limits
*/
declare function rateLimitMiddleware<I, O extends Output>(middlewareOptions: RateLimitOptions<I>): Middleware<I, O>;
export default rateLimitMiddleware;