mirror of
https://github.com/openai/codex.git
synced 2026-12-03 00:41:03 +00:00
Show unavailable slash commands when searched in side conversations (#50756)
## Why Side conversations hide unavailable slash commands, leaving users without an explanation when searching for a known command. ## What changed - Show matching unavailable commands as disabled rows with the reason `not available in a side conversation`. For example, `/arch` now shows a disabled `/archive` entry. Keep these commands hidden from the unfiltered `/` menu. - Rank available matches first and skip disabled rows during keyboard selection and completion. - Prevent `Tab` from queuing input when only disabled matches are visible, and insert `/` normally when no selectable command is present. ## Testing Add a rendering snapshot and regression tests for unavailable command discovery, navigation, available command completion and queuing, blocked queuing of unavailable matches, and slash insertion. GitOrigin-RevId: 7160b308ca57a24a5dcb6e2686b5248a8eb4b6a7
This commit is contained in:
committed by
copyberry
parent
550eb50545
commit
cd7d9e128c
@@ -288,7 +288,10 @@ impl ChatComposer {
|
||||
let filter_text = command_popup_filter_text(&first_line, cursor)
|
||||
.unwrap_or_else(|| first_line.clone());
|
||||
popup.on_composer_text_change(filter_text);
|
||||
if let Some(selected_cmd) = popup.selected_item() {
|
||||
let has_visible_items = !popup.filtered_items().is_empty();
|
||||
let selected_item = popup.selected_item();
|
||||
let has_selectable_item = selected_item.is_some();
|
||||
if let Some(selected_cmd) = selected_item {
|
||||
if selected_command_dispatches_immediately_on_tab(&selected_cmd)
|
||||
&& let CommandItem::Builtin(cmd) = &selected_cmd
|
||||
{
|
||||
@@ -320,6 +323,9 @@ impl ChatComposer {
|
||||
return (InputResult::None, true);
|
||||
}
|
||||
}
|
||||
if has_visible_items && !has_selectable_item {
|
||||
return (InputResult::None, true);
|
||||
}
|
||||
if self.is_task_running {
|
||||
return self.handle_submission(/*should_queue*/ true);
|
||||
}
|
||||
@@ -360,8 +366,9 @@ impl ChatComposer {
|
||||
.textarea
|
||||
.set_cursor(self.draft.textarea.text().len());
|
||||
}
|
||||
return (InputResult::None, true);
|
||||
}
|
||||
(InputResult::None, true)
|
||||
self.handle_key_event_without_popup(key_event)
|
||||
}
|
||||
KeyEvent {
|
||||
code: KeyCode::Enter,
|
||||
@@ -639,6 +646,79 @@ mod tests {
|
||||
composer_with_text_at_cursor(&format!("{prefix}{draft}"), prefix.len())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unavailable_side_conversation_command_still_opens_popup() {
|
||||
let mut composer = test_composer();
|
||||
composer.set_side_conversation_active(/*active*/ true);
|
||||
composer.draft.textarea.set_text_clearing_elements("/arch");
|
||||
composer.draft.textarea.set_cursor(/*pos*/ 5);
|
||||
composer.sync_popups();
|
||||
|
||||
assert!(matches!(composer.popups.active, ActivePopup::Command(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn side_conversation_tab_completes_available_match_after_unavailable_rows() {
|
||||
let mut composer = test_composer();
|
||||
composer.set_side_conversation_active(/*active*/ true);
|
||||
composer.draft.textarea.set_text_clearing_elements("/a");
|
||||
composer.draft.textarea.set_cursor(/*pos*/ 2);
|
||||
composer.sync_popups();
|
||||
|
||||
assert_eq!(press(&mut composer, KeyCode::Tab), InputResult::None);
|
||||
assert_eq!(composer.draft.textarea.text(), "/agents ");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn side_conversation_tab_does_not_queue_unavailable_match_while_task_running() {
|
||||
let mut composer = test_composer();
|
||||
composer.set_side_conversation_active(/*active*/ true);
|
||||
composer.set_task_running(/*running*/ true);
|
||||
composer.draft.textarea.set_text_clearing_elements("/arch");
|
||||
composer.draft.textarea.set_cursor(/*pos*/ 5);
|
||||
composer.sync_popups();
|
||||
|
||||
assert_eq!(press(&mut composer, KeyCode::Tab), InputResult::None);
|
||||
assert_eq!(composer.draft.textarea.text(), "/arch");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn side_conversation_tab_queues_exact_available_match_while_task_running() {
|
||||
let mut composer = test_composer();
|
||||
composer.set_side_conversation_active(/*active*/ true);
|
||||
composer.set_task_running(/*running*/ true);
|
||||
composer
|
||||
.draft
|
||||
.textarea
|
||||
.set_text_clearing_elements("/agents");
|
||||
composer.draft.textarea.set_cursor(/*pos*/ 7);
|
||||
composer.sync_popups();
|
||||
|
||||
assert_eq!(
|
||||
press(&mut composer, KeyCode::Tab),
|
||||
InputResult::Queued {
|
||||
text: "/agents".to_string(),
|
||||
text_elements: Vec::new(),
|
||||
action: QueuedInputAction::ParseSlash,
|
||||
pending_pastes: Vec::new(),
|
||||
}
|
||||
);
|
||||
assert!(composer.draft.textarea.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn side_conversation_slash_is_inserted_after_unavailable_match() {
|
||||
let mut composer = test_composer();
|
||||
composer.set_side_conversation_active(/*active*/ true);
|
||||
composer.set_disable_paste_burst(/*disabled*/ true);
|
||||
composer.draft.textarea.set_text_clearing_elements("/new");
|
||||
composer.draft.textarea.set_cursor(/*pos*/ 4);
|
||||
composer.sync_popups();
|
||||
|
||||
assert_eq!(press(&mut composer, KeyCode::Char('/')), InputResult::None);
|
||||
assert_eq!(composer.draft.textarea.text(), "/new/");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn esc_dismisses_slash_popup_while_idle() {
|
||||
let mut composer = composer_with_text_at_cursor("/rev", "/rev".len());
|
||||
|
||||
@@ -35,6 +35,7 @@ pub(crate) enum CommandItem {
|
||||
pub(crate) struct CommandPopup {
|
||||
command_filter: String,
|
||||
commands: Vec<CommandItem>,
|
||||
side_conversation_active: bool,
|
||||
state: ScrollState,
|
||||
daybreak_command_description: Option<&'static str>,
|
||||
}
|
||||
@@ -78,18 +79,25 @@ impl CommandPopup {
|
||||
service_tier_commands: Vec<ServiceTierCommand>,
|
||||
) -> Self {
|
||||
// Keep built-in availability in sync with the composer.
|
||||
let commands = commands_for_input(flags.into(), &service_tier_commands)
|
||||
.into_iter()
|
||||
.filter_map(|command| match command {
|
||||
SlashCommandItem::Builtin(cmd) => (!cmd.command().starts_with("debug")
|
||||
&& cmd != SlashCommand::Apps)
|
||||
.then_some(CommandItem::Builtin(cmd)),
|
||||
SlashCommandItem::ServiceTier(command) => Some(CommandItem::ServiceTier(command)),
|
||||
})
|
||||
.collect();
|
||||
let commands = commands_for_input(
|
||||
BuiltinCommandFlags {
|
||||
side_conversation_active: false,
|
||||
..flags.into()
|
||||
},
|
||||
&service_tier_commands,
|
||||
)
|
||||
.into_iter()
|
||||
.filter_map(|command| match command {
|
||||
SlashCommandItem::Builtin(cmd) => (!cmd.command().starts_with("debug")
|
||||
&& cmd != SlashCommand::Apps)
|
||||
.then_some(CommandItem::Builtin(cmd)),
|
||||
SlashCommandItem::ServiceTier(command) => Some(CommandItem::ServiceTier(command)),
|
||||
})
|
||||
.collect();
|
||||
Self {
|
||||
command_filter: String::new(),
|
||||
commands,
|
||||
side_conversation_active: flags.side_conversation_active,
|
||||
state: ScrollState::new(),
|
||||
daybreak_command_description: flags.daybreak_command_description,
|
||||
}
|
||||
@@ -156,6 +164,9 @@ impl CommandPopup {
|
||||
if matches!(command, CommandItem::Builtin(cmd) if ALIAS_COMMANDS.contains(cmd)) {
|
||||
continue;
|
||||
}
|
||||
if self.is_unavailable(command) {
|
||||
continue;
|
||||
}
|
||||
out.push((command.clone(), None));
|
||||
}
|
||||
return out;
|
||||
@@ -195,10 +206,13 @@ impl CommandPopup {
|
||||
|
||||
out.extend(exact);
|
||||
out.extend(prefix);
|
||||
if self.side_conversation_active {
|
||||
out.sort_by_key(|(item, _)| self.is_unavailable(item));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn filtered_items(&self) -> Vec<CommandItem> {
|
||||
pub(crate) fn filtered_items(&self) -> Vec<CommandItem> {
|
||||
self.filtered().into_iter().map(|(c, _)| c).collect()
|
||||
}
|
||||
|
||||
@@ -218,10 +232,13 @@ impl CommandPopup {
|
||||
item.description()
|
||||
}
|
||||
.to_string();
|
||||
let unavailable = self.is_unavailable(&item);
|
||||
GenericDisplayRow {
|
||||
category_tag: None,
|
||||
name,
|
||||
name_prefix_spans: vec![if self.state.selected_idx == Some(index) {
|
||||
name_prefix_spans: vec![if self.state.selected_idx == Some(index)
|
||||
&& !unavailable
|
||||
{
|
||||
"› ".into()
|
||||
} else {
|
||||
" ".into()
|
||||
@@ -231,8 +248,9 @@ impl CommandPopup {
|
||||
display_shortcut: None,
|
||||
description: Some(description),
|
||||
wrap_indent: None,
|
||||
is_disabled: false,
|
||||
disabled_reason: None,
|
||||
is_disabled: unavailable,
|
||||
disabled_reason: unavailable
|
||||
.then(|| "not available in a side conversation".to_string()),
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
@@ -241,14 +259,24 @@ impl CommandPopup {
|
||||
/// Move the selection cursor one step up.
|
||||
pub(crate) fn move_up(&mut self) {
|
||||
let len = self.filtered_items().len();
|
||||
self.state.move_up_wrap(len);
|
||||
for _ in 0..len {
|
||||
self.state.move_up_wrap(len);
|
||||
if self.selected_item().is_some() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
self.state.ensure_visible(len, MAX_POPUP_ROWS.min(len));
|
||||
}
|
||||
|
||||
/// Move the selection cursor one step down.
|
||||
pub(crate) fn move_down(&mut self) {
|
||||
let matches_len = self.filtered_items().len();
|
||||
self.state.move_down_wrap(matches_len);
|
||||
for _ in 0..matches_len {
|
||||
self.state.move_down_wrap(matches_len);
|
||||
if self.selected_item().is_some() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
self.state
|
||||
.ensure_visible(matches_len, MAX_POPUP_ROWS.min(matches_len));
|
||||
}
|
||||
@@ -259,6 +287,15 @@ impl CommandPopup {
|
||||
self.state
|
||||
.selected_idx
|
||||
.and_then(|idx| matches.get(idx).cloned())
|
||||
.filter(|item| !self.is_unavailable(item))
|
||||
}
|
||||
|
||||
fn is_unavailable(&self, item: &CommandItem) -> bool {
|
||||
self.side_conversation_active
|
||||
&& match item {
|
||||
CommandItem::Builtin(cmd) => !cmd.available_in_side_conversation(),
|
||||
CommandItem::ServiceTier(_) => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -298,6 +335,44 @@ mod tests {
|
||||
use super::*;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn unavailable_side_conversation_command_is_shown_only_when_searched() {
|
||||
let mut popup = CommandPopup::new(
|
||||
CommandPopupFlags {
|
||||
side_conversation_active: true,
|
||||
..CommandPopupFlags::default()
|
||||
},
|
||||
Vec::new(),
|
||||
);
|
||||
popup.on_composer_text_change("/".to_string());
|
||||
assert!(
|
||||
!popup
|
||||
.filtered_items()
|
||||
.contains(&CommandItem::Builtin(SlashCommand::Archive))
|
||||
);
|
||||
|
||||
popup.on_composer_text_change("/arch".to_string());
|
||||
assert_eq!(
|
||||
popup.filtered_items(),
|
||||
vec![CommandItem::Builtin(SlashCommand::Archive)]
|
||||
);
|
||||
assert_eq!(popup.selected_item(), None);
|
||||
|
||||
let width = 76;
|
||||
let area = Rect::new(
|
||||
/*x*/ 0,
|
||||
/*y*/ 0,
|
||||
width,
|
||||
popup.calculate_required_height(width),
|
||||
);
|
||||
let mut buf = Buffer::empty(area);
|
||||
popup.render_ref(area, &mut buf);
|
||||
insta::assert_snapshot!("side_conversation_unavailable_command", format!("{buf:?}"));
|
||||
popup.on_composer_text_change("/a".to_string());
|
||||
popup.move_up();
|
||||
assert!(popup.selected_item().is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filter_includes_init_when_typing_prefix() {
|
||||
let mut popup = CommandPopup::new(CommandPopupFlags::default(), Vec::new());
|
||||
|
||||
@@ -158,9 +158,17 @@ pub(crate) fn has_slash_command_prefix(
|
||||
flags: BuiltinCommandFlags,
|
||||
service_tier_commands: &[ServiceTierCommand],
|
||||
) -> bool {
|
||||
commands_for_input(flags, service_tier_commands)
|
||||
.into_iter()
|
||||
.any(|command| fuzzy_match(command.command(), name).is_some())
|
||||
// A side conversation can describe a known command as unavailable in the
|
||||
// popup, even though dispatch must continue to reject it.
|
||||
commands_for_input(
|
||||
BuiltinCommandFlags {
|
||||
side_conversation_active: false,
|
||||
..flags
|
||||
},
|
||||
service_tier_commands,
|
||||
)
|
||||
.into_iter()
|
||||
.any(|command| fuzzy_match(command.command(), name).is_some())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
---
|
||||
source: tui/src/bottom_pane/command_popup.rs
|
||||
expression: "format!(\"{buf:?}\")"
|
||||
---
|
||||
Buffer {
|
||||
area: Rect { x: 0, y: 0, width: 76, height: 2 },
|
||||
content: [
|
||||
" /archive (disabled) archive this session (disabled: not available in a ",
|
||||
" side conversation) ",
|
||||
],
|
||||
styles: [
|
||||
x: 0, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: DIM,
|
||||
x: 3, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: BOLD | DIM,
|
||||
x: 7, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: DIM,
|
||||
x: 73, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
|
||||
x: 0, y: 1, fg: Reset, bg: Reset, underline: Reset, modifier: DIM,
|
||||
x: 41, y: 1, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user