diff --git a/codex-rs/tui/src/bottom_pane/chat_composer.rs b/codex-rs/tui/src/bottom_pane/chat_composer.rs index 403a499bd..0a82f7cf8 100644 --- a/codex-rs/tui/src/bottom_pane/chat_composer.rs +++ b/codex-rs/tui/src/bottom_pane/chat_composer.rs @@ -29,6 +29,10 @@ //! Recalled entries move the cursor to end-of-line so repeated Up/Down presses keep shell-like //! history traversal semantics instead of dropping to column 0. //! +//! Slash commands are staged for local history instead of being recorded immediately. Command +//! recall is a two-phase handoff: stage the submitted slash text here, then record it after +//! `ChatWidget` dispatches the command. +//! //! # Submission and Prompt Expansion //! //! `Enter` submits immediately. `Tab` requests queuing while a task is running; if no task is @@ -228,7 +232,16 @@ pub enum InputResult { text: String, text_elements: Vec, }, + /// A bare slash command parsed by the composer. + /// + /// Callers that dispatch this variant are also responsible for resolving any pending local + /// command-history entry that the composer staged before clearing the visible input. Command(SlashCommand), + /// An inline slash command and its trimmed argument text. + /// + /// The `TextElement` ranges are rebased into the argument string, while any pending local + /// command-history entry still represents the original command invocation that should be + /// committed only if dispatch accepts it. CommandWithArgs(SlashCommand, String, Vec), None, } @@ -311,6 +324,11 @@ pub(crate) struct ChatComposer { /// Tracks keyboard selection for the remote-image rows so Up/Down + Delete/Backspace /// can highlight and remove remote attachments from the composer UI. selected_remote_image_index: Option, + /// Slash-command draft staged for local recall after application-level dispatch. + /// + /// This slot is intentionally separate from `ChatComposerHistory` so inline slash commands can + /// prepare their argument text without also double-recording the full command invocation. + pending_slash_command_history: Option, footer_flash: Option, context_window_percent: Option, // Monotonically increasing identifier for textarea elements we insert. @@ -434,6 +452,7 @@ impl ChatComposer { footer_hint_override: None, remote_image_urls: Vec::new(), selected_remote_image_index: None, + pending_slash_command_history: None, footer_flash: None, context_window_percent: None, #[cfg(not(target_os = "linux"))] @@ -1063,6 +1082,16 @@ impl ChatComposer { std::mem::take(&mut self.recent_submission_mention_bindings) } + /// Commit the staged slash-command draft to local Up-arrow recall. + /// + /// Call this after command dispatch. Calling it more than once is harmless because the pending + /// slot is consumed on the first call. + pub(crate) fn record_pending_slash_command_history(&mut self) { + if let Some(entry) = self.pending_slash_command_history.take() { + self.history.record_local_submission(entry); + } + } + fn prune_attached_images_for_submission(&mut self, text: &str, text_elements: &[TextElement]) { if self.attached_images.is_empty() { return; @@ -1281,6 +1310,7 @@ impl ChatComposer { if let Some(sel) = popup.selected_item() { let CommandItem::Builtin(cmd) = sel; if cmd == SlashCommand::Skills { + self.stage_selected_slash_command_history(cmd); self.textarea.set_text_clearing_elements(""); return (InputResult::Command(cmd), true); } @@ -1305,6 +1335,7 @@ impl ChatComposer { } => { if let Some(sel) = popup.selected_item() { let CommandItem::Builtin(cmd) = sel; + self.stage_selected_slash_command_history(cmd); self.textarea.set_text_clearing_elements(""); return (InputResult::Command(cmd), true); } @@ -2272,8 +2303,11 @@ impl ChatComposer { slash_commands::find_builtin_command(name, self.builtin_command_flags()) { if self.reject_slash_command_if_unavailable(cmd) { + self.stage_slash_command_history(); + self.record_pending_slash_command_history(); return Some(InputResult::None); } + self.stage_slash_command_history(); self.textarea.set_text_clearing_elements(""); Some(InputResult::Command(cmd)) } else { @@ -2303,9 +2337,13 @@ impl ChatComposer { return None; } if self.reject_slash_command_if_unavailable(cmd) { + self.stage_slash_command_history(); + self.record_pending_slash_command_history(); return Some(InputResult::None); } + self.stage_slash_command_history(); + let mut args_elements = Self::slash_command_args_elements(rest, rest_offset, &self.textarea.text_elements()); let trimmed_rest = rest.trim(); @@ -2320,9 +2358,13 @@ impl ChatComposer { /// Expand pending placeholders and extract normalized inline-command args. /// /// Inline-arg commands are initially dispatched using the raw draft so command rejection does - /// not consume user input. Once a command is accepted, this helper performs the usual + /// not consume user input. Once a command needs its args, this helper performs the usual /// submission preparation (paste expansion, element trimming) and rebases element ranges from /// full-text offsets to command-arg offsets. + /// + /// Callers that already staged slash-command history should normally pass `false` for + /// `record_history`; otherwise a command such as `/plan investigate` would be entered into + /// local recall through both the slash-command path and the message-submission path. pub(crate) fn prepare_inline_args_submission( &mut self, record_history: bool, @@ -2353,6 +2395,43 @@ impl ChatComposer { true } + /// Stage the current slash-command text for later local recall. + /// + /// Staging snapshots the rich composer state before the textarea is cleared. `ChatWidget` + /// commits the staged entry after dispatch so command recall follows the submitted text, not + /// the command outcome. + fn stage_slash_command_history(&mut self) { + self.stage_slash_command_history_text(self.textarea.text().trim().to_string()); + } + + /// Stage a popup-selected command using its canonical command text. + /// + /// Popup filtering text can be partial, so recording the selected command avoids recalling + /// `/di` after the user actually accepted `/diff`. + fn stage_selected_slash_command_history(&mut self, cmd: SlashCommand) { + self.stage_slash_command_history_text(format!("/{}", cmd.command())); + } + + /// Store the provided command text and the current composer adornments in the pending slot. + /// + /// The pending entry intentionally has the same shape as other local history entries so recall + /// can rehydrate attachments, mention bindings, and pending paste placeholders if command + /// workflows start carrying those through in the future. + fn stage_slash_command_history_text(&mut self, text: String) { + self.pending_slash_command_history = Some(HistoryEntry { + text, + text_elements: self.textarea.text_elements(), + local_image_paths: self + .attached_images + .iter() + .map(|img| img.path.clone()) + .collect(), + remote_image_urls: self.remote_image_urls.clone(), + mention_bindings: self.snapshot_mention_bindings(), + pending_pastes: self.pending_pastes.clone(), + }); + } + /// Translate full-text element ranges into command-argument ranges. /// /// `rest_offset` is the byte offset where `rest` begins in the full text. @@ -7863,6 +7942,90 @@ mod tests { ); } + #[test] + fn bare_slash_command_can_be_recalled_after_recording_pending_history() { + let (tx, _rx) = unbounded_channel::(); + let sender = AppEventSender::new(tx); + let mut composer = ChatComposer::new( + /*has_input_focus*/ true, + sender, + /*enhanced_keys_supported*/ false, + "Ask Codex to do anything".to_string(), + /*disable_paste_burst*/ false, + ); + + composer.set_text_content("/diff".to_string(), Vec::new(), Vec::new()); + let (result, _needs_redraw) = + composer.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)); + + assert_eq!(result, InputResult::Command(SlashCommand::Diff)); + composer.record_pending_slash_command_history(); + + let (result, _needs_redraw) = + composer.handle_key_event(KeyEvent::new(KeyCode::Up, KeyModifiers::NONE)); + assert_eq!(result, InputResult::None); + assert_eq!(composer.current_text(), "/diff"); + } + + #[test] + fn popup_selected_slash_command_records_canonical_command_history() { + let (tx, _rx) = unbounded_channel::(); + let sender = AppEventSender::new(tx); + let mut composer = ChatComposer::new( + /*has_input_focus*/ true, + sender, + /*enhanced_keys_supported*/ false, + "Ask Codex to do anything".to_string(), + /*disable_paste_burst*/ false, + ); + + composer.set_text_content("/di".to_string(), Vec::new(), Vec::new()); + let (result, _needs_redraw) = + composer.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)); + + assert_eq!(result, InputResult::Command(SlashCommand::Diff)); + composer.record_pending_slash_command_history(); + + let (result, _needs_redraw) = + composer.handle_key_event(KeyEvent::new(KeyCode::Up, KeyModifiers::NONE)); + assert_eq!(result, InputResult::None); + assert_eq!(composer.current_text(), "/diff"); + } + + #[test] + fn inline_slash_command_can_be_recalled_after_recording_pending_history() { + let (tx, _rx) = unbounded_channel::(); + let sender = AppEventSender::new(tx); + let mut composer = ChatComposer::new( + /*has_input_focus*/ true, + sender, + /*enhanced_keys_supported*/ false, + "Ask Codex to do anything".to_string(), + /*disable_paste_burst*/ false, + ); + composer.set_collaboration_modes_enabled(/*enabled*/ true); + + composer.set_text_content("/plan investigate this".to_string(), Vec::new(), Vec::new()); + composer.active_popup = ActivePopup::None; + let (result, _needs_redraw) = + composer.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)); + + match result { + InputResult::CommandWithArgs(cmd, args, text_elements) => { + assert_eq!(cmd, SlashCommand::Plan); + assert_eq!(args, "investigate this"); + assert!(text_elements.is_empty()); + } + other => panic!("expected inline /plan command, got {other:?}"), + } + composer.record_pending_slash_command_history(); + + let (result, _needs_redraw) = + composer.handle_key_event(KeyEvent::new(KeyCode::Up, KeyModifiers::NONE)); + assert_eq!(result, InputResult::None); + assert_eq!(composer.current_text(), "/plan investigate this"); + } + #[test] fn apply_external_edit_rebuilds_text_and_attachments() { let (tx, _rx) = unbounded_channel::(); diff --git a/codex-rs/tui/src/bottom_pane/mod.rs b/codex-rs/tui/src/bottom_pane/mod.rs index ef3898baf..bd3bd946a 100644 --- a/codex-rs/tui/src/bottom_pane/mod.rs +++ b/codex-rs/tui/src/bottom_pane/mod.rs @@ -280,6 +280,14 @@ impl BottomPane { self.composer.take_recent_submission_mention_bindings() } + /// Add a staged slash-command draft to the composer's local recall list. + /// + /// This should be called exactly once after `ChatWidget` dispatches a recognized command. + /// Slash recall records the submitted command text regardless of whether the command succeeds. + pub(crate) fn record_pending_slash_command_history(&mut self) { + self.composer.record_pending_slash_command_history(); + } + /// Clear pending attachments and mention bindings e.g. when a slash command doesn't submit text. pub(crate) fn drain_pending_submission_state(&mut self) { let _ = self.take_recent_submission_images_with_placeholders(); diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index b8b5d9027..1e5ad3e56 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -25,6 +25,10 @@ //! the final answer. During streaming we hide the status row to avoid duplicate //! progress indicators; once commentary completes and stream queues drain, we //! re-show it so users still see turn-in-progress state between output bursts. +//! +//! Slash-command parsing lives in the bottom-pane composer, but slash-command acceptance lives +//! here. That split lets the composer stage a recall entry before clearing input while this module +//! records the attempted slash command after dispatch just like ordinary submitted text. use std::collections::BTreeMap; use std::collections::BTreeSet; use std::collections::HashMap; @@ -355,6 +359,7 @@ use self::interrupts::InterruptManager; mod session_header; use self::session_header::SessionHeader; mod skills; +mod slash_dispatch; use self::skills::collect_tool_mentions; use self::skills::find_app_mentions; use self::skills::find_skill_mentions_with_tool_mentions; @@ -5088,10 +5093,10 @@ impl ChatWidget { self.queue_user_message(user_message); } InputResult::Command(cmd) => { - self.dispatch_command(cmd); + self.handle_slash_command_dispatch(cmd); } InputResult::CommandWithArgs(cmd, args, text_elements) => { - self.dispatch_command_with_args(cmd, args, text_elements); + self.handle_slash_command_with_args_dispatch(cmd, args, text_elements); } InputResult::None => {} }, @@ -5197,468 +5202,6 @@ impl ChatWidget { self.last_agent_markdown.as_deref() } - fn dispatch_command(&mut self, cmd: SlashCommand) { - if !cmd.available_during_task() && self.bottom_pane.is_task_running() { - let message = format!( - "'/{}' is disabled while a task is in progress.", - cmd.command() - ); - self.add_to_history(history_cell::new_error_event(message)); - self.bottom_pane.drain_pending_submission_state(); - self.request_redraw(); - return; - } - match cmd { - SlashCommand::Feedback => { - if !self.config.feedback_enabled { - let params = crate::bottom_pane::feedback_disabled_params(); - self.bottom_pane.show_selection_view(params); - self.request_redraw(); - return; - } - // Step 1: pick a category (UI built in feedback_view) - let params = - crate::bottom_pane::feedback_selection_params(self.app_event_tx.clone()); - self.bottom_pane.show_selection_view(params); - self.request_redraw(); - } - SlashCommand::New => { - self.app_event_tx.send(AppEvent::NewSession); - } - SlashCommand::Clear => { - self.app_event_tx.send(AppEvent::ClearUi); - } - SlashCommand::Resume => { - self.app_event_tx.send(AppEvent::OpenResumePicker); - } - SlashCommand::Fork => { - self.app_event_tx.send(AppEvent::ForkCurrentSession); - } - SlashCommand::Init => { - let init_target = self.config.cwd.join(DEFAULT_PROJECT_DOC_FILENAME); - if init_target.exists() { - let message = format!( - "{DEFAULT_PROJECT_DOC_FILENAME} already exists here. Skipping /init to avoid overwriting it." - ); - self.add_info_message(message, /*hint*/ None); - return; - } - const INIT_PROMPT: &str = include_str!("../prompt_for_init_command.md"); - self.submit_user_message(INIT_PROMPT.to_string().into()); - } - SlashCommand::Compact => { - self.clear_token_usage(); - if !self.bottom_pane.is_task_running() { - self.bottom_pane.set_task_running(/*running*/ true); - } - self.app_event_tx.compact(); - } - SlashCommand::Review => { - self.open_review_popup(); - } - SlashCommand::Rename => { - self.session_telemetry - .counter("codex.thread.rename", /*inc*/ 1, &[]); - self.show_rename_prompt(); - } - SlashCommand::Model => { - self.open_model_popup(); - } - SlashCommand::Fast => { - let next_tier = if matches!(self.config.service_tier, Some(ServiceTier::Fast)) { - None - } else { - Some(ServiceTier::Fast) - }; - self.set_service_tier_selection(next_tier); - } - SlashCommand::Realtime => { - if !self.realtime_conversation_enabled() { - return; - } - if self.realtime_conversation.is_live() { - self.stop_realtime_conversation_from_ui(); - } else { - self.start_realtime_conversation(); - } - } - SlashCommand::Settings => { - if !self.realtime_audio_device_selection_enabled() { - return; - } - self.open_realtime_audio_popup(); - } - SlashCommand::Personality => { - self.open_personality_popup(); - } - SlashCommand::Plan => { - if !self.collaboration_modes_enabled() { - self.add_info_message( - "Collaboration modes are disabled.".to_string(), - Some("Enable collaboration modes to use /plan.".to_string()), - ); - return; - } - if let Some(mask) = collaboration_modes::plan_mask(self.model_catalog.as_ref()) { - self.set_collaboration_mask(mask); - } else { - self.add_info_message( - "Plan mode unavailable right now.".to_string(), - /*hint*/ None, - ); - } - } - SlashCommand::Collab => { - if !self.collaboration_modes_enabled() { - self.add_info_message( - "Collaboration modes are disabled.".to_string(), - Some("Enable collaboration modes to use /collab.".to_string()), - ); - return; - } - self.open_collaboration_modes_popup(); - } - SlashCommand::Agent | SlashCommand::MultiAgents => { - self.app_event_tx.send(AppEvent::OpenAgentPicker); - } - SlashCommand::Approvals => { - self.open_permissions_popup(); - } - SlashCommand::Permissions => { - self.open_permissions_popup(); - } - SlashCommand::ElevateSandbox => { - #[cfg(target_os = "windows")] - { - let windows_sandbox_level = WindowsSandboxLevel::from_config(&self.config); - let windows_degraded_sandbox_enabled = - matches!(windows_sandbox_level, WindowsSandboxLevel::RestrictedToken); - if !windows_degraded_sandbox_enabled - || !crate::legacy_core::windows_sandbox::ELEVATED_SANDBOX_NUX_ENABLED - { - // This command should not be visible/recognized outside degraded mode, - // but guard anyway in case something dispatches it directly. - return; - } - - let Some(preset) = builtin_approval_presets() - .into_iter() - .find(|preset| preset.id == "auto") - else { - // Avoid panicking in interactive UI; treat this as a recoverable - // internal error. - self.add_error_message( - "Internal error: missing the 'auto' approval preset.".to_string(), - ); - return; - }; - - if let Err(err) = self - .config - .permissions - .approval_policy - .can_set(&preset.approval) - { - self.add_error_message(err.to_string()); - return; - } - - self.session_telemetry.counter( - "codex.windows_sandbox.setup_elevated_sandbox_command", - /*inc*/ 1, - &[], - ); - self.app_event_tx - .send(AppEvent::BeginWindowsSandboxElevatedSetup { preset }); - } - #[cfg(not(target_os = "windows"))] - { - let _ = &self.session_telemetry; - // Not supported; on non-Windows this command should never be reachable. - }; - } - SlashCommand::SandboxReadRoot => { - self.add_error_message( - "Usage: /sandbox-add-read-dir ".to_string(), - ); - } - SlashCommand::Experimental => { - self.open_experimental_popup(); - } - SlashCommand::Quit | SlashCommand::Exit => { - self.request_quit_without_confirmation(); - } - SlashCommand::Logout => { - if let Err(e) = codex_login::logout( - &self.config.codex_home, - self.config.cli_auth_credentials_store_mode, - ) { - tracing::error!("failed to logout: {e}"); - } - self.request_quit_without_confirmation(); - } - // SlashCommand::Undo => { - // self.app_event_tx.send(AppEvent::CodexOp(Op::Undo)); - // } - SlashCommand::Copy => { - self.copy_last_agent_markdown(); - } - SlashCommand::Diff => { - self.add_diff_in_progress(); - let tx = self.app_event_tx.clone(); - tokio::spawn(async move { - let text = match get_git_diff().await { - Ok((is_git_repo, diff_text)) => { - if is_git_repo { - diff_text - } else { - "`/diff` — _not inside a git repository_".to_string() - } - } - Err(e) => format!("Failed to compute diff: {e}"), - }; - tx.send(AppEvent::DiffResult(text)); - }); - } - SlashCommand::Mention => { - self.insert_str("@"); - } - SlashCommand::Skills => { - self.open_skills_menu(); - } - SlashCommand::Status => { - if self.should_prefetch_rate_limits() { - let request_id = self.next_status_refresh_request_id; - self.next_status_refresh_request_id = - self.next_status_refresh_request_id.wrapping_add(1); - self.add_status_output(/*refreshing_rate_limits*/ true, Some(request_id)); - self.app_event_tx.send(AppEvent::RefreshRateLimits { - origin: RateLimitRefreshOrigin::StatusCommand { request_id }, - }); - } else { - self.add_status_output( - /*refreshing_rate_limits*/ false, /*request_id*/ None, - ); - } - } - SlashCommand::DebugConfig => { - self.add_debug_config_output(); - } - SlashCommand::Title => { - self.open_terminal_title_setup(); - } - SlashCommand::Statusline => { - self.open_status_line_setup(); - } - SlashCommand::Theme => { - self.open_theme_picker(); - } - SlashCommand::Ps => { - self.add_ps_output(); - } - SlashCommand::Stop => { - self.clean_background_terminals(); - } - SlashCommand::MemoryDrop => { - self.add_app_server_stub_message("Memory maintenance"); - } - SlashCommand::MemoryUpdate => { - self.add_app_server_stub_message("Memory maintenance"); - } - SlashCommand::Mcp => { - self.add_mcp_output(); - } - SlashCommand::Apps => { - self.add_connectors_output(); - } - SlashCommand::Plugins => { - self.add_plugins_output(); - } - SlashCommand::Rollout => { - if let Some(path) = self.rollout_path() { - self.add_info_message( - format!("Current rollout path: {}", path.display()), - /*hint*/ None, - ); - } else { - self.add_info_message( - "Rollout path is not available yet.".to_string(), - /*hint*/ None, - ); - } - } - SlashCommand::TestApproval => { - use std::collections::HashMap; - - use codex_protocol::protocol::ApplyPatchApprovalRequestEvent; - use codex_protocol::protocol::FileChange; - - self.on_apply_patch_approval_request( - "1".to_string(), - ApplyPatchApprovalRequestEvent { - call_id: "1".to_string(), - turn_id: "turn-1".to_string(), - changes: HashMap::from([ - ( - PathBuf::from("/tmp/test.txt"), - FileChange::Add { - content: "test".to_string(), - }, - ), - ( - PathBuf::from("/tmp/test2.txt"), - FileChange::Update { - unified_diff: "+test\n-test2".to_string(), - move_path: None, - }, - ), - ]), - reason: None, - grant_root: Some(PathBuf::from("/tmp")), - }, - ); - } - } - } - - fn dispatch_command_with_args( - &mut self, - cmd: SlashCommand, - args: String, - _text_elements: Vec, - ) { - if !cmd.supports_inline_args() { - self.dispatch_command(cmd); - return; - } - if !cmd.available_during_task() && self.bottom_pane.is_task_running() { - let message = format!( - "'/{}' is disabled while a task is in progress.", - cmd.command() - ); - self.add_to_history(history_cell::new_error_event(message)); - self.request_redraw(); - return; - } - - let trimmed = args.trim(); - match cmd { - SlashCommand::Fast => { - if trimmed.is_empty() { - self.dispatch_command(cmd); - return; - } - match trimmed.to_ascii_lowercase().as_str() { - "on" => self.set_service_tier_selection(Some(ServiceTier::Fast)), - "off" => self.set_service_tier_selection(/*service_tier*/ None), - "status" => { - let status = if matches!(self.config.service_tier, Some(ServiceTier::Fast)) - { - "on" - } else { - "off" - }; - self.add_info_message( - format!("Fast mode is {status}."), - /*hint*/ None, - ); - } - _ => { - self.add_error_message("Usage: /fast [on|off|status]".to_string()); - } - } - } - SlashCommand::Rename if !trimmed.is_empty() => { - self.session_telemetry - .counter("codex.thread.rename", /*inc*/ 1, &[]); - let Some((prepared_args, _prepared_elements)) = self - .bottom_pane - .prepare_inline_args_submission(/*record_history*/ false) - else { - return; - }; - let Some(name) = crate::legacy_core::util::normalize_thread_name(&prepared_args) - else { - self.add_error_message("Thread name cannot be empty.".to_string()); - return; - }; - self.app_event_tx.set_thread_name(name); - self.bottom_pane.drain_pending_submission_state(); - } - SlashCommand::Plan if !trimmed.is_empty() => { - self.dispatch_command(cmd); - if self.active_mode_kind() != ModeKind::Plan { - return; - } - let Some((prepared_args, prepared_elements)) = self - .bottom_pane - .prepare_inline_args_submission(/*record_history*/ true) - else { - return; - }; - let local_images = self - .bottom_pane - .take_recent_submission_images_with_placeholders(); - let remote_image_urls = self.take_remote_image_urls(); - let user_message = UserMessage { - text: prepared_args, - local_images, - remote_image_urls, - text_elements: prepared_elements, - mention_bindings: self.bottom_pane.take_recent_submission_mention_bindings(), - }; - if self.is_session_configured() { - self.reasoning_buffer.clear(); - self.full_reasoning_buffer.clear(); - self.set_status_header(String::from("Working")); - self.submit_user_message(user_message); - } else { - self.queue_user_message(user_message); - } - } - SlashCommand::Review if !trimmed.is_empty() => { - let Some((prepared_args, _prepared_elements)) = self - .bottom_pane - .prepare_inline_args_submission(/*record_history*/ false) - else { - return; - }; - self.submit_op(AppCommand::review(ReviewRequest { - target: ReviewTarget::Custom { - instructions: prepared_args, - }, - user_facing_hint: None, - })); - self.bottom_pane.drain_pending_submission_state(); - } - SlashCommand::Resume if !trimmed.is_empty() => { - let Some((prepared_args, _prepared_elements)) = self - .bottom_pane - .prepare_inline_args_submission(/*record_history*/ false) - else { - return; - }; - self.app_event_tx - .send(AppEvent::ResumeSessionByIdOrName(prepared_args)); - self.bottom_pane.drain_pending_submission_state(); - } - SlashCommand::SandboxReadRoot if !trimmed.is_empty() => { - let Some((prepared_args, _prepared_elements)) = self - .bottom_pane - .prepare_inline_args_submission(/*record_history*/ false) - else { - return; - }; - self.app_event_tx - .send(AppEvent::BeginWindowsSandboxGrantReadRoot { - path: prepared_args, - }); - self.bottom_pane.drain_pending_submission_state(); - } - _ => self.dispatch_command(cmd), - } - } - fn show_rename_prompt(&mut self) { let tx = self.app_event_tx.clone(); let has_name = self diff --git a/codex-rs/tui/src/chatwidget/slash_dispatch.rs b/codex-rs/tui/src/chatwidget/slash_dispatch.rs new file mode 100644 index 000000000..e6e93d17f --- /dev/null +++ b/codex-rs/tui/src/chatwidget/slash_dispatch.rs @@ -0,0 +1,519 @@ +//! Slash-command dispatch and local-recall handoff for `ChatWidget`. +//! +//! `ChatComposer` parses slash input and stages recognized command text for local +//! Up-arrow recall before returning an input result. This module owns the app-level +//! dispatch step and records the staged entry once the command has been handled, so +//! slash-command recall follows the same submitted-input rule as ordinary text. + +use super::*; + +impl ChatWidget { + /// Dispatch a bare slash command and record its staged local-history entry. + /// + /// The composer stages history before returning `InputResult::Command`; this wrapper commits + /// that staged entry after dispatch so slash-command recall follows the same "submitted input" + /// rule as normal text. + pub(super) fn handle_slash_command_dispatch(&mut self, cmd: SlashCommand) { + self.dispatch_command(cmd); + self.bottom_pane.record_pending_slash_command_history(); + } + + /// Dispatch an inline slash command and record its staged local-history entry. + /// + /// Inline command arguments may later be prepared through the normal submission pipeline, but + /// local command recall still tracks the original command invocation. Treating this wrapper as + /// the only input-result entry point avoids double-recording commands with inline args. + pub(super) fn handle_slash_command_with_args_dispatch( + &mut self, + cmd: SlashCommand, + args: String, + text_elements: Vec, + ) { + self.dispatch_command_with_args(cmd, args, text_elements); + self.bottom_pane.record_pending_slash_command_history(); + } + + fn apply_plan_slash_command(&mut self) -> bool { + if !self.collaboration_modes_enabled() { + self.add_info_message( + "Collaboration modes are disabled.".to_string(), + Some("Enable collaboration modes to use /plan.".to_string()), + ); + return false; + } + if let Some(mask) = collaboration_modes::plan_mask(self.model_catalog.as_ref()) { + self.set_collaboration_mask(mask); + true + } else { + self.add_info_message( + "Plan mode unavailable right now.".to_string(), + /*hint*/ None, + ); + false + } + } + + pub(super) fn dispatch_command(&mut self, cmd: SlashCommand) { + if !cmd.available_during_task() && self.bottom_pane.is_task_running() { + let message = format!( + "'/{}' is disabled while a task is in progress.", + cmd.command() + ); + self.add_to_history(history_cell::new_error_event(message)); + self.bottom_pane.drain_pending_submission_state(); + self.request_redraw(); + return; + } + + match cmd { + SlashCommand::Feedback => { + if !self.config.feedback_enabled { + let params = crate::bottom_pane::feedback_disabled_params(); + self.bottom_pane.show_selection_view(params); + self.request_redraw(); + return; + } + // Step 1: pick a category (UI built in feedback_view) + let params = + crate::bottom_pane::feedback_selection_params(self.app_event_tx.clone()); + self.bottom_pane.show_selection_view(params); + self.request_redraw(); + } + SlashCommand::New => { + self.app_event_tx.send(AppEvent::NewSession); + } + SlashCommand::Clear => { + self.app_event_tx.send(AppEvent::ClearUi); + } + SlashCommand::Resume => { + self.app_event_tx.send(AppEvent::OpenResumePicker); + } + SlashCommand::Fork => { + self.app_event_tx.send(AppEvent::ForkCurrentSession); + } + SlashCommand::Init => { + let init_target = self.config.cwd.join(DEFAULT_PROJECT_DOC_FILENAME); + if init_target.exists() { + let message = format!( + "{DEFAULT_PROJECT_DOC_FILENAME} already exists here. Skipping /init to avoid overwriting it." + ); + self.add_info_message(message, /*hint*/ None); + return; + } + const INIT_PROMPT: &str = include_str!("../../prompt_for_init_command.md"); + self.submit_user_message(INIT_PROMPT.to_string().into()); + } + SlashCommand::Compact => { + self.clear_token_usage(); + if !self.bottom_pane.is_task_running() { + self.bottom_pane.set_task_running(/*running*/ true); + } + self.app_event_tx.compact(); + } + SlashCommand::Review => { + self.open_review_popup(); + } + SlashCommand::Rename => { + self.session_telemetry + .counter("codex.thread.rename", /*inc*/ 1, &[]); + self.show_rename_prompt(); + } + SlashCommand::Model => { + self.open_model_popup(); + } + SlashCommand::Fast => { + let next_tier = if matches!(self.config.service_tier, Some(ServiceTier::Fast)) { + None + } else { + Some(ServiceTier::Fast) + }; + self.set_service_tier_selection(next_tier); + } + SlashCommand::Realtime => { + if !self.realtime_conversation_enabled() { + return; + } + if self.realtime_conversation.is_live() { + self.stop_realtime_conversation_from_ui(); + } else { + self.start_realtime_conversation(); + } + } + SlashCommand::Settings => { + if !self.realtime_audio_device_selection_enabled() { + return; + } + self.open_realtime_audio_popup(); + } + SlashCommand::Personality => { + self.open_personality_popup(); + } + SlashCommand::Plan => { + self.apply_plan_slash_command(); + } + SlashCommand::Collab => { + if !self.collaboration_modes_enabled() { + self.add_info_message( + "Collaboration modes are disabled.".to_string(), + Some("Enable collaboration modes to use /collab.".to_string()), + ); + return; + } + self.open_collaboration_modes_popup(); + } + SlashCommand::Agent | SlashCommand::MultiAgents => { + self.app_event_tx.send(AppEvent::OpenAgentPicker); + } + SlashCommand::Approvals => { + self.open_permissions_popup(); + } + SlashCommand::Permissions => { + self.open_permissions_popup(); + } + SlashCommand::ElevateSandbox => { + #[cfg(target_os = "windows")] + { + let windows_sandbox_level = WindowsSandboxLevel::from_config(&self.config); + let windows_degraded_sandbox_enabled = + matches!(windows_sandbox_level, WindowsSandboxLevel::RestrictedToken); + if !windows_degraded_sandbox_enabled + || !crate::legacy_core::windows_sandbox::ELEVATED_SANDBOX_NUX_ENABLED + { + // This command should not be visible/recognized outside degraded mode, + // but guard anyway in case something dispatches it directly. + return; + } + + let Some(preset) = builtin_approval_presets() + .into_iter() + .find(|preset| preset.id == "auto") + else { + // Avoid panicking in interactive UI; treat this as a recoverable + // internal error. + self.add_error_message( + "Internal error: missing the 'auto' approval preset.".to_string(), + ); + return; + }; + + if let Err(err) = self + .config + .permissions + .approval_policy + .can_set(&preset.approval) + { + self.add_error_message(err.to_string()); + return; + } + + self.session_telemetry.counter( + "codex.windows_sandbox.setup_elevated_sandbox_command", + /*inc*/ 1, + &[], + ); + self.app_event_tx + .send(AppEvent::BeginWindowsSandboxElevatedSetup { preset }); + } + #[cfg(not(target_os = "windows"))] + { + let _ = &self.session_telemetry; + // Not supported; on non-Windows this command should never be reachable. + } + } + SlashCommand::SandboxReadRoot => { + self.add_error_message( + "Usage: /sandbox-add-read-dir ".to_string(), + ); + } + SlashCommand::Experimental => { + self.open_experimental_popup(); + } + SlashCommand::Quit | SlashCommand::Exit => { + self.request_quit_without_confirmation(); + } + SlashCommand::Logout => { + if let Err(e) = codex_login::logout( + &self.config.codex_home, + self.config.cli_auth_credentials_store_mode, + ) { + tracing::error!("failed to logout: {e}"); + } + self.request_quit_without_confirmation(); + } + // SlashCommand::Undo => { + // self.app_event_tx.send(AppEvent::CodexOp(Op::Undo)); + // } + SlashCommand::Copy => { + self.copy_last_agent_markdown(); + } + SlashCommand::Diff => { + self.add_diff_in_progress(); + let tx = self.app_event_tx.clone(); + tokio::spawn(async move { + let text = match get_git_diff().await { + Ok((is_git_repo, diff_text)) => { + if is_git_repo { + diff_text + } else { + "`/diff` — _not inside a git repository_".to_string() + } + } + Err(e) => format!("Failed to compute diff: {e}"), + }; + tx.send(AppEvent::DiffResult(text)); + }); + } + SlashCommand::Mention => { + self.insert_str("@"); + } + SlashCommand::Skills => { + self.open_skills_menu(); + } + SlashCommand::Status => { + if self.should_prefetch_rate_limits() { + let request_id = self.next_status_refresh_request_id; + self.next_status_refresh_request_id = + self.next_status_refresh_request_id.wrapping_add(1); + self.add_status_output(/*refreshing_rate_limits*/ true, Some(request_id)); + self.app_event_tx.send(AppEvent::RefreshRateLimits { + origin: RateLimitRefreshOrigin::StatusCommand { request_id }, + }); + } else { + self.add_status_output( + /*refreshing_rate_limits*/ false, /*request_id*/ None, + ); + } + } + SlashCommand::DebugConfig => { + self.add_debug_config_output(); + } + SlashCommand::Title => { + self.open_terminal_title_setup(); + } + SlashCommand::Statusline => { + self.open_status_line_setup(); + } + SlashCommand::Theme => { + self.open_theme_picker(); + } + SlashCommand::Ps => { + self.add_ps_output(); + } + SlashCommand::Stop => { + self.clean_background_terminals(); + } + SlashCommand::MemoryDrop => { + self.add_app_server_stub_message("Memory maintenance"); + } + SlashCommand::MemoryUpdate => { + self.add_app_server_stub_message("Memory maintenance"); + } + SlashCommand::Mcp => { + self.add_mcp_output(); + } + SlashCommand::Apps => { + self.add_connectors_output(); + } + SlashCommand::Plugins => { + self.add_plugins_output(); + } + SlashCommand::Rollout => { + if let Some(path) = self.rollout_path() { + self.add_info_message( + format!("Current rollout path: {}", path.display()), + /*hint*/ None, + ); + } else { + self.add_info_message( + "Rollout path is not available yet.".to_string(), + /*hint*/ None, + ); + } + } + SlashCommand::TestApproval => { + use std::collections::HashMap; + + use codex_protocol::protocol::ApplyPatchApprovalRequestEvent; + use codex_protocol::protocol::FileChange; + + self.on_apply_patch_approval_request( + "1".to_string(), + ApplyPatchApprovalRequestEvent { + call_id: "1".to_string(), + turn_id: "turn-1".to_string(), + changes: HashMap::from([ + ( + PathBuf::from("/tmp/test.txt"), + FileChange::Add { + content: "test".to_string(), + }, + ), + ( + PathBuf::from("/tmp/test2.txt"), + FileChange::Update { + unified_diff: "+test\n-test2".to_string(), + move_path: None, + }, + ), + ]), + reason: None, + grant_root: Some(PathBuf::from("/tmp")), + }, + ); + } + } + } + + /// Run an inline slash command. + /// + /// Branches that prepare arguments should pass `record_history: false` to the composer because + /// the staged slash-command entry is the recall record; using the normal submission-history + /// path as well would make a single command appear twice during Up-arrow navigation. + pub(super) fn dispatch_command_with_args( + &mut self, + cmd: SlashCommand, + args: String, + _text_elements: Vec, + ) { + if !cmd.supports_inline_args() { + self.dispatch_command(cmd); + return; + } + if !cmd.available_during_task() && self.bottom_pane.is_task_running() { + let message = format!( + "'/{}' is disabled while a task is in progress.", + cmd.command() + ); + self.add_to_history(history_cell::new_error_event(message)); + self.request_redraw(); + return; + } + + let trimmed = args.trim(); + match cmd { + SlashCommand::Fast => { + if trimmed.is_empty() { + self.dispatch_command(cmd); + return; + } + let prepared_args = if self.bottom_pane.composer_text().is_empty() { + args + } else { + let Some((prepared_args, _prepared_elements)) = self + .bottom_pane + .prepare_inline_args_submission(/*record_history*/ false) + else { + return; + }; + prepared_args + }; + match prepared_args.trim().to_ascii_lowercase().as_str() { + "on" => self.set_service_tier_selection(Some(ServiceTier::Fast)), + "off" => self.set_service_tier_selection(/*service_tier*/ None), + "status" => { + let status = if matches!(self.config.service_tier, Some(ServiceTier::Fast)) + { + "on" + } else { + "off" + }; + self.add_info_message( + format!("Fast mode is {status}."), + /*hint*/ None, + ); + } + _ => { + self.add_error_message("Usage: /fast [on|off|status]".to_string()); + } + } + } + SlashCommand::Rename if !trimmed.is_empty() => { + self.session_telemetry + .counter("codex.thread.rename", /*inc*/ 1, &[]); + let Some((prepared_args, _prepared_elements)) = self + .bottom_pane + .prepare_inline_args_submission(/*record_history*/ false) + else { + return; + }; + let Some(name) = crate::legacy_core::util::normalize_thread_name(&prepared_args) + else { + self.add_error_message("Thread name cannot be empty.".to_string()); + return; + }; + self.app_event_tx.set_thread_name(name); + self.bottom_pane.drain_pending_submission_state(); + } + SlashCommand::Plan if !trimmed.is_empty() => { + if !self.apply_plan_slash_command() { + return; + } + let Some((prepared_args, prepared_elements)) = self + .bottom_pane + .prepare_inline_args_submission(/*record_history*/ false) + else { + return; + }; + let local_images = self + .bottom_pane + .take_recent_submission_images_with_placeholders(); + let remote_image_urls = self.take_remote_image_urls(); + let user_message = UserMessage { + text: prepared_args, + local_images, + remote_image_urls, + text_elements: prepared_elements, + mention_bindings: self.bottom_pane.take_recent_submission_mention_bindings(), + }; + if self.is_session_configured() { + self.reasoning_buffer.clear(); + self.full_reasoning_buffer.clear(); + self.set_status_header(String::from("Working")); + self.submit_user_message(user_message); + } else { + self.queue_user_message(user_message); + } + } + SlashCommand::Review if !trimmed.is_empty() => { + let Some((prepared_args, _prepared_elements)) = self + .bottom_pane + .prepare_inline_args_submission(/*record_history*/ false) + else { + return; + }; + self.submit_op(AppCommand::review(ReviewRequest { + target: ReviewTarget::Custom { + instructions: prepared_args, + }, + user_facing_hint: None, + })); + self.bottom_pane.drain_pending_submission_state(); + } + SlashCommand::Resume if !trimmed.is_empty() => { + let Some((prepared_args, _prepared_elements)) = self + .bottom_pane + .prepare_inline_args_submission(/*record_history*/ false) + else { + return; + }; + self.app_event_tx + .send(AppEvent::ResumeSessionByIdOrName(prepared_args)); + self.bottom_pane.drain_pending_submission_state(); + } + SlashCommand::SandboxReadRoot if !trimmed.is_empty() => { + let Some((prepared_args, _prepared_elements)) = self + .bottom_pane + .prepare_inline_args_submission(/*record_history*/ false) + else { + return; + }; + self.app_event_tx + .send(AppEvent::BeginWindowsSandboxGrantReadRoot { + path: prepared_args, + }); + self.bottom_pane.drain_pending_submission_state(); + } + _ => self.dispatch_command(cmd), + } + } +} diff --git a/codex-rs/tui/src/chatwidget/tests/slash_commands.rs b/codex-rs/tui/src/chatwidget/tests/slash_commands.rs index e75129255..2afb95934 100644 --- a/codex-rs/tui/src/chatwidget/tests/slash_commands.rs +++ b/codex-rs/tui/src/chatwidget/tests/slash_commands.rs @@ -9,6 +9,20 @@ fn turn_complete_event(turn_id: &str, last_agent_message: Option<&str>) -> TurnC .expect("turn complete event should deserialize") } +fn submit_composer_text(chat: &mut ChatWidget, text: &str) { + chat.bottom_pane + .set_composer_text(text.to_string(), Vec::new(), Vec::new()); + chat.handle_key_event(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)); + chat.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)); +} + +fn recall_latest_after_clearing(chat: &mut ChatWidget) -> String { + chat.bottom_pane + .set_composer_text(String::new(), Vec::new(), Vec::new()); + chat.handle_key_event(KeyEvent::new(KeyCode::Up, KeyModifiers::NONE)); + chat.bottom_pane.composer_text() +} + #[tokio::test] async fn slash_compact_eagerly_queues_follow_up_before_turn_start() { let (mut chat, mut rx, mut op_rx) = make_chatwidget_manual(/*model_override*/ None).await; @@ -63,7 +77,7 @@ async fn slash_init_skips_when_project_doc_exists() { std::fs::write(&existing_path, "existing instructions").unwrap(); chat.config.cwd = tempdir.path().to_path_buf().abs(); - chat.dispatch_command(SlashCommand::Init); + submit_composer_text(&mut chat, "/init"); match op_rx.try_recv() { Err(TryRecvError::Empty) => {} @@ -85,6 +99,110 @@ async fn slash_init_skips_when_project_doc_exists() { std::fs::read_to_string(existing_path).unwrap(), "existing instructions" ); + assert_eq!(recall_latest_after_clearing(&mut chat), "/init"); +} + +#[tokio::test] +async fn bare_slash_command_is_available_from_local_recall_after_dispatch() { + let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await; + + submit_composer_text(&mut chat, "/diff"); + + let _ = drain_insert_history(&mut rx); + chat.handle_key_event(KeyEvent::new(KeyCode::Up, KeyModifiers::NONE)); + assert_eq!(chat.bottom_pane.composer_text(), "/diff"); +} + +#[tokio::test] +async fn inline_slash_command_is_available_from_local_recall_after_dispatch() { + let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await; + + submit_composer_text(&mut chat, "/rename Better title"); + + let _ = drain_insert_history(&mut rx); + chat.handle_key_event(KeyEvent::new(KeyCode::Up, KeyModifiers::NONE)); + assert_eq!(chat.bottom_pane.composer_text(), "/rename Better title"); +} + +#[tokio::test] +async fn usage_error_slash_command_is_available_from_local_recall() { + let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(Some("gpt-5.3-codex")).await; + chat.set_feature_enabled(Feature::FastMode, /*enabled*/ true); + + submit_composer_text(&mut chat, "/fast maybe"); + + assert_eq!(chat.bottom_pane.composer_text(), ""); + + let cells = drain_insert_history(&mut rx); + let rendered = cells + .iter() + .map(|cell| lines_to_single_string(cell)) + .collect::>() + .join("\n"); + assert!( + rendered.contains("Usage: /fast [on|off|status]"), + "expected usage message, got: {rendered:?}" + ); + assert_eq!(recall_latest_after_clearing(&mut chat), "/fast maybe"); +} + +#[tokio::test] +async fn unrecognized_slash_command_is_not_added_to_local_recall() { + let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await; + + submit_composer_text(&mut chat, "/does-not-exist"); + + let cells = drain_insert_history(&mut rx); + let rendered = cells + .iter() + .map(|cell| lines_to_single_string(cell)) + .collect::>() + .join("\n"); + assert!( + rendered.contains("Unrecognized command '/does-not-exist'"), + "expected unrecognized-command message, got: {rendered:?}" + ); + assert_eq!(chat.bottom_pane.composer_text(), "/does-not-exist"); + assert_eq!(recall_latest_after_clearing(&mut chat), ""); +} + +#[tokio::test] +async fn unavailable_slash_command_is_available_from_local_recall() { + let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await; + chat.bottom_pane.set_task_running(/*running*/ true); + + submit_composer_text(&mut chat, "/model"); + + let cells = drain_insert_history(&mut rx); + let rendered = cells + .iter() + .map(|cell| lines_to_single_string(cell)) + .collect::>() + .join("\n"); + assert!( + rendered.contains("'/model' is disabled while a task is in progress."), + "expected disabled-command message, got: {rendered:?}" + ); + assert_eq!(recall_latest_after_clearing(&mut chat), "/model"); +} + +#[tokio::test] +async fn no_op_stub_slash_command_is_available_from_local_recall() { + let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await; + + submit_composer_text(&mut chat, "/debug-m-drop"); + + let cells = drain_insert_history(&mut rx); + let rendered = cells + .iter() + .map(|cell| lines_to_single_string(cell)) + .collect::>() + .join("\n"); + assert!( + rendered.contains("Memory maintenance"), + "expected stub message, got: {rendered:?}" + ); + assert_eq!(recall_latest_after_clearing(&mut chat), "/debug-m-drop"); } #[tokio::test]