diff --git a/app/actions/generate.ts b/app/actions/generate.ts index 69e18d4..5dc10dc 100644 --- a/app/actions/generate.ts +++ b/app/actions/generate.ts @@ -163,7 +163,22 @@ export async function generateAnswer({ prompt, tool, authorId, authorEmail, atta const tokenCost = Number.isFinite(rawTokenCost) ? Math.max(1, Math.min(Math.round(rawTokenCost), 2_147_483_647)) : 1 - const creditsToCharge = Math.max(1, Math.min(tokenCost, creditsRemaining)) + + if (tokenCost > creditsRemaining) { + const cap = getPlanCreditCap(userProfile.subscriptionPlan) + const resetLabel = resetDate + ? new Date(resetDate).toLocaleDateString('en-US', { month: 'short', day: 'numeric' }) + : 'in 30 days' + const errorMessage = `You've reached your monthly credit cap (${cap}). Upgrade your plan now, or wait until your credits reset on ${resetLabel}.` + return { + answer: errorMessage, + sessionId: sessionId, + chatId: chatId, + error: errorMessage + } + } + + const creditsToCharge = tokenCost const currentSessionId = sessionId || crypto.randomUUID() diff --git a/components/PromptShell.tsx b/components/PromptShell.tsx index 92fef61..805c69f 100644 --- a/components/PromptShell.tsx +++ b/components/PromptShell.tsx @@ -558,7 +558,7 @@ export default function PromptShell({ researchMode: researchMode && useWebSearch // Only send researchMode if web search is enabled/active }) - const { answer, sessionId: newSessionId, chatId: savedChatId, error: limitError } = result + const { answer, sessionId: newSessionId, chatId: savedChatId, error: limitError, warning } = result // Clear web search status if (useWebSearch) { @@ -635,6 +635,12 @@ export default function PromptShell({ ) ) + if (warning) { + setAttachmentMessage(warning) + } else { + setAttachmentMessage(null) + } + dispatchUsageRefresh('messages') if (useWebSearch) { dispatchUsageRefresh('web-search')