Repository navigation
Expand file tree
/
Copy pathsession-ended-banner.tsx
More file actions
182 lines (173 loc) · 6.11 KB
/
Copy pathsession-ended-banner.tsx
File metadata and controls
182 lines (173 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { getRateLimitsByModel } from '@codebuff/common/types/freebuff-session'
import { TextAttributes } from '@opentui/core'
import { useKeyboard } from '@opentui/react'
import React, { useCallback, useState } from 'react'
import { Button } from './button'
import {
refreshFreebuffSession,
returnToFreebuffLanding,
} from '../hooks/use-freebuff-session'
import { useTheme } from '../hooks/use-theme'
import { useFreebuffSessionStore } from '../state/freebuff-session-store'
import { formatSessionUnits } from '../utils/format-session-units'
import { BORDER_CHARS } from '../utils/ui-constants'
import type { KeyEvent } from '@opentui/core'
interface SessionEndedBannerProps {
/** True while an agent request is still streaming under the server-side
* grace window. Swaps the Enter-to-rejoin affordance for a "let it
* finish" hint so the user doesn't abort their in-flight work. */
isStreaming: boolean
}
/**
* Replaces the chat input when the freebuff session has ended. Captures
* Enter to start a new same-chat session. Esc returns to model selection
* once no in-flight work needs the global stream-interrupt handler.
*/
export const SessionEndedBanner: React.FC<SessionEndedBannerProps> = ({
isStreaming,
}) => {
const theme = useTheme()
const [pendingAction, setPendingAction] = useState<
'waiting-room' | 'same-chat' | null
>(null)
// All premium models share one daily pool; the server replicates the same
// snapshot under each premium model id, so the first entry has the right
// count.
const premiumQuota = useFreebuffSessionStore(
(s) => Object.values(getRateLimitsByModel(s.session) ?? {})[0] ?? null,
)
const isQuotaExhausted = premiumQuota
? premiumQuota.recentCount >= premiumQuota.limit
: false
const accessTier = useFreebuffSessionStore((s) =>
s.session && 'accessTier' in s.session ? s.session.accessTier : 'full',
)
const quotaLabel =
accessTier === 'limited' ? 'limited sessions' : 'premium sessions'
const bannerTitle = premiumQuota
? `Session ended · ${formatSessionUnits(premiumQuota.recentCount)} of ${premiumQuota.limit} ${quotaLabel} used today`
: 'Session ended'
// While a request is still streaming, restart is disabled: it would
// unmount <Chat> and abort the in-flight agent run. The promise is "we
// let the agent finish" — honoring that means Enter does nothing until
// the stream ends or the user hits Esc.
const canRestart = !isStreaming && pendingAction === null
const pickNewModel = useCallback(() => {
if (!canRestart) return
setPendingAction('waiting-room')
// Drop back to the landing picker (status: 'none') so the user picks a
// model and hits Enter again to commit, instead of being silently
// re-queued. app.tsx swaps us into <WaitingRoomScreen> on the
// transition, unmounting this banner — no need to clear the pending state on
// success.
returnToFreebuffLanding({ resetChat: true }).catch(() =>
setPendingAction(null),
)
}, [canRestart])
const startSameChatSession = useCallback(() => {
if (!canRestart) return
setPendingAction('same-chat')
// Re-POST with the currently selected model and keep the chat/run state
// intact so the next prompt continues the same conversation.
refreshFreebuffSession().catch(() => setPendingAction(null))
}, [canRestart])
useKeyboard(
useCallback(
(key: KeyEvent) => {
if (!canRestart) return
if (key.name === 'return' || key.name === 'enter') {
key.preventDefault?.()
startSameChatSession()
return
}
if (key.name === 'escape') {
key.preventDefault?.()
pickNewModel()
}
},
[startSameChatSession, pickNewModel, canRestart],
),
)
return (
<box
title={bannerTitle}
titleAlignment="center"
style={{
width: '100%',
borderStyle: 'single',
// Amber border doubles as the "you've hit the cap" signal now that
// the quota count lives in the title (which can't carry per-char
// color); muted otherwise.
borderColor: isQuotaExhausted ? theme.secondary : theme.muted,
customBorderChars: BORDER_CHARS,
paddingLeft: 1,
paddingRight: 1,
paddingTop: 0,
paddingBottom: 0,
flexDirection: 'column',
gap: 0,
}}
>
{isStreaming ? (
<text style={{ fg: theme.muted, wrapMode: 'word' }}>
Agent is wrapping up. Rejoin the wait room after it's finished.
</text>
) : (
<box
style={{
width: '100%',
flexDirection: 'row',
alignItems: 'center',
gap: 2,
}}
>
<Button onClick={startSameChatSession}>
<text
style={{
fg:
pendingAction === 'same-chat'
? theme.muted
: theme.foreground,
}}
attributes={TextAttributes.BOLD}
>
{pendingAction === 'same-chat'
? 'Starting…'
: 'Press Enter to continue in a new session'}
</text>
</Button>
<box style={{ flexGrow: 1 }} />
<Button
onClick={pickNewModel}
style={{
borderStyle: 'single',
borderColor:
pendingAction === 'waiting-room' ? theme.muted : theme.border,
customBorderChars: BORDER_CHARS,
paddingLeft: 1,
paddingRight: 1,
}}
border={['top', 'bottom', 'left', 'right']}
>
<text
style={{
fg:
pendingAction === 'waiting-room'
? theme.muted
: theme.foreground,
}}
>
{pendingAction === 'waiting-room' ? (
'Opening model selection…'
) : (
<>
Change model<span fg={theme.muted}>{' Esc'}</span>
</>
)}
</text>
</Button>
</box>
)}
</box>
)
}