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): 保周用量缓存、过期催刷新,并在失败时 abort 在途配额请求

Dashecd36b361717
diff --git a/apps/desktop/src/main/usage/__tests__/xaiSubscriptionUsage.test.ts b/apps/desktop/src/main/usage/__tests__/xaiSubscriptionUsage.test.ts
index b326ca3780..e1424053e8 100644
--- a/apps/desktop/src/main/usage/__tests__/xaiSubscriptionUsage.test.ts
+++ b/apps/desktop/src/main/usage/__tests__/xaiSubscriptionUsage.test.ts
@@ -66,13 +66,15 @@ describe('fetchXaiSubscriptionUsageSnapshot', () => {
expect(JSON.stringify(snapshot)).not.toContain('keep-out');
});
- it('still returns plan-only data when userinfo fails', async () => {
+ it('still returns weekly data when userinfo fails', async () => {
const fetchFn = vi.fn(async (url: string) => {
if (url.includes('/user?')) return jsonResponse(200, { userId: 'user-1' });
if (url.includes('/settings')) {
return jsonResponse(200, { subscription_tier_display: 'SuperGrok' });
}
- if (url.includes('format=credits')) return jsonResponse(500, {});
+ if (url.includes('format=credits')) {
+ return jsonResponse(200, { config: { creditUsagePercent: 2 } });
+ }
if (url.includes('/userinfo')) return jsonResponse(500, {});
return jsonResponse(404, {});
});
@@ -82,10 +84,26 @@ describe('fetchXaiSubscriptionUsageSnapshot', () => {
});
expect(snapshot).toMatchObject({
planLabel: 'SuperGrok',
+ creditUsagePercent: 2,
accountFingerprint: null,
});
});
+ it('does not replace cache with plan-only data when billing fails', async () => {
+ const fetchFn = vi.fn(async (url: string) => {
+ if (url.includes('/settings')) {
+ return jsonResponse(200, { subscription_tier_display: 'SuperGrok Heavy' });
+ }
+ if (url.includes('format=credits')) return jsonResponse(503, {});
+ if (url.includes('/user?') || url.includes('/userinfo')) return jsonResponse(200, {});
+ return jsonResponse(404, {});
+ });
+ await expect(fetchXaiSubscriptionUsageSnapshot({
+ accessToken: 'tok',
+ fetchFn: fetchFn as unknown as typeof fetch,
+ })).resolves.toBeNull();
+ });
+
it('keeps quota when identity endpoints fail', async () => {
const fetchFn = vi.fn(async (url: string) => {
if (url.includes('/user?')) return jsonResponse(401, { error: 'nope' });
diff --git a/apps/desktop/src/main/usage/xaiSubscriptionUsage.ts b/apps/desktop/src/main/usage/xaiSubscriptionUsage.ts
index c84bed5336..eed2530464 100644
--- a/apps/desktop/src/main/usage/xaiSubscriptionUsage.ts
+++ b/apps/desktop/src/main/usage/xaiSubscriptionUsage.ts
@@ -172,9 +172,13 @@ export async function fetchXaiSubscriptionUsageSnapshot(opts: {
accountFingerprint: subject ? fingerprintXaiSubject(subject) : null,
now,
});
+ if (!credits.ok) {
+ // billing 瞬时失败时只剩套餐名,不能当全量快照盖掉上次的周百分比/重置。
+ return null;
+ }
if (!snapshot) {
// 两个权威端点都成功但解析不出字段 → empty(清缓存)。任一非 ok 当瞬时失败保缓存。
- if (settings.ok && credits.ok) {
+ if (settings.ok) {
log.warn('xAI subscription usage response had no parsable plan or weekly window');
return XAI_SUBSCRIPTION_USAGE_EMPTY;
}
@@ -195,6 +199,7 @@ export async function fetchXaiSubscriptionUsageSnapshot(opts: {
} finally {
clearTimeout(quotaTimeout);
clearTimeout(identityTimeout);
+ if (!quotaController.signal.aborted) quotaController.abort();
if (!identityController.signal.aborted) identityController.abort();
}
}
diff --git a/apps/desktop/src/renderer/__tests__/todaySpendChip.test.ts b/apps/desktop/src/renderer/__tests__/todaySpendChip.test.ts
index 5863a2666c..075608cc8a 100644
--- a/apps/desktop/src/renderer/__tests__/todaySpendChip.test.ts
+++ b/apps/desktop/src/renderer/__tests__/todaySpendChip.test.ts
@@ -406,6 +406,7 @@ describe('TodaySpendChip dashboard routing', () => {
expect(source).toContain(
'const hasPendingResetWindow = chipWindows.some((window) => window.resetPending);',
);
+ expect(source).toContain('const xaiWeeklyStale = usesXaiQuotaForm');
});
it('does not treat missing subscription credits as exhausted usage', () => {
diff --git a/apps/desktop/src/renderer/components/settings/ProvidersSection.tsx b/apps/desktop/src/renderer/components/settings/ProvidersSection.tsx
index f0f991d179..e2d2c5adba 100644
--- a/apps/desktop/src/renderer/components/settings/ProvidersSection.tsx
+++ b/apps/desktop/src/renderer/components/settings/ProvidersSection.tsx
@@ -71,7 +71,10 @@ import {
import { BILLING_CURRENCY, formatBillingAmount } from '@/features/billing/money';
import { canAccessBillingSettings } from './billingVisibility';
import { resolveXdAssetModuleState } from './providerAssetModule';
-import { useXaiSubscriptionUsage } from '@/hooks/useXaiSubscriptionUsage';
+import {
+ requestXaiSubscriptionRefresh,
+ useXaiSubscriptionUsage,
+} from '@/hooks/useXaiSubscriptionUsage';
import {
formatXaiProductLabel,
isXaiWeeklyUsageCurrent,
@@ -798,6 +801,11 @@ function XaiAssetModule({ connected }: { connected: boolean }) {
const timer = window.setInterval(() => setNowMs(Date.now()), 60_000);
return () => window.clearInterval(timer);
}, [connected]);
+ useEffect(() => {
+ if (!connected || !usage) return;
+ if (isXaiWeeklyUsageCurrent(usage, nowMs)) return;
+ requestXaiSubscriptionRefresh();
+ }, [connected, usage, nowMs]);
if (!connected || !usage) return null;
const hasWeekly = isXaiWeeklyUsageCurrent(usage, nowMs);
if (!usage.planLabel && !hasWeekly) return null;
diff --git a/apps/desktop/src/renderer/components/status/TodaySpendChip.tsx b/apps/desktop/src/renderer/components/status/TodaySpendChip.tsx
index 329bb05f83..37e1a511e5 100644
--- a/apps/desktop/src/renderer/components/status/TodaySpendChip.tsx
+++ b/apps/desktop/src/renderer/components/status/TodaySpendChip.tsx
@@ -1538,8 +1538,11 @@ export function TodaySpendChip({
// 且无空闲期 app-server 催刷通道, 靠下一个 turn 事件或悬念超时回落兜底;
// - Claude 订阅形态催订阅端点。
const hasPendingResetWindow = chipWindows.some((window) => window.resetPending);
+ const xaiWeeklyStale = usesXaiQuotaForm
+ && Boolean(xaiSubscriptionUsage)
+ && !isXaiWeeklyUsageCurrent(xaiSubscriptionUsage, windowLabelNowMs);
React.useEffect(() => {
- if (!hasPendingResetWindow) return;
+ if (!hasPendingResetWindow && !xaiWeeklyStale) return;
if (isChatgptBridge) {
requestCodexAccountRefresh();
} else if (usesXaiQuotaForm) {
@@ -1547,7 +1550,7 @@ export function TodaySpendChip({
} else if (isClaudeSubscription && !usesCodexQuotaForm) {
requestClaudeSubscriptionRefresh();
}
- }, [hasPendingResetWindow, isChatgptBridge, isClaudeSubscription, usesCodexQuotaForm, usesXaiQuotaForm, windowLabelNowMs]);
+ }, [hasPendingResetWindow, xaiWeeklyStale, isChatgptBridge, isClaudeSubscription, usesCodexQuotaForm, usesXaiQuotaForm, windowLabelNowMs]);
const isDashboardClickable = usageDashboardUrl !== null;
const handleClick = () => {