magi-code 0.63.1

Repository-aware CLI coding agent for terminal work
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
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
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use std::sync::LazyLock;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum TuiAction {
    SubmitPrompt,
    InsertPromptNewline,
    Exit,
    ClearInputOrExit,
    FocusPrompt,
    FocusActivityTree,
    FocusActivityDetail,
    CyclePrimaryAgent,
    CycleThinkingLevel,
    ToggleHelp,
    MoveSelectionUp,
    MoveSelectionDown,
    CollapseSelected,
    ExpandSelected,
    ExpandAll,
    CollapseAll,
    SelectFailed,
    SelectRunning,
    ScrollDetailUp,
    ScrollDetailDown,
    ScrollTranscriptUp,
    ScrollTranscriptDown,
    FocusTranscriptLayout,
    FocusActivityLayout,
    MovePromptCursorLeft,
    MovePromptCursorRight,
    MovePromptCursorUp,
    MovePromptCursorDown,
    OpenSetModel,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum KeyContext {
    Global,
    Prompt,
    Transcript,
    ActivityTree,
    ActivityDetail,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct KeyPattern {
    code: KeyCode,
    modifiers: KeyModifiers,
}

impl KeyPattern {
    const fn new(code: KeyCode, modifiers: KeyModifiers) -> Self {
        Self { code, modifiers }
    }

    fn matches(self, key: KeyEvent) -> bool {
        key.code == self.code && key.modifiers == self.modifiers
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct KeyBinding {
    pub(crate) action: TuiAction,
    pub(crate) context: KeyContext,
    pub(crate) key: KeyPattern,
    pub(crate) label: &'static str,
    pub(crate) description: &'static str,
}

const DEFAULT_KEYBINDINGS: &[KeyBinding] = &[
    KeyBinding {
        action: TuiAction::ToggleHelp,
        context: KeyContext::Global,
        key: KeyPattern::new(KeyCode::F(1), KeyModifiers::NONE),
        label: "F1",
        description: "toggle help",
    },
    KeyBinding {
        action: TuiAction::Exit,
        context: KeyContext::Global,
        key: KeyPattern::new(KeyCode::Char('d'), KeyModifiers::CONTROL),
        label: "Ctrl-D",
        description: "exit",
    },
    KeyBinding {
        action: TuiAction::ClearInputOrExit,
        context: KeyContext::Global,
        key: KeyPattern::new(KeyCode::Char('c'), KeyModifiers::CONTROL),
        label: "Ctrl-C",
        description: "clear prompt, or exit when empty",
    },
    KeyBinding {
        action: TuiAction::FocusActivityTree,
        context: KeyContext::Global,
        key: KeyPattern::new(KeyCode::Char('1'), KeyModifiers::ALT),
        label: "Alt-1",
        description: "focus activity tree pane",
    },
    KeyBinding {
        action: TuiAction::FocusActivityDetail,
        context: KeyContext::Global,
        key: KeyPattern::new(KeyCode::Char('2'), KeyModifiers::ALT),
        label: "Alt-2",
        description: "focus selected activity detail pane",
    },
    KeyBinding {
        action: TuiAction::FocusActivityTree,
        context: KeyContext::Global,
        key: KeyPattern::new(KeyCode::Char('t'), KeyModifiers::CONTROL),
        label: "Ctrl-T",
        description: "focus activity tree pane",
    },
    KeyBinding {
        action: TuiAction::FocusPrompt,
        context: KeyContext::Global,
        key: KeyPattern::new(KeyCode::Char('p'), KeyModifiers::ALT),
        label: "Alt-P",
        description: "focus prompt and close help",
    },
    KeyBinding {
        action: TuiAction::FocusPrompt,
        context: KeyContext::Global,
        key: KeyPattern::new(KeyCode::Esc, KeyModifiers::NONE),
        label: "Esc",
        description: "focus prompt and close help",
    },
    KeyBinding {
        action: TuiAction::CyclePrimaryAgent,
        context: KeyContext::Global,
        key: KeyPattern::new(KeyCode::BackTab, KeyModifiers::SHIFT),
        label: "Shift-Tab",
        description: "cycle primary agent selection",
    },
    KeyBinding {
        action: TuiAction::CycleThinkingLevel,
        context: KeyContext::Global,
        key: KeyPattern::new(KeyCode::Char('t'), KeyModifiers::ALT),
        label: "Alt-T",
        description: "cycle thinking level",
    },
    KeyBinding {
        action: TuiAction::OpenSetModel,
        context: KeyContext::Global,
        key: KeyPattern::new(KeyCode::Char('m'), KeyModifiers::ALT),
        label: "Alt-M",
        description: "open model picker",
    },
    KeyBinding {
        action: TuiAction::FocusActivityLayout,
        context: KeyContext::Global,
        key: KeyPattern::new(
            KeyCode::Right,
            KeyModifiers::CONTROL.union(KeyModifiers::SHIFT),
        ),
        label: "Ctrl-Shift-Right",
        description: "activity-focused layout",
    },
    KeyBinding {
        action: TuiAction::FocusTranscriptLayout,
        context: KeyContext::Global,
        key: KeyPattern::new(
            KeyCode::Left,
            KeyModifiers::CONTROL.union(KeyModifiers::SHIFT),
        ),
        label: "Ctrl-Shift-Left",
        description: "transcript-focused layout",
    },
    KeyBinding {
        action: TuiAction::FocusActivityLayout,
        context: KeyContext::Global,
        key: KeyPattern::new(KeyCode::Right, KeyModifiers::ALT),
        label: "Alt-Right",
        description: "activity-focused layout alias",
    },
    KeyBinding {
        action: TuiAction::FocusTranscriptLayout,
        context: KeyContext::Global,
        key: KeyPattern::new(KeyCode::Left, KeyModifiers::ALT),
        label: "Alt-Left",
        description: "transcript-focused layout alias",
    },
    KeyBinding {
        action: TuiAction::SubmitPrompt,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Enter, KeyModifiers::NONE),
        label: "Enter",
        description: "submit prompt",
    },
    KeyBinding {
        action: TuiAction::InsertPromptNewline,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Enter, KeyModifiers::SHIFT),
        label: "Shift-Enter",
        description: "insert newline",
    },
    KeyBinding {
        action: TuiAction::MovePromptCursorLeft,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Left, KeyModifiers::NONE),
        label: "Left",
        description: "move prompt cursor left",
    },
    KeyBinding {
        action: TuiAction::MovePromptCursorRight,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Right, KeyModifiers::NONE),
        label: "Right",
        description: "move prompt cursor right",
    },
    KeyBinding {
        action: TuiAction::MovePromptCursorUp,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Up, KeyModifiers::NONE),
        label: "Up",
        description: "move prompt cursor up",
    },
    KeyBinding {
        action: TuiAction::MovePromptCursorDown,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Down, KeyModifiers::NONE),
        label: "Down",
        description: "move prompt cursor down",
    },
    KeyBinding {
        action: TuiAction::ScrollTranscriptUp,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Up, KeyModifiers::ALT),
        label: "Alt-Up",
        description: "scroll transcript up",
    },
    KeyBinding {
        action: TuiAction::ScrollTranscriptDown,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Down, KeyModifiers::ALT),
        label: "Alt-Down",
        description: "scroll transcript down",
    },
    KeyBinding {
        action: TuiAction::ScrollTranscriptUp,
        context: KeyContext::Transcript,
        key: KeyPattern::new(KeyCode::Up, KeyModifiers::NONE),
        label: "Up",
        description: "scroll transcript up",
    },
    KeyBinding {
        action: TuiAction::ScrollTranscriptDown,
        context: KeyContext::Transcript,
        key: KeyPattern::new(KeyCode::Down, KeyModifiers::NONE),
        label: "Down",
        description: "scroll transcript down",
    },
    KeyBinding {
        action: TuiAction::MoveSelectionDown,
        context: KeyContext::ActivityTree,
        key: KeyPattern::new(KeyCode::Char('j'), KeyModifiers::ALT),
        label: "Alt-J",
        description: "move activity selection down",
    },
    KeyBinding {
        action: TuiAction::MoveSelectionUp,
        context: KeyContext::ActivityTree,
        key: KeyPattern::new(KeyCode::Char('k'), KeyModifiers::ALT),
        label: "Alt-K",
        description: "move activity selection up",
    },
    KeyBinding {
        action: TuiAction::MoveSelectionUp,
        context: KeyContext::ActivityTree,
        key: KeyPattern::new(KeyCode::Up, KeyModifiers::NONE),
        label: "Up",
        description: "move activity selection up",
    },
    KeyBinding {
        action: TuiAction::MoveSelectionDown,
        context: KeyContext::ActivityTree,
        key: KeyPattern::new(KeyCode::Down, KeyModifiers::NONE),
        label: "Down",
        description: "move activity selection down",
    },
    KeyBinding {
        action: TuiAction::CollapseSelected,
        context: KeyContext::ActivityTree,
        key: KeyPattern::new(KeyCode::Left, KeyModifiers::NONE),
        label: "Left",
        description: "collapse selected activity",
    },
    KeyBinding {
        action: TuiAction::ExpandSelected,
        context: KeyContext::ActivityTree,
        key: KeyPattern::new(KeyCode::Right, KeyModifiers::NONE),
        label: "Right",
        description: "expand selected activity",
    },
    KeyBinding {
        action: TuiAction::CollapseSelected,
        context: KeyContext::ActivityTree,
        key: KeyPattern::new(KeyCode::Char('h'), KeyModifiers::ALT),
        label: "Alt-H",
        description: "collapse selected activity",
    },
    KeyBinding {
        action: TuiAction::ExpandSelected,
        context: KeyContext::ActivityTree,
        key: KeyPattern::new(KeyCode::Char('l'), KeyModifiers::ALT),
        label: "Alt-L",
        description: "expand selected activity",
    },
    KeyBinding {
        action: TuiAction::ExpandAll,
        context: KeyContext::ActivityTree,
        key: KeyPattern::new(KeyCode::Char('e'), KeyModifiers::ALT),
        label: "Alt-E",
        description: "expand all activities",
    },
    KeyBinding {
        action: TuiAction::CollapseAll,
        context: KeyContext::ActivityTree,
        key: KeyPattern::new(KeyCode::Char('c'), KeyModifiers::ALT),
        label: "Alt-C",
        description: "collapse all activities",
    },
    KeyBinding {
        action: TuiAction::SelectFailed,
        context: KeyContext::ActivityTree,
        key: KeyPattern::new(KeyCode::Char('f'), KeyModifiers::ALT),
        label: "Alt-F",
        description: "select first failed activity",
    },
    KeyBinding {
        action: TuiAction::SelectRunning,
        context: KeyContext::ActivityTree,
        key: KeyPattern::new(KeyCode::Char('r'), KeyModifiers::ALT),
        label: "Alt-R",
        description: "select first running activity",
    },
    KeyBinding {
        action: TuiAction::ScrollDetailUp,
        context: KeyContext::ActivityDetail,
        key: KeyPattern::new(KeyCode::Up, KeyModifiers::NONE),
        label: "Up",
        description: "scroll selected activity detail up",
    },
    KeyBinding {
        action: TuiAction::ScrollDetailDown,
        context: KeyContext::ActivityDetail,
        key: KeyPattern::new(KeyCode::Down, KeyModifiers::NONE),
        label: "Down",
        description: "scroll selected activity detail down",
    },
    KeyBinding {
        action: TuiAction::ScrollDetailUp,
        context: KeyContext::ActivityDetail,
        key: KeyPattern::new(KeyCode::PageUp, KeyModifiers::NONE),
        label: "PageUp",
        description: "scroll selected activity detail up",
    },
    KeyBinding {
        action: TuiAction::ScrollDetailDown,
        context: KeyContext::ActivityDetail,
        key: KeyPattern::new(KeyCode::PageDown, KeyModifiers::NONE),
        label: "PageDown",
        description: "scroll selected activity detail down",
    },
    KeyBinding {
        action: TuiAction::ScrollDetailUp,
        context: KeyContext::ActivityDetail,
        key: KeyPattern::new(KeyCode::Up, KeyModifiers::ALT),
        label: "Alt-Up",
        description: "scroll selected activity detail up",
    },
    KeyBinding {
        action: TuiAction::ScrollDetailDown,
        context: KeyContext::ActivityDetail,
        key: KeyPattern::new(KeyCode::Down, KeyModifiers::ALT),
        label: "Alt-Down",
        description: "scroll selected activity detail down",
    },
];

