Skip to content

Commit 2d8abc0

Browse files
committed
fix(worker): trigger auto-topoff check on PTB insufficient balance errors
When PTB requests fail due to insufficient credits, the auto-topoff check was never triggered because: 1. The checkAndScheduleAutoTopoffAlarm() call only existed in ProxyForwarder.ts 2. Failed escrow reservations returned early without reaching that code path Now when escrow fails with "insufficient_credit_limit", we trigger the auto-topoff alarm check. This ensures users with auto-topoff configured will have their accounts recharged even when requests are being rejected.
1 parent 6c16a41 commit 2d8abc0

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

worker/src/lib/ai-gateway/AttemptExecutor.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ export class AttemptExecutor {
151151

152152
const awaitedEscrow = await pendingEscrow;
153153
if (awaitedEscrow.error) {
154+
// Trigger auto-topoff check when escrow fails due to insufficient credits
155+
// This ensures auto-topoff can trigger even when requests are being rejected
156+
if (awaitedEscrow.error.type === "insufficient_credit_limit") {
157+
const walletId = this.env.WALLET.idFromName(props.orgId);
158+
const walletStub = this.env.WALLET.get(walletId);
159+
this.ctx.waitUntil(
160+
walletStub.checkAndScheduleAutoTopoffAlarm(props.orgId)
161+
);
162+
}
154163
if (walletSpanId) {
155164
this.tracer.setError(walletSpanId, awaitedEscrow.error.message);
156165
this.tracer.finishSpan(walletSpanId);

0 commit comments

Comments
 (0)