fix(cloud): fetch AI credits on startup with endpoint fallback and preserve cached credits
This commit is contained in:
1 parent
ed0dd811a8
commit
61716bb512
5 files changed
+65
-12
No files matched your search
@@ -324,6 +324,7 @@ export function CloudAccountCard({
|
||||
);
|
||||
|
||||
const aiCredits = account.quota?.ai_credits;
|
||||
const shouldShowAiCredits = !!aiCredits && Number.isFinite(aiCredits.credits) && aiCredits.credits >= 0;
|
||||
|
||||
const formatCreditsExpiry = (expiryDate: string) => {
|
||||
if (!expiryDate) return '';
|
||||
@@ -369,7 +370,7 @@ export function CloudAccountCard({
|
||||
</CardTitle>
|
||||
<CardDescription className="truncate text-xs">{account.email}</CardDescription>
|
||||
|
||||
{aiCredits && aiCredits.credits > 0 && (
|
||||
{shouldShowAiCredits && aiCredits && (
|
||||
<div className="mt-1 flex items-center gap-1 text-[10px] font-medium text-amber-500">
|
||||
<span>${aiCredits.credits.toFixed(2)}</span>
|
||||
{aiCredits.expiryDate && (
|
||||
@@ -623,6 +624,7 @@ export function CompactCloudAccountCard({
|
||||
);
|
||||
|
||||
const aiCredits = account.quota?.ai_credits;
|
||||
const shouldShowAiCredits = !!aiCredits && Number.isFinite(aiCredits.credits) && aiCredits.credits >= 0;
|
||||
|
||||
const formatCreditsExpiry = (expiryDate: string) => {
|
||||
if (!expiryDate) return '';
|
||||
@@ -672,7 +674,7 @@ export function CompactCloudAccountCard({
|
||||
<div className="text-muted-foreground flex items-center gap-3 text-xs">
|
||||
<span className="truncate">{account.email}</span>
|
||||
|
||||
{aiCredits && aiCredits.credits > 0 && (
|
||||
{shouldShowAiCredits && aiCredits && (
|
||||
<span className="shrink-0 text-amber-500">
|
||||
${aiCredits.credits.toFixed(2)}
|
||||
{aiCredits.expiryDate && (
|
||||
|
||||
@@ -280,7 +280,21 @@ export function CloudAccountList() {
|
||||
refreshMutation.mutate(
|
||||
{ accountId: id },
|
||||
{
|
||||
onSuccess: () => toast({ title: t('cloud.toast.quotaRefreshed') }),
|
||||
onSuccess: (updatedAccount) => {
|
||||
const credits = updatedAccount.quota?.ai_credits?.credits;
|
||||
if (typeof credits === 'number') {
|
||||
toast({
|
||||
title: t('cloud.toast.quotaRefreshed'),
|
||||
description: `AI credits: $${credits.toFixed(2)}`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
toast({
|
||||
title: t('cloud.toast.quotaRefreshed'),
|
||||
description: 'AI credits not available for this refresh.',
|
||||
});
|
||||
},
|
||||
onError: () => toast({ title: t('cloud.toast.refreshFailed'), variant: 'destructive' }),
|
||||
},
|
||||
);
|
||||
|
||||
@@ -86,8 +86,18 @@ export async function addGoogleAccount(authCode: string): Promise<CloudAccount>
|
||||
|
||||
try {
|
||||
const quota = await GoogleAPIService.fetchQuota(account.token.access_token);
|
||||
try {
|
||||
const aiCredits = await GoogleAPIService.fetchAICredits(account.token.access_token);
|
||||
if (aiCredits) {
|
||||
quota.ai_credits = aiCredits;
|
||||
} else {
|
||||
logger.info(`No AI credits returned for ${account.email} during initial sync`);
|
||||
}
|
||||
} catch (e) {
|
||||
logger.warn('Failed to fetch initial AI credits', e);
|
||||
}
|
||||
account.quota = quota;
|
||||
await CloudAccountRepo.updateQuota(account.id, quota);
|
||||
await CloudAccountRepo.updateQuota(account.id, account.quota);
|
||||
notifyTrayUpdate(account);
|
||||
} catch (e) {
|
||||
logger.warn('Failed to fetch initial quota', e);
|
||||
@@ -135,9 +145,8 @@ export async function refreshAccountQuota(accountId: string): Promise<CloudAccou
|
||||
}
|
||||
|
||||
try {
|
||||
const previousAICredits = account.quota?.ai_credits;
|
||||
const quota = await GoogleAPIService.fetchQuota(account.token.access_token, account.proxy_url);
|
||||
account.quota = quota;
|
||||
await CloudAccountRepo.updateQuota(account.id, quota);
|
||||
|
||||
try {
|
||||
const aiCredits = await GoogleAPIService.fetchAICredits(
|
||||
@@ -145,13 +154,22 @@ export async function refreshAccountQuota(accountId: string): Promise<CloudAccou
|
||||
account.proxy_url,
|
||||
);
|
||||
if (aiCredits) {
|
||||
account.quota = { ...quota, ai_credits: aiCredits };
|
||||
await CloudAccountRepo.updateQuota(account.id, account.quota);
|
||||
quota.ai_credits = aiCredits;
|
||||
} else {
|
||||
logger.info(`No AI credits returned for ${account.email} during quota refresh`);
|
||||
if (previousAICredits) {
|
||||
quota.ai_credits = previousAICredits;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
logger.warn('Failed to fetch AI credits', e);
|
||||
if (previousAICredits) {
|
||||
quota.ai_credits = previousAICredits;
|
||||
}
|
||||
}
|
||||
|
||||
account.quota = quota;
|
||||
await CloudAccountRepo.updateQuota(account.id, account.quota);
|
||||
await CloudAccountRepo.updateLastUsed(account.id);
|
||||
account.last_used = Math.floor(Date.now() / 1000);
|
||||
notifyTrayUpdate(account);
|
||||
@@ -172,12 +190,11 @@ export async function refreshAccountQuota(accountId: string): Promise<CloudAccou
|
||||
|
||||
await CloudAccountRepo.updateToken(account.id, account.token);
|
||||
|
||||
const previousAICredits = account.quota?.ai_credits;
|
||||
const quota = await GoogleAPIService.fetchQuota(
|
||||
account.token.access_token,
|
||||
account.proxy_url,
|
||||
);
|
||||
account.quota = quota;
|
||||
await CloudAccountRepo.updateQuota(account.id, quota);
|
||||
|
||||
try {
|
||||
const aiCredits = await GoogleAPIService.fetchAICredits(
|
||||
@@ -185,13 +202,22 @@ export async function refreshAccountQuota(accountId: string): Promise<CloudAccou
|
||||
account.proxy_url,
|
||||
);
|
||||
if (aiCredits) {
|
||||
account.quota = { ...quota, ai_credits: aiCredits };
|
||||
await CloudAccountRepo.updateQuota(account.id, account.quota);
|
||||
quota.ai_credits = aiCredits;
|
||||
} else {
|
||||
logger.info(`No AI credits returned for ${account.email} after token refresh`);
|
||||
if (previousAICredits) {
|
||||
quota.ai_credits = previousAICredits;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
logger.warn('Failed to fetch AI credits after token refresh', e);
|
||||
if (previousAICredits) {
|
||||
quota.ai_credits = previousAICredits;
|
||||
}
|
||||
}
|
||||
|
||||
account.quota = quota;
|
||||
await CloudAccountRepo.updateQuota(account.id, account.quota);
|
||||
await CloudAccountRepo.updateLastUsed(account.id);
|
||||
account.last_used = Math.floor(Date.now() / 1000);
|
||||
return account;
|
||||
|
||||
@@ -425,6 +425,11 @@ app
|
||||
if (enabled) {
|
||||
logger.info('Startup: Auto-Switch enabled, starting monitor...');
|
||||
CloudMonitorService.start();
|
||||
} else {
|
||||
logger.info(
|
||||
'Startup: Auto-Switch disabled, running one-time quota and AI credits sync...',
|
||||
);
|
||||
await CloudMonitorService.poll();
|
||||
}
|
||||
} catch (e) {
|
||||
logger.error('Startup: Failed to initialize services', e);
|
||||
|
||||
@@ -120,14 +120,20 @@ export class CloudMonitorService {
|
||||
|
||||
await new Promise((r) => setTimeout(r, 1000));
|
||||
const quota = await GoogleAPIService.fetchQuota(accessToken, account.proxy_url);
|
||||
const previousAICredits = account.quota?.ai_credits;
|
||||
|
||||
try {
|
||||
const aiCredits = await GoogleAPIService.fetchAICredits(accessToken, account.proxy_url);
|
||||
if (aiCredits) {
|
||||
quota.ai_credits = aiCredits;
|
||||
} else if (previousAICredits) {
|
||||
quota.ai_credits = previousAICredits;
|
||||
}
|
||||
} catch (creditError) {
|
||||
logger.warn(`Monitor: Failed to fetch credits for ${account.email}`, creditError);
|
||||
if (previousAICredits) {
|
||||
quota.ai_credits = previousAICredits;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Update DB
|
||||
|
||||
Reference in new issue
Block a user