#[cfg(test)]
pub(super) fn default_keybindings() -> &'static [KeyBinding] {
    DEFAULT_KEYBINDINGS
}

fn bracketed_label(label: &str) -> String {
    format!("[{label}]")
}

static HELP_TEXT: LazyLock<String> = LazyLock::new(build_help_text);

pub(super) fn help_text() -> &'static str {
    &HELP_TEXT
}

fn build_help_text() -> String {
    let mut lines = vec![
        "Mission Control Help".to_string(),
        "".to_string(),
        "Focus: [Alt-1] Activity Tree, [Alt-2] Selected Activity Detail, [Alt-P] Prompt.".to_string(),
        "Prompt: type normally when focused; [Enter] submits; [Shift-Enter] inserts a newline; arrows move the cursor.".to_string(),
        "Transcript: [Alt-Up]/[Alt-Down] scroll transcript while Prompt is focused; mouse wheel over Transcript also scrolls transcript.".to_string(),
        "Activity Tree: with Activity Tree focused, [Up]/[Down] move selection; [Alt-E]/[Alt-C] expand/collapse all; [Alt-F]/[Alt-R] select failed/running.".to_string(),
        "Selected Activity Detail: with Selected Activity Detail focused, [Up]/[Down]/[PageUp]/[PageDown] scroll detail.".to_string(),
        "Long prompt: mouse wheel over Prompt scrolls the editor.".to_string(),
        "Activity: [Up]/[Down] move selection; [Alt-E]/[Alt-C] expand/collapse all; [Alt-F]/[Alt-R] select failed/running. Compatibility aliases: [Ctrl-T] focuses activity tree; [Esc] focuses prompt and closes help; [Shift-Tab] cycles primary agent selection; [Alt-T] cycles thinking level; [Alt-M] opens the model picker when no prompt is running.".to_string(),
        "Layout: [Ctrl-Shift-Right] uses activity-focused columns; [Ctrl-Shift-Left] restores transcript-focused columns. [Alt-Right]/[Alt-Left] remain secondary aliases where terminals deliver them.".to_string(),
        "Help: [F1] toggles this overlay; mouse wheel over Help scrolls this text.".to_string(),
        "Autocomplete: first-character / shows supported TUI slash commands; @ anywhere tags cwd files with fuzzy matching; $ anywhere tags enabled skills with fuzzy matching; [Up]/[Down] navigate; [Tab]/[Enter] accept only when suggestions are visible; [Esc] dismiss.".to_string(),
        "System prompt modal: [Esc]/[q] close; [Up]/[Down]/[Alt-Up]/[Alt-Down]/[PageUp]/[PageDown]/[Home]/[End] and mouse wheel scroll.".to_string(),
        "Sessions modal: two columns when space permits; [Tab]/[Shift-Tab] focus list or preview; [Up]/[Down] moves list or scrolls preview; mouse wheel scrolls hovered column; [Enter] switches; [Esc] closes. [Shift-Tab] depends on terminal delivery.".to_string(),
        "TUI slash commands: /help opens this overlay; /login openai-codex authenticates; /logout openai-codex removes local auth; /setmodel selects models; /system-prompt opens read-only prompt inspection; /skills manages skills; /sessions opens the preview session switcher; /new starts a fresh session and clears transcript/activity/detail/help scroll; /quit exits.".to_string(),
        "Mouse: wheel follows the hovered scrollable pane; click activity items to select, click selected parents to expand/collapse.".to_string(),
        "".to_string(),
    ];
    for context in [
        KeyContext::Global,
        KeyContext::Prompt,
        KeyContext::Transcript,
        KeyContext::ActivityTree,
        KeyContext::ActivityDetail,
    ] {
        lines.push(format!("{context:?}"));
        for binding in DEFAULT_KEYBINDINGS
            .iter()
            .filter(|binding| binding.context == context)
        {
            lines.push(format!(
                "  {:<18} {}",
                bracketed_label(binding.label),
                binding.description
            ));
        }
        lines.push(String::new());
    }
    lines.join("\n")
}

