fix(ui): roll formatTokens over to "M" instead of rendering "1000k" (… · openclaw/openclaw@4a0cd56
ly-wang19
·
2026-06-29
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -6,6 +6,7 @@ import {
|
6 | 6 | formatMs, |
7 | 7 | formatRelativeTimestamp, |
8 | 8 | formatTimeMs, |
| 9 | +formatTokens, |
9 | 10 | formatUnknownText, |
10 | 11 | parseSessionKeyParts, |
11 | 12 | setUiTimeFormatPreference, |
@@ -217,3 +218,15 @@ describe("parseSessionKeyParts", () => {
|
217 | 218 | expect(parseSessionKeyParts("agent:main:telegram")).toBeNull(); |
218 | 219 | }); |
219 | 220 | }); |
| 221 | + |
| 222 | +describe("formatTokens", () => { |
| 223 | +it("rolls a value that rounds up to 1000k over into the M branch", () => { |
| 224 | +expect(formatTokens(999_500)).toBe("1.0M"); |
| 225 | +expect(formatTokens(999_999)).toBe("1.0M"); |
| 226 | +expect(formatTokens(999_499)).toBe("999k"); |
| 227 | +expect(formatTokens(1_000_000)).toBe("1.0M"); |
| 228 | +expect(formatTokens(12_345)).toBe("12k"); |
| 229 | +expect(formatTokens(5_500)).toBe("5.5k"); |
| 230 | +expect(formatTokens(null)).toBe("0"); |
| 231 | +}); |
| 232 | +}); |
| Original file line number | Diff line number | Diff line change |
|---|
@@ -162,7 +162,14 @@ export function formatTokens(tokens: number | null | undefined, fallback = "0"):
|
162 | 162 | } |
163 | 163 | if (tokens < 1_000_000) { |
164 | 164 | const k = tokens / 1000; |
165 | | -return k < 10 ? `${k.toFixed(1)}k` : `${Math.round(k)}k`; |
| 165 | +if (k < 10) { |
| 166 | +return `${k.toFixed(1)}k`; |
| 167 | +} |
| 168 | +const rounded = Math.round(k); |
| 169 | +// 999_500..999_999 rounds to 1000k; roll it over to the M branch instead of emitting "1000k". |
| 170 | +if (rounded < 1000) { |
| 171 | +return `${rounded}k`; |
| 172 | +} |
166 | 173 | } |
167 | 174 | const m = tokens / 1_000_000; |
168 | 175 | return m < 10 ? `${m.toFixed(1)}M` : `${Math.round(m)}M`; |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。