feat(ai-chat) - Stop ai thinking if credits exhausted (#20526)
Billing is now decremented per-step, not per-turn. The onStepFinish callback in chat-execution.service.ts calls a new decrementAndCheckAvailableCredits method on each model step, so Redis is debited incrementally as the agent runs rather than all at once at the end. Credit exhaustion stops the stream mid-run. When a step depletes the remaining credits, a hasNoMoreAvailableCredits flag is set and passed into the stopWhen predicate of streamText, causing the agent to halt before starting the next step. A new credits-exhausted event is introduced. After the stream drains and the response is persisted, if credits ran out the job publishes a dedicated credits-exhausted event to the frontend instead of the normal message-persisted event. The frontend handles this new event. useAgentChatSubscription has a new credits-exhausted case that sets a BILLING_CREDITS_EXHAUSTED-coded error on the atom, closes the writer, and stops the streaming state — triggering the existing AiChatCreditsExhaustedMessage UI.
This commit is contained in:
+8
-6
@@ -285,7 +285,7 @@ export class BillingUsageService {
|
||||
);
|
||||
}
|
||||
|
||||
private async warmAvailableCredits(
|
||||
private async warmAvailableCreditsInCache(
|
||||
workspaceId: string,
|
||||
periodStart: Date | string,
|
||||
periodEnd: Date | string,
|
||||
@@ -421,13 +421,13 @@ export class BillingUsageService {
|
||||
return Number(resourceCreditPrice.metadata?.credit_amount ?? 0);
|
||||
}
|
||||
|
||||
async decrementAvailableCredits({
|
||||
async decrementAvailableCreditsInCache({
|
||||
workspaceId,
|
||||
usedCredits,
|
||||
}: {
|
||||
workspaceId: string;
|
||||
usedCredits: number;
|
||||
}): Promise<void> {
|
||||
}): Promise<number> {
|
||||
const {
|
||||
billingSubscription: { currentPeriodStart, currentPeriodEnd },
|
||||
} = await this.workspaceCacheService.getOrRecompute(workspaceId, [
|
||||
@@ -447,7 +447,7 @@ export class BillingUsageService {
|
||||
});
|
||||
|
||||
if (!isDefined(cachedAvailableCredits)) {
|
||||
await this.warmAvailableCredits(
|
||||
await this.warmAvailableCreditsInCache(
|
||||
workspaceId,
|
||||
currentPeriodStart,
|
||||
currentPeriodEnd,
|
||||
@@ -470,9 +470,11 @@ export class BillingUsageService {
|
||||
true,
|
||||
);
|
||||
}
|
||||
|
||||
return decrementedAvailableCredits;
|
||||
}
|
||||
|
||||
async invalidateAvailableCredits(
|
||||
async invalidateAvailableCreditsInCache(
|
||||
workspaceId: string,
|
||||
periodStart: Date,
|
||||
): Promise<void> {
|
||||
@@ -504,7 +506,7 @@ export class BillingUsageService {
|
||||
currentPeriodStart: subscription.currentPeriodStart,
|
||||
});
|
||||
|
||||
await this.warmAvailableCredits(
|
||||
await this.warmAvailableCreditsInCache(
|
||||
subscription.workspaceId,
|
||||
subscription.currentPeriodStart,
|
||||
subscription.currentPeriodEnd,
|
||||
|
||||
+1
-1
@@ -376,7 +376,7 @@ export class LogicFunctionExecutorService {
|
||||
|
||||
periodStart = currentPeriodStart;
|
||||
|
||||
await this.billingUsageService.decrementAvailableCredits({
|
||||
await this.billingUsageService.decrementAvailableCreditsInCache({
|
||||
workspaceId,
|
||||
usedCredits: 100,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user