Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

💄 style: shorten usage token details (#14849)

YuTengjing93492382cab3
diff --git a/src/features/Conversation/Messages/components/Extras/Usage/UsageDetail/TokenProgress.test.ts b/src/features/Conversation/Messages/components/Extras/Usage/UsageDetail/TokenProgress.test.ts
new file mode 100644
index 00000000000..4bed73159b3
--- /dev/null
+++ b/src/features/Conversation/Messages/components/Extras/Usage/UsageDetail/TokenProgress.test.ts
@@ -0,0 +1,28 @@
+import { describe, expect, it } from 'vitest';
+
+import { formatUsageValue } from './TokenProgress';
+
+describe('formatUsageValue', () => {
+ it('formats token usage details with short units', () => {
+ expect(formatUsageValue(93_405)).toBe('93.4K');
+ expect(formatUsageValue(92_119)).toBe('92.1K');
+ expect(formatUsageValue(3_488)).toBe('3.5K');
+ expect(formatUsageValue(189_018)).toBe('189K');
+ });
+
+ it('formats credit usage details with the same short units', () => {
+ expect(formatUsageValue(16_127)).toBe('16.1K');
+ expect(formatUsageValue(16_179)).toBe('16.2K');
+ });
+
+ it('keeps small token counts readable without suffixes', () => {
+ expect(formatUsageValue(0)).toBe('0');
+ expect(formatUsageValue(6)).toBe('6');
+ expect(formatUsageValue(999)).toBe('999');
+ });
+
+ it('formats million-level token counts with M suffix', () => {
+ expect(formatUsageValue(1_000_000)).toBe('1M');
+ expect(formatUsageValue(1_500_000)).toBe('1.5M');
+ });
+});
diff --git a/src/features/Conversation/Messages/components/Extras/Usage/UsageDetail/TokenProgress.tsx b/src/features/Conversation/Messages/components/Extras/Usage/UsageDetail/TokenProgress.tsx
index 7a86252a3d0..c194175c785 100644
--- a/src/features/Conversation/Messages/components/Extras/Usage/UsageDetail/TokenProgress.tsx
+++ b/src/features/Conversation/Messages/components/Extras/Usage/UsageDetail/TokenProgress.tsx
@@ -15,7 +15,11 @@ interface TokenProgressProps {
showIcon?: boolean;
}
-const format = (number: number) => numeral(number).format('0,0');
+export const formatUsageValue = (number: number) => {
+ if (number >= 1_000_000) return numeral(number / 1_000_000).format('0.[0]') + 'M';
+ if (number >= 1_000) return numeral(number / 1_000).format('0.[0]') + 'K';
+ return numeral(number).format('0,0');
+};
const TokenProgress = memo<TokenProgressProps>(({ data, showIcon }) => {
const total = data.reduce((acc, item) => acc + item.value, 0);
@@ -58,7 +62,7 @@ const TokenProgress = memo<TokenProgressProps>(({ data, showIcon }) => {
)}
<div style={{ color: cssVar.colorTextSecondary }}>{item.title}</div>
</Flexbox>
- <div style={{ fontWeight: 500 }}>{format(item.value)}</div>
+ <div style={{ fontWeight: 500 }}>{formatUsageValue(item.value)}</div>
</Flexbox>
))}
</Flexbox>
diff --git a/src/features/Conversation/Messages/components/Extras/Usage/UsageDetail/index.tsx b/src/features/Conversation/Messages/components/Extras/Usage/UsageDetail/index.tsx
index 1c2bc04c538..8173ad3912d 100644
--- a/src/features/Conversation/Messages/components/Extras/Usage/UsageDetail/index.tsx
+++ b/src/features/Conversation/Messages/components/Extras/Usage/UsageDetail/index.tsx
@@ -14,8 +14,8 @@ import { formatNumber, formatShortenNumber } from '@/utils/format';
import AnimatedNumber from './AnimatedNumber';
import ModelCard from './ModelCard';
-import { type TokenProgressItem } from './TokenProgress';
-import TokenProgress from './TokenProgress';
+import type { TokenProgressItem } from './TokenProgress';
+import TokenProgress, { formatUsageValue } from './TokenProgress';
import { getDetailsToken } from './tokens';
interface TokenDetailProps {
@@ -127,7 +127,7 @@ const TokenDetail = memo<TokenDetailProps>(({ usage, performance, model, provide
? detailTokens.totalTokens.credit
: detailTokens.totalTokens!.token;
- const detailTotal = formatNumber(totalCount);
+ const detailTotal = formatUsageValue(totalCount);
const averagePricing = formatNumber(
detailTokens.totalTokens!.credit / detailTokens.totalTokens!.token,