diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index eb99106f3a..27c0abf397 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -272,6 +272,8 @@ use crate::render::renderable::Renderable; use crate::render::renderable::RenderableExt; use crate::render::renderable::RenderableItem; use crate::slash_command::SlashCommand; +use crate::slash_command_input::FastSlashCommandArgs; +use crate::slash_command_input::ParsedSlashCommand; use crate::status::RateLimitSnapshotDisplay; use crate::status_indicator_widget::STATUS_DETAILS_DEFAULT_MAX_LINES; use crate::status_indicator_widget::StatusDetailsCapitalization; @@ -4748,34 +4750,25 @@ impl ChatWidget { return; } - let trimmed = args.trim(); - if trimmed.is_empty() { - self.dispatch_command(cmd); - return; - } - match cmd { - SlashCommand::Fast => { - 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()); - } - } + match cmd.parse_invocation(&args) { + Ok(ParsedSlashCommand::Bare(_)) => { + self.dispatch_command(cmd); } - SlashCommand::Rename if !trimmed.is_empty() => { + Ok(ParsedSlashCommand::Fast(FastSlashCommandArgs::On)) => { + self.set_service_tier_selection(Some(ServiceTier::Fast)); + } + Ok(ParsedSlashCommand::Fast(FastSlashCommandArgs::Off)) => { + self.set_service_tier_selection(/*service_tier*/ None); + } + Ok(ParsedSlashCommand::Fast(FastSlashCommandArgs::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); + } + Ok(ParsedSlashCommand::Rename) => { self.session_telemetry .counter("codex.thread.rename", /*inc*/ 1, &[]); let Some((prepared_args, _prepared_elements)) = self @@ -4795,7 +4788,7 @@ impl ChatWidget { .send(AppEvent::CodexOp(Op::SetThreadName { name })); self.bottom_pane.drain_pending_submission_state(); } - SlashCommand::Plan if !trimmed.is_empty() => { + Ok(ParsedSlashCommand::Plan) => { self.dispatch_command(cmd); if self.active_mode_kind() != ModeKind::Plan { return; @@ -4826,7 +4819,7 @@ impl ChatWidget { self.queue_user_message(user_message); } } - SlashCommand::Review if !trimmed.is_empty() => { + Ok(ParsedSlashCommand::Review) => { let Some((prepared_args, _prepared_elements)) = self .bottom_pane .prepare_inline_args_submission(/*record_history*/ false) @@ -4843,7 +4836,7 @@ impl ChatWidget { }); self.bottom_pane.drain_pending_submission_state(); } - SlashCommand::SandboxReadRoot if !trimmed.is_empty() => { + Ok(ParsedSlashCommand::SandboxReadRoot) => { let Some((prepared_args, _prepared_elements)) = self .bottom_pane .prepare_inline_args_submission(/*record_history*/ false) @@ -4856,12 +4849,8 @@ impl ChatWidget { }); self.bottom_pane.drain_pending_submission_state(); } - _ => { - let usage = cmd.usage_lines().join(" | "); - self.add_error_message(format!( - "'/{}' does not accept inline arguments. Usage: {usage}", - cmd.command() - )); + Err(err) => { + self.add_error_message(err.message()); } } } diff --git a/codex-rs/tui/src/lib.rs b/codex-rs/tui/src/lib.rs index 8647eb4494..b807f45ded 100644 --- a/codex-rs/tui/src/lib.rs +++ b/codex-rs/tui/src/lib.rs @@ -122,6 +122,7 @@ mod session_log; mod shimmer; mod skills_helpers; mod slash_command; +mod slash_command_input; mod slash_command_invocation; mod status; mod status_indicator_widget; diff --git a/codex-rs/tui/src/slash_command_input.rs b/codex-rs/tui/src/slash_command_input.rs new file mode 100644 index 0000000000..04981d0e57 --- /dev/null +++ b/codex-rs/tui/src/slash_command_input.rs @@ -0,0 +1,200 @@ +use crate::slash_command::SlashCommand; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum FastSlashCommandArgs { + On, + Off, + Status, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum SlashCommandBareBehavior { + DispatchesDirectly, + OpensUi, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum ParsedSlashCommand { + Bare(SlashCommandBareBehavior), + Fast(FastSlashCommandArgs), + Rename, + Plan, + Review, + SandboxReadRoot, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum SlashCommandUsageErrorKind { + UnexpectedInlineArgs, + InvalidInlineArgs, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) struct SlashCommandUsageError { + command: SlashCommand, + kind: SlashCommandUsageErrorKind, +} + +impl SlashCommandUsageError { + pub(crate) fn message(self) -> String { + let usage = self.command.usage_lines().join(" | "); + match self.kind { + SlashCommandUsageErrorKind::UnexpectedInlineArgs => format!( + "'/{}' does not accept inline arguments. Usage: {usage}", + self.command.command() + ), + SlashCommandUsageErrorKind::InvalidInlineArgs => format!("Usage: {usage}"), + } + } +} + +impl SlashCommand { + pub(crate) fn bare_behavior(self) -> SlashCommandBareBehavior { + match self { + SlashCommand::Model + | SlashCommand::Approvals + | SlashCommand::Permissions + | SlashCommand::Experimental + | SlashCommand::Skills + | SlashCommand::Review + | SlashCommand::Rename + | SlashCommand::Resume + | SlashCommand::Collab + | SlashCommand::Agent + | SlashCommand::Statusline + | SlashCommand::Theme + | SlashCommand::Feedback + | SlashCommand::Personality + | SlashCommand::Settings + | SlashCommand::MultiAgents => SlashCommandBareBehavior::OpensUi, + SlashCommand::Fast + | SlashCommand::ElevateSandbox + | SlashCommand::SandboxReadRoot + | SlashCommand::New + | SlashCommand::Fork + | SlashCommand::Init + | SlashCommand::Compact + | SlashCommand::Plan + | SlashCommand::Diff + | SlashCommand::Copy + | SlashCommand::Mention + | SlashCommand::Status + | SlashCommand::DebugConfig + | SlashCommand::Title + | SlashCommand::Mcp + | SlashCommand::Apps + | SlashCommand::Logout + | SlashCommand::Quit + | SlashCommand::Exit + | SlashCommand::Rollout + | SlashCommand::Ps + | SlashCommand::Stop + | SlashCommand::Clear + | SlashCommand::Realtime + | SlashCommand::TestApproval + | SlashCommand::MemoryDrop + | SlashCommand::MemoryUpdate => SlashCommandBareBehavior::DispatchesDirectly, + } + } + + pub(crate) fn parse_invocation( + self, + args: &str, + ) -> Result { + let trimmed = args.trim(); + if trimmed.is_empty() { + return Ok(ParsedSlashCommand::Bare(self.bare_behavior())); + } + + match self { + SlashCommand::Fast => match trimmed.to_ascii_lowercase().as_str() { + "on" => Ok(ParsedSlashCommand::Fast(FastSlashCommandArgs::On)), + "off" => Ok(ParsedSlashCommand::Fast(FastSlashCommandArgs::Off)), + "status" => Ok(ParsedSlashCommand::Fast(FastSlashCommandArgs::Status)), + _ => Err(SlashCommandUsageError { + command: self, + kind: SlashCommandUsageErrorKind::InvalidInlineArgs, + }), + }, + SlashCommand::Rename => Ok(ParsedSlashCommand::Rename), + SlashCommand::Plan => Ok(ParsedSlashCommand::Plan), + SlashCommand::Review => Ok(ParsedSlashCommand::Review), + SlashCommand::SandboxReadRoot => Ok(ParsedSlashCommand::SandboxReadRoot), + SlashCommand::Model + | SlashCommand::Approvals + | SlashCommand::Permissions + | SlashCommand::ElevateSandbox + | SlashCommand::Experimental + | SlashCommand::Skills + | SlashCommand::New + | SlashCommand::Resume + | SlashCommand::Fork + | SlashCommand::Init + | SlashCommand::Compact + | SlashCommand::Collab + | SlashCommand::Agent + | SlashCommand::Diff + | SlashCommand::Copy + | SlashCommand::Mention + | SlashCommand::Status + | SlashCommand::DebugConfig + | SlashCommand::Title + | SlashCommand::Statusline + | SlashCommand::Theme + | SlashCommand::Mcp + | SlashCommand::Apps + | SlashCommand::Logout + | SlashCommand::Quit + | SlashCommand::Exit + | SlashCommand::Feedback + | SlashCommand::Rollout + | SlashCommand::Ps + | SlashCommand::Stop + | SlashCommand::Clear + | SlashCommand::Personality + | SlashCommand::Realtime + | SlashCommand::Settings + | SlashCommand::TestApproval + | SlashCommand::MultiAgents + | SlashCommand::MemoryDrop + | SlashCommand::MemoryUpdate => Err(SlashCommandUsageError { + command: self, + kind: SlashCommandUsageErrorKind::UnexpectedInlineArgs, + }), + } + } +} + +#[cfg(test)] +mod tests { + use pretty_assertions::assert_eq; + + use super::*; + + #[test] + fn review_bare_form_is_marked_as_ui_driven() { + assert_eq!( + SlashCommand::Review.parse_invocation(""), + Ok(ParsedSlashCommand::Bare(SlashCommandBareBehavior::OpensUi)) + ); + } + + #[test] + fn fast_accepts_nonempty_inline_args() { + assert_eq!( + SlashCommand::Fast.parse_invocation("status"), + Ok(ParsedSlashCommand::Fast(FastSlashCommandArgs::Status)) + ); + } + + #[test] + fn clear_rejects_unexpected_inline_args() { + assert_eq!( + SlashCommand::Clear + .parse_invocation("now") + .unwrap_err() + .message(), + "'/clear' does not accept inline arguments. Usage: /clear" + ); + } +} diff --git a/codex-rs/tui_app_server/src/chatwidget.rs b/codex-rs/tui_app_server/src/chatwidget.rs index 1ecd842eef..6e58768b45 100644 --- a/codex-rs/tui_app_server/src/chatwidget.rs +++ b/codex-rs/tui_app_server/src/chatwidget.rs @@ -316,6 +316,8 @@ use crate::render::renderable::Renderable; use crate::render::renderable::RenderableExt; use crate::render::renderable::RenderableItem; use crate::slash_command::SlashCommand; +use crate::slash_command_input::FastSlashCommandArgs; +use crate::slash_command_input::ParsedSlashCommand; use crate::status::RateLimitSnapshotDisplay; use crate::status_indicator_widget::STATUS_DETAILS_DEFAULT_MAX_LINES; use crate::status_indicator_widget::StatusDetailsCapitalization; @@ -4895,34 +4897,25 @@ impl ChatWidget { return; } - let trimmed = args.trim(); - if trimmed.is_empty() { - self.dispatch_command(cmd); - return; - } - match cmd { - SlashCommand::Fast => { - 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()); - } - } + match cmd.parse_invocation(&args) { + Ok(ParsedSlashCommand::Bare(_)) => { + self.dispatch_command(cmd); } - SlashCommand::Rename if !trimmed.is_empty() => { + Ok(ParsedSlashCommand::Fast(FastSlashCommandArgs::On)) => { + self.set_service_tier_selection(Some(ServiceTier::Fast)); + } + Ok(ParsedSlashCommand::Fast(FastSlashCommandArgs::Off)) => { + self.set_service_tier_selection(/*service_tier*/ None); + } + Ok(ParsedSlashCommand::Fast(FastSlashCommandArgs::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); + } + Ok(ParsedSlashCommand::Rename) => { self.session_telemetry .counter("codex.thread.rename", /*inc*/ 1, &[]); let Some((prepared_args, _prepared_elements)) = self @@ -4941,7 +4934,7 @@ impl ChatWidget { self.app_event_tx.set_thread_name(name); self.bottom_pane.drain_pending_submission_state(); } - SlashCommand::Plan if !trimmed.is_empty() => { + Ok(ParsedSlashCommand::Plan) => { self.dispatch_command(cmd); if self.active_mode_kind() != ModeKind::Plan { return; @@ -4972,7 +4965,7 @@ impl ChatWidget { self.queue_user_message(user_message); } } - SlashCommand::Review if !trimmed.is_empty() => { + Ok(ParsedSlashCommand::Review) => { let Some((prepared_args, _prepared_elements)) = self .bottom_pane .prepare_inline_args_submission(/*record_history*/ false) @@ -4987,7 +4980,7 @@ impl ChatWidget { })); self.bottom_pane.drain_pending_submission_state(); } - SlashCommand::SandboxReadRoot if !trimmed.is_empty() => { + Ok(ParsedSlashCommand::SandboxReadRoot) => { let Some((prepared_args, _prepared_elements)) = self .bottom_pane .prepare_inline_args_submission(/*record_history*/ false) @@ -5000,12 +4993,8 @@ impl ChatWidget { }); self.bottom_pane.drain_pending_submission_state(); } - _ => { - let usage = cmd.usage_lines().join(" | "); - self.add_error_message(format!( - "'/{}' does not accept inline arguments. Usage: {usage}", - cmd.command() - )); + Err(err) => { + self.add_error_message(err.message()); } } } diff --git a/codex-rs/tui_app_server/src/lib.rs b/codex-rs/tui_app_server/src/lib.rs index 1ebe7fd770..f892197cab 100644 --- a/codex-rs/tui_app_server/src/lib.rs +++ b/codex-rs/tui_app_server/src/lib.rs @@ -136,6 +136,7 @@ mod session_log; mod shimmer; mod skills_helpers; mod slash_command; +mod slash_command_input; mod slash_command_invocation; mod status; mod status_indicator_widget; diff --git a/codex-rs/tui_app_server/src/slash_command_input.rs b/codex-rs/tui_app_server/src/slash_command_input.rs new file mode 100644 index 0000000000..9b39a59b66 --- /dev/null +++ b/codex-rs/tui_app_server/src/slash_command_input.rs @@ -0,0 +1,198 @@ +use crate::slash_command::SlashCommand; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum FastSlashCommandArgs { + On, + Off, + Status, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum SlashCommandBareBehavior { + DispatchesDirectly, + OpensUi, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum ParsedSlashCommand { + Bare(SlashCommandBareBehavior), + Fast(FastSlashCommandArgs), + Rename, + Plan, + Review, + SandboxReadRoot, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum SlashCommandUsageErrorKind { + UnexpectedInlineArgs, + InvalidInlineArgs, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) struct SlashCommandUsageError { + command: SlashCommand, + kind: SlashCommandUsageErrorKind, +} + +impl SlashCommandUsageError { + pub(crate) fn message(self) -> String { + let usage = self.command.usage_lines().join(" | "); + match self.kind { + SlashCommandUsageErrorKind::UnexpectedInlineArgs => format!( + "'/{}' does not accept inline arguments. Usage: {usage}", + self.command.command() + ), + SlashCommandUsageErrorKind::InvalidInlineArgs => format!("Usage: {usage}"), + } + } +} + +impl SlashCommand { + pub(crate) fn bare_behavior(self) -> SlashCommandBareBehavior { + match self { + SlashCommand::Model + | SlashCommand::Approvals + | SlashCommand::Permissions + | SlashCommand::Experimental + | SlashCommand::Skills + | SlashCommand::Review + | SlashCommand::Rename + | SlashCommand::Resume + | SlashCommand::Collab + | SlashCommand::Agent + | SlashCommand::Statusline + | SlashCommand::Theme + | SlashCommand::Feedback + | SlashCommand::Personality + | SlashCommand::Settings + | SlashCommand::MultiAgents => SlashCommandBareBehavior::OpensUi, + SlashCommand::Fast + | SlashCommand::ElevateSandbox + | SlashCommand::SandboxReadRoot + | SlashCommand::New + | SlashCommand::Fork + | SlashCommand::Init + | SlashCommand::Compact + | SlashCommand::Plan + | SlashCommand::Diff + | SlashCommand::Copy + | SlashCommand::Mention + | SlashCommand::Status + | SlashCommand::DebugConfig + | SlashCommand::Mcp + | SlashCommand::Apps + | SlashCommand::Logout + | SlashCommand::Quit + | SlashCommand::Exit + | SlashCommand::Rollout + | SlashCommand::Ps + | SlashCommand::Stop + | SlashCommand::Clear + | SlashCommand::Realtime + | SlashCommand::TestApproval + | SlashCommand::MemoryDrop + | SlashCommand::MemoryUpdate => SlashCommandBareBehavior::DispatchesDirectly, + } + } + + pub(crate) fn parse_invocation( + self, + args: &str, + ) -> Result { + let trimmed = args.trim(); + if trimmed.is_empty() { + return Ok(ParsedSlashCommand::Bare(self.bare_behavior())); + } + + match self { + SlashCommand::Fast => match trimmed.to_ascii_lowercase().as_str() { + "on" => Ok(ParsedSlashCommand::Fast(FastSlashCommandArgs::On)), + "off" => Ok(ParsedSlashCommand::Fast(FastSlashCommandArgs::Off)), + "status" => Ok(ParsedSlashCommand::Fast(FastSlashCommandArgs::Status)), + _ => Err(SlashCommandUsageError { + command: self, + kind: SlashCommandUsageErrorKind::InvalidInlineArgs, + }), + }, + SlashCommand::Rename => Ok(ParsedSlashCommand::Rename), + SlashCommand::Plan => Ok(ParsedSlashCommand::Plan), + SlashCommand::Review => Ok(ParsedSlashCommand::Review), + SlashCommand::SandboxReadRoot => Ok(ParsedSlashCommand::SandboxReadRoot), + SlashCommand::Model + | SlashCommand::Approvals + | SlashCommand::Permissions + | SlashCommand::ElevateSandbox + | SlashCommand::Experimental + | SlashCommand::Skills + | SlashCommand::New + | SlashCommand::Resume + | SlashCommand::Fork + | SlashCommand::Init + | SlashCommand::Compact + | SlashCommand::Collab + | SlashCommand::Agent + | SlashCommand::Diff + | SlashCommand::Copy + | SlashCommand::Mention + | SlashCommand::Status + | SlashCommand::DebugConfig + | SlashCommand::Statusline + | SlashCommand::Theme + | SlashCommand::Mcp + | SlashCommand::Apps + | SlashCommand::Logout + | SlashCommand::Quit + | SlashCommand::Exit + | SlashCommand::Feedback + | SlashCommand::Rollout + | SlashCommand::Ps + | SlashCommand::Stop + | SlashCommand::Clear + | SlashCommand::Personality + | SlashCommand::Realtime + | SlashCommand::Settings + | SlashCommand::TestApproval + | SlashCommand::MultiAgents + | SlashCommand::MemoryDrop + | SlashCommand::MemoryUpdate => Err(SlashCommandUsageError { + command: self, + kind: SlashCommandUsageErrorKind::UnexpectedInlineArgs, + }), + } + } +} + +#[cfg(test)] +mod tests { + use pretty_assertions::assert_eq; + + use super::*; + + #[test] + fn review_bare_form_is_marked_as_ui_driven() { + assert_eq!( + SlashCommand::Review.parse_invocation(""), + Ok(ParsedSlashCommand::Bare(SlashCommandBareBehavior::OpensUi)) + ); + } + + #[test] + fn fast_accepts_nonempty_inline_args() { + assert_eq!( + SlashCommand::Fast.parse_invocation("status"), + Ok(ParsedSlashCommand::Fast(FastSlashCommandArgs::Status)) + ); + } + + #[test] + fn clear_rejects_unexpected_inline_args() { + assert_eq!( + SlashCommand::Clear + .parse_invocation("now") + .unwrap_err() + .message(), + "'/clear' does not accept inline arguments. Usage: /clear" + ); + } +}