fix(usage): 修复缓存创建 token 展示并在总量中计入缓存组件

- gateway/admin 用量载荷在 cache_creation_input_tokens 为 0 时回填 ephemeral_5m/1h 合计
- 标准化用量写入时将 cache_creation/cache_read token 计入 total_tokens
- 前端列表与轮询同步新增分类字段,修复缓存 token 列显示
This commit is contained in:
fawney19
2026-04-19 02:46:28 +08:00
parent e82c9c5104
commit 2f487fda66
11 changed files with 316 additions and 19 deletions
@@ -1,6 +1,9 @@
type UsageTokenLike = {
effective_input_tokens?: number | null
input_tokens?: number | null
cache_creation_input_tokens?: number | null
cache_creation_ephemeral_5m_input_tokens?: number | null
cache_creation_ephemeral_1h_input_tokens?: number | null
cache_read_input_tokens?: number | null
api_format?: string | null
endpoint_api_format?: string | null
@@ -17,6 +20,16 @@ function apiFamily(apiFormat: string | null | undefined): string {
.toLowerCase()
}
export function getCacheCreationTokens(usage: UsageTokenLike): number {
const explicit = toNonNegativeNumber(usage.cache_creation_input_tokens)
const classified = toNonNegativeNumber(usage.cache_creation_ephemeral_5m_input_tokens)
+ toNonNegativeNumber(usage.cache_creation_ephemeral_1h_input_tokens)
if (explicit === 0 && classified > 0) {
return classified
}
return explicit
}
export function getEffectiveInputTokens(usage: UsageTokenLike): number {
const explicit = toNonNegativeNumber(usage.effective_input_tokens)
if (explicit > 0) {