Skip to content

Commit ea30d6f

Browse files
authored
refactor(chat): Add command suggestions for common single-word inputs (#953)
Improve user experience by detecting when users type common commands like exit, quit, or help without the leading slash prefix. Instead of making unnecessary LLM calls, provide helpful suggestions to use the proper command format (e.g., '/quit', '/help'). 🤖 Assisted by [Amazon Q Developer](https://aws.amazon.com/q/developer)
1 parent 0e85517 commit ea30d6f

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

crates/q_cli/src/cli/chat/command.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,32 @@ Adding relevant files to your context helps Amazon Q provide more accurate and h
126126
}
127127

128128
impl Command {
129+
// Check if input is a common single-word command that should use slash prefix
130+
fn check_common_command(input: &str) -> Option<String> {
131+
let input_lower = input.trim().to_lowercase();
132+
match input_lower.as_str() {
133+
"exit" | "quit" | "q" | "exit()" => {
134+
Some("Did you mean to use the command '/quit' to exit? Type '/quit' to exit.".to_string())
135+
},
136+
"clear" | "cls" => Some(
137+
"Did you mean to use the command '/clear' to clear the conversation? Type '/clear' to clear."
138+
.to_string(),
139+
),
140+
"help" | "?" => Some(
141+
"Did you mean to use the command '/help' for help? Type '/help' to see available commands.".to_string(),
142+
),
143+
_ => None,
144+
}
145+
}
146+
129147
pub fn parse(input: &str) -> Result<Self, String> {
130148
let input = input.trim();
131149

150+
// Check for common single-word commands without slash prefix
151+
if let Some(suggestion) = Self::check_common_command(input) {
152+
return Err(suggestion);
153+
}
154+
132155
if let Some(command) = input.strip_prefix("/") {
133156
let parts: Vec<&str> = command.split_whitespace().collect();
134157

@@ -426,4 +449,44 @@ mod tests {
426449
assert_eq!(&Command::parse(input).unwrap(), parsed, "{}", input);
427450
}
428451
}
452+
453+
#[test]
454+
fn test_common_command_suggestions() {
455+
let test_cases = vec![
456+
(
457+
"exit",
458+
"Did you mean to use the command '/quit' to exit? Type '/quit' to exit.",
459+
),
460+
(
461+
"quit",
462+
"Did you mean to use the command '/quit' to exit? Type '/quit' to exit.",
463+
),
464+
(
465+
"q",
466+
"Did you mean to use the command '/quit' to exit? Type '/quit' to exit.",
467+
),
468+
(
469+
"clear",
470+
"Did you mean to use the command '/clear' to clear the conversation? Type '/clear' to clear.",
471+
),
472+
(
473+
"cls",
474+
"Did you mean to use the command '/clear' to clear the conversation? Type '/clear' to clear.",
475+
),
476+
(
477+
"help",
478+
"Did you mean to use the command '/help' for help? Type '/help' to see available commands.",
479+
),
480+
(
481+
"?",
482+
"Did you mean to use the command '/help' for help? Type '/help' to see available commands.",
483+
),
484+
];
485+
486+
for (input, expected_message) in test_cases {
487+
let result = Command::parse(input);
488+
assert!(result.is_err(), "Expected error for input: {}", input);
489+
assert_eq!(result.unwrap_err(), expected_message);
490+
}
491+
}
429492
}

0 commit comments

Comments
 (0)