feat(chat): live token counter in composer - #524
Open
chujieHong wants to merge 1 commit into
Open
Conversation
在 ChatInputActionsRow 输入框右下角加一个 BPE token 计数 chip,
点击展开 popover 显示本轮用量明细(input / output / cache hit /
cache write / total / model calls)和缓存命中率。
后端零改动。
实现要点:
- estimateTokens 用 js-tiktoken/lite + cl100k_base ranks,cl100k_base 与
GPT-3.5/4 token 数 1:1 对齐;o200k_base 留给 GPT-4o 单独引入
- 计数懒加载,首次 ~250ms 冷启动,后续 < 0.5ms
- popover 数据从 chatStore.runUsage 通过 useSyncExternalStore 订阅,
SSE 用量 chunk 实时刷新
- showUsage = model_calls > 0,新 thread 不弹空 popover
- 缓存命中率 = cache_read / (cache_read + uncached_input),
OpenAI 官方口径;无缓存字段时显示 —
触碰文件:
- 新增: dashboard/src/utils/estimateTokens.ts + .test.ts
- 新增: dashboard/src/pages/Chat/components/TokenCounter.tsx + .test.tsx
- 修改: dashboard/package.json + package-lock.json (加 js-tiktoken 依赖)
- 修改: dashboard/vite.config.ts (vendor-tiktoken 分块)
- 修改: dashboard/src/locales/{en,zh}.json (7 条 i18n key)
- 修改: dashboard/src/pages/Chat/chatInputCore.partial.less (chip + panel + hint)
- 修改: dashboard/src/pages/Chat/components/ChatInputActionsRow.tsx (渲染 TokenCounter)
bundle 影响: vendor-tiktoken 1.10 MB raw / 505 KB gzip
(SW precache 已验证通过)
Collaborator
|
评估结论:不建议按现状合并。 实现能跑、测试也有,但解决不了「上下文会不会爆 / 这一轮花了多少」这类问题,和现有 UI 重叠,还要给 PWA 多 precache 大约 505KB gzip 的词表。 和现有能力重叠
主要问题
建议
|
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.
在 ChatInputActionsRow 输入框右下角加一个 BPE token 计数 chip, 点击展开 popover 显示本轮用量明细(input / output / cache hit / cache write / total / model calls)和缓存命中率。
后端零改动。
实现要点:
触碰文件:
bundle 影响: vendor-tiktoken 1.10 MB raw / 505 KB gzip (SW precache 已验证通过)
Summary
Target branch
develop(feature / fix — default)main(release/*orhotfix/*only)Type of change
Test plan
make allpasses locallyChecklist
CHANGELOG.md(if user-facing)