abstract-cli 0.1.9

A high-performance Rust-native CLI coding agent
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
//! Main TUI event loop — multiplexes agent streaming, terminal input, mouse, and paste.

use crate::config::AppConfig;
use crate::tui::{
    app::{AppState, Overlay, SidePanelTab, ToolCall, ToolStatus},
    layout,
    theme::Theme,
    widgets::{footer, header, input, messages, overlay, side_panel, status},
    Terminal,
};
use cersei::events::{AgentEvent, AgentStream};
use cersei::Agent;
use cersei_memory::manager::MemoryManager;
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyModifiers};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio_util::sync::CancellationToken;

const TICK_RATE: Duration = Duration::from_millis(16); // ~62 FPS

pub async fn run(
    terminal: &mut Terminal,
    agent: Arc<Agent>,
    config: &AppConfig,
    _memory_manager: &MemoryManager,
    session_id: &str,
    cancel_token: CancellationToken,
    shared_mode: crate::permissions::SharedPermissionMode,
    mut permission_rx: tokio::sync::mpsc::Receiver<crate::permissions::TuiPermissionRequest>,
) -> anyhow::Result<()> {
    let theme = Theme::from_name(&config.theme);
    let mut state = AppState::new(&config.model, session_id, &config.effort);
    state.set_shared_mode(shared_mode);
    let mut agent_stream: Option<AgentStream> = None;

    // Initial render
    draw(terminal, &mut state, &theme)?;

    loop {
        if state.should_quit {
            break;
        }

        tokio::select! {
            // ── Permission requests from agent ──────────────────────────
            Some(perm_req) = permission_rx.recv() => {
                state.overlay = Overlay::Permission(crate::tui::app::PermissionOverlay {
                    tool_name: perm_req.tool_name,
                    description: perm_req.description,
                    selected: 0,
                });
                state.pending_permission_tx = Some(perm_req.response_tx);
                state.dirty = true;
            }

            // ── Agent stream events ─────────────────────────────────────
            event = poll_agent_stream(&mut agent_stream) => {
                match event {
                    Some(agent_event) => {
                        handle_agent_event(&mut state, agent_event);
                    }
                    None => {
                        if state.is_streaming {
                            state.commit_turn();
                            state.is_streaming = false;
                        }
                        agent_stream = None;
                    }
                }
                state.dirty = true;
            }

            // ── Terminal events + tick ───────────────────────────────────
            _ = tokio::time::sleep(TICK_RATE) => {
                // Drain pending events (cap at 50 to prevent infinite loop on resize storms)
                let mut event_count = 0u32;
                while event_count < 50 && event::poll(Duration::ZERO)? {
                    event_count += 1;
                    match event::read()? {
                        Event::Key(key) => {
                            if let Some(prompt) = handle_key(&mut state, key, config, &cancel_token) {
                                state.push_user(&prompt);
                                state.is_streaming = true;
                                state.stream_start = Some(Instant::now());
                                state.scroll.scroll_to_bottom();
                                agent_stream = Some(agent.run_stream(&prompt));
                            }
                            state.dirty = true;
                        }
                        Event::Mouse(_) => {
                            // Mouse capture disabled to allow native text selection
                        }
                        Event::Paste(text) => {
                            if !state.is_streaming {
                                state.input.insert_str(state.cursor_pos, &text);
                                state.cursor_pos += text.len();
                                state.dirty = true;
                            }
                        }
                        Event::Resize(_, _) => {
                            // Re-push keyboard enhancement after resize
                            // (some terminals drop the protocol on resize)
                            let _ = crossterm::execute!(
                                std::io::stdout(),
                                crossterm::event::PushKeyboardEnhancementFlags(
                                    crossterm::event::KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES
                                )
                            );
                            state.dirty = true;
                        }
                        _ => {}
                    }
                }
                state.frame_count += 1;
            }
        }

        if state.dirty || state.is_streaming {
            draw(terminal, &mut state, &theme)?;
            state.dirty = false;
        }
    }

    Ok(())
}

async fn poll_agent_stream(stream: &mut Option<AgentStream>) -> Option<AgentEvent> {
    match stream {
        Some(ref mut s) => s.next().await,
        None => std::future::pending().await,
    }
}

