(usage: Record<string, unknown> | undefined | null)
| 93 | const SILICONFLOW_OUTPUT_COST_PER_TOKEN = 1.20 / 1_000_000 |
| 94 | |
| 95 | function extractUsageAndCost(usage: Record<string, unknown> | undefined | null): UsageData { |
| 96 | if (!usage) return { inputTokens: 0, outputTokens: 0, cacheReadInputTokens: 0, reasoningTokens: 0, cost: 0 } |
| 97 | const promptDetails = usage.prompt_tokens_details as Record<string, unknown> | undefined | null |
| 98 | const completionDetails = usage.completion_tokens_details as Record<string, unknown> | undefined | null |
| 99 | |
| 100 | const inputTokens = typeof usage.prompt_tokens === 'number' ? usage.prompt_tokens : 0 |
| 101 | const outputTokens = typeof usage.completion_tokens === 'number' ? usage.completion_tokens : 0 |
| 102 | const cacheReadInputTokens = typeof promptDetails?.cached_tokens === 'number' ? promptDetails.cached_tokens : 0 |
| 103 | const reasoningTokens = typeof completionDetails?.reasoning_tokens === 'number' ? completionDetails.reasoning_tokens : 0 |
| 104 | |
| 105 | const nonCachedInputTokens = Math.max(0, inputTokens - cacheReadInputTokens) |
| 106 | const cost = |
| 107 | nonCachedInputTokens * SILICONFLOW_INPUT_COST_PER_TOKEN + |
| 108 | cacheReadInputTokens * SILICONFLOW_CACHED_INPUT_COST_PER_TOKEN + |
| 109 | outputTokens * SILICONFLOW_OUTPUT_COST_PER_TOKEN |
| 110 | |
| 111 | return { inputTokens, outputTokens, cacheReadInputTokens, reasoningTokens, cost } |
| 112 | } |
| 113 | |
| 114 | export async function handleSiliconFlowNonStream({ |
| 115 | body, |
no outgoing calls
no test coverage detected