d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
62 lines
2.3 KiB
TypeScript
62 lines
2.3 KiB
TypeScript
import { env } from '@/lib/core/config/env'
|
|
|
|
/**
|
|
* Rotates through available API keys for a provider
|
|
* @param provider - The provider to get a key for (e.g., 'openai')
|
|
* @returns The selected API key
|
|
* @throws Error if no API keys are configured for rotation
|
|
*/
|
|
export function getRotatingApiKey(provider: string): string {
|
|
if (
|
|
provider !== 'openai' &&
|
|
provider !== 'anthropic' &&
|
|
provider !== 'gemini' &&
|
|
provider !== 'cohere' &&
|
|
provider !== 'zai' &&
|
|
provider !== 'xai'
|
|
) {
|
|
throw new Error(`No rotation implemented for provider: ${provider}`)
|
|
}
|
|
|
|
const keys = []
|
|
|
|
if (provider === 'openai') {
|
|
if (env.OPENAI_API_KEY_1) keys.push(env.OPENAI_API_KEY_1)
|
|
if (env.OPENAI_API_KEY_2) keys.push(env.OPENAI_API_KEY_2)
|
|
if (env.OPENAI_API_KEY_3) keys.push(env.OPENAI_API_KEY_3)
|
|
} else if (provider === 'anthropic') {
|
|
if (env.ANTHROPIC_API_KEY_1) keys.push(env.ANTHROPIC_API_KEY_1)
|
|
if (env.ANTHROPIC_API_KEY_2) keys.push(env.ANTHROPIC_API_KEY_2)
|
|
if (env.ANTHROPIC_API_KEY_3) keys.push(env.ANTHROPIC_API_KEY_3)
|
|
} else if (provider === 'gemini') {
|
|
if (env.GEMINI_API_KEY_1) keys.push(env.GEMINI_API_KEY_1)
|
|
if (env.GEMINI_API_KEY_2) keys.push(env.GEMINI_API_KEY_2)
|
|
if (env.GEMINI_API_KEY_3) keys.push(env.GEMINI_API_KEY_3)
|
|
} else if (provider === 'cohere') {
|
|
if (env.COHERE_API_KEY_1) keys.push(env.COHERE_API_KEY_1)
|
|
if (env.COHERE_API_KEY_2) keys.push(env.COHERE_API_KEY_2)
|
|
if (env.COHERE_API_KEY_3) keys.push(env.COHERE_API_KEY_3)
|
|
} else if (provider === 'zai') {
|
|
if (env.ZAI_API_KEY_1) keys.push(env.ZAI_API_KEY_1)
|
|
if (env.ZAI_API_KEY_2) keys.push(env.ZAI_API_KEY_2)
|
|
if (env.ZAI_API_KEY_3) keys.push(env.ZAI_API_KEY_3)
|
|
} else if (provider === 'xai') {
|
|
if (env.XAI_API_KEY_1) keys.push(env.XAI_API_KEY_1)
|
|
if (env.XAI_API_KEY_2) keys.push(env.XAI_API_KEY_2)
|
|
if (env.XAI_API_KEY_3) keys.push(env.XAI_API_KEY_3)
|
|
}
|
|
|
|
if (keys.length === 0) {
|
|
throw new Error(
|
|
`No API keys configured for rotation. Please configure ${provider.toUpperCase()}_API_KEY_1, ${provider.toUpperCase()}_API_KEY_2, or ${provider.toUpperCase()}_API_KEY_3.`
|
|
)
|
|
}
|
|
|
|
// Simple round-robin rotation based on current minute
|
|
// This distributes load across keys and is stateless
|
|
const currentMinute = new Date().getMinutes()
|
|
const keyIndex = currentMinute % keys.length
|
|
|
|
return keys[keyIndex]
|
|
}
|