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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
mod i18n;
mod input;
mod model;
mod report;
mod state;
mod terminal;
mod types;
mod view;
pub use i18n::Language;
pub use i18n::{detect_system_language, parse_language};
#[allow(unused_imports)]
pub use model::{ProviderOption, UpstreamSummary, build_provider_options};
use std::io;
use std::sync::Arc;
use std::time::Duration;
use std::time::Instant;
use crossterm::ExecutableCommand;
use crossterm::event::{Event, EventStream};
use crossterm::terminal::LeaveAlternateScreen;
use futures_util::StreamExt;
use ratatui::Terminal;
use ratatui::backend::CrosstermBackend;
use tokio::sync::watch;
use crate::state::ProxyState;
use self::model::{Palette, now_ms, refresh_snapshot};
use self::state::UiState;
use self::terminal::TerminalGuard;
pub async fn run_dashboard(
state: Arc<ProxyState>,
service_name: &'static str,
port: u16,
providers: Vec<ProviderOption>,
language: Language,
shutdown: watch::Sender<bool>,
mut shutdown_rx: watch::Receiver<bool>,
) -> anyhow::Result<()> {
let refresh_ms = std::env::var("CODEX_HELPER_TUI_REFRESH_MS")
.ok()
.and_then(|s| s.trim().parse::<u64>().ok())
.filter(|&n| n > 0)
.unwrap_or(500)
.clamp(100, 5_000);
let io_timeout = Duration::from_millis((refresh_ms / 2).clamp(50, 250));
let mut term_guard = TerminalGuard::enter()?;
let stdout = io::stdout();
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
terminal.hide_cursor()?;
let mut ui = UiState {
service_name,
port,
language,
refresh_ms,
..Default::default()
};
let palette = Palette::default();
let mut events = EventStream::new();
let mut ticker = tokio::time::interval(Duration::from_millis(refresh_ms));
ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
let mut ctrl_c = Box::pin(tokio::signal::ctrl_c());
let mut snapshot = refresh_snapshot(&state, service_name, ui.stats_days).await;
let mut providers = providers;
ui.clamp_selection(&snapshot, providers.len());
let mut should_redraw = true;
let mut last_drawn_page = ui.page;
loop {
if should_redraw {
if ui.page != last_drawn_page {
// Defensive: some terminals occasionally leave stale cells when only a small
// region changes (e.g., switching tabs). A full clear on page switch keeps the
// UI visually consistent without clearing on every tick.
terminal.clear()?;
last_drawn_page = ui.page;
}
terminal.draw(|f| {
view::render_app(
f,
palette,
&mut ui,
&snapshot,
service_name,
port,
&providers,
)
})?;
should_redraw = false;
}
if ui.should_exit || *shutdown_rx.borrow() {
let _ = shutdown.send(true);
break;
}
tokio::select! {
_ = ticker.tick() => {
let refresh = refresh_snapshot(&state, service_name, ui.stats_days);
if let Ok(new_snapshot) = tokio::time::timeout(io_timeout, refresh).await {
snapshot = new_snapshot;
ui.clamp_selection(&snapshot, providers.len());
}
if ui.page == crate::tui::types::Page::Settings
&& ui
.last_runtime_config_refresh_at
.is_none_or(|t| t.elapsed() > Duration::from_secs(1))
{
let url =
format!("http://127.0.0.1:{}/__codex_helper/config/runtime", ui.port);
let fetch = async {
let client = reqwest::Client::new();
client
.get(&url)
.send()
.await?
.error_for_status()?
.json::<serde_json::Value>()
.await
};
if let Ok(Ok(v)) = tokio::time::timeout(io_timeout, fetch).await {
ui.last_runtime_config_loaded_at_ms =
v.get("loaded_at_ms").and_then(|x| x.as_u64());
ui.last_runtime_config_source_mtime_ms =
v.get("source_mtime_ms").and_then(|x| x.as_u64());
ui.last_runtime_retry = v
.get("retry")
.and_then(|x| serde_json::from_value(x.clone()).ok());
}
ui.last_runtime_config_refresh_at = Some(Instant::now());
}
should_redraw = true;
}
changed = shutdown_rx.changed() => {
let _ = changed;
ui.should_exit = true;
let _ = shutdown.send(true);
break;
}
_ = &mut ctrl_c => {
ui.should_exit = true;
let _ = shutdown.send(true);
break;
}
maybe_event = events.next() => {
let Some(Ok(event)) = maybe_event else { continue; };
match event {
Event::Key(key) if input::should_accept_key_event(&key) => {
if input::handle_key_event(state.clone(), &mut providers, &mut ui, &snapshot, key).await {
if ui.needs_snapshot_refresh {
snapshot = refresh_snapshot(&state, service_name, ui.stats_days).await;
ui.clamp_selection(&snapshot, providers.len());
ui.needs_snapshot_refresh = false;
}
if ui.needs_codex_history_refresh {
ui.codex_history_error = None;
match crate::sessions::find_codex_sessions_for_current_dir(200).await {
Ok(list) => {
ui.codex_history_sessions = list;
ui.codex_history_loaded_at_ms = Some(now_ms());
ui.selected_codex_history_idx = 0;
ui.codex_history_table.select(if ui.codex_history_sessions.is_empty() {
None
} else {
Some(0)
});
let count = ui.codex_history_sessions.len();
let zh = format!("history: 已加载 {count} 个会话");
let en = format!("history: loaded {count} sessions");
ui.toast = Some((
crate::tui::i18n::pick(ui.language, &zh, &en).to_string(),
Instant::now(),
));
}
Err(e) => {
ui.codex_history_sessions.clear();
ui.codex_history_loaded_at_ms = Some(now_ms());
ui.codex_history_error = Some(e.to_string());
let zh = format!("history: 加载失败:{e}");
let en = format!("history: load failed: {e}");
ui.toast = Some((
crate::tui::i18n::pick(ui.language, &zh, &en).to_string(),
Instant::now(),
));
}
}
ui.needs_codex_history_refresh = false;
}
if ui.needs_codex_recent_refresh {
ui.codex_recent_error = None;
let since = Duration::from_secs(24 * 60 * 60);
match crate::sessions::find_recent_codex_sessions(since, 500).await {
Ok(list) => {
let mut rows = Vec::with_capacity(list.len());
for s in list {
let cwd_opt = s.cwd.clone();
let cwd = cwd_opt.as_deref().unwrap_or("-");
let root = if ui.codex_recent_raw_cwd {
cwd.to_string()
} else {
crate::sessions::infer_project_root_from_cwd(cwd)
};
let branch = if root.trim().is_empty()
|| root == "-"
|| !std::path::Path::new(&root).exists()
{
None
} else if let Some(v) = ui.codex_recent_branch_cache.get(&root) {
v.clone()
} else {
let v = read_git_branch_shallow(&root).await;
ui.codex_recent_branch_cache
.insert(root.clone(), v.clone());
v
};
rows.push(crate::tui::state::RecentCodexRow {
root,
branch,
session_id: s.id,
cwd: cwd_opt,
mtime_ms: s.mtime_ms,
});
}
ui.codex_recent_rows = rows;
ui.codex_recent_loaded_at_ms = Some(now_ms());
ui.codex_recent_selected_idx = 0;
ui.codex_recent_selected_id = ui
.codex_recent_rows
.first()
.map(|r| r.session_id.clone());
ui.codex_recent_table.select(if ui.codex_recent_rows.is_empty() {
None
} else {
Some(0)
});
let count = ui.codex_recent_rows.len();
let zh = format!("recent: 已加载 {count} 个会话");
let en = format!("recent: loaded {count} sessions");
ui.toast = Some((
crate::tui::i18n::pick(ui.language, &zh, &en)
.to_string(),
Instant::now(),
));
}
Err(e) => {
ui.codex_recent_rows.clear();
ui.codex_recent_loaded_at_ms = Some(now_ms());
ui.codex_recent_error = Some(e.to_string());
let zh = format!("recent: 加载失败:{e}");
let en = format!("recent: load failed: {e}");
ui.toast = Some((
crate::tui::i18n::pick(ui.language, &zh, &en)
.to_string(),
Instant::now(),
));
}
}
ui.needs_codex_recent_refresh = false;
}
should_redraw = true;
}
}
Event::Resize(_, _) => {
should_redraw = true;
}
_ => {}
}
}
}
}
terminal.show_cursor()?;
crossterm::terminal::disable_raw_mode()?;
terminal.backend_mut().execute(LeaveAlternateScreen)?;
term_guard.disarm();
Ok(())
}
async fn read_git_branch_shallow(workdir: &str) -> Option<String> {
use tokio::fs;
let root = std::path::PathBuf::from(workdir);
if !root.is_absolute() {
return None;
}
let dot_git = root.join(".git");
if !dot_git.exists() {
return None;
}
let gitdir = if dot_git.is_dir() {
dot_git
} else {
let content = fs::read_to_string(&dot_git).await.ok()?;
let first = content.lines().next()?.trim();
let path = first.strip_prefix("gitdir:")?.trim();
let mut p = std::path::PathBuf::from(path);
if p.is_relative() {
p = root.join(p);
}
p
};
let head = fs::read_to_string(gitdir.join("HEAD")).await.ok()?;
let head = head.lines().next().unwrap_or("").trim();
if let Some(r) = head.strip_prefix("ref:") {
let r = r.trim();
return Some(r.rsplit('/').next().unwrap_or(r).to_string());
}
if head.len() >= 8 {
Some(head[..8].to_string())
} else if head.is_empty() {
None
} else {
Some(head.to_string())
}
}