( usage: Record<string, unknown> | undefined | null, model: string, )
| 188 | } |
| 189 | |
| 190 | function extractUsageAndCost( |
| 191 | usage: Record<string, unknown> | undefined | null, |
| 192 | model: string, |
| 193 | ): UsageData { |
| 194 | if (!usage) { |
| 195 | return { |
| 196 | inputTokens: 0, |
| 197 | outputTokens: 0, |
| 198 | cacheReadInputTokens: 0, |
| 199 | reasoningTokens: 0, |
| 200 | cost: 0, |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | const promptDetails = usage.prompt_tokens_details as |
| 205 | | Record<string, unknown> |
| 206 | | undefined |
| 207 | | null |
| 208 | const completionDetails = usage.completion_tokens_details as |
| 209 | | Record<string, unknown> |
| 210 | | undefined |
| 211 | | null |
| 212 | const inputTokens = |
| 213 | typeof usage.prompt_tokens === 'number' ? usage.prompt_tokens : 0 |
| 214 | const outputTokens = |
| 215 | typeof usage.completion_tokens === 'number' ? usage.completion_tokens : 0 |
| 216 | const cacheReadInputTokens = |
| 217 | typeof promptDetails?.cached_tokens === 'number' |
| 218 | ? promptDetails.cached_tokens |
| 219 | : 0 |
| 220 | const reasoningTokens = |
| 221 | typeof completionDetails?.reasoning_tokens === 'number' |
| 222 | ? completionDetails.reasoning_tokens |
| 223 | : 0 |
| 224 | |
| 225 | const pricing = getOpenCodeZenPricing(model) |
| 226 | const nonCachedInputTokens = Math.max(0, inputTokens - cacheReadInputTokens) |
| 227 | const cost = |
| 228 | nonCachedInputTokens * pricing.inputCostPerToken + |
| 229 | cacheReadInputTokens * pricing.cachedInputCostPerToken + |
| 230 | outputTokens * pricing.outputCostPerToken |
| 231 | |
| 232 | return { |
| 233 | inputTokens, |
| 234 | outputTokens, |
| 235 | cacheReadInputTokens, |
| 236 | reasoningTokens, |
| 237 | cost, |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | export async function handleOpenCodeZenNonStream({ |
| 242 | body, |
no test coverage detected