feat(packages): 为 usage 增加输出 token 趋势指令#895
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a new "tokens" command to visualize ChatLuna token usage trends and plugin breakdowns, rendering charts via Puppeteer. Feedback highlights a potential call stack overflow risk in "index.ts" when using "Math.max" with the spread operator on large datasets. Additionally, in "renderer.ts", it is recommended to make the "__dirname" resolution safer for ESM environments and to delete temporary HTML files immediately in the "finally" block rather than using a delayed "setTimeout" to prevent disk clutter and memory leaks.
|
Warning Review limit reached
More reviews will be available in 7 minutes and 21 seconds. Learn how PR review limits work. Your organization has run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
Walkthrough新增 token 消耗趋势功能:定义报表数据结构、在控制台注册 /tokens 命令以生成报表并可选渲染图表,提供 SVG 绘制与 HTML 模板并通过 Puppeteer 截图输出图片。 变更Token Trend 趋势图渲染功能
序列图sequenceDiagram
participant User
participant TokensCommand as /tokens 命令
participant tokenReport
participant Database as chatluna_usage
participant renderTokenTrend
participant Puppeteer
User->>TokensCommand: 触发 /tokens [range] [plugin]
TokensCommand->>tokenReport: 请求 TokenReport(range, withPlugins)
tokenReport->>Database: 查询 chatluna_usage
Database-->>tokenReport: 返回记录
tokenReport-->>TokensCommand: 返回 TokenReport
TokensCommand->>renderTokenTrend: 请求渲染(若有 Puppeteer)
renderTokenTrend->>Puppeteer: 打开本地 HTML, 等待字体, 截图 .stage
Puppeteer-->>renderTokenTrend: 返回图片 buffer
TokensCommand-->>User: 发送图片
代码审查工作量估计🎯 3 (中等) | ⏱️ ~25 分钟 诗
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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 |
- 在 chatluna-usage 中新增 tokens 指令,支持 day、week、month、all 统计范围及插件明细参数。 - 基于 chatluna_usage 表聚合 total、input、output token 与请求峰值,按不同范围生成分桶趋势数据。 - 新增 puppeteer HTML 图表渲染模板,支持浅色和深色主题以及颜色图例。 - 将 puppeteer 声明为可选依赖服务,未启用时保留文字统计输出。
1efe8d3 to
fb89391
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/extension-usage/src/index.ts (1)
469-488:range === 'all'会全表扫描,建议加上观测/治理手段。当
range为all时time = { $lt: end },相当于把chatluna_usage全表拉进内存再聚合。随着记录增长,单次tokens -a可能造成明显的内存与延迟峰值。表上已有createdAt索引,但无范围过滤时索引无济于事。可考虑:为all设定一个上限时间窗(或最大行数),或在数据库侧做聚合统计而非全量取回。🤖 Prompt for 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. In `@packages/extension-usage/src/index.ts` around lines 469 - 488, The tokenReport method currently sets time = { $lt: end } for range === 'all', causing a full-table fetch of chatluna_usage by createdAt; change tokenReport to avoid full-table scans by applying a sensible cap or DB-side aggregation: either compute a start = end - MAX_TOKEN_REPORT_WINDOW (configurable, e.g. using Time constants) or add a limit/aggregation in the database query (e.g. request aggregated token sums or top-N rows instead of pulling all records) before calling createTokenReport; ensure the change references tokenReport, createTokenReport, chatluna_usage, createdAt and use a configurable MAX_ROWS or MAX_WINDOW so future growth is governed and include a warning/log when the “all” request is capped.
🤖 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.
Nitpick comments:
In `@packages/extension-usage/src/index.ts`:
- Around line 469-488: The tokenReport method currently sets time = { $lt: end }
for range === 'all', causing a full-table fetch of chatluna_usage by createdAt;
change tokenReport to avoid full-table scans by applying a sensible cap or
DB-side aggregation: either compute a start = end - MAX_TOKEN_REPORT_WINDOW
(configurable, e.g. using Time constants) or add a limit/aggregation in the
database query (e.g. request aggregated token sums or top-N rows instead of
pulling all records) before calling createTokenReport; ensure the change
references tokenReport, createTokenReport, chatluna_usage, createdAt and use a
configurable MAX_ROWS or MAX_WINDOW so future growth is governed and include a
warning/log when the “all” request is capped.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: f33ed7f6-cc8e-48e0-acb4-596d7be53841
⛔ Files ignored due to path filters (1)
packages/extension-usage/package.jsonis excluded by!**/*.json
📒 Files selected for processing (3)
packages/extension-usage/resources/token-trend/template.htmlpackages/extension-usage/src/index.tspackages/extension-usage/src/renderer.ts
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 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/extension-usage/src/index.ts`:
- Line 66: The assignment to variable from mixes a ternary and nullish
coalescing without grouping, which triggers prettier; update the expression in
index.ts so the conditional is wrapped in parentheses (i.e., parenthesize the
RHS ternary so the nullish coalescing binds as intended) for the declaration
using range, sorted, start and end.
In `@packages/extension-usage/src/renderer.ts`:
- Around line 240-244: Compute dirname without referencing __dirname at
evaluation time: replace the current __dirname?.length check with a safe runtime
check like typeof __dirname !== 'undefined' ? __dirname :
path.dirname(fileURLToPath(import.meta.url)), and then build templatePath using
path.resolve(dirname, '..', 'resources', 'token-trend', 'template.html') (use
path.dirname(fileURLToPath(import.meta.url)) for ESM) so that the resolved
templatePath points to
packages/extension-usage/resources/token-trend/template.html rather than
src/resources/..., referencing the existing symbols __dirname,
fileURLToPath(import.meta.url), dirname, templatePath, and path.resolve to
locate the change.
🪄 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: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 38469b34-be9c-432c-a787-978f51cfda8c
⛔ Files ignored due to path filters (1)
packages/extension-usage/package.jsonis excluded by!**/*.json
📒 Files selected for processing (3)
packages/extension-usage/resources/token-trend/template.htmlpackages/extension-usage/src/index.tspackages/extension-usage/src/renderer.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- packages/extension-usage/resources/token-trend/template.html
- 调整 chatluna-usage tokens 报表中 TPM 与 RPM 的计算方式,在统计循环内维护单分钟峰值,避免大量分钟桶通过参数展开传入 Math.max。 - 修复 token 趋势图渲染模块在 ESM 环境下的目录解析逻辑,使用 import.meta.url 时转换为所在目录。 - 调整 puppeteer 渲染临时 HTML 的清理时机,在渲染流程结束后立即删除临时文件,避免延迟定时器残留。
概览
chatluna-usage增加tokens指令,支持day、week、month、all范围参数,以及可选插件用量明细chatluna_usage表生成 token 用量统计,包含累计 token、累计请求、TPM、RPMpuppeteer作为可选服务接入;未启用时仍返回文字统计并提示需要启用 puppeteer验证
yarn workspace @root/chatluna-koishi fast-build extension-usage