mermaid-cli 0.18.0

Open-source AI pair programmer with agentic capabilities. Local-first with Ollama, native tool calling, and beautiful TUI.
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
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
//! Single source of truth for slash commands. Used by:
//! - The palette widget (rendering + filtering)
//! - The dispatcher in `command_handler.rs` (validating known commands)
//! - The `/help` handler (rendering the command list)
//!
//! Adding a new command means adding one entry to `COMMAND_REGISTRY`
//! plus wiring its handler into the `match` in `handle_command`.
//! The palette + help text update automatically.

/// Metadata for one slash command. Names exclude the leading `/`.
#[derive(Debug, Clone, Copy)]
pub struct SlashCommand {
    /// Canonical command name without the leading `/`. Lowercase.
    pub name: &'static str,
    /// Alternative names that route to the same handler. Used by the
    /// dispatcher AND prefix filter — typing `/q` matches `/quit`.
    pub aliases: &'static [&'static str],
    /// One-line user-visible description shown in palette and `/help`.
    pub description: &'static str,
    /// Optional argument hint shown after the command name in the
    /// palette, e.g. `Some("[name]")` for `/model [name]`.
    pub arg_hint: Option<&'static str>,
    /// UX grouping used by `/help`; the palette still preserves registry order.
    pub group: SlashCommandGroup,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SlashCommandGroup {
    Everyday,
    ModelContext,
    SafetyRecovery,
    Integrations,
    AdvancedRuntime,
}

impl SlashCommandGroup {
    pub fn title(self) -> &'static str {
        match self {
            SlashCommandGroup::Everyday => "Everyday",
            SlashCommandGroup::ModelContext => "Model and context",
            SlashCommandGroup::SafetyRecovery => "Safety and recovery",
            SlashCommandGroup::Integrations => "Integrations",
            SlashCommandGroup::AdvancedRuntime => "Advanced runtime",
        }
    }
}

pub const COMMAND_GROUPS: &[SlashCommandGroup] = &[
    SlashCommandGroup::Everyday,
    SlashCommandGroup::ModelContext,
    SlashCommandGroup::SafetyRecovery,
    SlashCommandGroup::Integrations,
    SlashCommandGroup::AdvancedRuntime,
];

/// Authoritative list of keyboard shortcuts (key → description), surfaced in
/// `/help` beside the commands and reusable for a future `?` overlay. Kept in
/// sync by hand with the bindings in `reducer::handle_key`. Plain text only.
pub const KEYBINDINGS: &[(&str, &str)] = &[
    ("Enter", "Submit the prompt"),
    ("Ctrl+J", "Insert a newline (multi-line input)"),
    ("Esc", "Interrupt the current turn"),
    ("Up / Down", "Browse input history"),
    ("PageUp / PageDown", "Scroll the transcript"),
    ("Shift+Up / Shift+Down", "Scroll the transcript one line"),
    ("End", "Jump to the newest message"),
    ("Ctrl+V", "Paste (including images)"),
    ("Ctrl+O", "Compose the prompt in $VISUAL/$EDITOR"),
    ("Ctrl+B", "Background a running command"),
    ("Ctrl+T", "Expand or collapse the task checklist"),
    ("Alt+T", "Cycle reasoning depth"),
    ("Alt+P", "Toggle plan mode"),
    ("Shift+Tab", "Cycle the safety mode"),
    ("Ctrl+C", "Cancel, then exit"),
    ("Ctrl+D", "Quit"),
];

