|
| 1 | +//! Command handling and execution for the session. |
| 2 | +//! |
| 3 | +//! This module handles all `InlineCommand` processing and execution. |
| 4 | +//! Commands are the primary way the backend communicates state changes |
| 5 | +//! and actions to the UI session. |
| 6 | +//! |
| 7 | +//! ## Command Categories |
| 8 | +//! |
| 9 | +//! 1. **Message Commands** |
| 10 | +//! - `AppendLine` - Add a new message line to transcript |
| 11 | +//! - `Inline` - Append content to the current line |
| 12 | +//! - `ReplaceLast` - Replace the last N lines |
| 13 | +//! |
| 14 | +//! 2. **UI State Commands** |
| 15 | +//! - `SetPrompt` - Update the input prompt prefix |
| 16 | +//! - `SetPlaceholder` - Update the input placeholder text |
| 17 | +//! - `SetTheme` - Change the color theme |
| 18 | +//! - `SetInputStatus` - Update status indicators |
| 19 | +//! - `SetHeaderContext` - Update header information |
| 20 | +//! |
| 21 | +//! 3. **Modal Commands** |
| 22 | +//! - `ShowModal` - Display a modal dialog |
| 23 | +//! - `ShowListModal` - Display a modal with selectable list |
| 24 | +//! - `CloseModal` - Close the active modal |
| 25 | +//! |
| 26 | +//! 4. **Input Commands** |
| 27 | +//! - `SetInput` - Set the input field content |
| 28 | +//! - `ClearInput` - Clear the input field |
| 29 | +//! - `SetInputEnabled` - Enable/disable input |
| 30 | +//! - `SetCursorVisible` - Show/hide cursor |
| 31 | +//! |
| 32 | +//! 5. **Palette Commands** |
| 33 | +//! - `LoadFilePalette` - Load file palette with workspace files |
| 34 | +//! - `SetCustomPrompts` - Load custom prompt registry |
| 35 | +//! |
| 36 | +//! 6. **Special Commands** |
| 37 | +//! - `SetQueuedInputs` - Set queued input entries |
| 38 | +//! - `SetPlan` - Set the task plan |
| 39 | +//! - `ClearScreen` - Clear all messages |
| 40 | +//! - `ForceRedraw` - Force screen redraw |
| 41 | +//! - `Shutdown` - Request application exit |
| 42 | +//! |
| 43 | +//! ## Architecture |
| 44 | +//! |
| 45 | +//! The `handle_command` function is the main dispatcher that receives |
| 46 | +//! `InlineCommand` enum variants and delegates to appropriate handler |
| 47 | +//! methods. Most handlers are small, focused functions that update |
| 48 | +//! specific pieces of session state. |
| 49 | +//! |
| 50 | +//! After handling each command, the session is marked as dirty to |
| 51 | +//! ensure the UI is redrawn on the next frame. |
| 52 | +
|
| 53 | +use crate::tools::TaskPlan; |
| 54 | +use super::Session; |
| 55 | +use super::super::types::{InlineCommand, SecurePromptConfig}; |
| 56 | +use super::modal::{ModalState, ModalListState, ModalSearchState}; |
| 57 | +use tui_popup::PopupState; |
| 58 | + |
| 59 | +// ============================================================================ |
| 60 | +// Main Command Dispatcher |
| 61 | +// ============================================================================ |
| 62 | + |
| 63 | +impl Session { |
| 64 | + /// Handle an incoming command from the backend. |
| 65 | + /// |
| 66 | + /// This is the main entry point for all command processing. It dispatches |
| 67 | + /// to specific handler methods based on the command type, then marks the |
| 68 | + /// session as dirty to trigger a redraw. |
| 69 | + /// |
| 70 | + /// # Arguments |
| 71 | + /// * `command` - The command to execute |
| 72 | + /// |
| 73 | + /// # Example Command Flow |
| 74 | + /// |
| 75 | + /// ```text |
| 76 | + /// Backend sends InlineCommand::AppendLine |
| 77 | + /// ↓ |
| 78 | + /// handle_command() receives it |
| 79 | + /// ↓ |
| 80 | + /// Calls self.push_line() |
| 81 | + /// ↓ |
| 82 | + /// Marks session dirty |
| 83 | + /// ↓ |
| 84 | + /// UI redraws on next frame |
| 85 | + /// ``` |
| 86 | + pub fn handle_command(&mut self, command: InlineCommand) { |
| 87 | + match command { |
| 88 | + InlineCommand::AppendLine { kind, segments } => { |
| 89 | + self.push_line(kind, segments); |
| 90 | + } |
| 91 | + InlineCommand::Inline { kind, segment } => { |
| 92 | + self.append_inline(kind, segment); |
| 93 | + } |
| 94 | + InlineCommand::ReplaceLast { count, kind, lines } => { |
| 95 | + self.replace_last(count, kind, lines); |
| 96 | + } |
| 97 | + InlineCommand::SetPrompt { prefix, style } => { |
| 98 | + self.prompt_state.prompt_prefix = prefix; |
| 99 | + self.prompt_state.prompt_style = style; |
| 100 | + self.ensure_prompt_style_color(); |
| 101 | + } |
| 102 | + InlineCommand::SetPlaceholder { hint, style } => { |
| 103 | + self.prompt_state.placeholder = hint; |
| 104 | + self.prompt_state.placeholder_style = style; |
| 105 | + } |
| 106 | + InlineCommand::SetMessageLabels { agent, user } => { |
| 107 | + self.display_state.labels.agent = agent.filter(|label| !label.is_empty()); |
| 108 | + self.display_state.labels.user = user.filter(|label| !label.is_empty()); |
| 109 | + self.invalidate_scroll_metrics(); |
| 110 | + } |
| 111 | + InlineCommand::SetHeaderContext { context } => { |
| 112 | + self.render_state.header_context = context; |
| 113 | + self.ui_state.needs_redraw = true; |
| 114 | + } |
| 115 | + InlineCommand::SetInputStatus { left, right } => { |
| 116 | + self.prompt_state.input_status_left = left; |
| 117 | + self.prompt_state.input_status_right = right; |
| 118 | + self.ui_state.needs_redraw = true; |
| 119 | + } |
| 120 | + InlineCommand::SetTheme { theme } => { |
| 121 | + self.display_state.theme = theme; |
| 122 | + self.ensure_prompt_style_color(); |
| 123 | + self.invalidate_transcript_cache(); |
| 124 | + } |
| 125 | + InlineCommand::SetQueuedInputs { entries } => { |
| 126 | + self.set_queued_inputs_entries(entries); |
| 127 | + self.mark_dirty(); |
| 128 | + } |
| 129 | + InlineCommand::SetPlan { plan } => { |
| 130 | + self.set_plan(plan); |
| 131 | + } |
| 132 | + InlineCommand::SetCursorVisible(value) => { |
| 133 | + self.ui_state.cursor_visible = value; |
| 134 | + } |
| 135 | + InlineCommand::SetInputEnabled(value) => { |
| 136 | + self.ui_state.input_enabled = value; |
| 137 | + self.update_slash_suggestions(); |
| 138 | + } |
| 139 | + InlineCommand::SetInput(content) => { |
| 140 | + self.input_manager.set_content(content); |
| 141 | + self.scroll_manager.set_offset(0); |
| 142 | + self.update_slash_suggestions(); |
| 143 | + } |
| 144 | + InlineCommand::ClearInput => { |
| 145 | + self.clear_input(); |
| 146 | + } |
| 147 | + InlineCommand::ForceRedraw => { |
| 148 | + self.mark_dirty(); |
| 149 | + } |
| 150 | + InlineCommand::ShowModal { |
| 151 | + title, |
| 152 | + lines, |
| 153 | + secure_prompt, |
| 154 | + } => { |
| 155 | + self.show_modal(title, lines, secure_prompt); |
| 156 | + } |
| 157 | + InlineCommand::ShowListModal { |
| 158 | + title, |
| 159 | + lines, |
| 160 | + items, |
| 161 | + selected, |
| 162 | + search, |
| 163 | + } => { |
| 164 | + self.show_list_modal(title, lines, items, selected, search); |
| 165 | + } |
| 166 | + InlineCommand::CloseModal => { |
| 167 | + self.close_modal(); |
| 168 | + } |
| 169 | + InlineCommand::SetCustomPrompts { registry } => { |
| 170 | + self.set_custom_prompts(registry); |
| 171 | + } |
| 172 | + InlineCommand::LoadFilePalette { files, workspace } => { |
| 173 | + self.load_file_palette(files, workspace); |
| 174 | + } |
| 175 | + InlineCommand::ClearScreen => { |
| 176 | + self.clear_screen(); |
| 177 | + } |
| 178 | + InlineCommand::Shutdown => { |
| 179 | + self.request_exit(); |
| 180 | + } |
| 181 | + } |
| 182 | + self.mark_dirty(); |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +// ============================================================================ |
| 187 | +// Plan Management |
| 188 | +// ============================================================================ |
| 189 | + |
| 190 | +impl Session { |
| 191 | + /// Set the task plan for the session. |
| 192 | + /// |
| 193 | + /// This updates the current task plan and marks the session dirty. |
| 194 | + /// The task plan is typically displayed in a timeline pane or overlay. |
| 195 | + /// |
| 196 | + /// # Arguments |
| 197 | + /// * `plan` - The new task plan to display |
| 198 | + fn set_plan(&mut self, plan: TaskPlan) { |
| 199 | + self.render_state.plan = plan; |
| 200 | + self.mark_dirty(); |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +// ============================================================================ |
| 205 | +// Modal Management |
| 206 | +// ============================================================================ |
| 207 | + |
| 208 | +impl Session { |
| 209 | + /// Show a modal dialog with text content. |
| 210 | + /// |
| 211 | + /// This creates and displays a modal dialog that overlays the main UI. |
| 212 | + /// The modal can optionally include a secure prompt for password entry. |
| 213 | + /// |
| 214 | + /// When a modal is shown: |
| 215 | + /// - Input is disabled (unless secure_prompt is Some) |
| 216 | + /// - Cursor is hidden |
| 217 | + /// - Previous input/cursor state is saved for restoration |
| 218 | + /// |
| 219 | + /// # Arguments |
| 220 | + /// * `title` - The modal title text |
| 221 | + /// * `lines` - Content lines to display in the modal |
| 222 | + /// * `secure_prompt` - Optional secure password prompt configuration |
| 223 | + fn show_modal( |
| 224 | + &mut self, |
| 225 | + title: String, |
| 226 | + lines: Vec<String>, |
| 227 | + secure_prompt: Option<SecurePromptConfig>, |
| 228 | + ) { |
| 229 | + let state = ModalState { |
| 230 | + title, |
| 231 | + lines, |
| 232 | + list: None, |
| 233 | + secure_prompt, |
| 234 | + popup_state: PopupState::default(), |
| 235 | + restore_input: self.ui_state.input_enabled, |
| 236 | + restore_cursor: self.ui_state.cursor_visible, |
| 237 | + search: None, |
| 238 | + }; |
| 239 | + if state.secure_prompt.is_none() { |
| 240 | + self.ui_state.input_enabled = false; |
| 241 | + } |
| 242 | + self.ui_state.cursor_visible = false; |
| 243 | + self.render_state.modal = Some(state); |
| 244 | + self.mark_dirty(); |
| 245 | + } |
| 246 | + |
| 247 | + /// Show a modal dialog with a selectable list. |
| 248 | + /// |
| 249 | + /// This creates and displays a modal with both text content and a list |
| 250 | + /// of selectable items. Users can navigate the list with arrow keys and |
| 251 | + /// optionally search/filter the items. |
| 252 | + /// |
| 253 | + /// # Arguments |
| 254 | + /// * `title` - The modal title text |
| 255 | + /// * `lines` - Content lines to display above the list |
| 256 | + /// * `items` - List items to display |
| 257 | + /// * `selected` - Initially selected item (by ID or index) |
| 258 | + /// * `search` - Optional search/filter configuration |
| 259 | + fn show_list_modal( |
| 260 | + &mut self, |
| 261 | + title: String, |
| 262 | + lines: Vec<String>, |
| 263 | + items: Vec<crate::tui::types::InlineListItem>, |
| 264 | + selected: Option<crate::tui::types::InlineListSelection>, |
| 265 | + search: Option<crate::tui::types::InlineListSearchConfig>, |
| 266 | + ) { |
| 267 | + let mut list_state = ModalListState::new(items, selected); |
| 268 | + let search_state = search.map(ModalSearchState::from); |
| 269 | + if let Some(search) = &search_state { |
| 270 | + list_state.apply_search(&search.query); |
| 271 | + } |
| 272 | + let state = ModalState { |
| 273 | + title, |
| 274 | + lines, |
| 275 | + list: Some(list_state), |
| 276 | + secure_prompt: None, |
| 277 | + popup_state: PopupState::default(), |
| 278 | + restore_input: self.ui_state.input_enabled, |
| 279 | + restore_cursor: self.ui_state.cursor_visible, |
| 280 | + search: search_state, |
| 281 | + }; |
| 282 | + self.ui_state.input_enabled = false; |
| 283 | + self.ui_state.cursor_visible = false; |
| 284 | + self.render_state.modal = Some(state); |
| 285 | + self.mark_dirty(); |
| 286 | + } |
| 287 | + |
| 288 | + /// Close the currently open modal dialog. |
| 289 | + /// |
| 290 | + /// This removes the modal from the UI and restores the previous input |
| 291 | + /// and cursor state. It also forces a full screen clear and transcript |
| 292 | + /// cache invalidation to ensure the modal is completely removed without |
| 293 | + /// visual artifacts. |
| 294 | + pub(super) fn close_modal(&mut self) { |
| 295 | + if let Some(state) = self.render_state.modal.take() { |
| 296 | + self.ui_state.input_enabled = state.restore_input; |
| 297 | + self.ui_state.cursor_visible = state.restore_cursor; |
| 298 | + // Force full screen clear on next render to remove modal artifacts |
| 299 | + self.ui_state.needs_full_clear = true; |
| 300 | + // Force transcript cache invalidation to ensure full redraw |
| 301 | + self.render_state.transcript_cache = None; |
| 302 | + self.mark_dirty(); |
| 303 | + } |
| 304 | + } |
| 305 | +} |
| 306 | + |
| 307 | +// ============================================================================ |
| 308 | +// Screen Management |
| 309 | +// ============================================================================ |
| 310 | + |
| 311 | +impl Session { |
| 312 | + /// Clear all messages from the screen. |
| 313 | + /// |
| 314 | + /// This removes all message lines from the transcript, resets scroll position, |
| 315 | + /// invalidates caches, and forces a full screen clear on the next render. |
| 316 | + /// |
| 317 | + /// This is typically used when starting a new conversation or when the user |
| 318 | + /// explicitly requests to clear the screen. |
| 319 | + fn clear_screen(&mut self) { |
| 320 | + self.display_state.lines.clear(); |
| 321 | + self.scroll_manager.set_offset(0); |
| 322 | + self.invalidate_transcript_cache(); |
| 323 | + self.invalidate_scroll_metrics(); |
| 324 | + self.ui_state.needs_full_clear = true; |
| 325 | + self.mark_dirty(); |
| 326 | + } |
| 327 | + |
| 328 | + /// Mark the session as needing a redraw. |
| 329 | + /// |
| 330 | + /// This sets the `needs_redraw` flag to true, which signals the render |
| 331 | + /// loop that the UI should be redrawn on the next frame. |
| 332 | + /// |
| 333 | + /// This is called after virtually every state change to ensure the UI |
| 334 | + /// stays in sync with the session state. |
| 335 | + pub fn mark_dirty(&mut self) { |
| 336 | + self.ui_state.needs_redraw = true; |
| 337 | + } |
| 338 | +} |
0 commit comments