pub(super) fn action_for_key(key: KeyEvent, active_context: KeyContext) -> Option<TuiAction> {
    DEFAULT_KEYBINDINGS
        .iter()
        .find(|binding| {
            (binding.context == KeyContext::Global || binding.context == active_context)
                && binding.key.matches(key)
        })
        .map(|binding| binding.action)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    #[test]
    fn help_text_brackets_visible_key_labels() {
        let help = help_text();
        for label in [
            "[Alt-1]",
            "[Alt-2]",
            "[Shift-Enter]",
            "[Alt-C]",
            "[Ctrl-Shift-Right]",
            "[Enter]",
            "[Alt-P]",
            "[Alt-M]",
            "[Esc]",
        ] {
            assert!(help.contains(label), "missing {label}");
        }
        assert!(help.contains("[Alt-P] Prompt"));
        assert!(!help.contains("[Ctrl-P] Prompt"));
        assert!(!help.contains("[Ctrl Ctrl] Prompt"));
        assert!(!help.contains("[Alt-3]"));
        assert!(!help.contains("focus transcript pane"));
        assert!(help.contains("  [Enter]"));
    }

    #[test]
    fn prompt_focus_keybindings_match_reachable_focus_panes() {
        assert_eq!(
            action_for_key(
                KeyEvent::new(KeyCode::Char('p'), KeyModifiers::ALT),
                KeyContext::ActivityTree
            ),
            Some(TuiAction::FocusPrompt)
        );
        assert_eq!(
            action_for_key(
                KeyEvent::new(KeyCode::Char('p'), KeyModifiers::CONTROL),
                KeyContext::ActivityTree
            ),
            None
        );
        assert_eq!(
            action_for_key(
                KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE),
                KeyContext::ActivityTree
            ),
            Some(TuiAction::FocusPrompt)
        );
    }

    #[test]
    fn alt_number_keybindings_match_reachable_focus_panes() {
        assert_eq!(
            action_for_key(
                KeyEvent::new(KeyCode::Char('1'), KeyModifiers::ALT),
                KeyContext::Prompt
            ),
            Some(TuiAction::FocusActivityTree)
        );
        assert_eq!(
            action_for_key(
                KeyEvent::new(KeyCode::Char('2'), KeyModifiers::ALT),
                KeyContext::Prompt
            ),
            Some(TuiAction::FocusActivityDetail)
        );
        assert_eq!(
            action_for_key(
                KeyEvent::new(KeyCode::Char('3'), KeyModifiers::ALT),
                KeyContext::Prompt
            ),
            None
        );
    }
}