* Create a model that routes through the Codebuff backend. * This is the existing behavior - requests go to Codebuff backend which forwards to OpenRouter.
( apiKey: string, model: string, )
| 196 | * This is the existing behavior - requests go to Codebuff backend which forwards to OpenRouter. |
| 197 | */ |
| 198 | function createCodebuffBackendModel( |
| 199 | apiKey: string, |
| 200 | model: string, |
| 201 | ): LanguageModel { |
| 202 | const openrouterUsage: OpenRouterUsageAccounting = { |
| 203 | cost: null, |
| 204 | costDetails: { |
| 205 | upstreamInferenceCost: null, |
| 206 | }, |
| 207 | } |
| 208 | |
| 209 | const openrouterApiKey = getByokOpenrouterApiKeyFromEnv() |
| 210 | |
| 211 | return new OpenAICompatibleChatLanguageModel(model, { |
| 212 | provider: 'codebuff', |
| 213 | url: ({ path: endpoint }) => |
| 214 | new URL(path.join('/api/v1', endpoint), WEBSITE_URL).toString(), |
| 215 | headers: () => ({ |
| 216 | Authorization: `Bearer ${apiKey}`, |
| 217 | 'user-agent': `ai-sdk/openai-compatible/${VERSION}/codebuff`, |
| 218 | ...(openrouterApiKey && { [BYOK_OPENROUTER_HEADER]: openrouterApiKey }), |
| 219 | }), |
| 220 | metadataExtractor: { |
| 221 | extractMetadata: async ({ parsedBody }: { parsedBody: any }) => { |
| 222 | if (openrouterApiKey !== undefined) { |
| 223 | return { codebuff: { usage: openrouterUsage } } |
| 224 | } |
| 225 | |
| 226 | if (typeof parsedBody?.usage?.cost === 'number') { |
| 227 | openrouterUsage.cost = parsedBody.usage.cost |
| 228 | } |
| 229 | if ( |
| 230 | typeof parsedBody?.usage?.cost_details?.upstream_inference_cost === |
| 231 | 'number' |
| 232 | ) { |
| 233 | openrouterUsage.costDetails.upstreamInferenceCost = |
| 234 | parsedBody.usage.cost_details.upstream_inference_cost |
| 235 | } |
| 236 | return { codebuff: { usage: openrouterUsage } } |
| 237 | }, |
| 238 | createStreamExtractor: () => ({ |
| 239 | processChunk: (parsedChunk: any) => { |
| 240 | if (openrouterApiKey !== undefined) { |
| 241 | return |
| 242 | } |
| 243 | |
| 244 | if (typeof parsedChunk?.usage?.cost === 'number') { |
| 245 | openrouterUsage.cost = parsedChunk.usage.cost |
| 246 | } |
| 247 | if ( |
| 248 | typeof parsedChunk?.usage?.cost_details?.upstream_inference_cost === |
| 249 | 'number' |
| 250 | ) { |
| 251 | openrouterUsage.costDetails.upstreamInferenceCost = |
| 252 | parsedChunk.usage.cost_details.upstream_inference_cost |
| 253 | } |
| 254 | }, |
| 255 | buildMetadata: () => { |
no test coverage detected