Bỏ qua, tới nội dung chính

Kiến trúc · ADR 0057

One currency, points: AI is paid from the points wallet after the free monthly uses

Trạng thái
accepted
Ngày
2026-11-23
Người quyết định
SmartLearn team (#183)

Context

A user had to understand two units: points (earned, bought, spent on courses) and AI credits (a free monthly quota, a purchased balance, credits bought with points, AI plans and PLUS). Only points can be bought with money, and ADR-0049 already priced 1 credit at 6 points. The exchange screen, the plan admin and the credit wording added concepts without adding value.

Decision

  • Points are the only currency. Every AI action has a price in points (ai.pricing: tutorAnswerPoints 6, advancedExplanationPoints 12, …; ai.project-review.pointsPerReview 60). The values are today's credit prices × 6, so nothing gets cheaper or dearer.
  • Debit order: a points course's tutor allowance (ADR-0052, counts answers) → the free monthly AI uses (QuotaPeriod.usesGranted/usesUsed, one use per action whatever its price; students 50, instructors 200 by default) → the points wallet through PointsService.debit (SPEND / AI_USAGE, key ai-spend:<generationId>). ADMIN is never charged. A project review is still charged to the course owner (ADR-0054).
  • Refused before the call: when nothing can pay, 409 INSUFFICIENT_POINTS ("Không đủ điểm…", details.topUpPath), and the provider is never called.
  • Refund on failure: the use goes back to its window, or the points back to the wallet as REFUND / AI_USAGE (ai-refund:<id>), idempotent on refund:<id> in AiUsageLedger.
  • AiUsageLedger stays the AI log: source = POINTS rows carry points; CREDIT, GRANT_PURCHASE and GRANT_REWARD remain only for history.
  • Retired: AiCreditBalance, AiPlan, Entitlement, Payment, PaymentWebhookEvent, RewardItem, RewardRedemption, the ai.plans setting, /points/rewards, /points/redemptions, /admin/points/rewards, /admin/ai/plans. Admin billing keeps the point top-ups and one price list (/admin/billing/pricing).
  • Leftover credits become points in migration 20261123100100_single_points_currency: (creditsGranted − creditsUsed) × 6 added to the wallet with a visible `ADJUST /

CREDIT_CONVERSION ledger row keyed credit-conversion:<userId>, plus a closing convert:<userId> row in AiUsageLedger. The step is keyed per user and guarded by to_regclass`, so a rerun converts nothing twice. The pricing settings are rewritten from the legacy keys the same way.

Consequences

  • One AI action takes one free use, so a 60-point review and a 6-point answer cost the same free use. This is simpler to explain; if reviews eat the free uses, price them out of the free uses later rather than bring a second unit back.
  • The AI module imports the points module (AiModule → PointsModule); the points module must never import AiModule.
  • Payment rows of AI plans were never written by the app (plans lived in a setting and no plan checkout existed), so dropping those tables loses no history. Points-for-credits exchanges stay visible as REDEMPTION rows in the points history.