fn draw(terminal: &mut Terminal, state: &mut AppState, theme: &Theme) -> anyhow::Result<()> {
    // Compute input height with terminal width
    let term_width = terminal.size()?.width;
    let input_h = input::desired_height(&state.input, term_width.saturating_sub(4));

    terminal.draw(|f| {
        let layout = layout::compute(f.area(), input_h, state.side_panel_open);

        header::render(f, layout.main.header, state, theme);
        messages::render(f, layout.main.messages, state, theme);
        status::render(f, layout.main.status, state, theme);
        input::render(f, layout.main.input, state, theme);
        footer::render(
            f,
            layout.main.footer,
            state.is_streaming,
            state.side_panel_open,
            state.side_panel_focused,
            theme,
        );

        // Side panel
        if let Some(panel_area) = layout.side_panel {
            side_panel::render(f, panel_area, state, theme);
        }

        // Overlay on top
        overlay::render(f, state, theme);
    })?;
    Ok(())
}

/// Handle a key event. Returns Some(prompt) if the user submitted input.
fn handle_key(
    state: &mut AppState,
    key: KeyEvent,
    config: &AppConfig,
    cancel_token: &CancellationToken,
) -> Option<String> {
    // Handle overlay-specific keys first
    if state.overlay != Overlay::None {
        handle_overlay_key(state, key);
        return None;
    }

    // ── Side panel focused: j/k scroll, Tab switches tabs, Esc returns focus ──
    if state.side_panel_focused {
        match key.code {
            KeyCode::Char('j') => {
                state.side_panel_scroll.scroll_down(1);
            }
            KeyCode::Char('k') => {
                state.side_panel_scroll.scroll_up(1);
            }
            KeyCode::Char('d') => {
                state.side_panel_scroll.page_down();
            }
            KeyCode::Char('u') => {
                state.side_panel_scroll.page_up();
            }
            KeyCode::Char('g') => {
                state
                    .side_panel_scroll
                    .scroll_up(state.side_panel_scroll.content_height);
            }
            KeyCode::Char('G') => {
                state.side_panel_scroll.scroll_to_bottom();
            }
            KeyCode::Tab => {
                state.side_panel_tab = state.side_panel_tab.toggle();
            }
            KeyCode::Char('r') => {
                side_panel::refresh_content(state, &config.working_dir);
            }
            KeyCode::Esc | KeyCode::Char('q') => {
                state.side_panel_focused = false;
            }
            _ => {
                // Global keys still work
                match (key.modifiers, key.code) {
                    (KeyModifiers::CONTROL, KeyCode::Char('b')) => {
                        state.side_panel_open = false;
                        state.side_panel_focused = false;
                    }
                    (KeyModifiers::CONTROL, KeyCode::Char('d')) => {
                        state.should_quit = true;
                    }
                    (KeyModifiers::CONTROL, KeyCode::Char('c')) => {
                        if state.is_streaming {
                            cancel_token.cancel();
                            state.is_streaming = false;
                            state.commit_turn();
                        }
                    }
                    _ => {}
                }
            }
        }
        return None;
    }

    match (key.modifiers, key.code) {
        // Ctrl+D — exit
        (KeyModifiers::CONTROL, KeyCode::Char('d')) => {
            state.should_quit = true;
        }

        // Ctrl+C — cancel or quit
        (KeyModifiers::CONTROL, KeyCode::Char('c')) => {
            if state.is_streaming {
                cancel_token.cancel();
                state.is_streaming = false;
                state.commit_turn();
            } else if state.input.is_empty() {
                state.should_quit = true;
            } else {
                state.input.clear();
                state.cursor_pos = 0;
            }
        }

        // Ctrl+B — toggle side panel (and focus it)
        (KeyModifiers::CONTROL, KeyCode::Char('b')) => {
            if state.side_panel_open && !state.side_panel_focused {
                // Panel open but not focused — focus it
                state.side_panel_focused = true;
            } else if state.side_panel_focused {
                // Focused — close panel
                state.side_panel_open = false;
                state.side_panel_focused = false;
            } else {
                // Closed — open and focus
                state.side_panel_open = true;
                state.side_panel_focused = true;
                side_panel::refresh_content(state, &config.working_dir);
            }
        }

        // Shift+Tab — cycle permission mode
        (KeyModifiers::SHIFT, KeyCode::BackTab) => {
            state.cycle_permission_mode();
        }

        // Tab — switch side panel tabs (when panel open but not focused)
        (_, KeyCode::Tab) if state.side_panel_open => {
            state.side_panel_tab = state.side_panel_tab.toggle();
        }

        // Scroll messages
        (_, KeyCode::PageUp) => {
            state.scroll.page_up();
        }
        (_, KeyCode::PageDown) => {
            state.scroll.page_down();
        }
        (_, KeyCode::Home) => {
            state.scroll.scroll_up(state.scroll.content_height);
        }
        (_, KeyCode::End) => {
            state.scroll.scroll_to_bottom();
        }

        // Alt+Enter / Ctrl+J / Shift+Enter — insert newline
        (KeyModifiers::ALT, KeyCode::Enter) if !state.is_streaming => {
            state.input.insert(state.cursor_pos, '\n');
            state.cursor_pos += 1;
        }
        (KeyModifiers::SHIFT, KeyCode::Enter) if !state.is_streaming => {
            state.input.insert(state.cursor_pos, '\n');
            state.cursor_pos += 1;
        }
        (KeyModifiers::CONTROL, KeyCode::Char('j')) if !state.is_streaming => {
            state.input.insert(state.cursor_pos, '\n');
            state.cursor_pos += 1;
        }

        // Enter — submit input
        (_, KeyCode::Enter) if !state.is_streaming => {
            let input_text = state.input.trim().to_string();
            if input_text.is_empty() {
                return None;
            }

            state.input.clear();
            state.cursor_pos = 0;

            if input_text.starts_with('/') {
                handle_slash_command(state, &input_text, config);
                return None;
            }

            state.input_history.push(input_text.clone());
            state.history_index = None;
            return Some(input_text);
        }

        // Backspace
        (_, KeyCode::Backspace) if !state.is_streaming => {
            if state.cursor_pos > 0 {
                state.cursor_pos -= 1;
                state.input.remove(state.cursor_pos);
            }
        }

        // Delete
        (_, KeyCode::Delete) if !state.is_streaming => {
            if state.cursor_pos < state.input.len() {
                state.input.remove(state.cursor_pos);
            }
        }

        // Left arrow
        (_, KeyCode::Left) if !state.is_streaming => {
            if state.cursor_pos > 0 {
                state.cursor_pos -= 1;
            }
        }

        // Right arrow
        (_, KeyCode::Right) if !state.is_streaming => {
            if state.cursor_pos < state.input.len() {
                state.cursor_pos += 1;
            }
        }

        // Up arrow — scroll if input empty, else history
        (_, KeyCode::Up) if !state.is_streaming => {
            if state.input.is_empty() && state.history_index.is_none() {
                state.scroll.scroll_up(1);
            } else if !state.input_history.is_empty() {
                let idx = state
                    .history_index
                    .map(|i| i.saturating_sub(1))
                    .unwrap_or(state.input_history.len() - 1);
                state.history_index = Some(idx);
                state.input = state.input_history[idx].clone();
                state.cursor_pos = state.input.len();
            }
        }
        (_, KeyCode::Up) if state.is_streaming => {
            state.scroll.scroll_up(1);
        }

        // Down arrow — scroll if input empty, else history
        (_, KeyCode::Down) if !state.is_streaming => {
            if let Some(idx) = state.history_index {
                if idx + 1 < state.input_history.len() {
                    let new_idx = idx + 1;
                    state.history_index = Some(new_idx);
                    state.input = state.input_history[new_idx].clone();
                    state.cursor_pos = state.input.len();
                } else {
                    state.history_index = None;
                    state.input.clear();
                    state.cursor_pos = 0;
                }
            } else if state.input.is_empty() {
                state.scroll.scroll_down(1);
            }
        }
        (_, KeyCode::Down) if state.is_streaming => {
            state.scroll.scroll_down(1);
        }

        // Esc
        (_, KeyCode::Esc) => {
            if state.overlay != Overlay::None {
                state.overlay = Overlay::None;
            }
        }

        // Character input
        (_, KeyCode::Char(c)) if !state.is_streaming => {
            state.input.insert(state.cursor_pos, c);
            state.cursor_pos += 1;
        }

        _ => {}
    }

    None
}

