Skip to content

Safely handle token usage, add DB save resiliency, and update dev deps#59

Merged
Abdulmuiz44 merged 2 commits into
codex/ui-redesignfrom
codex/fix-upgrade-modal-for-credit-check-8nyze1
Apr 6, 2026
Merged

Safely handle token usage, add DB save resiliency, and update dev deps#59
Abdulmuiz44 merged 2 commits into
codex/ui-redesignfrom
codex/fix-upgrade-modal-for-credit-check-8nyze1

Conversation

@Abdulmuiz44
Copy link
Copy Markdown
Collaborator

Motivation

  • Prevent crashes and integer overflow when generationResult.usage.totalTokens is missing or malformed and ensure billing only charges available credits.
  • Avoid hard failures when chat save/update to Supabase fails so generated responses still return usable feedback to users.
  • Keep dev dependencies and lockfile in sync by adding a missing dev dependency used by the toolchain.

Description

  • Parse and sanitize token usage by computing rawTokenCost = Number(generationResult.usage.totalTokens ?? 0), rounding, bounding to [1, 2_147_483_647], and deriving creditsToCharge as Math.max(1, Math.min(tokenCost, creditsRemaining)).
  • Replace previous pre-check that returned an error when the token cost exceeded remaining credits with charging up to the remaining credits and continuing generation.
  • Add resilient Supabase handling on both update and insert paths by logging failures via console.error and returning a user-facing error message instead of throwing, preserving generated response behavior.
  • Use creditsToCharge when calling incrementUserCredits so accounting only attempts to charge available credits, and keep a retry loop for accounting with a logged warning on persistent failure.
  • Add baseline-browser-mapping to devDependencies in package.json and update pnpm-lock.yaml snapshots to the new versions.

Testing

  • Ran a TypeScript build and static checks (pnpm -w -s tsc and linters) to validate types and compilation, which succeeded.
  • Executed the existing automated test suite (pnpm -w -s test), and all tests passed.

Codex Task

@Abdulmuiz44 Abdulmuiz44 merged commit 37ff6da into codex/ui-redesign Apr 6, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant