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(status): 额度告警只把达到阈值的剩余百分比染红 (#5135)

Dashab113697354e
diff --git a/apps/desktop/src/renderer/components/status/TodaySpendChip.tsx b/apps/desktop/src/renderer/components/status/TodaySpendChip.tsx
index 03524f89f8..d5de500633 100644
--- a/apps/desktop/src/renderer/components/status/TodaySpendChip.tsx
+++ b/apps/desktop/src/renderer/components/status/TodaySpendChip.tsx
@@ -63,6 +63,7 @@ import {
} from '@/hooks/useRemoteDeviceUsage';
import {
isClaudeSubscriptionAlerting,
+ isClaudeUsageWindowAlerting,
matchScopedWindowForModel,
type ClaudeUsageWindow,
} from '../../../shared/claudeSubscriptionUsage';
@@ -267,6 +268,8 @@ interface ChipWindowSegment extends ChipWindowSlot {
* 而不是僵住的旧百分比, 新快照落地时由重置滚动动画揭晓。
*/
resetPending: boolean;
+ /** 本窗口自身达到告警条件:chip 只把这一段的剩余百分比染红, 其余段保持常色。 */
+ alerting: boolean;
}
// 悬念期上限常量在 quotaResetRollup.ts(tick 节奏要踩着超时边界调度, 判定与
@@ -337,6 +340,7 @@ function toCodexChipWindow(
remainingPercent: 100 - clampPercent(window.usedPercent),
resetsAtMs,
resetPending: isResetPending(resetsAtMs, nowMs),
+ alerting: false,
};
}
@@ -438,6 +442,7 @@ function getClaudeChipWindows(
remainingPercent: 100 - clampPercent(fiveHour.utilization),
resetsAtMs,
resetPending: isResetPending(resetsAtMs, nowMs),
+ alerting: isClaudeUsageWindowAlerting(fiveHour),
});
}
const weekly = resolveClaudeWeeklyWindow(snapshot, modelId, t);
@@ -462,6 +467,7 @@ function getClaudeChipWindows(
remainingPercent: 100 - clampPercent(weekly.window.utilization),
resetsAtMs,
resetPending: isResetPending(resetsAtMs, nowMs),
+ alerting: isClaudeUsageWindowAlerting(weekly.window),
});
}
return windows.map((window) => ({
@@ -664,10 +670,26 @@ function getXaiChipWindows(
remainingPercent: 100 - clampPercent(used),
resetsAtMs,
resetPending: isResetPending(resetsAtMs, nowMs),
+ alerting: isXaiSubscriptionAlerting(snapshot, nowMs),
},
];
}
+/** 文案里剩余百分比的占位符: 先整句翻译, 再把它换成独立节点, 语序跟随各语言。 */
+const REMAINING_PLACEHOLDER = '\uE000';
+
+function interpolateRemaining(text: string, remaining: React.ReactNode): React.ReactNode {
+ const index = text.indexOf(REMAINING_PLACEHOLDER);
+ if (index < 0) return text;
+ return (
+ <>
+ {text.slice(0, index)}
+ {remaining}
+ {text.slice(index + REMAINING_PLACEHOLDER.length)}
+ </>
+ );
+}
+
function renderSegmentedLabel(segments: React.ReactNode[]): React.ReactNode {
return segments.map((seg, i) => (
<React.Fragment key={i}>
@@ -1226,11 +1248,9 @@ export function TodaySpendChip({
: usesXaiQuotaForm
? 'todaySpend.xai.windowSegment'
: 'todaySpend.claude.windowSegment',
- {
- label: window.label,
- remaining: formatPercent(rollup?.percent ?? window.remainingPercent),
- },
+ { label: window.label, remaining: REMAINING_PLACEHOLDER },
);
+ const remaining = formatPercent(rollup?.percent ?? window.remainingPercent);
// 段落包一层 span 并登记元素: 撒花以「正在揭晓的这一段」的矩形为迸发范围
// (粒子沿整段文字宽度散布, 而非集中在 chip 中心一点)。
return (
@@ -1240,7 +1260,10 @@ export function TodaySpendChip({
segmentElsRef.current[window.key] = el;
}}
>
- {text}
+ {interpolateRemaining(
+ text,
+ window.alerting ? <span className="text-[var(--error-fg)]">{remaining}</span> : remaining,
+ )}
</span>
);
});
@@ -1499,21 +1522,24 @@ export function TodaySpendChip({
account.emptyText = t('todaySpend.codex.noUsageDetail');
}
- // Claude 订阅告警态: 影响当前会话的窗口 (5h / 总周限 / 当前模型 scoped) 任一逼近 /
- // 打满, 或 headers 报 rejected → chip 变 error 色 (语义豁免色, 跨主题一致)。
- // 其它模型的周限吃紧不染红 —— chip 上没有那一段, 红了也无从解释 (见
- // isClaudeSubscriptionAlerting 对 allowed_warning 的取舍)。
- const claudeSubscriptionAlerting =
- isClaudeSubscription && isClaudeSubscriptionAlerting(claudeSubscriptionUsage, modelId);
- const xaiSubscriptionAlerting =
- usesXaiQuotaForm && isXaiSubscriptionAlerting(xaiSubscriptionUsage);
+ // 告警态: 达到条件的窗口只把它自己的剩余百分比染 error 色 (语义豁免色, 跨主题一致),
+ // 其它窗口段、分隔线与任务价值保持常色。其它模型的周限吃紧不染红 —— chip 上没有
+ // 那一段, 红了也无从解释 (见 isClaudeSubscriptionAlerting 对 allowed_warning 的取舍)。
+ // 兜底: 当前会话确实受限 (headers 报 rejected, 或 scoped 周限占位时总周限告警) 但
+ // chip 上没有任何一个窗口达到条件 → 仍整条变红, 不丢受限信号。悬念期的告警窗口也算
+ // 「chip 上有」: 它的旧数据已失真, 段上显示「重置中…」不染红, 也不借兜底把整条染红。
+ const hasAlertingChipWindow = chipWindows.some((window) => window.alerting);
+ const chipAlertingFallback =
+ !hasAlertingChipWindow &&
+ ((isClaudeSubscription && isClaudeSubscriptionAlerting(claudeSubscriptionUsage, modelId)) ||
+ (usesXaiQuotaForm && isXaiSubscriptionAlerting(xaiSubscriptionUsage)));
// 与 ContextCapacityRing 视觉对齐 (h-5 = 20px) + reset button UA 默认 padding/border。
// tabular-nums 让 "$306 / $1.2k" 这类数字段的字符宽度等宽, 段间数字落点对齐。
const buttonClass = cn(
'inline-flex h-5 shrink-0 items-center',
'text-12 font-medium leading-none tabular-nums',
- claudeSubscriptionAlerting || xaiSubscriptionAlerting
+ chipAlertingFallback
? 'text-[var(--error-fg)] hover:text-[var(--error-fg-strong)]'
: 'text-[var(--msg-tool-card-chevron)] hover:text-foreground',
'border-0 bg-transparent p-0 m-0',
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 da698afaf1..b1d3fb4040 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
@@ -403,6 +403,45 @@ describe('TodaySpendChip Claude subscription popover', () => {
expect(trigger.textContent).toContain('Opus 周限 剩余 66%');
});
+ it('告警时只把达到条件的剩余百分比染红,其余段与任务价值保持常色', () => {
+ const estimatedValueMoney = usdMoney(7.82, 'value-estimate');
+ mocks.sessionUsage = {
+ actualMoney: null,
+ estimatedValueMoney,
+ totalMoney: estimatedValueMoney,
+ };
+ mocks.claudeSnapshot = {
+ source: 'oauth-endpoint',
+ fiveHour: { utilization: 88, severity: 'warning' },
+ sevenDay: { utilization: 20 },
+ };
+
+ renderClaudeSubscriptionChip();
+
+ const trigger = screen.getByRole('button', { name: '打开 Claude 用量页面' });
+ expect(trigger.className).not.toContain('error-fg');
+ const red = Array.from(trigger.querySelectorAll('[class*="error-fg"]'));
+ expect(red.map((el) => el.textContent)).toEqual(['12%']);
+ expect(trigger.textContent).toContain('5h 剩余 12%');
+ expect(trigger.textContent).toContain('周限 剩余 80%');
+ expect(trigger.textContent).toContain('本任务价值 $7.82');
+ });
+
+ it('受限但 chip 上没有达到条件的数字时整条变红兜底', () => {
+ mocks.claudeSnapshot = {
+ source: 'unified-headers',
+ rateLimitStatus: 'rejected',
+ fiveHour: { utilization: 30 },
+ sevenDay: { utilization: 20 },
+ };
+
+ renderClaudeSubscriptionChip();
+
+ const trigger = screen.getByRole('button', { name: '打开 Claude 用量页面' });
+ expect(trigger.className).toContain('error-fg');
+ expect(trigger.querySelector('[class*="error-fg"]')).toBeNull();
+ });
+
it('完整渲染 Codex app-server 的两个权威窗口', () => {
mocks.codexAuthInjection = 'oauth-bearer';
mocks.codexSnapshot = {