Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ select.cc-input{appearance:none;-webkit-appearance:none;-moz-appearance:none;box
.cc-usageHint{color:var(--dsw-alias-label-tertiary);margin:0;font-size:12px;line-height:1.5}
.cc-usageError{align-items:center;gap:8px;color:var(--dsw-alias-label-error);margin:0;font-size:12px;line-height:1.5;display:flex}
.cc-usageStats{grid-template-columns:repeat(auto-fit,minmax(120px,1fr));gap:8px;display:grid}
.cc-usageStat{border:1px solid var(--dsw-alias-border-l2);background:var(--dsw-alias-bg-layer-1);border-radius:8px;padding:8px 10px;flex-direction:column;gap:2px;display:flex}
.cc-usageStat{border:1px solid var(--dsw-alias-border-l2);background:var(--dsw-alias-bg-layer-1);border-radius:8px;padding:8px 10px;flex-direction:column;gap:2px;display:flex;min-width:0}
.cc-usageStatLabel{color:var(--dsw-alias-label-tertiary);font-size:11px;line-height:1.5}
.cc-usageStatValue{color:var(--dsw-alias-label-primary);font-size:15px;font-weight:600;line-height:1.4}
.cc-usageStatValue{color:var(--dsw-alias-label-primary);font-size:15px;font-weight:600;line-height:1.4;min-width:0;overflow-wrap:anywhere}
.cc-usageStatSub{color:var(--dsw-alias-label-tertiary);font-size:11px;line-height:1.5}
.cc-usageWindows{flex-direction:column;gap:16px;display:flex}
.cc-usageWindow{flex-direction:column;gap:6px;display:flex}
Expand Down
4 changes: 2 additions & 2 deletions src/client/section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import type { AccountItemState, CatalogModelOption, RuleItemState, SettingsPageS
import type { CommandCodeLoginFailureReason } from '../login-wire.ts'
import type { LoginPageState } from './login.ts'
import type { UsagePageState } from './usage.ts'
import { formatMoney, formatMoneyExact, formatResetAt, formatTokensCompact, windowRatio } from './usage.ts'
import { formatMoney, formatMoneyExact, formatPercent, formatResetAt, formatTokensCompact, windowRatio } from './usage.ts'
import { PLUGIN_RELEASES_URL, PLUGIN_VERSION } from './version.ts'
import { checkForUpdate, localStorageUpdateStore } from './update.ts'

Expand Down Expand Up @@ -540,7 +540,7 @@ function AccountReport({ entry, fetchedAt, t, onRemove }: {
value={String(report.usage.completedCount)}
sub={`${t('usageFailed')} ${report.usage.failedCount}`}
/>
<UsageStat label={t('usageSuccessRate')} value={`${report.usage.successRate}%`} />
<UsageStat label={t('usageSuccessRate')} value={formatPercent(report.usage.successRate)} />
<UsageStat
label={t('usageCost')}
value={formatMoneyExact(report.usage.totalCost)}
Expand Down
5 changes: 5 additions & 0 deletions src/client/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ export function formatTokensCompact(value: number): string {
return String(value)
}

/** Format a rate as a percentage with 2 decimals (99.99% style). */
export function formatPercent(value: number): string {
return `${value.toFixed(2)}%`
}

/** One window's fill ratio in [0, 1]; 0 when uncapped. */
export function windowRatio(used: number, cap: number): number {
if (cap <= 0) return 0
Expand Down
2 changes: 1 addition & 1 deletion src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function renderReport(report: CommandCodeUsageReport, locale: LocaleId, title?:
commandCopy(locale, 'requestsLine')
.replace('{n}', String(u.completedCount))
.replace('{f}', String(u.failedCount))
.replace('{r}', String(u.successRate)),
.replace('{r}', u.successRate.toFixed(2)),
commandCopy(locale, 'costLine')
.replace('{money}', money(u.totalCost))
.replace('{credits}', moneyShort(u.totalCredits)),
Expand Down
4 changes: 2 additions & 2 deletions tests/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ test('command renders a full usage report in zh', async () => {
assert.equal(result.kind, 'success')
assert.match(result.text, /mars-sea/)
assert.match(result.text, /935 次/)
assert.match(result.text, /成功率 100%/)
assert.match(result.text, /成功率 100.00%/)
assert.match(result.text, /\$1\.3187/)
assert.match(result.text, /\$8\.68/)
assert.match(result.text, /5 小时/)
Expand Down Expand Up @@ -232,7 +232,7 @@ test('command renders a full usage report in en', async () => {
assert.equal(result.kind, 'success')
assert.match(result.text, /mars-sea/)
assert.match(result.text, /Requests 935/)
assert.match(result.text, /success rate 100%/)
assert.match(result.text, /success rate 100.00%/)
assert.match(result.text, /\$1\.3187/)
assert.match(result.text, /\$8\.68/)
assert.match(result.text, /5-hour/)
Expand Down
8 changes: 8 additions & 0 deletions tests/usage-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
CommandCodeUsageController,
formatMoney,
formatMoneyExact,
formatPercent,
formatResetAt,
formatTokensCompact,
windowRatio,
Expand Down Expand Up @@ -148,6 +149,13 @@ test('formatTokensCompact renders K/M/B suffixes', () => {
assert.equal(formatTokensCompact(3_000_000_000), '3.0B')
})

test('formatPercent renders a percentage with 2 decimals', () => {
assert.equal(formatPercent(99.9882890268181), '99.99%')
assert.equal(formatPercent(100), '100.00%')
assert.equal(formatPercent(99), '99.00%')
assert.equal(formatPercent(0), '0.00%')
})

test('windowRatio clamps into [0, 1] and treats cap 0 as empty', () => {
assert.equal(windowRatio(1.5, 5), 0.3)
assert.equal(windowRatio(10, 5), 1)
Expand Down