Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): 周限卡片改为显示剩余额度 (#4008)

Dash41610a9b2482
diff --git a/apps/desktop/src/renderer/__tests__/todaySpendChip.test.ts b/apps/desktop/src/renderer/__tests__/todaySpendChip.test.ts
index 35ec1ff8b3..a5bca4c068 100644
--- a/apps/desktop/src/renderer/__tests__/todaySpendChip.test.ts
+++ b/apps/desktop/src/renderer/__tests__/todaySpendChip.test.ts
@@ -361,8 +361,14 @@ describe('shared usage-card provider projections', () => {
);
expect(card.windows.map((window) => window.window.utilization)).toEqual([9]);
expect(card.windows[0].breakdown).toEqual([
- { label: 'quotaCard.includedLabel:{"name":"Grok Build"}', value: '2%' },
- { label: 'quotaCard.includedLabel:{"name":"Other Product"}', value: '7%' },
+ {
+ label: 'quotaCard.includedLabel:{"name":"Grok Build"}',
+ value: 'quotaCard.usedPercent:{"percent":2}',
+ },
+ {
+ label: 'quotaCard.includedLabel:{"name":"Other Product"}',
+ value: 'quotaCard.usedPercent:{"percent":7}',
+ },
]);
expect(card.windows[0].detail).toBe('todaySpend.xai.accountWeeklyHint');
expect(card.details).toEqual(
diff --git a/apps/desktop/src/renderer/components/status/QuotaBar.tsx b/apps/desktop/src/renderer/components/status/QuotaBar.tsx
index faa8f7a418..81fe677771 100644
--- a/apps/desktop/src/renderer/components/status/QuotaBar.tsx
+++ b/apps/desktop/src/renderer/components/status/QuotaBar.tsx
@@ -10,6 +10,9 @@ export type QuotaSeverity = 'normal' | 'warn' | 'crit';
export interface QuotaBarProps {
usedPercent: number;
+ /** Invert only the displayed value; warnings still follow usage. */
+ showRemaining?: boolean;
+ 'aria-valuetext'?: string;
size?: 'regular' | 'mini';
/** 调用方已合并本地阈值与上游信号后的展示级别。 */
severity?: QuotaSeverity;
@@ -40,6 +43,8 @@ const FILL_COLOR_CLASSES: Record<QuotaSeverity, string> = {
export function QuotaBar({
usedPercent,
+ showRemaining = false,
+ 'aria-valuetext': ariaValueText,
size = 'regular',
severity: severityOverride,
ariaLabel,
@@ -47,6 +52,7 @@ export function QuotaBar({
className,
}: QuotaBarProps) {
const clampedPercent = clampPercent(usedPercent);
+ const displayedPercent = showRemaining ? 100 - clampedPercent : clampedPercent;
const severity = severityOverride ?? quotaSeverity(clampedPercent);
const isMini = size === 'mini';
@@ -55,7 +61,8 @@ export function QuotaBar({
role="progressbar"
aria-valuemin={0}
aria-valuemax={100}
- aria-valuenow={Math.round(clampedPercent)}
+ aria-valuenow={Math.round(displayedPercent)}
+ aria-valuetext={ariaValueText}
aria-label={ariaLabel}
aria-labelledby={ariaLabelledBy}
data-severity={severity}
@@ -68,10 +75,10 @@ export function QuotaBar({
<div
className={cn(
'h-full rounded-full transition-[width] duration-[var(--motion-base)] ease-[var(--motion-ease-move)] motion-reduce:transition-none',
- clampedPercent > 0 && (isMini ? 'min-w-[4px]' : 'min-w-[7px]'),
+ displayedPercent > 0 && (isMini ? 'min-w-[4px]' : 'min-w-[7px]'),
FILL_COLOR_CLASSES[severity],
)}
- style={{ width: `${clampedPercent}%` }}
+ style={{ width: `${displayedPercent}%` }}
/>
</div>
);
diff --git a/apps/desktop/src/renderer/components/status/QuotaHoverCard.tsx b/apps/desktop/src/renderer/components/status/QuotaHoverCard.tsx
index 2d887a4a2f..4d79f0e6c5 100644
--- a/apps/desktop/src/renderer/components/status/QuotaHoverCard.tsx
+++ b/apps/desktop/src/renderer/components/status/QuotaHoverCard.tsx
@@ -106,6 +106,7 @@ function formatPaceLine(pace: QuotaPace, t: TFunction): string {
function WindowBlock({
title,
window,
+ showRemaining = false,
paceWindowMinutes,
detail,
breakdown,
@@ -116,6 +117,7 @@ function WindowBlock({
}: {
title: string;
window: UsageCardWindow['window'];
+ showRemaining?: boolean;
paceWindowMinutes?: number;
detail?: string;
breakdown?: UsageCardWindow['breakdown'];
@@ -126,6 +128,9 @@ function WindowBlock({
}) {
const titleId = React.useId();
const usedPercent = clampPercent(window.utilization);
+ const percentText = t(showRemaining ? 'quotaCard.remainingPercent' : 'quotaCard.usedPercent', {
+ percent: Math.round(showRemaining ? 100 - usedPercent : usedPercent),
+ });
const severity = effectiveQuotaSeverity(window);
const severityAnnouncement =
severity === 'crit'
@@ -166,11 +171,15 @@ function WindowBlock({
<span className="sr-only">,{severityAnnouncement}</span>
) : null}
</div>
- <QuotaBar usedPercent={window.utilization} severity={severity} aria-labelledby={titleId} />
+ <QuotaBar
+ usedPercent={window.utilization}
+ showRemaining={showRemaining}
+ severity={severity}
+ aria-labelledby={titleId}
+ aria-valuetext={percentText}
+ />
<div className="mt-[7px] flex items-baseline justify-between gap-3 tabular-nums">
- <span className="font-medium text-[var(--text-primary)]">
- {t('quotaCard.usedPercent', { percent: Math.round(usedPercent) })}
- </span>
+ <span className="font-medium text-[var(--text-primary)]">{percentText}</span>
{resetAt !== null ? (
<span className="text-12 text-[var(--text-secondary)]">
{t('quotaCard.resetAt', { at: resetAt })}
diff --git a/apps/desktop/src/renderer/components/status/__tests__/QuotaHoverCard.test.tsx b/apps/desktop/src/renderer/components/status/__tests__/QuotaHoverCard.test.tsx
index 16cd15a9ec..35f0be5401 100644
--- a/apps/desktop/src/renderer/components/status/__tests__/QuotaHoverCard.test.tsx
+++ b/apps/desktop/src/renderer/components/status/__tests__/QuotaHoverCard.test.tsx
@@ -14,6 +14,7 @@ vi.mock('react-i18next', () => ({
if (key === 'quotaCard.modelWeeklyLabel') return `${options.model} 周限`;
if (key === 'quotaCard.windowsRegionLabel') return '配额窗口列表';
if (key === 'quotaCard.usedPercent') return `已用 ${options.percent}%`;
+ if (key === 'quotaCard.remainingPercent') return `剩余 ${options.percent}%`;
if (key === 'quotaCard.usageCritical') return '用量较高';
if (key === 'quotaCard.usageWarning') return '用量偏高';
if (key === 'quotaCard.limitRejected') return '已触发套餐限额,请求可能被拒绝';
@@ -139,9 +140,9 @@ describe('QuotaHoverCard', () => {
expect(screen.getByText('Fable 周限')).toBeTruthy();
expect(screen.getByText('Opus 周限')).toBeTruthy();
expect(screen.getByText('已用 1%')).toBeTruthy();
- expect(screen.getByText('已用 4%')).toBeTruthy();
- expect(screen.getByText('已用 0%')).toBeTruthy();
- expect(screen.getByText('已用 76%')).toBeTruthy();
+ expect(screen.getByText('剩余 96%')).toBeTruthy();
+ expect(screen.getByText('剩余 100%')).toBeTruthy();
+ expect(screen.getByText('剩余 24%')).toBeTruthy();
expect(screen.getByText('17:05 重置')).toBeTruthy();
expect(screen.getByText('8月7日 00:00 重置')).toBeTruthy();
expect(screen.getByText('8月6日 23:59 重置')).toBeTruthy();
@@ -229,7 +230,7 @@ describe('QuotaHoverCard', () => {
/>,
);
- expect(screen.getByText('已用 30%')).toBeTruthy();
+ expect(screen.getByText('剩余 70%')).toBeTruthy();
expect(screen.queryByText(/重置$/)).toBeNull();
});
@@ -244,6 +245,27 @@ describe('QuotaHoverCard', () => {
expect(screen.getByText('已用 100%')).toBeTruthy();
});
+ it.each([
+ { utilization: 94, remaining: 6, severity: 'crit' },
+ { utilization: 100, remaining: 0, severity: 'crit' },
+ { utilization: 250, remaining: 0, severity: 'crit' },
+ { utilization: 0, remaining: 100, severity: 'normal' },
+ { utilization: -5, remaining: 100, severity: 'normal' },
+ ])(
+ 'shows $remaining% remaining for weekly usage $utilization without reversing warnings',
+ ({ utilization, remaining, severity }) => {
+ render(
+ <QuotaHoverCard nowMs={NOW_MS} snapshot={makeSnapshot({ sevenDay: { utilization } })} />,
+ );
+ const bar = screen.getByRole('progressbar');
+ expect(screen.getByText(`剩余 ${remaining}%`)).toBeTruthy();
+ expect(bar.getAttribute('aria-valuenow')).toBe(String(remaining));
+ expect(bar.getAttribute('aria-valuetext')).toBe(`剩余 ${remaining}%`);
+ expect((bar.firstElementChild as HTMLElement).style.width).toBe(`${remaining}%`);
+ expect(bar.getAttribute('data-severity')).toBe(severity);
+ },
+ );
+
it('omits a null subscription badge and maps max to Max', () => {
const { rerender } = render(
<QuotaHoverCard snapshot={makeSnapshot({ subscriptionType: null })} nowMs={NOW_MS} />,
diff --git a/apps/desktop/src/renderer/components/status/__tests__/TodaySpendChip.popover.test.tsx b/apps/desktop/src/renderer/components/status/__tests__/TodaySpendChip.popover.test.tsx
index c71934ba86..3ad488d8ec 100644
--- a/apps/desktop/src/renderer/components/status/__tests__/TodaySpendChip.popover.test.tsx
+++ b/apps/desktop/src/renderer/components/status/__tests__/TodaySpendChip.popover.test.tsx
@@ -60,6 +60,7 @@ vi.mock('react-i18next', () => ({
'quotaCard.includedLabel': '其中 {{name}}',
'quotaCard.modelWeeklyLabel': '{{model}} 周限',
'quotaCard.usedPercent': '已用 {{percent}}%',
+ 'quotaCard.remainingPercent': '剩余 {{percent}}%',
'quotaCard.resetAt': '{{at}} 重置',
'quotaCard.turnCostUnavailable': '本轮费用暂无法估算',
'quotaCard.tokenLabel': 'Token',
@@ -808,7 +809,8 @@ describe('TodaySpendChip Claude subscription popover', () => {
within(card)
.getAllByRole('progressbar')
.map((bar) => bar.getAttribute('aria-valuenow')),
- ).toEqual(['22', '48']);
+ ).toEqual(['22', '52']);
+ expect(within(card).getByText('剩余 52%')).toBeTruthy();
});
it('Grok 产品用量属于周限明细,不重复进度条或重置时间,过期后移除旧百分比', () => {
@@ -826,7 +828,9 @@ describe('TodaySpendChip Claude subscription popover', () => {
expect(screen.getAllByRole('progressbar')).toHaveLength(1);
const breakdown = screen.getByTestId('quota-window-breakdown');
expect(within(breakdown).getByText('其中 Grok Build')).toBeTruthy();
- expect(within(breakdown).getByText('8%')).toBeTruthy();
+ expect(within(breakdown).getByText('已用 8%')).toBeTruthy();
+ expect(screen.getByRole('progressbar').getAttribute('aria-valuenow')).toBe('92');
+ expect(screen.getByText('剩余 92%')).toBeTruthy();
expect(screen.getAllByText(/重置$/)).toHaveLength(1);
mocks.xaiSnapshot = { ...mocks.xaiSnapshot, updatedAt: Date.now() - 31 * 60_000 };
view.rerender(
diff --git a/apps/desktop/src/renderer/components/status/usageCardModel.ts b/apps/desktop/src/renderer/components/status/usageCardModel.ts
index 73711a675e..6a03e57374 100644
--- a/apps/desktop/src/renderer/components/status/usageCardModel.ts
+++ b/apps/desktop/src/renderer/components/status/usageCardModel.ts
@@ -18,6 +18,8 @@ import {
export interface UsageCardWindow {
key: string;
title: string;
+ /** Weekly quotas show the available percentage; other windows retain used percentage. */
+ showRemaining?: boolean;
window: {
utilization: number;
/** Epoch seconds, matching provider reset timestamps. */
@@ -77,7 +79,7 @@ export function buildClaudeUsageCard(
paceWindowMinutes?: number,
) => {
if (window && Number.isFinite(window.utilization))
- windows.push({ key, title, window, paceWindowMinutes });
+ windows.push({ key, title, window, paceWindowMinutes, showRemaining: key !== 'five-hour' });
};
add('five-hour', t('quotaCard.fiveHourLabel'), snapshot.fiveHour);
add('seven-day', t('quotaCard.weeklyLabel'), snapshot.sevenDay, 10_080);
@@ -132,6 +134,7 @@ export function buildCodexUsageCard(
windows.push({
key,
title: quotaWindowLabel(window.windowMinutes, t),
+ showRemaining: window.windowMinutes === 10_080,
window: { utilization: window.usedPercent, resetsAt: window.resetsAt },
paceWindowMinutes: window.windowMinutes === 10_080 ? 10_080 : undefined,
});
@@ -199,6 +202,7 @@ export function buildXaiUsageCard(
windows.push({
key: 'weekly',
title: t('quotaCard.weeklyLabel'),
+ showRemaining: true,
window: { utilization: weekly.creditUsagePercent, resetsAt: weekly.resetsAt },
paceWindowMinutes: 10_080,
detail: t('todaySpend.xai.accountWeeklyHint'),
@@ -206,7 +210,9 @@ export function buildXaiUsageCard(
.filter((product) => Number.isFinite(product.usagePercent))
.map((product) => ({
label: t('quotaCard.includedLabel', { name: formatXaiProductLabel(product.product) }),
- value: `${Math.round(Math.min(100, Math.max(0, product.usagePercent)))}%`,
+ value: t('quotaCard.usedPercent', {
+ percent: Math.round(Math.min(100, Math.max(0, product.usagePercent))),
+ }),
})),
});
if (
diff --git a/apps/desktop/src/renderer/i18n/locales/en/common.json b/apps/desktop/src/renderer/i18n/locales/en/common.json
index 654c41bf89..a1b7f2092f 100644
--- a/apps/desktop/src/renderer/i18n/locales/en/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/en/common.json
@@ -5874,6 +5874,7 @@
"weeklyLabel": "Weekly",
"modelWeeklyLabel": "{{model}} weekly",
"usedPercent": "Used {{percent}}%",
+ "remainingPercent": "Remaining {{percent}}%",
"resetAt": "Resets {{at}}",
"noWindows": "No quota window data available",
"limitRejected": "Plan limit reached; requests may be rejected",
diff --git a/apps/desktop/src/renderer/i18n/locales/ja/common.json b/apps/desktop/src/renderer/i18n/locales/ja/common.json
index 9846c6a192..cc3a1afe9b 100644
--- a/apps/desktop/src/renderer/i18n/locales/ja/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/ja/common.json
@@ -5872,6 +5872,7 @@
"weeklyLabel": "週間",
"modelWeeklyLabel": "{{model}} 週間",
"usedPercent": "使用済み {{percent}}%",
+ "remainingPercent": "残り {{percent}}%",
"resetAt": "{{at}} にリセット",
"noWindows": "クォータ期間のデータはありません",
"limitRejected": "プランの上限に到達しました。リクエストが拒否される可能性があります",
diff --git a/apps/desktop/src/renderer/i18n/locales/ko/common.json b/apps/desktop/src/renderer/i18n/locales/ko/common.json
index b3a13b89f9..c462057e5b 100644
--- a/apps/desktop/src/renderer/i18n/locales/ko/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/ko/common.json
@@ -5872,6 +5872,7 @@
"weeklyLabel": "주간",
"modelWeeklyLabel": "{{model}} 주간",
"usedPercent": "사용 {{percent}}%",
+ "remainingPercent": "남은 {{percent}}%",
"resetAt": "{{at}} 재설정",
"noWindows": "할당량 구간 데이터가 없습니다",
"limitRejected": "플랜 한도에 도달하여 요청이 거부될 수 있습니다",
diff --git a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
index a78ef6b66f..c7b65bbac3 100644
--- a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
@@ -5872,6 +5872,7 @@
"weeklyLabel": "周限",
"modelWeeklyLabel": "{{model}} 周限",
"usedPercent": "已用 {{percent}}%",
+ "remainingPercent": "剩余 {{percent}}%",
"resetAt": "{{at}} 重置",
"noWindows": "暂无配额窗口数据",
"limitRejected": "已触发套餐限额,请求可能被拒绝",
diff --git a/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json b/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json
index f0e2e9c6f5..3f557c8258 100644
--- a/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json
@@ -5872,6 +5872,7 @@
"weeklyLabel": "週限",
"modelWeeklyLabel": "{{model}} 週限",
"usedPercent": "已用 {{percent}}%",
+ "remainingPercent": "剩餘 {{percent}}%",
"resetAt": "{{at}} 重置",
"noWindows": "暫無配額視窗資料",
"limitRejected": "已觸發套餐限額,請求可能被拒絕",