fn handle_agent_event(state: &mut AppState, event: AgentEvent) {
    match event {
        AgentEvent::TextDelta(text) => {
            state.streaming_text.push_str(&text);
        }
        AgentEvent::ThinkingDelta(text) => {
            state.streaming_thinking.push_str(&text);
        }
        AgentEvent::ToolStart {
            name, id: _, input, ..
        } => {
            let summary = tool_input_summary(&name, &input);
            state.active_tools.push(ToolCall {
                name,
                input_summary: summary,
                status: ToolStatus::Running,
                output_preview: None,
                started_at: Instant::now(),
                duration_ms: None,
            });
            state.tool_count += 1;
        }
        AgentEvent::ToolEnd {
            name,
            id: _,
            result,
            is_error,
            duration,
        } => {
            if let Some(tool) = state.active_tools.iter_mut().rev().find(|t| t.name == name) {
                tool.status = if is_error {
                    ToolStatus::Error
                } else {
                    ToolStatus::Done
                };
                tool.duration_ms = Some(duration.as_millis() as u64);
                tool.output_preview = Some(result.chars().take(200).collect());
            }
        }
        AgentEvent::CostUpdate {
            cumulative_cost,
            input_tokens,
            output_tokens,
            ..
        } => {
            state.input_tokens = input_tokens;
            state.output_tokens = output_tokens;
            // Use reported cost or estimate from model pricing
            state.cost_usd = if cumulative_cost > 0.0 {
                cumulative_cost
            } else {
                cersei_tools::estimate_cost(&state.model, input_tokens, output_tokens)
            };
        }
        AgentEvent::TurnComplete { usage, .. } => {
            state.input_tokens = usage.input_tokens;
            state.output_tokens = usage.output_tokens;
            state.cost_usd = usage.cost_usd.filter(|c| *c > 0.0).unwrap_or_else(|| {
                cersei_tools::estimate_cost(&state.model, usage.input_tokens, usage.output_tokens)
            });
        }
        AgentEvent::TokenWarning { pct_used, .. } => {
            state.context_pct = pct_used;
        }
        AgentEvent::Error(msg) => {
            state.commit_turn();
            state.is_streaming = false;
            state.turns.push(crate::tui::app::Turn {
                role: crate::tui::app::TurnRole::System,
                content: format!("Error: {msg}"),
                tools: Vec::new(),
                thinking: None,
            });
        }
        AgentEvent::Complete(_) => {
            state.commit_turn();
            state.is_streaming = false;
        }
        _ => {}
    }
}