/// Authoritative registry of all slash commands. Order here is the
/// order shown in the palette and `/help`.
pub const COMMAND_REGISTRY: &[SlashCommand] = &[
    SlashCommand {
        name: "model",
        aliases: &[],
        description: "Switch model (auto-pulls if needed) or show current",
        arg_hint: Some("[name]"),
        group: SlashCommandGroup::ModelContext,
    },
    SlashCommand {
        name: "reasoning",
        aliases: &[],
        description: "Set reasoning depth (none, minimal, low, medium, high, max, xhigh)",
        arg_hint: Some("[level]"),
        group: SlashCommandGroup::ModelContext,
    },
    SlashCommand {
        name: "visible-reasoning",
        aliases: &["visiblereasoning"],
        description: "Show, hide, or toggle reasoning blocks in the transcript",
        arg_hint: Some("[on|off|toggle]"),
        group: SlashCommandGroup::ModelContext,
    },
    SlashCommand {
        name: "clear",
        aliases: &[],
        description: "Clear chat history",
        arg_hint: None,
        group: SlashCommandGroup::Everyday,
    },
    SlashCommand {
        name: "save",
        aliases: &[],
        description: "Save current conversation",
        arg_hint: Some("[name]"),
        group: SlashCommandGroup::Everyday,
    },
    SlashCommand {
        name: "load",
        aliases: &[],
        description: "Load a conversation",
        arg_hint: Some("[name]"),
        group: SlashCommandGroup::Everyday,
    },
    SlashCommand {
        name: "list",
        aliases: &[],
        description: "List saved conversations",
        arg_hint: None,
        group: SlashCommandGroup::Everyday,
    },
    SlashCommand {
        name: "todos",
        aliases: &["todo"],
        description: "Show or edit the task checklist",
        arg_hint: Some("[add <subject>|rm <id>|done <id>|clear]"),
        group: SlashCommandGroup::Everyday,
    },
    SlashCommand {
        name: "scratchpad",
        aliases: &[],
        description: "Show the session scratch directory and its contents",
        arg_hint: None,
        group: SlashCommandGroup::Everyday,
    },
    SlashCommand {
        name: "usage",
        aliases: &[],
        description: "Show provider token usage and session totals",
        arg_hint: None,
        group: SlashCommandGroup::ModelContext,
    },
    SlashCommand {
        name: "context",
        aliases: &[],
        description: "Show context window/budget; set Ollama num_ctx (Ollama auto-fits to VRAM)",
        arg_hint: Some("[n|auto|max|offload on|off]"),
        group: SlashCommandGroup::ModelContext,
    },
    SlashCommand {
        name: "compact",
        aliases: &["compress", "summarize"],
        description: "Compact conversation context with optional focus instructions",
        arg_hint: Some("[instructions]"),
        group: SlashCommandGroup::ModelContext,
    },
    SlashCommand {
        name: "memory",
        aliases: &["memories"],
        description: "List durable memories saved across sessions",
        arg_hint: None,
        group: SlashCommandGroup::ModelContext,
    },
    SlashCommand {
        name: "remember",
        aliases: &[],
        description: "Save a fact to private durable memory",
        arg_hint: Some("<fact>"),
        group: SlashCommandGroup::ModelContext,
    },
    SlashCommand {
        name: "forget",
        aliases: &[],
        description: "Delete a saved memory by name",
        arg_hint: Some("<name>"),
        group: SlashCommandGroup::ModelContext,
    },
    SlashCommand {
        name: "consolidate-memory",
        aliases: &["memory-consolidate", "prune-memory"],
        description: "Prune duplicate or obsolete memories (model-assisted, reversible)",
        arg_hint: None,
        group: SlashCommandGroup::ModelContext,
    },
    SlashCommand {
        name: "doctor",
        aliases: &[],
        description: "Show in-session readiness, model, safety, and instruction status",
        arg_hint: None,
        group: SlashCommandGroup::Everyday,
    },
    SlashCommand {
        name: "tasks",
        aliases: &[],
        description: "List durable runtime tasks",
        arg_hint: None,
        group: SlashCommandGroup::AdvancedRuntime,
    },
    SlashCommand {
        name: "task",
        aliases: &[],
        description: "Show a durable runtime task",
        arg_hint: Some("<id>"),
        group: SlashCommandGroup::AdvancedRuntime,
    },
    SlashCommand {
        name: "pause",
        aliases: &[],
        description: "Pause a durable task by marking it blocked",
        arg_hint: Some("<task-id>"),
        group: SlashCommandGroup::AdvancedRuntime,
    },
    SlashCommand {
        name: "resume",
        aliases: &[],
        description: "Resume a durable task by marking it running",
        arg_hint: Some("<task-id>"),
        group: SlashCommandGroup::AdvancedRuntime,
    },
    SlashCommand {
        name: "cancel",
        aliases: &[],
        description: "Cancel the active turn or a durable task",
        arg_hint: Some("[task-id]"),
        group: SlashCommandGroup::Everyday,
    },
    SlashCommand {
        name: "handoff",
        aliases: &[],
        description: "Write a handoff report for the current or named task",
        arg_hint: Some("[task-id]"),
        group: SlashCommandGroup::Everyday,
    },
    SlashCommand {
        name: "report",
        aliases: &[],
        description: "Show current context report or task report",
        arg_hint: Some("[task-id]"),
        group: SlashCommandGroup::Everyday,
    },
    SlashCommand {
        name: "agents",
        aliases: &[],
        description: "List or kill background agents",
        arg_hint: Some("[kill <id>|all]"),
        group: SlashCommandGroup::AdvancedRuntime,
    },
    SlashCommand {
        name: "processes",
        aliases: &["procs"],
        description: "List durable runtime processes",
        arg_hint: None,
        group: SlashCommandGroup::AdvancedRuntime,
    },
    SlashCommand {
        name: "logs",
        aliases: &[],
        description: "Show a durable runtime process log",
        arg_hint: Some("<process-id>"),
        group: SlashCommandGroup::AdvancedRuntime,
    },
    SlashCommand {
        name: "stop",
        aliases: &[],
        description: "Stop a durable runtime process",
        arg_hint: Some("<process-id>"),
        group: SlashCommandGroup::AdvancedRuntime,
    },
    SlashCommand {
        name: "restart",
        aliases: &[],
        description: "Restart a durable runtime process",
        arg_hint: Some("<process-id>"),
        group: SlashCommandGroup::AdvancedRuntime,
    },
    SlashCommand {
        name: "open",
        aliases: &[],
        description: "Open a URL, file, or process target",
        arg_hint: Some("<url|path|process-id>"),
        group: SlashCommandGroup::AdvancedRuntime,
    },
    SlashCommand {
        name: "ports",
        aliases: &[],
        description: "Show listening TCP ports",
        arg_hint: None,
        group: SlashCommandGroup::AdvancedRuntime,
    },
    SlashCommand {
        name: "safety",
        aliases: &["permission"],
        description: "Show or set the session safety mode (Shift+Tab also cycles)",
        arg_hint: Some("[read_only|ask|auto|full_access]"),
        group: SlashCommandGroup::SafetyRecovery,
    },
    SlashCommand {
        name: "plan",
        aliases: &[],
        description: "Enter or leave plan mode (Alt+P also toggles)",
        arg_hint: Some("[off|show|config]"),
        group: SlashCommandGroup::SafetyRecovery,
    },
    SlashCommand {
        name: "config",
        aliases: &[],
        description: "Open the settings picker (plan mode section)",
        arg_hint: None,
        group: SlashCommandGroup::Everyday,
    },
    SlashCommand {
        name: "approvals",
        aliases: &[],
        description: "List pending approvals",
        arg_hint: None,
        group: SlashCommandGroup::SafetyRecovery,
    },
    SlashCommand {
        name: "approve",
        aliases: &[],
        description: "Approve a pending action",
        arg_hint: Some("<approval-id>"),
        group: SlashCommandGroup::SafetyRecovery,
    },
    SlashCommand {
        name: "deny",
        aliases: &[],
        description: "Deny a pending action",
        arg_hint: Some("<approval-id>"),
        group: SlashCommandGroup::SafetyRecovery,
    },
    SlashCommand {
        name: "checkpoint",
        aliases: &[],
        description: "Create a restore checkpoint for one or more paths",
        arg_hint: Some("<path...>"),
        group: SlashCommandGroup::SafetyRecovery,
    },
    SlashCommand {
        name: "checkpoints",
        aliases: &[],
        description: "List restore checkpoints",
        arg_hint: None,
        group: SlashCommandGroup::SafetyRecovery,
    },
    SlashCommand {
        name: "restore",
        aliases: &[],
        description: "Restore a checkpoint",
        arg_hint: Some("<id>"),
        group: SlashCommandGroup::SafetyRecovery,
    },
    SlashCommand {
        name: "plugins",
        aliases: &[],
        description: "List installed plugins",
        arg_hint: None,
        group: SlashCommandGroup::Integrations,
    },
    SlashCommand {
        name: "model-info",
        aliases: &[],
        description: "Show provider/model capability information",
        arg_hint: Some("<model>"),
        group: SlashCommandGroup::ModelContext,
    },
    SlashCommand {
        name: "cloud-setup",
        aliases: &[],
        description: "Configure Ollama Cloud API key",
        arg_hint: None,
        group: SlashCommandGroup::Integrations,
    },
    SlashCommand {
        name: "theme",
        aliases: &[],
        description: "Switch the color theme or show the current one",
        arg_hint: Some("[dark|light]"),
        group: SlashCommandGroup::Everyday,
    },
    SlashCommand {
        name: "editor",
        aliases: &[],
        description: "Compose the prompt in $VISUAL/$EDITOR (Ctrl+O)",
        arg_hint: None,
        group: SlashCommandGroup::Everyday,
    },
    SlashCommand {
        name: "help",
        aliases: &["h"],
        description: "Show command help",
        arg_hint: None,
        group: SlashCommandGroup::Everyday,
    },
    SlashCommand {
        name: "quit",
        aliases: &["q"],
        description: "Quit the application",
        arg_hint: None,
        group: SlashCommandGroup::Everyday,
    },
];

