fix(openai): refresh usage after quota reset

This commit is contained in:
zenor0
2026-08-27 10:09:42 +08:00
parent efb46db0a9
commit 5f09442fce
9 changed files with 66 additions and 22 deletions
@@ -656,8 +656,6 @@ import { cnQuotaCellVisible as cnQuotaCellVisibleFn, cnBalanceCellVisible as cnB
// Module-level cache shared across all AccountUsageCell instances
const _usageCache = new Map<number, { data: AccountUsageInfo; ts: number }>()
const USAGE_CACHE_TTL = 5 * 60 * 1000 // 5 minutes
// How long a quota-reset response may suppress the row-patch usage refetch.
const SUPPRESS_USAGE_REFRESH_WINDOW_MS = 5 * 1000
const props = withDefaults(
defineProps<{
@@ -699,7 +697,6 @@ const usageInfo = ref<AccountUsageInfo | null>(null)
watch(usageInfo, (usage) => {
if (usage) emit('usage-loaded', usage)
})
const suppressOpenAIUsageRefreshUntil = ref(0)
const rootRef = ref<HTMLElement | null>(null)
const isDesktopViewport = ref(
typeof window === 'undefined' ? true : window.matchMedia(desktopViewportQuery).matches
@@ -1525,11 +1522,6 @@ const quotaTotalBar = computed((): QuotaBarInfo | null => {
})
const handleQuotaResetAccountUpdated = (account: Account) => {
// The reset response already carries authoritative quota and account data.
// Avoid turning the parent patch into a second automatic /usage request.
// The suppression is time-boxed so an unhandled emit (parent that ignores
// account-updated) cannot latch it and swallow a later, unrelated refresh.
suppressOpenAIUsageRefreshUntil.value = Date.now() + SUPPRESS_USAGE_REFRESH_WINDOW_MS
emit('account-updated', account)
}
@@ -1619,10 +1611,6 @@ watch(
watch(openAIUsageRefreshKey, (nextKey, prevKey) => {
if (!prevKey || nextKey === prevKey) return
if (props.account.platform !== 'openai' || props.account.type !== 'oauth') return
if (Date.now() < suppressOpenAIUsageRefreshUntil.value) {
suppressOpenAIUsageRefreshUntil.value = 0
return
}
if (isBatchManaged.value) {
requestParentBatchUsage({ force: true })
@@ -519,7 +519,7 @@ describe('AccountUsageCell', () => {
expect(wrapper.text()).toContain('5h|0|200')
})
it('OpenAI 重置响应更新账号行时不会额外拉取 usage', async () => {
it('OpenAI 重置响应更新账号行后重新拉取 usage', async () => {
getUsage.mockResolvedValue({
five_hour: {
utilization: 0,
@@ -560,7 +560,7 @@ describe('AccountUsageCell', () => {
await wrapper.setProps({ account: updatedAccount as Account })
await flushPromises()
expect(getUsage).toHaveBeenCalledTimes(1)
expect(getUsage).toHaveBeenCalledTimes(2)
})
it('OpenAI OAuth 已限额时显示 /usage API 返回的限额数据', async () => {