Skip to content

Commit 6635249

Browse files
committed
Usage limit
1 parent 80830f5 commit 6635249

File tree

1 file changed

+30
-0
lines changed
  • apps/sim/lib/copilot/orchestrator/stream

1 file changed

+30
-0
lines changed

apps/sim/lib/copilot/orchestrator/stream/core.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,36 @@ export async function runStreamLoop(
7878

7979
if (!response.ok) {
8080
const errorText = await response.text().catch(() => '')
81+
82+
if (response.status === 402) {
83+
const upgradePayload = JSON.stringify({
84+
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.",
88+
})
89+
const syntheticContent = `<usage_upgrade>${upgradePayload}</usage_upgrade>`
90+
91+
const syntheticEvents: SSEEvent[] = [
92+
{ type: 'content', data: syntheticContent },
93+
{ type: 'done', data: {} },
94+
]
95+
for (const event of syntheticEvents) {
96+
try {
97+
await options.onEvent?.(event)
98+
} catch {
99+
// best-effort forwarding
100+
}
101+
102+
const handler = sseHandlers[event.type]
103+
if (handler) {
104+
await handler(event, context, execContext, options)
105+
}
106+
if (context.streamComplete) break
107+
}
108+
return
109+
}
110+
81111
throw new Error(
82112
`Copilot backend error (${response.status}): ${errorText || response.statusText}`
83113
)

0 commit comments

Comments
 (0)