i18n-ai-translate
Version:
AI-powered localization CLI, Node library, and GitHub Action. Translate i18next JSON, Gettext PO, Java .properties, and iOS .strings with ChatGPT, Claude, Gemini, or local Ollama models.
33 lines (32 loc) • 1.33 kB
TypeScript
export default class RateLimiter {
lastAPICall: number | null;
delayBetweenCallsMs: number;
verboseLogging: boolean;
/** Optional tokens-per-minute cap enforced alongside the RPM slot. */
tokensPerMinute?: number;
private nextSlot;
/**
* Sliding 60-second window of recent token consumption. Each entry
* is [unix-millis-at-consume-time, token-count]. Entries older than
* 60s are pruned on every acquire() call.
*/
private tokenWindow;
constructor(delayBetweenCallsMs: number, verboseLogging: boolean, tokensPerMinute?: number);
/**
* Reserve the next RPM slot and, when configured, also wait for
* enough TPM headroom to fit the estimated token cost of the call
* about to be made. Concurrent callers reserve distinct slots
* synchronously; the TPM check runs afterward.
* @param estimatedTokens - tokens this call is expected to consume; 0 skips the TPM check
*/
acquire(estimatedTokens?: number): Promise<void>;
/**
* TPM enforcement. Prunes stale entries, and if adding this call
* would exceed the cap, waits until the oldest entry is old enough
* to fall out of the 60-second window.
*/
private awaitTokenBudget;
penalize(penaltyMs: number): void;
apiCalled(): void;
wait(): Promise<void>;
}