feat: 添加 prompt 缓存命中率检测与警告功能

每次 API 请求后自动计算缓存命中率,低于阈值(默认 80%)时在对话流中显示黄色警告消息。
同时更新 /context 命令输出中显示缓存命中率。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
claude-code-best
2026-05-07 10:49:06 +08:00
parent e8759f3402
commit e3c0699f5b
5 changed files with 191 additions and 0 deletions

View File

@@ -229,6 +229,10 @@ export interface ContextData {
cache_creation_input_tokens: number
cache_read_input_tokens: number
} | null
/** Cache hit rate percentage (0-100), undefined if no data */
readonly cacheHitRate?: number
/** Cache warning threshold percentage */
readonly cacheThreshold?: number
}
export async function countToolDefinitionTokens(
@@ -1396,5 +1400,13 @@ export async function analyzeContextUsage(
isAutoCompactEnabled: isAutoCompact,
messageBreakdown: formattedMessageBreakdown,
apiUsage,
...(() => {
if (!apiUsage) return {}
const { calculateCacheHitRate, getCacheThreshold } =
require('./cacheWarning.js') as typeof import('./cacheWarning.js')
const hitRate = calculateCacheHitRate(apiUsage)
if (hitRate === null) return {}
return { cacheHitRate: hitRate, cacheThreshold: getCacheThreshold() }
})(),
}
}