Skip to content

Conversation

@AnthonyRonning
Copy link
Contributor

@AnthonyRonning AnthonyRonning commented Feb 27, 2025

Summary by CodeRabbit

  • New Features

    • Introduced a new credit usage display in the account menu, featuring a progress bar that visually represents token consumption and includes monthly credit reset information.
    • Added interactive fields in the billing debugger for entering token values.
  • Enhancements

    • Updated token thresholds and warning messages in the chat interface, ensuring notifications are based on flexible limits for different user plans.

@coderabbitai
Copy link

coderabbitai bot commented Feb 27, 2025

Walkthrough

This pull request expands the billing token information across the frontend. The BillingStatus type is enhanced with new properties (total_tokens and used_tokens) in the billing API and reflected in the BillingDebugger UI. The AccountMenu now renders a new CreditUsage component that conditionally displays a progress bar based on token usage. In addition, token threshold constants for starter and pro users have been introduced in ChatBox to update warning and submission logic.

Changes

File(s) Change Summary
frontend/src/billing/billingApi.ts Updated BillingStatus type by adding `total_tokens: number
frontend/src/components/BillingDebugger.tsx Added new state properties for total_tokens and used_tokens, introduced corresponding input fields with onChange handlers, and updated the test button ("Test Pro + No Chats") to include fixed token values.
frontend/src/components/AccountMenu.tsx, frontend/src/components/CreditUsage.tsx Introduced and integrated the new CreditUsage component in AccountMenu. The component uses a custom hook to retrieve billing data and conditionally renders a progress bar with visual indicators based on token usage.
frontend/src/components/ChatBox.tsx Added constants (STARTER_WARNING_THRESHOLD, STARTER_LIMIT_THRESHOLD, PRO_WARNING_THRESHOLD) for token thresholds and updated logic in the TokenWarning and StarterTokenLimit components to use these values instead of hardcoded limits.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant AccountMenu
    participant CreditUsage
    participant LocalState
    User->>AccountMenu: Open Account Menu
    AccountMenu->>CreditUsage: Render CreditUsage component
    CreditUsage->>LocalState: Retrieve billingStatus (total_tokens, used_tokens)
    LocalState-->>CreditUsage: Return token data
    CreditUsage->>User: Display progress bar with token usage
Loading
sequenceDiagram
    participant User
    participant BillingDebugger
    participant State
    User->>BillingDebugger: Input values for total_tokens/used_tokens
    BillingDebugger->>State: Update debugStatus state via onChange handlers
    User->>BillingDebugger: Click "Test Pro + No Chats" button
    BillingDebugger->>State: Create newStatus with total_tokens=20000, used_tokens=18500
    BillingDebugger->>User: Display updated token status
Loading

Possibly related PRs

  • Starter plan input limits #29: Enhancements in billing status handling with token-related properties in the TokenWarning component, which directly relates to the modifications in token tracking and usage added in this PR.

Poem

I'm a rabbit on a coding spree,
Hoppin' through tokens so merrily.
New fields sprout in Billing and Debug scenes,
CreditUsage glows with progress dreams.
With each hop, our code sings with glee!
🐇💻

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@cloudflare-workers-and-pages
Copy link

Deploying maple with  Cloudflare Pages  Cloudflare Pages

Latest commit: 8780683
Status: ✅  Deploy successful!
Preview URL: https://12adecf6.maple-ca8.pages.dev
Branch Preview URL: https://usage-visibility.maple-ca8.pages.dev

View logs

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
frontend/src/components/ChatBox.tsx (1)

302-304: Consider consolidating duplicate threshold constants.

The STARTER_LIMIT_THRESHOLD constant is defined in three different places (lines 44, 111, and 303) with the same value of 7000. This violates the DRY principle and could lead to maintenance issues if one value needs to be changed but others are forgotten.

Consider moving these constants to a shared location, either at the top of the file or in a separate constants file:

+ // Token thresholds for different plan types
+ const STARTER_WARNING_THRESHOLD = 4000;
+ const STARTER_LIMIT_THRESHOLD = 7000;
+ const PRO_WARNING_THRESHOLD = 10000;
+
  function TokenWarning({
    messages,
    currentInput,
    chatId,
    className,
    billingStatus
  }: {
    messages: ChatMessage[];
    currentInput: string;
    chatId?: string;
    className?: string;
    billingStatus?: BillingStatus;
  }) {
    const totalTokens =
      messages.reduce((acc, msg) => acc + estimateTokenCount(msg.content), 0) +
      (currentInput ? estimateTokenCount(currentInput) : 0);

    const navigate = useNavigate();

    // Check if user is on starter plan
    const isStarter = billingStatus?.product_name.toLowerCase().includes("starter") || false;

-   // Token thresholds for different plan types
-   const STARTER_WARNING_THRESHOLD = 4000;
-   const STARTER_LIMIT_THRESHOLD = 7000;
-   const PRO_WARNING_THRESHOLD = 10000;

And then remove the other duplicate constant declarations in the other components.

Also applies to: 44-45, 111-112

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 65fe669 and 8780683.

📒 Files selected for processing (5)
  • frontend/src/billing/billingApi.ts (1 hunks)
  • frontend/src/components/AccountMenu.tsx (2 hunks)
  • frontend/src/components/BillingDebugger.tsx (3 hunks)
  • frontend/src/components/ChatBox.tsx (5 hunks)
  • frontend/src/components/CreditUsage.tsx (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Cloudflare Pages
🔇 Additional comments (13)
frontend/src/components/AccountMenu.tsx (2)

17-17: LGTM: Clean import of the new CreditUsage component.

The import is correctly placed with other component imports.


133-133: LGTM: Good placement of the CreditUsage component.

The CreditUsage component is appropriately placed after the billing plan badge and before the account dropdown menu trigger, creating a logical grouping of billing-related information.

frontend/src/billing/billingApi.ts (1)

11-12: LGTM: Appropriate addition of token tracking fields.

The new properties total_tokens and used_tokens are correctly typed as number | null, consistent with the typing pattern of other nullable numeric properties in the BillingStatus type (like chats_remaining).

frontend/src/components/BillingDebugger.tsx (3)

25-26: LGTM: Correctly added new token fields to debugStatus initialization.

The new token properties are properly initialized from currentStatus with null fallbacks, maintaining consistency with the existing pattern.


99-128: LGTM: Well-implemented input fields for token debugging.

The implementation for total and used credits follows the same pattern as other numeric inputs, with proper handling of empty values and conversion to integers.


153-154: LGTM: Added token values to Pro test scenario.

The "Test Pro + No Chats" scenario now includes realistic token values (20000 total, 18500 used), providing a good way to test the near-limit state (92.5% usage).

frontend/src/components/CreditUsage.tsx (1)

1-35: Well-implemented token usage visualization component.

The component effectively visualizes token usage with:

  • Conditional rendering when token data is available
  • Proper percentage calculation with min/max constraints
  • Color-coded progress bar that changes based on usage thresholds
  • Clear labeling and "Resets Monthly" indication
frontend/src/components/ChatBox.tsx (6)

42-46: Good addition of threshold constants for different plan types.

Extracting these magic numbers into named constants improves code readability and maintainability.


48-48: LGTM: Appropriate use of constants based on plan type.

This logic correctly applies different thresholds depending on whether the user is on a starter or pro plan.


50-52: Good implementation of conditional warning logic.

This prevents showing both warning and limit messages simultaneously for starter users who exceed the limit.


110-114: LGTM: Clear threshold implementation for token limit.

The condition correctly shows the limit message only when the threshold is exceeded.


314-316: LGTM: Properly implemented submission restriction for starter users.

The logic correctly prevents submission when a starter user exceeds their token limit.


330-333: LGTM: Appropriate placeholder text for token limit.

Good user experience enhancement - showing a helpful message that explains why input is disabled and offering an upgrade path.

@marksftw
Copy link
Contributor

let's good.

Tested with Free, Starter, and Team users.

Free still works as before.
Starter and Team worked correctly.
Also tested a Starter user who had never chatted. The gauge didn't appear until I made my first chat, then it appeared automatically. Nice.

@AnthonyRonning AnthonyRonning merged commit a56c9ee into master Feb 27, 2025
2 checks passed
@AnthonyRonning AnthonyRonning deleted the usage-visibility branch February 27, 2025 21:17
@coderabbitai coderabbitai bot mentioned this pull request Feb 27, 2025
@coderabbitai coderabbitai bot mentioned this pull request Aug 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants