-
Notifications
You must be signed in to change notification settings - Fork 3.3k
feat(cost): added hidden cost breakdown component to settings > subscription, start collecting current period copilot cost and last period copilot cost #1770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…ription, start collecting current period copilot cost and last period copilot cost
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Greptile Overview
Greptile Summary
This PR adds infrastructure to track copilot costs separately from workflow execution costs by introducing two new database columns (current_period_copilot_cost and last_period_copilot_cost) and updating the billing system throughout the codebase.
Key Changes:
- Database schema adds period-based copilot cost tracking columns to
user_statstable - Cost tracking API (
/api/billing/update-cost) increments copilot cost counters when recording usage - Billing core aggregates copilot costs for both individual and team billing contexts
- Period reset logic in invoice webhooks properly saves and resets copilot costs at billing boundaries
- Team invitation flow resets copilot costs when users join teams
- Cost breakdown UI component created but currently commented out until sufficient data collected
Implementation Quality:
The implementation is thorough and systematic, updating all necessary touchpoints where billing periods are managed. The decision to defer displaying the cost breakdown component until the next billing period is sensible to avoid showing misleading zero values.
Confidence Score: 5/5
- This PR is safe to merge - it's a well-structured billing enhancement that adds new tracking without breaking existing functionality
- The implementation is comprehensive and correctly handles all billing lifecycle events (cost tracking, period resets, team transitions). The database migration is straightforward with safe defaults. No breaking changes or risky logic modifications.
- No files require special attention
Important Files Changed
File Analysis
| Filename | Score | Overview |
|---|---|---|
| packages/db/schema.ts | 5/5 | Adds currentPeriodCopilotCost and lastPeriodCopilotCost decimal fields to userStats table schema for tracking copilot usage costs |
| apps/sim/app/api/billing/update-cost/route.ts | 5/5 | Updates the cost tracking API to increment currentPeriodCopilotCost alongside existing copilot cost fields when recording copilot usage |
| apps/sim/lib/billing/core/billing.ts | 5/5 | Extends billing summary to fetch and aggregate copilot costs (copilotCost, lastPeriodCopilotCost) for both individual and team billing contexts |
| apps/sim/lib/billing/webhooks/invoices.ts | 5/5 | Updates period reset logic to save current copilot cost to lastPeriodCopilotCost and reset currentPeriodCopilotCost to '0' at billing period boundaries |
| apps/sim/app/workspace/[workspaceId]/w/components/sidebar/components/settings-modal/components/subscription/subscription.tsx | 4/5 | Adds commented-out CostBreakdown component and fixes fallback logic for team/enterprise billing to use individual usage when org data unavailable |
Sequence Diagram
sequenceDiagram
participant Client as Copilot Client
participant API as /api/billing/update-cost
participant DB as Database (user_stats)
participant Stripe as Stripe Webhook
participant Invoice as Invoice Handler
participant Billing as Billing Core
Note over Client,DB: Cost Tracking Flow
Client->>API: POST cost data (userId, cost)
API->>DB: UPDATE user_stats SET<br/>totalCopilotCost += cost<br/>currentPeriodCopilotCost += cost<br/>totalCopilotCalls += 1
DB-->>API: Success
API-->>Client: Cost recorded
Note over Stripe,Billing: Billing Period Reset Flow
Stripe->>Invoice: invoice.paid webhook
Invoice->>DB: SELECT currentPeriodCost,<br/>currentPeriodCopilotCost
DB-->>Invoice: Current period costs
Invoice->>DB: UPDATE user_stats SET<br/>lastPeriodCost = currentPeriodCost<br/>lastPeriodCopilotCost = currentPeriodCopilotCost<br/>currentPeriodCost = '0'<br/>currentPeriodCopilotCost = '0'
DB-->>Invoice: Period reset complete
Note over Client,Billing: Billing Summary Retrieval
Client->>Billing: getBillingSummary(userId)
Billing->>DB: SELECT currentPeriodCopilotCost,<br/>lastPeriodCopilotCost
DB-->>Billing: Copilot cost data
Billing->>Billing: Aggregate costs for<br/>team members if applicable
Billing-->>Client: Summary with copilotCost,<br/>lastPeriodCopilotCost
13 files reviewed, no comments
…ription, start collecting current period copilot cost and last period copilot cost (#1770) * feat(cost): added hidden cost breakdown component to settings > subscription, start collecting current period copilot cost and last period copilot cost * don't rerender envvars when switching between workflows in the same workspace
Summary
Type of Change
Testing
Tested manually
Checklist