opendev-tui 0.1.4

Ratatui-based terminal UI for OpenDev
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
//! Slash command execution: /mode, /models, /help, etc.

use crate::event::AppEvent;

use super::{App, AutonomyLevel, DisplayMessage, DisplayRole, OperationMode};

impl App {
    pub(super) fn push_system_message(&mut self, content: String) {
        self.state
            .messages
            .push(DisplayMessage::new(DisplayRole::System, content));
        self.state.message_generation += 1;
    }

    /// Push a slash command echo line (e.g. `❯ /mode plan`).
    pub(super) fn push_slash_echo(&mut self, cmd: &str) {
        self.state
            .messages
            .push(DisplayMessage::new(DisplayRole::SlashCommand, cmd));
    }

    /// Push a command result line that attaches below the echo.
    pub(super) fn push_command_result(&mut self, content: String) {
        self.state
            .messages
            .push(DisplayMessage::new(DisplayRole::CommandResult, content));
        self.state.message_generation += 1;
    }

    /// Execute a slash command locally.
    pub(super) fn execute_slash_command(&mut self, cmd: &str) {
        let parts: Vec<&str> = cmd[1..].splitn(2, ' ').collect();
        let name = parts[0];
        let args = parts.get(1).map(|s| s.trim());

        match name {
            "exit" | "quit" | "q" => {
                self.state.running = false;
            }
            "clear" => {
                self.state.messages.clear();
                self.state.scroll_offset = 0;
                self.state.user_scrolled = false;
                self.state.message_generation += 1;
            }
            "mode" => {
                self.push_slash_echo(cmd);
                match args {
                    Some(arg) => {
                        if let Some(mode) = OperationMode::from_str_loose(arg) {
                            self.state.mode = mode;
                        } else {
                            self.push_command_result(format!(
                                "Unknown mode '{arg}'. Use: normal, plan"
                            ));
                            return;
                        }
                    }
                    None => {
                        self.state.mode = match self.state.mode {
                            OperationMode::Normal => OperationMode::Plan,
                            OperationMode::Plan => OperationMode::Normal,
                        };
                    }
                }
                self.push_command_result(format!("Mode set to {}", self.state.mode));
            }
            "autonomy" => {
                self.push_slash_echo(cmd);
                match args {
                    Some(arg) => {
                        if let Some(level) = AutonomyLevel::from_str_loose(arg) {
                            self.state.autonomy = level;
                        } else {
                            self.push_command_result(format!(
                                "Unknown autonomy level '{arg}'. Use: manual, semi-auto, auto"
                            ));
                            return;
                        }
                    }
                    None => {
                        self.state.autonomy = match self.state.autonomy {
                            AutonomyLevel::Manual => AutonomyLevel::SemiAuto,
                            AutonomyLevel::SemiAuto => AutonomyLevel::Auto,
                            AutonomyLevel::Auto => AutonomyLevel::Manual,
                        };
                    }
                }
                self.push_command_result(format!("Autonomy set to {}", self.state.autonomy));
            }
            "models" => {
                // Always open interactive model picker
                let cache_dir = opendev_config::Paths::new(None).global_cache_dir();
                let picker = crate::controllers::ModelPickerController::from_registry(
                    &cache_dir,
                    &self.state.model,
                );
                if picker.filtered_count() == 0 {
                    self.push_slash_echo(cmd);
                    self.push_command_result(
                        "No models available. Run 'opendev setup' to configure providers."
                            .to_string(),
                    );
                } else {
                    self.model_picker_controller = Some(picker);
                }
            }
            "session-models" => {
                self.push_slash_echo(cmd);
                match args {
                    Some("clear") => {
                        self.push_command_result("Session model override cleared".to_string());
                    }
                    Some(model_name) if !model_name.is_empty() => {
                        self.state.model = model_name.to_string();
                        self.push_command_result(format!(
                            "Session model set to {}",
                            self.state.model
                        ));
                        if let Some(ref tx) = self.user_message_tx {
                            let _ = tx.send(format!("\x00__MODEL_CHANGE__{}", self.state.model));
                        }
                    }
                    _ => {
                        self.push_command_result(format!(
                            "Current model: {}. Usage: /session-models <name>",
                            self.state.model
                        ));
                    }
                }
            }
            "mcp" => {
                self.push_slash_echo(cmd);
                let result = self.mcp_controller.handle_command(args.unwrap_or(""));
                self.push_command_result(result);
            }
            "tasks" => {
                self.push_slash_echo(cmd);
                let msg = if let Ok(mgr) = self.task_manager.try_lock() {
                    let tasks = mgr.all_tasks();
                    if tasks.is_empty() {
                        "No background tasks".to_string()
                    } else {
                        let mut lines = vec![format!(
                            "Background tasks ({} total, {} running):",
                            tasks.len(),
                            mgr.running_count()
                        )];
                        for task in &tasks {
                            lines.push(format!(
                                "  {} [{}] {} ({:.1}s)",
                                task.task_id,
                                task.state,
                                task.description,
                                task.runtime_seconds()
                            ));
                        }
                        lines.join("\n")
                    }
                } else {
                    "Task manager busy. Try again.".to_string()
                };
                self.push_command_result(msg);
            }
            "task" => {
                self.push_slash_echo(cmd);
                match args {
                    Some(id) => {
                        let msg = if let Ok(mgr) = self.task_manager.try_lock() {
                            let output = mgr.read_output(id, 50);
                            if output.is_empty() {
                                format!("No output for task '{id}'")
                            } else {
                                format!("Output for task {id}:\n{output}")
                            }
                        } else {
                            "Task manager busy. Try again.".to_string()
                        };
                        self.push_command_result(msg);
                    }
                    None => {
                        self.push_command_result("Usage: /task <id>".to_string());
                    }
                }
            }
            "kill" => {
                self.push_slash_echo(cmd);
                match args {
                    Some(id) => {
                        let id = id.to_string();
                        let _ = self.event_tx.send(AppEvent::KillTask(id));
                    }
                    None => {
                        self.push_command_result("Usage: /kill <id>".to_string());
                    }
                }
            }
            "init" => {
                self.push_slash_echo(cmd);
                if self.state.agent_active {
                    self.push_command_result("Cannot run /init while agent is active".to_string());
                    return;
                }
                let prompt =
                    opendev_agents::prompts::embedded::build_init_prompt(args.unwrap_or(""));
                self.push_command_result("Generating AGENTS.md...".to_string());
                let _ = self.event_tx.send(AppEvent::UserSubmit(prompt));
            }
            "agents" => {
                self.push_slash_echo(cmd);
                match args {
                    Some("create") => {
                        self.push_command_result("Agent creation coming soon.".to_string());
                    }
                    _ => {
                        self.push_command_result("No custom agents configured".to_string());
                    }
                }
            }
            "skills" => {
                self.push_slash_echo(cmd);
                match args {
                    Some("create") => {
                        self.push_command_result("Skill creation coming soon.".to_string());
                    }
                    _ => {
                        self.push_command_result("No custom skills configured".to_string());
                    }
                }
            }
            "plugins" => {
                self.push_slash_echo(cmd);
                match args {
                    Some("install") => {
                        self.push_command_result("Plugin installation coming soon.".to_string());
                    }
                    Some("remove") => {
                        self.push_command_result("Plugin removal coming soon.".to_string());
                    }
                    _ => {
                        self.push_command_result("No plugins installed".to_string());
                    }
                }
            }
            "sound" => {
                self.push_slash_echo(cmd);
                opendev_runtime::play_finish_sound();
                self.push_command_result("Playing test sound...".to_string());
            }
            "compact" => {
                self.push_slash_echo(cmd);
                // Account for the echo we just pushed (< 6 means < 5 real messages)
                if self.state.messages.len() < 6 {
                    self.push_command_result(
                        "Not enough messages to compact (need at least 5)".to_string(),
                    );
                } else if self.state.compaction_active {
                    self.push_command_result("Compaction already in progress".to_string());
                } else if self.state.agent_active {
                    self.push_command_result("Cannot compact while agent is running".to_string());
                } else {
                    // Send special sentinel to trigger compaction in the backend
                    if let Some(ref tx) = self.user_message_tx {
                        let _ = tx.send("\x00__COMPACT__".to_string());
                    }
                }
            }
            "bg" => {
                self.push_slash_echo(cmd);
                match args {
                    None => {
                        // List all background agent tasks
                        let tasks = self.state.bg_agent_manager.all_tasks();
                        if tasks.is_empty() {
                            self.push_command_result("No background agents".to_string());
                        } else {
                            let mut lines = vec![format!(
                                "Background agents ({} total, {} running):",
                                tasks.len(),
                                self.state.bg_agent_manager.running_count()
                            )];
                            for task in &tasks {
                                let elapsed = task.runtime_seconds();
                                let elapsed_str = if elapsed >= 60.0 {
                                    format!("{}m {:.0}s", elapsed as u64 / 60, elapsed % 60.0)
                                } else {
                                    format!("{elapsed:.1}s")
                                };
                                let tool_info = if let Some(ref tool) = task.current_tool {
                                    format!(" ({})", tool)
                                } else {
                                    String::new()
                                };
                                let query_preview: String = task.query.chars().take(50).collect();
                                lines.push(format!(
                                    "  [{id}] [{state}] {query}{tool_info}{elapsed_str}, {tools} tools",
                                    id = task.task_id,
                                    state = task.state,
                                    query = query_preview,
                                    tools = task.tool_call_count,
                                ));
                            }
                            self.push_command_result(lines.join("\n"));
                        }
                    }
                    Some(sub) if sub.starts_with("kill ") => {
                        let id = sub.strip_prefix("kill ").unwrap().trim();
                        if self.state.bg_agent_manager.kill_task(id) {
                            let _ = self.event_tx.send(AppEvent::BackgroundAgentKilled {
                                task_id: id.to_string(),
                            });
                        } else {
                            self.push_command_result(format!(
                                "Background agent '{id}' not found or not running"
                            ));
                        }
                    }
                    Some(sub) if sub.starts_with("merge ") => {
                        let id = sub.strip_prefix("merge ").unwrap().trim();
                        if let Some(task) = self.state.bg_agent_manager.get_task(id) {
                            if let Some(ref summary) = task.result_summary {
                                let content = format!("[Background agent {id} result]\n{summary}");
                                self.push_command_result(content);
                            } else {
                                self.push_command_result(format!(
                                    "Background agent '{id}' has no result yet"
                                ));
                            }
                        } else {
                            self.push_command_result(format!("Background agent '{id}' not found"));
                        }
                    }
                    Some(id) => {
                        // Show details for a specific task
                        if let Some(task) = self.state.bg_agent_manager.get_task(id) {
                            let elapsed = task.runtime_seconds();
                            let summary =
                                task.result_summary.as_deref().unwrap_or("(still running)");
                            self.push_command_result(format!(
                                "Background agent [{id}]:\n  Query: {}\n  State: {}\n  Tools: {}\n  Cost: ${:.4}\n  Elapsed: {:.1}s\n  Result: {summary}",
                                task.query, task.state, task.tool_call_count, task.cost_usd, elapsed
                            ));
                        } else {
                            self.push_command_result(format!("Background agent '{id}' not found"));
                        }
                    }
                }
            }
            "undo" => {
                self.push_slash_echo(cmd);
                if self.state.agent_active {
                    self.push_command_result("Cannot undo while agent is running".to_string());
                } else if let Some(ref tx) = self.user_message_tx {
                    let _ = tx.send("\x00__UNDO__".to_string());
                } else {
                    self.push_command_result("Undo not available".to_string());
                }
            }
            "redo" => {
                self.push_slash_echo(cmd);
                if self.state.agent_active {
                    self.push_command_result("Cannot redo while agent is running".to_string());
                } else if let Some(ref tx) = self.user_message_tx {
                    let _ = tx.send("\x00__REDO__".to_string());
                } else {
                    self.push_command_result("Redo not available".to_string());
                }
            }
            "share" => {
                self.push_slash_echo(cmd);
                if let Some(ref tx) = self.user_message_tx {
                    let _ = tx.send("\x00__SHARE__".to_string());
                } else {
                    self.push_command_result("Share not available".to_string());
                }
            }
            "sessions" => {
                self.push_slash_echo(cmd);
                // Open session picker
                if let Some(ref tx) = self.user_message_tx {
                    let _ = tx.send("\x00__LIST_SESSIONS__".to_string());
                }
                self.push_command_result("Loading sessions...".to_string());
            }
            "help" => {
                self.push_slash_echo(cmd);
                self.push_command_result(
                    [
                        "Available commands:",
                        "  /help              — Show this help",
                        "  /clear             — Clear conversation",
                        "  /mode [plan|normal]      — Toggle or set mode",
                        "  /autonomy [manual|semi-auto|auto] — Cycle or set autonomy",
                        "  /models              — Open model picker",
                        "  /session-models [name|clear] — Set model for session",
                        "  /sessions          — List saved sessions",
                        "  /undo              — Undo last file changes",
                        "  /redo              — Redo undone changes",
                        "  /share             — Share session as HTML",
                        "  /mcp [list|add|remove|enable|disable] — Manage MCP servers",
                        "  /tasks             — List background tasks",
                        "  /task <id>         — Show task output",
                        "  /kill <id>         — Kill a background task",
                        "  /init [path]       — Generate AGENTS.md",
                        "  /agents [list|create] — Manage custom agents",
                        "  /skills [list|create] — Manage custom skills",
                        "  /plugins [list|install|remove] — Manage plugins",
                        "  /sound             — Play test notification sound",
                        "  /compact           — Compact conversation context",
                        "  /bg                — List background agents",
                        "  /bg <id>           — Show background agent details",
                        "  /bg merge <id>     — Inject background agent result into conversation",
                        "  /bg kill <id>      — Kill a background agent",
                        "  /exit              — Quit OpenDev",
                        "",
                        "Keyboard shortcuts:",
                        "  Ctrl+C      — Clear input / interrupt / quit",
                        "  Ctrl+B      — Background running agent / toggle panel",
                        "  Ctrl+D      — Toggle debug panel",
                        "  Ctrl+R      — Open session picker",
                        "  Ctrl+X ...  — Leader key (u=undo, r=redo, s=share, m=models, d=debug)",
                        "  Alt+B       — Toggle task watcher panel",
                        "  Escape      — Interrupt agent",
                        "  Tab         — Accept autocomplete / toggle mode (when empty)",
                        "  Ctrl+I      — Toggle thinking block expand/collapse",
                        "  Shift+Tab   — Toggle mode",
                        "  PageUp/Down — Scroll conversation",
                    ]
                    .join("\n"),
                );
            }
            _ => {
                self.push_slash_echo(cmd);
                self.push_command_result(format!(
                    "Unknown command: /{name}. Type /help for available commands."
                ));
            }
        }
    }
}

#[cfg(test)]
#[path = "slash_commands_tests.rs"]
mod tests;