()
| 50 | * billing terms. Fetches quota and utilization in parallel. |
| 51 | */ |
| 52 | export async function checkOverageGate(): Promise<OverageGate> { |
| 53 | // Team and Enterprise plans include ultrareview — no free-review quota |
| 54 | // or Extra Usage dialog. The quota endpoint is scoped to consumer plans |
| 55 | // (pro/max); hitting it on team/ent would surface a confusing dialog. |
| 56 | if (isTeamSubscriber() || isEnterpriseSubscriber()) { |
| 57 | return { kind: 'proceed', billingNote: '' } |
| 58 | } |
| 59 | |
| 60 | const [quota, utilization] = await Promise.all([ |
| 61 | fetchUltrareviewQuota(), |
| 62 | fetchUtilization().catch(() => null), |
| 63 | ]) |
| 64 | |
| 65 | // No quota info (non-subscriber or endpoint down) — let it through, |
| 66 | // server-side billing will handle it. |
| 67 | if (!quota) { |
| 68 | return { kind: 'proceed', billingNote: '' } |
| 69 | } |
| 70 | |
| 71 | if (quota.reviews_remaining > 0) { |
| 72 | return { |
| 73 | kind: 'proceed', |
| 74 | billingNote: ` This is free ultrareview ${quota.reviews_used + 1} of ${quota.reviews_limit}.`, |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Utilization fetch failed (transient network error, timeout, etc.) — |
| 79 | // let it through, same rationale as the quota fallback above. |
| 80 | if (!utilization) { |
| 81 | return { kind: 'proceed', billingNote: '' } |
| 82 | } |
| 83 | |
| 84 | // Free reviews exhausted — check Extra Usage setup. |
| 85 | const extraUsage = utilization.extra_usage |
| 86 | if (!extraUsage?.is_enabled) { |
| 87 | logEvent('tengu_review_overage_not_enabled', {}) |
| 88 | return { kind: 'not-enabled' } |
| 89 | } |
| 90 | |
| 91 | // Check available balance (null monthly_limit = unlimited). |
| 92 | const monthlyLimit = extraUsage.monthly_limit |
| 93 | const usedCredits = extraUsage.used_credits ?? 0 |
| 94 | const available = |
| 95 | monthlyLimit === null || monthlyLimit === undefined |
| 96 | ? Infinity |
| 97 | : monthlyLimit - usedCredits |
| 98 | |
| 99 | if (available < 10) { |
| 100 | logEvent('tengu_review_overage_low_balance', { available }) |
| 101 | return { kind: 'low-balance', available } |
| 102 | } |
| 103 | |
| 104 | if (!sessionOverageConfirmed) { |
| 105 | logEvent('tengu_review_overage_dialog_shown', {}) |
| 106 | return { kind: 'needs-confirm' } |
| 107 | } |
| 108 | |
| 109 | return { |
no test coverage detected