fix: Gemini 适配器补全 usage 字段映射#1233
Conversation
Gemini API 的 usageMetadata 包含 cachedContentTokenCount 字段, 但此前未映射到 Anthropic 格式的 cache_read_input_tokens,导致 cache_creation_input_tokens 和 cache_read_input_tokens 始终为 0。 同时 message_delta 事件此前只携带 output_tokens,缺失 input_tokens、cache_creation_input_tokens、cache_read_input_tokens, 导致下游从 message_delta 读取最终 token 计数时获取不完整数据。 修复: - 新增 cachedContentTokenCount → cache_read_input_tokens 映射 - message_start 和 message_delta 携带完整四个 usage 字段 - cache_creation_input_tokens 保持为 0(Gemini API 无等价概念) Co-Authored-By: deepseek-v4-pro[1m] <deepseek-ai@claude-code-best.win>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
✅ Files skipped from review due to trivial changes (1)
📝 WalkthroughWalkthroughThe Gemini stream adapter now accumulates cached-content token reads from Gemini chunk usage metadata and reports them as cache_read_input_tokens in both initial (message_start) and final (message_delta) streamed usage payloads; the type for cachedContentTokenCount was added. ChangesGemini Cache Token Tracking
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint skipped: no ESLint configuration detected in root package.json. To enable, add Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/`@ant/model-provider/src/providers/gemini/streamAdapter.ts:
- Line 19: The code references a non-existent field cachedContentTokenCount on
GeminiUsageMetadata which causes a TypeScript error; update the
GeminiUsageMetadata type (add cachedContentTokenCount?: number) in types.ts so
the assignment in streamAdapter.ts that updates cachedReadTokens from
usage.cachedContentTokenCount compiles, or if the Gemini API never supplies
cached content counts, remove the usage.cachedContentTokenCount fallback
assignment and rely solely on existing fields (ensure to update the type or
remove the line referencing cachedContentTokenCount accordingly).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: e047b224-3c03-4e23-a320-17458fa1c19e
📒 Files selected for processing (1)
packages/@ant/model-provider/src/providers/gemini/streamAdapter.ts
streamAdapter.ts 使用 usage.cachedContentTokenCount 但该字段未 在 GeminiUsageMetadata 类型中声明。CodeRabbit 审查发现此问题。 Co-Authored-By: deepseek-v4-pro[1m] <deepseek-ai@claude-code-best.win>
Summary
Gemini API 的
usageMetadata包含cachedContentTokenCount字段,但此前未映射到 Anthropic 格式的
cache_read_input_tokens,导致cache 相关字段始终为 0。
同时
message_delta事件此前只携带output_tokens,缺失input_tokens、cache_creation_input_tokens、cache_read_input_tokens,与 OpenAI/Anthropic 适配器不一致。下游代码(如
cacheWarning.ts、cacheStatsState.ts、StatusLine.tsx)从message_delta读取最终 token 计数时会获得不完整数据。
Changes
cachedContentTokenCount→cache_read_input_tokens映射message_start中的cache_read_input_tokens从固定 0 改为实际值message_delta从只发送output_tokens改为发送完整四个字段Gemini usageMetadata fields (per API docs)
promptTokenCountinput_tokenscandidatesTokenCountoutput_tokens(part)thoughtsTokenCountoutput_tokens(part)cachedContentTokenCountcache_read_input_tokenscache_creation_input_tokensTest plan
🤖 Generated with Claude Code Best
Summary by CodeRabbit