feat: add /context command to display context window token usage breakdown#1835
feat: add /context command to display context window token usage breakdown#1835pomelo-nwu merged 11 commits intomainfrom
Conversation
…kdown Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
📋 Review SummaryThis PR introduces a valuable 🔍 General Feedback
🎯 Specific Feedback🟡 High
🟢 Medium
🔵 Low
✅ Highlights
|
Code Coverage Summary
CLI Package - Full Text ReportCore Package - Full Text ReportFor detailed HTML reports, please see the 'coverage-reports-22.x-ubuntu-latest' artifact from the main CI run. |
pomelo-nwu
left a comment
There was a problem hiding this comment.
代码审查意见
✅ 优点
- 功能完整:实现了
/context命令,展示上下文窗口的 token 使用情况分解 - 文档详尽:
CONTEXT_COMMAND.md文档非常详细,解释了每个分类的含义和计算方式 - 渐进式设计:正确处理了两种模式(有/无 API 数据)的展示逻辑
- 视觉设计:使用 Ink 组件实现了美观的终端 UI,包括进度条和颜色编码
- 代码风格一致:遵循了项目现有的代码规范和架构模式
🔴 Critical(必须修复)
1. 缺少测试 ⚠️
新增了 contextCommand.ts 和 ContextUsage.tsx 两个核心文件,但没有对应的测试文件。
参考 compressCommand.test.ts 和 toolsCommand.test.ts,建议添加:
-
contextCommand.test.ts- 测试命令逻辑- 测试 config 未加载时的错误处理
- 测试无 API 数据时的估算模式
- 测试有 API 数据时的正常流程
- 测试 MCP 工具存在/不存在的情况
-
ContextUsage.test.tsx- 测试 UI 组件渲染- 测试估算模式的 UI 展示
- 测试正常模式的 UI 展示
- 测试进度条颜色变化(>60% 警告,>80% 错误)
- 测试详情列表的排序和截断
🟡 Suggestion(建议改进)
1. estimateTokens 函数复用
在 contextCommand.ts 中定义了 estimateTokens 函数,但 packages/core/src/utils/request-tokenizer/textTokenizer.ts 中已有 TextTokenizer 类。
建议:考虑复用核心模块的 tokenizer,或在核心模块中提供统一的估算函数,避免重复实现。
2. 正则表达式可优化
const regex = /--- Context from: (.+?) ---\n([\s\S]*?)--- End of Context from: \1 ---/g;建议:使用 [^]*? 替代 [\s\S]*? 以获得更好的性能。
3. 错误处理增强
parseMemoryFiles 函数在解析失败时返回空数组,但没有日志记录。
建议:添加调试日志或使用项目的日志系统记录异常情况,便于排查问题。
4. UI 组件可测试性
ContextUsage.tsx 中的 ProgressBar、CategoryRow、DetailRow 都是内部组件。
建议:将这些子组件提取为独立导出的组件,便于单独测试和复用。
🟢 Nice to have(可选优化)
-
国际化检查:文档中使用了很多中文,但代码中的 i18n 键值都是英文,建议检查
.qwen/i18n配置确保所有文本都有对应的翻译键。 -
性能优化:每次调用
/context都会重新计算所有 token 估算,可以考虑缓存结果,仅在相关数据变化时重新计算。
📸 终端 UI 视觉验证
已运行终端截图脚本验证 UI 显示效果,截图已上传。
验证结果:
- ✅ 进度条显示正常(紫色=已使用,灰色=空闲,黄色=自动压缩缓冲)
- ✅ 分类详情列表按 token 数降序排列
- ✅ 长名称正确截断(最多 30 字符)
- ✅ 两种模式(估算/实际)切换逻辑正确
总体评价:这是一个功能完整、设计良好的 PR,但缺少测试是主要问题。建议在合并前补充测试用例。
|
@pomelo-nwu This PR implements a key feature we are always looking for, pls get it ready and we cannot wait to push it release! |
- Add i18n keys for Context Usage component in all locales - Add 'Model:' prefix label for better clarity - Rename 'Autocompact' to 'Autocompact buffer' Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
- Add /context command to the Interface and Workspace Control table - Document the context window usage breakdown feature Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
- Add cachedContentTokenCount tracking in uiTelemetry service - Collect cached_content_token_count from streaming usage metadata - Use cached tokens instead of estimated overhead when available - Fix messages token calculation to avoid 'messages = 0' issue This improves context window display accuracy when using providers that support prefix caching (e.g., DashScope). Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
…ntext Root cause: PR #1835 accidentally overwrote PR #1912's correct telemetry isolation during a merge conflict resolution. This restores the original guard logic so subagent GeminiChat instances (which don't receive a telemetryService param) no longer write to the global uiTelemetryService. - Remove unused uiTelemetryService import from geminiChat.ts - Guard telemetry calls with this.telemetryService checks - Add test verifying subagent isolation Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
…ntext Root cause: PR QwenLM#1835 accidentally overwrote PR QwenLM#1912's correct telemetry isolation during a merge conflict resolution. This restores the original guard logic so subagent GeminiChat instances (which don't receive a telemetryService param) no longer write to the global uiTelemetryService. - Remove unused uiTelemetryService import from geminiChat.ts - Guard telemetry calls with this.telemetryService checks - Add test verifying subagent isolation Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
Overview
This PR adds a new
/contextslash command that displays detailed context window token usage breakdown.Features
Demo
Usage
`/context` - Display context window usage breakdown