oxi-cli 0.61.0

Terminal-based AI coding assistant — multi-provider, streaming-first, extensible
Documentation
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
//! Key dispatch for the Agent Hub overlay.
//!
//! The dispatcher is view-aware: the table view binds row navigation +
//! transcript-entry, the transcript view binds scroll/tail-follow. Both
//! views share `Esc` / `q` (back / close).

use crossterm::event::{KeyCode, KeyEvent, KeyEventKind};

use super::state::{HubState, HubView};

/// Result of handling one key inside the overlay.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HubAction {
    /// Nothing happened (other key, or navigation that doesn't change view).
    None,
    /// Close the overlay entirely (table view, `Esc`/`q`).
    Close,
    /// Switch into transcript view for the named agent id.
    OpenTranscript(String),
}

/// Page step for `PageDown` / `PageUp` — matches the issues_panel convention.
const PAGE_STEP: isize = 10;
/// Sentinel stored in `transcript_scroll` when the user has not yet scrolled
/// away from the tail-follow default. The renderer treats this as "pin to
/// the last `window` lines". Handlers step from here with plain `isize`
/// arithmetic — `0 - 1 == -1` is the natural one-line-up-from-tail offset.
pub const FOLLOW_TAIL: isize = 0;
/// Sentinel stored in `transcript_scroll` when the user pressed `g` to jump
/// to the top of history. Renders as `start = 0`.
pub const JUMP_TOP: isize = isize::MAX;

/// Top-level dispatch — picks the per-view handler.
pub fn handle_key(state: &mut HubState, key: KeyEvent) -> HubAction {
    // Release events don't drive this overlay.
    if key.kind != KeyEventKind::Press {
        return HubAction::None;
    }
    match &state.view {
        HubView::Table => handle_table_key(state, key),
        HubView::Transcript { .. } => handle_transcript_key(state, key),
    }
}

/// Table view keys: row navigation, Enter to open transcript, close.
fn handle_table_key(state: &mut HubState, key: KeyEvent) -> HubAction {
    match key.code {
        KeyCode::Char('j') | KeyCode::Down => {
            if !state.rows.is_empty() {
                // Clamp at len-1 so the highlight sits on the last row.
                let max = state.rows.len() - 1;
                if state.selected < max {
                    state.selected += 1;
                }
            }
            HubAction::None
        }
        KeyCode::Char('k') | KeyCode::Up => {
            state.selected = state.selected.saturating_sub(1);
            HubAction::None
        }
        KeyCode::Enter => {
            if let Some(row) = state.rows.get(state.selected) {
                HubAction::OpenTranscript(row.id.clone())
            } else {
                HubAction::None
            }
        }
        KeyCode::Esc | KeyCode::Char('q') => HubAction::Close,
        _ => HubAction::None,
    }
}