fn handle_overlay_key(state: &mut AppState, key: KeyEvent) {
    use cersei_tools::permissions::PermissionDecision;

    match key.code {
        KeyCode::Esc => {
            // Dismiss overlay — for permissions, send Deny
            if matches!(state.overlay, Overlay::Permission(_)) {
                if let Some(tx) = state.pending_permission_tx.take() {
                    let _ = tx.send(PermissionDecision::Deny("User cancelled".into()));
                }
            }
            state.overlay = Overlay::None;
        }
        KeyCode::Up => match &mut state.overlay {
            Overlay::Permission(p) => {
                p.selected = p.selected.saturating_sub(1);
            }
            Overlay::Recovery(r) => {
                r.selected = r.selected.saturating_sub(1);
            }
            Overlay::Graph(g) => {
                g.select_prev();
            }
            _ => {}
        },
        KeyCode::Down => match &mut state.overlay {
            Overlay::Permission(p) => {
                p.selected = (p.selected + 1).min(3);
            }
            Overlay::Recovery(r) => {
                if !r.options.is_empty() {
                    r.selected = (r.selected + 1).min(r.options.len() - 1);
                }
            }
            Overlay::Graph(g) => {
                g.select_next();
            }
            _ => {}
        },
        KeyCode::Left => {
            if let Overlay::Graph(g) = &mut state.overlay {
                g.pan_x -= 3;
            }
        }
        KeyCode::Right => {
            if let Overlay::Graph(g) = &mut state.overlay {
                g.pan_x += 3;
            }
        }
        KeyCode::Enter => {
            // For permission overlays, send the selected decision
            if let Overlay::Permission(ref p) = state.overlay {
                if let Some(tx) = state.pending_permission_tx.take() {
                    let decision = match p.selected {
                        0 => PermissionDecision::AllowOnce,
                        1 => PermissionDecision::AllowForSession,
                        2 => PermissionDecision::Allow, // Always
                        _ => PermissionDecision::Deny("User denied".into()),
                    };
                    let _ = tx.send(decision);
                }
            }
            state.overlay = Overlay::None;
        }
        _ => {}
    }
}

