feat: 使用正式用量完善上下文与会话统计 - #162
Merged
Merged
Conversation
- 使用 provider response usage 记录实际上下文总量,界面不再累计估算值 - 拆分并持久化输入、输出、缓存读取、缓存写入与逐回合费用 - 新增消息、工具调用/结果、Token、费用和上下文统计浮层 - 统一模型价格计算,支持缓存计价和长上下文费率 - 补充 usage 映射、数据库迁移、渲染与压缩回归测试 参考:Pi Web 的会话统计布局与正式 usage 语义,仅作为产品设计参考。
Contributor
There was a problem hiding this comment.
Pull request overview
该 PR 将“上下文占用 / Token / 成本”统计统一切换到以 provider response usage 为正式数据源,并把缓存读写 Token、逐回合费用等明细规范化后持久化到任务与会话侧,同时在聊天界面加入 Pi 风格的双栏统计浮层。
Changes:
- 新增共享模型价格表与统一成本计算(含缓存读写与长上下文档位),并复用到 GAIA 评测成本核算。
- 扩展任务与会话的 usage 数据结构:新增 cacheRead/cacheWrite token 与 costUsd 字段,包含数据库 schema/迁移与聚合逻辑更新。
- UI 侧移除“估算/累计输入”展示,改为“等待正式用量”,并新增会话消息与 Token/费用统计浮层。
Reviewed changes
Copilot reviewed 25 out of 25 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/shared/model-pricing.ts | 新增共享模型定价表、modelId 规范化与统一的 usage 成本计算逻辑(含缓存与长上下文档位)。 |
| src/renderer/components/chat/useSessionCrud.ts | 持久化 usage 明细时增加 cacheRead/cacheWrite 与 costUsd 字段。 |
| src/renderer/components/chat/useChatSession.ts | 移除向 executeTask 传递基于历史 usage 的 contextInputTokens。 |
| src/renderer/components/chat/session-stats.ts | 新增会话级消息/工具/Token/成本聚合统计构建器。 |
| src/renderer/components/chat/ContextUsageButton.tsx | Tooltip 改为“等待正式用量”语义,并新增 Pi 风格统计浮层(消息与 Token 双栏)。 |
| src/renderer/components/chat/ChatPanel.tsx | 用正式 provider usage 驱动 context usage;引入会话统计并传入 ContextUsageButton。 |
| src/renderer/components/chat/tests/useChatSession.test.tsx | 更新断言:executeTask 不再包含 contextInputTokens。 |
| src/renderer/components/chat/tests/ContextUsageButton.test.tsx | 更新“非估算展示”语义与新增统计浮层渲染覆盖。 |
| src/renderer/components/chat/tests/ChatPanel.rendering.test.tsx | 更新 UI 断言:未拿到正式用量时显示等待态;context 展示逻辑改为正式 usage。 |
| src/preload/index.ts | IPC 参数类型中移除 contextInputTokens。 |
| src/main/ipc/usage-handlers.ts | usage:getTaskUsage 返回值新增 cacheRead/cacheWrite/costUsd,并引入共享成本计算。 |
| src/main/ipc/usage-aggregation.ts | TokenUsageLike / mergeTokenUsage 支持缓存读写 Token 并改进 totalTokens 归一化。 |
| src/main/ipc/ai-handlers.ts | 以 provider response usage.totalTokens 作为正式上下文占用;计算并写入 costUsd;移除 contextInputTokens。 |
| src/main/ipc/tests/usage-aggregation.test.ts | 覆盖缓存读写 token 合并与仅 cache usage 场景的 total 推导。 |
| src/main/ipc/tests/ai-handlers-media-runtime.test.ts | 覆盖逐步成本累加写入与不再使用 contextInputTokens 的行为。 |
| src/main/db/schema.ts | tasks 表新增 cache_read_tokens/cache_write_tokens/cost_usd 字段。 |
| src/main/db/index.ts | tasks 表 DDL 与迁移新增新列;Task/updateTask 映射支持新字段。 |
| src/main/db/tests/automation-runs-migration.test.ts | 覆盖 tasks 表新列迁移语句。 |
| src/main/core/session/message-parts.ts | UsagePart 扩展 cacheRead/cacheWrite 与 costUsd 字段。 |
| src/main/core/agent/agent-loop.ts | latestStepContextTokens 改为基于 totalTokens;mapUsage 支持 v7 noCache/cacheRead/cacheWrite 明细并修正 total 推导。 |
| src/main/core/agent/tests/agent-loop-reasoning.test.ts | 增加缓存读写拆分与 total 推导的测试覆盖。 |
| src/main/core/tests/agent-loop.test.ts | 更新 contextUsage 断言以匹配 latest/max step context tokens 新语义。 |
| src/eval/gaia/types.ts | GAIA TokenUsage 增加 cacheRead/cacheWrite 字段。 |
| src/eval/gaia/pricing.ts | GAIA 成本核算改为复用 shared/model-pricing 的统一计算逻辑。 |
| src/eval/gaia/tests/pricing.test.ts | 增加 cache read/write 单独定价与 GPT-5.5 长上下文档位测试。 |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| assistantMessages, | ||
| toolCalls, | ||
| toolResults, | ||
| totalMessages: userMessages + assistantMessages + toolResults, |
Comment on lines
+28
to
+37
| costUsd: | ||
| task.costUsd ?? | ||
| (task.provider === "ollama" | ||
| ? 0 | ||
| : calculateModelUsageCost(task.modelId, { | ||
| inputTokens: task.inputTokens ?? 0, | ||
| outputTokens: task.outputTokens ?? 0, | ||
| cacheReadTokens: task.cacheReadTokens, | ||
| cacheWriteTokens: task.cacheWriteTokens, | ||
| })), |
Comment on lines
+62
to
+71
| const rowCost = | ||
| usage.costUsd ?? | ||
| (usage.provider === "ollama" | ||
| ? 0 | ||
| : calculateModelUsageCost(usage.modelId, { | ||
| inputTokens: input, | ||
| outputTokens: output, | ||
| cacheReadTokens: cacheRead, | ||
| cacheWriteTokens: cacheWrite, | ||
| })); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概要
实现细节
totalTokens记录为正式上下文占用,未获得正式 usage 前显示等待状态。验证
pnpm lintpnpm typecheckpnpm test(378 个测试文件、3505 个测试)pnpm build参考
会话统计布局与正式 usage 语义参考 Pi Web,仅作为产品设计参考,不引入运行时依赖或代码依赖。