/// Transcript view keys: scroll, tail-follow toggle, back to table.
fn handle_transcript_key(state: &mut HubState, key: KeyEvent) -> HubAction {
    match key.code {
        KeyCode::Esc | KeyCode::Char('q') => {
            state.view = HubView::Table;
            // Reset scroll for next transcript visit.
            state.transcript_scroll = FOLLOW_TAIL;
            state.transcript_follow = true;
            HubAction::None
        }
        KeyCode::Char('f') => {
            // Toggling follow: when enabling, snap back to the FOLLOW_TAIL
            // sentinel. When disabling, leave the offset alone — the renderer
            // re-derives the window from the current integer offset.
            state.transcript_follow = !state.transcript_follow;
            if state.transcript_follow {
                state.transcript_scroll = FOLLOW_TAIL;
            }
            HubAction::None
        }
        KeyCode::Char('j') | KeyCode::Down => {
            // Tail direction. At FOLLOW_TAIL we are already pinned to the
            // tail — further "down" has no meaning, so no-op.
            if state.transcript_follow {
                HubAction::None
            } else {
                state.transcript_scroll = state.transcript_scroll.saturating_add(1);
                HubAction::None
            }
        }
        KeyCode::Char('k') | KeyCode::Up => {
            // Away from tail. Works from FOLLOW_TAIL (0 - 1 = -1, one line
            // below the tail) and from JUMP_TOP (MAX - 1 saturates to MAX,
            // still at the top — no visible movement; intentional, you have
            // already paged all the way to the head).
            state.transcript_follow = false;
            state.transcript_scroll = state.transcript_scroll.saturating_sub(1);
            HubAction::None
        }
        KeyCode::PageDown => {
            // Tail direction. At FOLLOW_TAIL we are already pinned to the
            // tail — further "down" has no meaning, so no-op.
            if state.transcript_follow {
                HubAction::None
            } else {
                state.transcript_scroll = state.transcript_scroll.saturating_add(PAGE_STEP);
                HubAction::None
            }
        }
        KeyCode::PageUp => {
            // Away from tail. Works from FOLLOW_TAIL (0 - 10 = -10, ten
            // lines below the tail) and from JUMP_TOP (saturates).
            state.transcript_follow = false;
            state.transcript_scroll = state.transcript_scroll.saturating_sub(PAGE_STEP);
            HubAction::None
        }
        KeyCode::Char('G') => {
            // Jump to tail and re-engage follow.
            state.transcript_scroll = FOLLOW_TAIL;
            state.transcript_follow = true;
            HubAction::None
        }
        KeyCode::Char('g') => {
            // Jump to top of history; follow disengages.
            state.transcript_scroll = JUMP_TOP;
            state.transcript_follow = false;
            HubAction::None
        }
        _ => HubAction::None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tui::overlay::agent_hub::state::HubRow;
    use crossterm::event::KeyModifiers;
    use oxi_sdk::{HubKind, HubStatus};
    use std::collections::HashMap;
    use std::path::PathBuf;

    fn press(code: KeyCode) -> KeyEvent {
        let mut k = KeyEvent::new(code, KeyModifiers::NONE);
        k.kind = KeyEventKind::Press;
        k
    }

    fn state_with_rows(n: usize) -> HubState {
        let rows = (0..n)
            .map(|i| HubRow {
                id: format!("agent-{i}"),
                kind: HubKind::Subagent,
                status: HubStatus::Running,
                display_name: format!("agent-{i}"),
                current_task: None,
                age_text: "0s ago".into(),
                session_file: Some(PathBuf::from("/tmp/x.jsonl")),
            })
            .collect();
        HubState {
            rows,
            view: HubView::Table,
            selected: 0,
            row_order: HashMap::new(),
            transcript_scroll: 0,
            transcript_follow: true,
        }
    }

    #[test]
    fn j_moves_selection_down_and_clamps_at_last() {
        let mut s = state_with_rows(2);
        assert_eq!(
            handle_key(&mut s, press(KeyCode::Char('j'))),
            HubAction::None
        );
        assert_eq!(s.selected, 1);
        // Second j clamps at len-1 (no wrap).
        assert_eq!(
            handle_key(&mut s, press(KeyCode::Char('j'))),
            HubAction::None
        );
        assert_eq!(s.selected, 1);
    }

    #[test]
    fn down_arrow_moves_selection() {
        let mut s = state_with_rows(3);
        assert_eq!(handle_key(&mut s, press(KeyCode::Down)), HubAction::None);
        assert_eq!(s.selected, 1);
    }

    #[test]
    fn k_moves_selection_up_and_saturates_at_zero() {
        let mut s = state_with_rows(2);
        s.selected = 1;
        assert_eq!(
            handle_key(&mut s, press(KeyCode::Char('k'))),
            HubAction::None
        );
        assert_eq!(s.selected, 0);
        // Past the top stays at 0.
        assert_eq!(
            handle_key(&mut s, press(KeyCode::Char('k'))),
            HubAction::None
        );
        assert_eq!(s.selected, 0);
    }

    #[test]
    fn up_arrow_moves_selection_up() {
        let mut s = state_with_rows(2);
        s.selected = 1;
        assert_eq!(handle_key(&mut s, press(KeyCode::Up)), HubAction::None);
        assert_eq!(s.selected, 0);
    }

    #[test]
    fn enter_opens_transcript_for_selected_row() {
        let mut s = state_with_rows(3);
        s.selected = 1;
        assert_eq!(
            handle_key(&mut s, press(KeyCode::Enter)),
            HubAction::OpenTranscript("agent-1".into())
        );
    }

    #[test]
    fn esc_closes_from_table() {
        let mut s = state_with_rows(1);
        assert_eq!(handle_key(&mut s, press(KeyCode::Esc)), HubAction::Close);
        assert_eq!(
            handle_key(&mut s, press(KeyCode::Char('q'))),
            HubAction::Close
        );
    }

    #[test]
    fn esc_returns_to_table_from_transcript() {
        let mut s = state_with_rows(2);
        s.view = HubView::Transcript {
            agent_id: "agent-0".into(),
        };
        assert_eq!(handle_key(&mut s, press(KeyCode::Esc)), HubAction::None);
        assert!(matches!(s.view, HubView::Table));
    }

    #[test]
    fn q_returns_to_table_from_transcript() {
        let mut s = state_with_rows(2);
        s.view = HubView::Transcript {
            agent_id: "agent-0".into(),
        };
        assert_eq!(
            handle_key(&mut s, press(KeyCode::Char('q'))),
            HubAction::None
        );
        assert!(matches!(s.view, HubView::Table));
    }

    #[test]
    fn f_toggles_tail_follow() {
        let mut s = state_with_rows(1);
        s.view = HubView::Transcript {
            agent_id: "agent-0".into(),
        };
        s.transcript_follow = false;
        s.transcript_scroll = 5;
        // `f` re-enables tail-follow and snaps scroll to the sentinel.
        assert_eq!(
            handle_key(&mut s, press(KeyCode::Char('f'))),
            HubAction::None
        );
        assert!(s.transcript_follow);
        assert_eq!(s.transcript_scroll, FOLLOW_TAIL);
        // `f` again disables follow; the offset stays at FOLLOW_TAIL until
        // the user moves with a directional key.
        assert_eq!(
            handle_key(&mut s, press(KeyCode::Char('f'))),
            HubAction::None
        );
        assert!(!s.transcript_follow);
        assert_eq!(s.transcript_scroll, FOLLOW_TAIL);
    }

    #[test]
    fn g_jumps_to_start_and_disables_follow() {
        let mut s = state_with_rows(1);
        s.view = HubView::Transcript {
            agent_id: "agent-0".into(),
        };
        s.transcript_scroll = 5;
        s.transcript_follow = true;
        assert_eq!(
            handle_key(&mut s, press(KeyCode::Char('g'))),
            HubAction::None
        );
        assert_eq!(s.transcript_scroll, JUMP_TOP);
        assert!(!s.transcript_follow);
    }

    #[test]
    fn capital_g_jumps_to_tail_and_reenables_follow() {
        let mut s = state_with_rows(1);
        s.view = HubView::Transcript {
            agent_id: "agent-0".into(),
        };
        s.transcript_scroll = 0;
        s.transcript_follow = false;
        assert_eq!(
            handle_key(&mut s, press(KeyCode::Char('G'))),
            HubAction::None
        );
        assert!(s.transcript_follow);
        assert_eq!(s.transcript_scroll, FOLLOW_TAIL);
    }

    #[test]
    fn page_keys_step_by_page() {
        let mut s = state_with_rows(1);
        s.view = HubView::Transcript {
            agent_id: "agent-0".into(),
        };
        s.transcript_follow = false;
        s.transcript_scroll = 0;
        assert_eq!(
            handle_key(&mut s, press(KeyCode::PageDown)),
            HubAction::None
        );
        assert_eq!(s.transcript_scroll, PAGE_STEP);
        assert_eq!(handle_key(&mut s, press(KeyCode::PageUp)), HubAction::None);
        assert_eq!(s.transcript_scroll, 0);
    }

    #[test]
    fn release_event_is_ignored() {
        let mut s = state_with_rows(3);
        s.selected = 1;
        let mut k = KeyEvent::new(KeyCode::Char('j'), KeyModifiers::NONE);
        k.kind = KeyEventKind::Release;
        assert_eq!(handle_key(&mut s, k), HubAction::None);
        assert_eq!(s.selected, 1);
    }

    #[test]
    fn empty_rows_j_does_not_move() {
        let mut s = state_with_rows(0);
        assert_eq!(
            handle_key(&mut s, press(KeyCode::Char('j'))),
            HubAction::None
        );
        assert_eq!(s.selected, 0);
    }

    // ── Follow-mode scroll regression tests (P2 amendment) ──
    // The reviewer's amended verdict: the FOLLOW_TAIL sentinel used to make
    // j / k / PageUp / PageDown all misbehave from the default (follow=true)
    // state — the handler had no line count to convert the sentinel to a
    // real offset, so the renderer pinned the viewport to the tail. The
    // signed-scroll refactor (FOLLOW_TAIL=0, JUMP_TOP=isize::MAX) makes all
    // four keys work from any starting state.

    fn transcript_state() -> HubState {
        let mut s = state_with_rows(1);
        s.view = HubView::Transcript {
            agent_id: "agent-0".into(),
        };
        s.transcript_follow = true;
        s.transcript_scroll = FOLLOW_TAIL;
        s
    }

    #[test]
    fn k_in_follow_mode_steps_one_line_below_tail() {
        let mut s = transcript_state();
        assert_eq!(
            handle_key(&mut s, press(KeyCode::Char('k'))),
            HubAction::None
        );
        assert!(!s.transcript_follow, "k must disengage tail-follow");
        assert_eq!(s.transcript_scroll, -1isize, "k from FOLLOW_TAIL (0) → -1");
    }

    #[test]
    fn page_up_in_follow_mode_steps_ten_lines_below_tail() {
        let mut s = transcript_state();
        assert_eq!(handle_key(&mut s, press(KeyCode::PageUp)), HubAction::None);
        assert!(!s.transcript_follow);
        assert_eq!(
            s.transcript_scroll, -10isize,
            "PgUp from FOLLOW_TAIL (0) → -10"
        );
    }

    #[test]
    fn j_in_follow_mode_is_noop_pinned_at_tail() {
        // j is "down/toward tail" — at FOLLOW_TAIL the user is already at
        // the tail, so further "down" has no meaning. This is the deliberate
        // follow semantics, not a bug: the user can press `g` first to leave
        // follow, then `j` advances the offset.
        let mut s = transcript_state();
        assert_eq!(
            handle_key(&mut s, press(KeyCode::Char('j'))),
            HubAction::None
        );
        assert!(
            s.transcript_follow,
            "j in follow mode must not disengage follow"
        );
        assert_eq!(s.transcript_scroll, FOLLOW_TAIL);
    }

    #[test]
    fn page_down_in_follow_mode_is_noop_pinned_at_tail() {
        // Same semantics as j: PgDn at FOLLOW_TAIL is a no-op, not a
        // viewport jump. Deliberate; the user can press `g` to leave.
        let mut s = transcript_state();
        assert_eq!(
            handle_key(&mut s, press(KeyCode::PageDown)),
            HubAction::None
        );
        assert!(s.transcript_follow);
        assert_eq!(s.transcript_scroll, FOLLOW_TAIL);
    }

    #[test]
    fn round_trip_g_then_j_stays_at_top() {
        // End-to-end: g leaves follow and jumps to top (JUMP_TOP sentinel),
        // j stays at top (saturating_add on MAX stays at MAX), follow is off.
        let mut s = transcript_state();
        handle_key(&mut s, press(KeyCode::Char('g')));
        assert!(!s.transcript_follow);
        assert_eq!(s.transcript_scroll, JUMP_TOP);
        handle_key(&mut s, press(KeyCode::Char('j')));
        assert!(!s.transcript_follow);
        assert_eq!(
            s.transcript_scroll, JUMP_TOP,
            "saturating_add on MAX stays at MAX"
        );
    }
}