( usage: Usage | null | undefined, )
| 53 | * 返回值范围 0-100,null 表示无有效数据 |
| 54 | */ |
| 55 | export function calculateCacheHitRate( |
| 56 | usage: Usage | null | undefined, |
| 57 | ): number | null { |
| 58 | if (!usage) return null |
| 59 | |
| 60 | const { input_tokens, cache_creation_input_tokens, cache_read_input_tokens } = |
| 61 | usage |
| 62 | |
| 63 | // 所有缓存字段为 0 表示无缓存数据 |
| 64 | if (cache_read_input_tokens === 0 && cache_creation_input_tokens === 0) { |
| 65 | return null |
| 66 | } |
| 67 | |
| 68 | const totalInputTokens = |
| 69 | input_tokens + cache_creation_input_tokens + cache_read_input_tokens |
| 70 | if (totalInputTokens === 0) return null |
| 71 | |
| 72 | return (cache_read_input_tokens / totalInputTokens) * 100 |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * 检测是否需要显示缓存警告 |
no outgoing calls
no test coverage detected