buroventures-harald-code-core
Version:
Harald Code Core - Core functionality for AI-powered coding assistant
96 lines (95 loc) • 2.73 kB
TypeScript
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
export interface ApiKeyRotationSettings {
/** List of API keys to rotate between */
apiKeys?: string[];
/** Current active key index */
currentKeyIndex?: number;
/** Whether to enable automatic rotation on rate limits */
autoRotateOnRateLimit?: boolean;
/** Reset rotation daily (to handle daily limits) */
resetRotationDaily?: boolean;
/** Track usage per key per day */
dailyUsageTracking?: Record<string, {
date: string;
requests: number;
}>;
}
export interface ApiKeyRotationConfig {
settings: ApiKeyRotationSettings;
onSettingsUpdate?: (settings: ApiKeyRotationSettings) => Promise<void>;
}
export declare class ApiKeyRotationManager {
private settings;
private onSettingsUpdate?;
private fallbackApiKey?;
constructor(config: ApiKeyRotationConfig, fallbackApiKey?: string);
/**
* Get the current active API key
*/
getCurrentApiKey(): string | undefined;
/**
* Get all available API keys (from settings + fallback)
*/
getAvailableKeys(): string[];
/**
* Rotate to the next available API key
*/
rotateToNextKey(): Promise<string | undefined>;
/**
* Handle rate limit error and potentially rotate keys
*/
handleRateLimit(error: unknown): Promise<string | undefined>;
/**
* Track API usage for a key
*/
trackUsage(apiKey: string, isRateLimit?: boolean): Promise<void>;
/**
* Add a new API key to the rotation
*/
addApiKey(apiKey: string): Promise<void>;
/**
* Remove an API key from rotation
*/
removeApiKey(apiKey: string): Promise<void>;
/**
* Get rotation status information
*/
getRotationStatus(): {
totalKeys: number;
currentKeyIndex: number;
currentKeyPreview: string;
autoRotateEnabled: boolean;
dailyUsage: Record<string, {
date: string;
requests: number;
}>;
};
/**
* Reset daily usage tracking if it's a new day
*/
private resetDailyTrackingIfNeeded;
/**
* Create a hash of the API key for tracking (privacy-safe)
*/
private hashApiKey;
/**
* Save settings changes
*/
private saveSettings;
/**
* Update rotation settings
*/
updateSettings(newSettings: Partial<ApiKeyRotationSettings>): Promise<void>;
/**
* Check if rate limiting error should trigger rotation
*/
static isRateLimitError(error: unknown): boolean;
/**
* Validate API key format (basic validation)
*/
static validateApiKey(apiKey: string): boolean;
}