fn handle_slash_command(state: &mut AppState, input: &str, config: &AppConfig) {
    let cmd = input
        .trim_start_matches('/')
        .split_whitespace()
        .next()
        .unwrap_or("");
    match cmd {
        "help" | "h" | "?" => {
            state.overlay = Overlay::Help;
        }
        "clear" => {
            state.turns.clear();
            state.streaming_text.clear();
            state.streaming_thinking.clear();
            state.active_tools.clear();
        }
        "exit" | "quit" | "q" => {
            state.should_quit = true;
        }
        "panel" => {
            state.side_panel_open = !state.side_panel_open;
            if state.side_panel_open {
                side_panel::refresh_content(state, &config.working_dir);
            }
        }
        "graph" => {
            use crate::tui::widgets::graph::{GraphOverlayState, MemoryGraphData, MemoryNode};
            // Build graph data from available info
            let mut data = MemoryGraphData::default();
            // Add LSP servers as nodes
            for server in cersei_lsp::config::builtin_servers() {
                if which::which(&server.command).is_ok() {
                    data.lsp_servers.push(server.name.clone());
                }
            }
            let graph_state = GraphOverlayState::from_memory_stats(&data);
            state.overlay = Overlay::Graph(graph_state);
        }
        "diff" => {
            state.side_panel_open = true;
            state.side_panel_focused = true;
            state.side_panel_tab = SidePanelTab::GitDiff;
            side_panel::refresh_content(state, &config.working_dir);
        }
        "files" | "tree" => {
            state.side_panel_open = true;
            state.side_panel_focused = true;
            state.side_panel_tab = SidePanelTab::FileTree;
            side_panel::refresh_content(state, &config.working_dir);
        }
        "rewind" => {
            // Remove the last assistant turn (rewind one step)
            if let Some(pos) = state
                .turns
                .iter()
                .rposition(|t| t.role == crate::tui::app::TurnRole::Assistant)
            {
                let removed = state.turns.len() - pos;
                state.turns.truncate(pos);
                state.turns.push(crate::tui::app::Turn {
                    role: crate::tui::app::TurnRole::System,
                    content: format!(
                        "Rewound {removed} turn(s). You can now re-send your last message."
                    ),
                    tools: Vec::new(),
                    thinking: None,
                });
            } else {
                state.turns.push(crate::tui::app::Turn {
                    role: crate::tui::app::TurnRole::System,
                    content: "Nothing to rewind.".into(),
                    tools: Vec::new(),
                    thinking: None,
                });
            }
        }
        "undo" => {
            // Undo last file modification via snapshot manager
            let snapshots = cersei_tools::file_snapshot::session_snapshots(&state.session_id);
            let mut mgr = snapshots.lock();
            let files = mgr.modified_files();
            if let Some(last_file) = files.last() {
                let path = last_file.display().to_string();
                if mgr.undo_last(std::path::Path::new(&path)).is_some() {
                    state.turns.push(crate::tui::app::Turn {
                        role: crate::tui::app::TurnRole::System,
                        content: format!("Undid last change to {path}"),
                        tools: Vec::new(),
                        thinking: None,
                    });
                } else {
                    state.turns.push(crate::tui::app::Turn {
                        role: crate::tui::app::TurnRole::System,
                        content: "Failed to undo.".into(),
                        tools: Vec::new(),
                        thinking: None,
                    });
                }
            } else {
                state.turns.push(crate::tui::app::Turn {
                    role: crate::tui::app::TurnRole::System,
                    content: "No file changes to undo.".into(),
                    tools: Vec::new(),
                    thinking: None,
                });
            }
        }
        "memory" | "mem" => {
            state.turns.push(crate::tui::app::Turn {
                role: crate::tui::app::TurnRole::System,
                content: "Memory is injected into the system prompt automatically.\nUse AGENTS.md or .abstract/instructions.md in your project for persistent instructions.".into(),
                tools: Vec::new(),
                thinking: None,
            });
        }
        "sessions" | "session" | "ls" => {
            state.turns.push(crate::tui::app::Turn {
                role: crate::tui::app::TurnRole::System,
                content: format!("Current session: {}\nUse `abstract sessions list` from the terminal to manage sessions.", state.session_id),
                tools: Vec::new(),
                thinking: None,
            });
        }
        "model" => {
            state.turns.push(crate::tui::app::Turn {
                role: crate::tui::app::TurnRole::System,
                content: format!(
                    "Current model: {}\nChange with: abstract --model <provider/model>",
                    state.model
                ),
                tools: Vec::new(),
                thinking: None,
            });
        }
        "cost" => {
            // Estimate cost if provider didn't report it
            let cost = if state.cost_usd > 0.0 {
                state.cost_usd
            } else {
                cersei_tools::estimate_cost(&state.model, state.input_tokens, state.output_tokens)
            };
            state.turns.push(crate::tui::app::Turn {
                role: crate::tui::app::TurnRole::System,
                content: format!(
                    "Session cost: ${:.4}\nInput: {} tokens | Output: {} tokens\nTurns: {} | Tools: {}",
                    cost, state.input_tokens, state.output_tokens,
                    state.turn_count, state.tool_count
                ),
                tools: Vec::new(),
                thinking: None,
            });
        }
        "compact" => {
            state.turns.push(crate::tui::app::Turn {
                role: crate::tui::app::TurnRole::System,
                content: "Compaction will run automatically at 90% context usage.".into(),
                tools: Vec::new(),
                thinking: None,
            });
        }
        "proxy" => {
            let proxy_url = &config.proxy.url;
            let is_via_proxy = state.model.contains("via proxy");

            // Check for authenticated accounts
            let mut accounts = Vec::new();
            if let Some(home) = dirs::home_dir() {
                let auth_dir = home.join(".cli-proxy-api");
                if auth_dir.exists() {
                    if let Ok(entries) = std::fs::read_dir(&auth_dir) {
                        for entry in entries.flatten() {
                            let name = entry.file_name().to_string_lossy().to_string();
                            if name.ends_with(".json") {
                                if let Ok(content) = std::fs::read_to_string(entry.path()) {
                                    if let Ok(json) =
                                        serde_json::from_str::<serde_json::Value>(&content)
                                    {
                                        let provider = json["type"].as_str().unwrap_or("?");
                                        let email = json["email"].as_str().unwrap_or("?");
                                        let expired = json["expired"].as_str().unwrap_or("?");
                                        accounts.push(format!(
                                            "  {} ({}) — expires {}",
                                            provider, email, expired
                                        ));
                                    }
                                }
                            }
                        }
                    }
                }
            }

            let status = if is_via_proxy { "active" } else { "inactive" };
            let accounts_str = if accounts.is_empty() {
                "  No accounts found in ~/.cli-proxy-api/".to_string()
            } else {
                accounts.join("\n")
            };

            state.turns.push(crate::tui::app::Turn {
                role: crate::tui::app::TurnRole::System,
                content: format!(
                    "Proxy: {status}\nURL: {proxy_url}\nAccounts:\n{accounts_str}\n\nConfigure in .abstract/config.toml:\n[proxy]\nenabled = true\nurl = \"http://localhost:8317/v1\""
                ),
                tools: Vec::new(),
                thinking: None,
            });
        }
        _ => {
            state.turns.push(crate::tui::app::Turn {
                role: crate::tui::app::TurnRole::System,
                content: format!("Unknown command: /{cmd}. Type /help for commands."),
                tools: Vec::new(),
                thinking: None,
            });
        }
    }
}

fn tool_input_summary(name: &str, input: &serde_json::Value) -> String {
    match name {
        "Bash" | "bash" => input
            .get("command")
            .and_then(|v| v.as_str())
            .map(|s| truncate(s, 60))
            .unwrap_or_default(),
        "Read" | "Write" | "Edit" => input
            .get("file_path")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string(),
        "Glob" => input
            .get("pattern")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string(),
        "Grep" => input
            .get("pattern")
            .and_then(|v| v.as_str())
            .map(|s| truncate(s, 40))
            .unwrap_or_default(),
        "LSP" => {
            let action = input.get("action").and_then(|v| v.as_str()).unwrap_or("?");
            let file = input.get("file").and_then(|v| v.as_str()).unwrap_or("?");
            format!("{action} {file}")
        }
        _ => truncate(&serde_json::to_string(input).unwrap_or_default(), 60),
    }
}

fn truncate(s: &str, max: usize) -> String {
    if s.len() <= max {
        s.to_string()
    } else {
        format!("{}...", &s[..max])
    }
}