( provider: string, model: string, workspaceId: string | undefined | null, userProvidedKey?: string )
| 88 | } |
| 89 | |
| 90 | export async function getApiKeyWithBYOK( |
| 91 | provider: string, |
| 92 | model: string, |
| 93 | workspaceId: string | undefined | null, |
| 94 | userProvidedKey?: string |
| 95 | ): Promise<{ apiKey: string; isBYOK: boolean }> { |
| 96 | const isOllamaModel = |
| 97 | provider === 'ollama' || useProvidersStore.getState().providers.ollama.models.includes(model) |
| 98 | if (isOllamaModel) { |
| 99 | return { apiKey: 'empty', isBYOK: false } |
| 100 | } |
| 101 | |
| 102 | const isVllmModel = |
| 103 | provider === 'vllm' || useProvidersStore.getState().providers.vllm.models.includes(model) |
| 104 | if (isVllmModel) { |
| 105 | return { apiKey: userProvidedKey || env.VLLM_API_KEY || 'empty', isBYOK: false } |
| 106 | } |
| 107 | |
| 108 | const isLitellmModel = |
| 109 | provider === 'litellm' || useProvidersStore.getState().providers.litellm.models.includes(model) |
| 110 | if (isLitellmModel) { |
| 111 | return { apiKey: userProvidedKey || env.LITELLM_API_KEY || 'empty', isBYOK: false } |
| 112 | } |
| 113 | |
| 114 | const isFireworksModel = |
| 115 | provider === 'fireworks' || |
| 116 | useProvidersStore.getState().providers.fireworks.models.includes(model) |
| 117 | if (isFireworksModel) { |
| 118 | if (workspaceId) { |
| 119 | const byokResult = await getBYOKKey(workspaceId, 'fireworks') |
| 120 | if (byokResult) { |
| 121 | logger.info('Using BYOK key for Fireworks', { model, workspaceId }) |
| 122 | return byokResult |
| 123 | } |
| 124 | } |
| 125 | if (userProvidedKey) { |
| 126 | return { apiKey: userProvidedKey, isBYOK: false } |
| 127 | } |
| 128 | if (env.FIREWORKS_API_KEY) { |
| 129 | return { apiKey: env.FIREWORKS_API_KEY, isBYOK: false } |
| 130 | } |
| 131 | throw new Error(`API key is required for Fireworks ${model}`) |
| 132 | } |
| 133 | |
| 134 | const isTogetherModel = |
| 135 | provider === 'together' || |
| 136 | useProvidersStore.getState().providers.together.models.includes(model) |
| 137 | if (isTogetherModel) { |
| 138 | if (workspaceId) { |
| 139 | const byokResult = await getBYOKKey(workspaceId, 'together') |
| 140 | if (byokResult) { |
| 141 | logger.info('Using BYOK key for Together AI', { model, workspaceId }) |
| 142 | return byokResult |
| 143 | } |
| 144 | } |
| 145 | if (userProvidedKey) { |
| 146 | return { apiKey: userProvidedKey, isBYOK: false } |
| 147 | } |
no test coverage detected