Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions apps/open-swe/src/utils/llms/model-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ export interface CircuitBreakerState {
openedAt?: number;
}

export interface ApiKeyRotationState {
keys: string[];
currentIndex: number;
lastUsedTime: number;
cooldownUntil: number;
}

interface ModelLoadConfig {
provider: Provider;
modelName: string;
Expand All @@ -47,6 +54,7 @@ export const PROVIDER_FALLBACK_ORDER = [
"openai",
"anthropic",
"google-genai",
"openrouter",
] as const;
export type Provider = (typeof PROVIDER_FALLBACK_ORDER)[number];

Expand Down Expand Up @@ -82,6 +90,8 @@ const providerToApiKey = (
return apiKeys.anthropicApiKey;
case "google-genai":
return apiKeys.googleApiKey;
case "openrouter":
return apiKeys.openrouterApiKey;
default:
throw new Error(`Unknown provider: ${providerName}`);
}
Expand All @@ -90,6 +100,9 @@ const providerToApiKey = (
export class ModelManager {
private config: ModelManagerConfig;
private circuitBreakers: Map<string, CircuitBreakerState> = new Map();
private apiKeys: Map<Provider, ApiKeyRotationState> = new Map();
// Store the last reset time for daily rotation
private lastDailyReset: Map<Provider, number> = new Map();

constructor(config: Partial<ModelManagerConfig> = {}) {
this.config = { ...DEFAULT_MODEL_MANAGER_CONFIG, ...config };
Expand Down
3 changes: 2 additions & 1 deletion apps/web/src/lib/api-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export function hasApiKeySet(config: Record<string, any>) {
if (
(enabledProviders.includes("anthropic") && !apiKeys.anthropicApiKey) ||
(enabledProviders.includes("openai") && !apiKeys.openaiApiKey) ||
(enabledProviders.includes("google-genai") && !apiKeys.googleApiKey)
(enabledProviders.includes("google-genai") && !apiKeys.googleApiKey) ||
(enabledProviders.includes("openrouter") && !apiKeys.openrouterApiKey)
) {
return false;
}
Expand Down