MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / createOpenRouterRequest

Function createOpenRouterRequest

web/src/llm-api/openrouter.ts:61–84  ·  view source on GitHub ↗
(params: {
  body: ChatCompletionRequestBody
  openrouterApiKey: string | null
  fetch: typeof globalThis.fetch
})

Source from the content-addressed store, hash-verified

59}
60
61function 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.

Callers 2

handleOpenRouterStreamFunction · 0.85

Calls 3

isKimiModelFunction · 0.90
fetchFunction · 0.50

Tested by

no test coverage detected