import type { CesMigration } from "./types.js"; /** * Providers whose bare API-key entries (e.g. `anthropic`) must be moved to * the canonical `credential/{provider}/api_key` namespace. * * Note: `elevenlabs` is intentionally omitted — it was already migrated by * `migrateElevenLabsToCredential()` in the Swift layer before CES migrations * were introduced. */ const PROVIDERS_TO_MIGRATE = [ "anthropic", "openai", "gemini", "ollama", "fireworks", "openrouter", "brave", "perplexity", "deepgram", "xai", ] as const; export const apiKeyToCredentialsMigration: CesMigration = { id: "002-api-keys-to-credentials", description: "Rekey bare provider API keys to credential/{provider}/api_key namespace", async run(backend): Promise { for (const provider of PROVIDERS_TO_MIGRATE) { const bareValue = await backend.get(provider); if (bareValue === undefined) continue; // nothing to migrate for this provider const credKey = `credential/${provider}/api_key`; const existingCred = await backend.get(credKey); if (existingCred === undefined) { // Write new key first — safe to re-run if we crash after this. // Skip delete if the write fails so the bare key is preserved for retry. const ok = await backend.set(credKey, bareValue); if (!ok) continue; } // Always delete old bare key (idempotent: harmless if already absent) await backend.delete(provider); } }, async down(backend): Promise { for (const provider of PROVIDERS_TO_MIGRATE) { const credKey = `credential/${provider}/api_key`; const credValue = await backend.get(credKey); if (credValue === undefined) continue; const existingBare = await backend.get(provider); if (existingBare === undefined) { await backend.set(provider, credValue); } await backend.delete(credKey); } }, };