/// One row of the slash palette: a built-in registry command or a
/// plugin-contributed prompt command. Unifying them in ONE list, produced
/// by ONE function ([`filter_entries`]), keeps the palette widget, the
/// row-count layout, and the reducer's cursor/Tab handling agreeing on
/// indices.
pub enum PaletteEntry<'a> {
    Builtin(&'static SlashCommand),
    Plugin(&'a crate::domain::PluginCommand),
}

impl PaletteEntry<'_> {
    pub fn name(&self) -> &str {
        match self {
            PaletteEntry::Builtin(c) => c.name,
            PaletteEntry::Plugin(p) => &p.name,
        }
    }

    /// Palette/hint description; plugin rows carry their origin.
    pub fn description(&self) -> String {
        match self {
            PaletteEntry::Builtin(c) => c.description.to_string(),
            PaletteEntry::Plugin(p) => {
                if p.description.is_empty() {
                    format!("(plugin:{})", p.plugin)
                } else {
                    format!("{} (plugin:{})", p.description, p.plugin)
                }
            },
        }
    }

    pub fn arg_hint(&self) -> Option<&'static str> {
        match self {
            PaletteEntry::Builtin(c) => c.arg_hint,
            PaletteEntry::Plugin(_) => Some("[args]"),
        }
    }
}

