|
1 | 1 | import { createLogger } from '@sim/logger' |
| 2 | +import { getHighestPrioritySubscription } from '@/lib/billing/core/plan' |
| 3 | +import { isPaid } from '@/lib/billing/plan-helpers' |
2 | 4 | import { ORCHESTRATION_TIMEOUT_MS } from '@/lib/copilot/constants' |
3 | 5 | import { |
4 | 6 | handleSubagentRouting, |
@@ -80,16 +82,28 @@ export async function runStreamLoop( |
80 | 82 | const errorText = await response.text().catch(() => '') |
81 | 83 |
|
82 | 84 | if (response.status === 402) { |
| 85 | + let action = 'upgrade_plan' |
| 86 | + let message = "You've reached your usage limit. Please upgrade your plan to continue." |
| 87 | + try { |
| 88 | + const sub = await getHighestPrioritySubscription(execContext.userId) |
| 89 | + if (sub && isPaid(sub.plan)) { |
| 90 | + action = 'increase_limit' |
| 91 | + message = |
| 92 | + "You've reached your usage limit for this billing period. Please increase your usage limit to continue." |
| 93 | + } |
| 94 | + } catch { |
| 95 | + // Fall back to upgrade_plan if we can't determine the plan |
| 96 | + } |
| 97 | + |
83 | 98 | const upgradePayload = JSON.stringify({ |
84 | 99 | reason: 'usage_limit', |
85 | | - action: 'increase_limit', |
86 | | - message: |
87 | | - "You've reached your usage limit for this billing period. Please upgrade your plan or increase your usage limit to continue.", |
| 100 | + action, |
| 101 | + message, |
88 | 102 | }) |
89 | 103 | const syntheticContent = `<usage_upgrade>${upgradePayload}</usage_upgrade>` |
90 | 104 |
|
91 | 105 | const syntheticEvents: SSEEvent[] = [ |
92 | | - { type: 'content', data: syntheticContent }, |
| 106 | + { type: 'content', data: syntheticContent as unknown as Record<string, unknown> }, |
93 | 107 | { type: 'done', data: {} }, |
94 | 108 | ] |
95 | 109 | for (const event of syntheticEvents) { |
|
0 commit comments