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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//! Coordinates asynchronous `/usage` cards in the chat widget.
//!
//! The slash command builds a composite history cell immediately, but the widget
//! keeps that cell transient while the account request runs. The transient card is
//! rendered above the composer through [`ChatWidget::pending_token_activity_output`]
//! so loading never requires clearing or rewriting transcript history. When the
//! matching response arrives, [`TokenActivityHandle`] updates the shared card state
//! and [`ChatWidget::finish_token_activity_refresh`] moves the cell into a completed
//! slot. Event dispatch commits that completed cell into history only after active
//! output and stream consolidation no longer block insertion.
//!
//! Pure chart rendering and date bucketing live in [`chart`]. This module owns
//! request correlation, transient/completed card state, and integration with
//! `ChatWidget` history insertion.
mod chart;
use std::sync::Arc;
use std::sync::RwLock;
use chrono::NaiveDate;
use chrono::Utc;
use lemurclaw_core::app_server_protocol::GetAccountTokenUsageResponse;
use ratatui::style::Stylize;
use ratatui::text::Line;
use super::ChatWidget;
use crate::tui_internal::app_event::AppEvent;
use crate::tui_internal::history_cell::CompositeHistoryCell;
use crate::tui_internal::history_cell::HistoryCell;
use crate::tui_internal::history_cell::PlainHistoryCell;
use crate::tui_internal::history_cell::plain_lines;
pub(crate) use chart::TokenActivityView;
/// Tracks the renderable lifecycle of one token activity history cell.
#[derive(Debug)]
enum TokenActivityState {
Loading,
Loaded {
response: GetAccountTokenUsageResponse,
today: NaiveDate,
},
Error,
}
/// Completes an asynchronously rendered token activity history cell.
///
/// Clones share the same card state, allowing the background request path to
/// update a cell still owned by the widget's transient-output state. The widget
/// remains responsible for request-ID matching, redraws, and history insertion.
#[derive(Clone, Debug)]
pub(super) struct TokenActivityHandle {
state: Arc<RwLock<TokenActivityState>>,
}
/// Holds the one transient token activity card waiting on its background response.
///
/// The request ID prevents late results from mutating a newer `/usage` card. The
/// cell stays out of transcript history until the matching response completes and
/// the widget confirms that active output no longer blocks insertion.
pub(super) struct PendingTokenActivityOutput {
request_id: u64,
cell: CompositeHistoryCell,
handle: TokenActivityHandle,
}
impl TokenActivityHandle {
/// Replaces the loading state with either fetched activity or an unavailable state.
///
/// This method intentionally discards the error string because the TUI exposes
/// one stable unavailable message. Calling it more than once replaces the prior
/// terminal state, so request-ID matching should happen before completion.
pub(super) fn finish(&self, result: Result<GetAccountTokenUsageResponse, String>) {
self.finish_with_today(result, Utc::now().date_naive());
}
fn finish_with_today(
&self,
result: Result<GetAccountTokenUsageResponse, String>,
today: NaiveDate,
) {
let state = match result {
Ok(response) => TokenActivityState::Loaded { response, today },
Err(_) => TokenActivityState::Error,
};
#[expect(clippy::expect_used)]
let mut current = self.state.write().expect("token activity state poisoned");
*current = state;
}
}
/// Renders one `/usage` card from shared asynchronous state.
#[derive(Debug)]
struct TokenActivityHistoryCell {
view: TokenActivityView,
state: Arc<RwLock<TokenActivityState>>,
}
/// Creates the card contents and completion handle for one `/usage` invocation.
///
/// The composite cell includes the echoed slash command and a loading card from
/// the start. Callers must retain the returned handle and complete it when the
/// matching background response arrives; otherwise the transient card stays loading.
pub(super) fn new_token_activity_output(
view: TokenActivityView,
) -> (CompositeHistoryCell, TokenActivityHandle) {
let command = PlainHistoryCell::new(vec![
format!("/usage {}", view.label().to_lowercase())
.magenta()
.into(),
]);
let state = Arc::new(RwLock::new(TokenActivityState::Loading));
let handle = TokenActivityHandle {
state: Arc::clone(&state),
};
let card = TokenActivityHistoryCell { view, state };
(
CompositeHistoryCell::new(vec![Box::new(command), Box::new(card)]),
handle,
)
}
impl HistoryCell for TokenActivityHistoryCell {
fn display_lines(&self, width: u16) -> Vec<Line<'static>> {
#[expect(clippy::expect_used)]
let state = self.state.read().expect("token activity state poisoned");
match &*state {
TokenActivityState::Loading => {
vec![
" Token activity".bold().into(),
" Loading...".dim().into(),
]
}
TokenActivityState::Error => vec![
" Token activity".bold().into(),
" Token activity unavailable".dim().into(),
],
TokenActivityState::Loaded { response, today } => {
chart::loaded_lines(self.view, response, *today, width)
}
}
}
fn raw_lines(&self) -> Vec<Line<'static>> {
plain_lines(self.display_lines(u16::MAX))
}
}
impl ChatWidget {
/// Starts a token activity refresh and replaces the current transient card.
///
/// Each invocation receives a request ID so background responses update only
/// their own card. The card remains outside transcript history until completion,
/// which keeps loading visible without disturbing existing transcript content.
pub(crate) fn add_token_activity_output(&mut self, view: TokenActivityView) {
let request_id = self.next_token_activity_request_id;
self.next_token_activity_request_id =
self.next_token_activity_request_id.wrapping_add(/*rhs*/ 1);
let (cell, handle) = new_token_activity_output(view);
self.completed_token_activity_output = None;
self.refreshing_token_activity_output = Some(PendingTokenActivityOutput {
request_id,
cell,
handle,
});
self.bump_active_cell_revision();
self.request_redraw();
self.app_event_tx
.send(AppEvent::RefreshTokenActivity { request_id });
}
/// Returns the transient token activity card that should render above the composer.
///
/// A loading card takes precedence over a completed card waiting for history
/// insertion. Callers should render the returned cell but leave ownership with
/// the widget so completion and insertion can update it safely.
pub(super) fn pending_token_activity_output(&self) -> Option<&dyn HistoryCell> {
self.refreshing_token_activity_output
.as_ref()
.map(|output| &output.cell as &dyn HistoryCell)
.or_else(|| {
self.completed_token_activity_output
.as_ref()
.map(|cell| cell as &dyn HistoryCell)
})
}
/// Applies a background token activity result to its matching transient card.
///
/// Returns `true` when the pending request matched and moved into the completed
/// slot. Late responses return `false`, including responses for cards replaced
/// by a newer `/usage` invocation or cleared during transcript changes.
pub(crate) fn finish_token_activity_refresh(
&mut self,
request_id: u64,
result: Result<GetAccountTokenUsageResponse, String>,
) -> bool {
let Some(output) = self.refreshing_token_activity_output.take() else {
return false;
};
if output.request_id != request_id {
self.refreshing_token_activity_output = Some(output);
return false;
}
output.handle.finish(result);
self.completed_token_activity_output = Some(output.cell);
self.bump_active_cell_revision();
self.request_redraw();
true
}
/// Reports whether completed asynchronous usage output must wait before insertion.
///
/// Inserting while a stream, queued consolidation, or active transcript cell is
/// present can reorder output relative to visible work, so callers retry once
/// these barriers clear.
pub(crate) fn usage_history_insertion_blocked(&self) -> bool {
self.stream_controller.is_some()
|| self.plan_stream_controller.is_some()
|| self.pending_stream_consolidations > 0
|| self.transcript.active_cell.is_some()
|| self.active_hook_cell.is_some()
}
/// Records a stream consolidation barrier that delays token card insertion.
///
/// Each queued consolidation should eventually call
/// [`ChatWidget::note_stream_consolidation_completed`].
pub(crate) fn note_stream_consolidation_queued(&mut self) {
self.pending_stream_consolidations =
self.pending_stream_consolidations.saturating_add(/*rhs*/ 1);
}
/// Releases one queued stream consolidation barrier.
///
/// The counter saturates at zero so an unmatched completion does not underflow,
/// but paired queue/completion calls are still the intended contract.
pub(crate) fn note_stream_consolidation_completed(&mut self) {
self.pending_stream_consolidations =
self.pending_stream_consolidations.saturating_sub(/*rhs*/ 1);
}
/// Transfers the completed token activity card into the history insertion path.
///
/// Callers should use this only after
/// [`ChatWidget::usage_history_insertion_blocked`] returns `false`;
/// taking the card removes it from the transient render area.
pub(crate) fn take_completed_token_activity_output(&mut self) -> Option<CompositeHistoryCell> {
let output = self.completed_token_activity_output.take()?;
self.bump_active_cell_revision();
Some(output)
}
/// Requests another insertion attempt when completed usage output is waiting.
///
/// This is used after stream or history lifecycle events that may have cleared
/// the insertion barriers without directly owning the completed output.
pub(crate) fn request_pending_usage_output_insertion(&self) {
if self.completed_token_activity_output.is_some()
|| self.pending_rate_limit_reset_hint().is_some()
{
self.app_event_tx.send(AppEvent::CommitPendingUsageOutput);
}
}
pub(crate) fn request_pending_usage_output_insertion_after_stream_shutdown(&self) {
if self.completed_token_activity_output.is_some()
|| self.pending_rate_limit_reset_hint().is_some()
{
self.app_event_tx
.send(AppEvent::CommitPendingUsageOutputAfterStreamShutdown);
}
}
/// Drops transient and completed token cards that must no longer update.
///
/// Late background responses cannot mutate cards after a transcript reset,
/// backtrack, or replacement flow clears this widget-owned state.
pub(crate) fn clear_pending_token_activity_refreshes(&mut self) {
let cleared_refresh = self.refreshing_token_activity_output.take().is_some();
let cleared_completed = self.completed_token_activity_output.take().is_some();
if cleared_refresh || cleared_completed {
self.bump_active_cell_revision();
self.request_redraw();
}
}
}
#[cfg(test)]
#[path = "tokens_tests.rs"]
mod tests;