/// The palette's single source of truth: built-ins (registry order) then
/// plugin commands (already name-sorted by the loader), both prefix-filtered.
/// EVERY palette consumer (widget rows, layout row count, reducer cursor)
/// must use this so their indices agree.
pub fn filter_entries<'a>(
    typed: &str,
    plugin: &'a [crate::domain::PluginCommand],
) -> Vec<PaletteEntry<'a>> {
    let needle = typed.to_lowercase();
    let mut entries: Vec<PaletteEntry<'a>> = filter_by_prefix(typed)
        .into_iter()
        .map(PaletteEntry::Builtin)
        .collect();
    entries.extend(
        plugin
            .iter()
            .filter(|p| needle.is_empty() || p.name.starts_with(&needle))
            .map(PaletteEntry::Plugin),
    );
    entries
}

/// Filter the registry by a typed prefix (after stripping the leading
/// `/`). An empty prefix returns the full registry. Matches against the
/// canonical name AND any aliases — typing `/q` finds `quit` because
/// `q` is a `quit` alias. Result preserves registry order (stable).
pub fn filter_by_prefix(typed: &str) -> Vec<&'static SlashCommand> {
    let needle = typed.to_lowercase();
    if needle.is_empty() {
        return COMMAND_REGISTRY.iter().collect();
    }
    // Plain prefix match against the canonical name and aliases. Typing the
    // first word of a hyphenated command (`consolidate`) must reveal it
    // (`consolidate-memory`) — an earlier carve-out that hid hyphenated names
    // from hyphenless prefixes broke that for commands without a hyphenless
    // alias.
    COMMAND_REGISTRY
        .iter()
        .filter(|cmd| {
            cmd.name.starts_with(&needle) || cmd.aliases.iter().any(|a| a.starts_with(&needle))
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn filter_by_prefix_empty_returns_all() {
        let result = filter_by_prefix("");
        assert_eq!(result.len(), COMMAND_REGISTRY.len());
        // Order preserved.
        assert_eq!(result[0].name, COMMAND_REGISTRY[0].name);
    }

    #[test]
    fn filter_by_prefix_includes_exact_name_first() {
        // Prefix match surfaces `model` (and any longer `model-*`); the exact
        // name stays first by registry order.
        let result = filter_by_prefix("model");
        assert!(result.iter().any(|c| c.name == "model"));
        assert_eq!(result[0].name, "model");
    }

    #[test]
    fn filter_by_prefix_partial_prefix_includes_model() {
        let result = filter_by_prefix("mod");
        assert!(result.iter().any(|c| c.name == "model"));
    }

    #[test]
    fn filter_by_prefix_matches_hyphenated_command_by_first_word() {
        // Regression: typing the first word of a hyphenated command must
        // reveal it, even before the hyphen — and at shorter prefixes too.
        // Previously `/consolidate` showed nothing until `/consolidate-`.
        assert!(
            filter_by_prefix("consolidate")
                .iter()
                .any(|c| c.name == "consolidate-memory"),
            "/consolidate must surface /consolidate-memory"
        );
        assert!(
            filter_by_prefix("conso")
                .iter()
                .any(|c| c.name == "consolidate-memory")
        );
        // Other hyphenated commands too.
        assert!(
            filter_by_prefix("cloud")
                .iter()
                .any(|c| c.name == "cloud-setup")
        );
    }

    #[test]
    fn filter_by_prefix_no_match_returns_empty() {
        let result = filter_by_prefix("zzzzz");
        assert!(result.is_empty());
    }

    #[test]
    fn filter_by_prefix_matches_aliases() {
        // `/q` should find `quit` via its alias.
        let result = filter_by_prefix("q");
        assert!(
            result.iter().any(|c| c.name == "quit"),
            "expected quit in: {:?}",
            result.iter().map(|c| c.name).collect::<Vec<_>>()
        );
    }

    #[test]
    fn filter_by_prefix_is_case_insensitive() {
        // User shouldn't have to type lowercase /Q or /MODEL.
        let upper = filter_by_prefix("MODEL");
        assert!(upper.iter().any(|c| c.name == "model"));
        assert_eq!(upper[0].name, "model");
    }

    #[test]
    fn registry_has_no_duplicate_names() {
        // Defensive: catches accidental duplicate entries during
        // registry maintenance. Duplicate names would route ambiguously
        // in the dispatcher.
        let mut names: Vec<&str> = COMMAND_REGISTRY.iter().map(|c| c.name).collect();
        names.sort_unstable();
        let len_before = names.len();
        names.dedup();
        assert_eq!(
            names.len(),
            len_before,
            "duplicate command name detected in COMMAND_REGISTRY"
        );
    }
}