(params: {
body: ChatCompletionRequestBody
openrouterApiKey: string | null
fetch: typeof globalThis.fetch
})
| 59 | } |
| 60 | |
| 61 | function createOpenRouterRequest(params: { |
| 62 | body: ChatCompletionRequestBody |
| 63 | openrouterApiKey: string | null |
| 64 | fetch: typeof globalThis.fetch |
| 65 | }) { |
| 66 | const { body, openrouterApiKey, fetch } = params |
| 67 | const providerBody = isKimiModel(body.model) |
| 68 | ? addKimiToolCompatibilityFields(body) |
| 69 | : body |
| 70 | |
| 71 | return fetch('https://openrouter.ai/api/v1/chat/completions', { |
| 72 | method: 'POST', |
| 73 | headers: { |
| 74 | Authorization: `Bearer ${openrouterApiKey ?? env.OPEN_ROUTER_API_KEY}`, |
| 75 | 'HTTP-Referer': 'https://codebuff.com', |
| 76 | 'X-Title': 'Codebuff', |
| 77 | 'Content-Type': 'application/json', |
| 78 | }, |
| 79 | body: JSON.stringify(providerBody), |
| 80 | // Use custom agent with extended headers timeout for deep-thinking models |
| 81 | // @ts-expect-error - dispatcher is a valid undici option not in fetch types |
| 82 | dispatcher: openrouterAgent, |
| 83 | }) |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Extract token counts and billed cost from an OpenRouter `usage` object. |
no test coverage detected