diff --git a/Cargo.lock b/Cargo.lock index 9b8d4460..8b2e97b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4337,6 +4337,7 @@ dependencies = [ "sha2", "shellexpand", "sqlx", + "strum", "symphonia", "sys-info", "tempfile", @@ -4455,6 +4456,7 @@ dependencies = [ "serde_json", "serde_yaml", "shlex", + "strum", "tar", "tempfile", "test-case", diff --git a/Cargo.toml b/Cargo.toml index 159519f1..b4424fbc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,6 +44,7 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" serde_yaml = "0.9" shellexpand = "3.1" +strum = { version = "0.27", features = ["derive"] } tempfile = "3" thiserror = "1.0" tokio = { version = "1.49", features = ["full"] } diff --git a/crates/goose-cli/Cargo.toml b/crates/goose-cli/Cargo.toml index 41e5d64c..20811343 100644 --- a/crates/goose-cli/Cargo.toml +++ b/crates/goose-cli/Cargo.toml @@ -36,6 +36,7 @@ tokio = { workspace = true } futures = { workspace = true } serde = { workspace = true } serde_yaml = { workspace = true } +strum = { workspace = true } tempfile = { workspace = true } etcetera = { workspace = true } rand = { workspace = true } diff --git a/crates/goose-cli/src/session/completion.rs b/crates/goose-cli/src/session/completion.rs index 1cf83792..e2edbfb1 100644 --- a/crates/goose-cli/src/session/completion.rs +++ b/crates/goose-cli/src/session/completion.rs @@ -1,3 +1,4 @@ +use goose::config::GooseMode; use rustyline::completion::{Completer, FilenameCompleter, Pair}; use rustyline::highlight::{CmdKind, Highlighter}; use rustyline::hint::Hinter; @@ -5,6 +6,7 @@ use rustyline::validate::Validator; use rustyline::{Context, Helper, Result}; use std::borrow::Cow; use std::sync::Arc; +use strum::VariantNames; use super::{CompletionCache, HintStatus}; @@ -81,7 +83,7 @@ impl GooseCompleter { /// Complete flags for the /mode command fn complete_mode_flags(&self, line: &str) -> Result<(usize, Vec)> { - let modes = ["auto", "approve", "smart_approve", "chat"]; + let modes = GooseMode::VARIANTS; let parts: Vec<&str> = line.split_whitespace().collect(); diff --git a/crates/goose-cli/src/session/input.rs b/crates/goose-cli/src/session/input.rs index 11765998..956fb678 100644 --- a/crates/goose-cli/src/session/input.rs +++ b/crates/goose-cli/src/session/input.rs @@ -1,11 +1,12 @@ use super::completion::GooseCompleter; use super::{CompletionCache, HintStatus}; use anyhow::Result; -use goose::config::Config; +use goose::config::{Config, GooseMode}; use rustyline::Editor; use shlex; use std::collections::HashMap; use std::sync::Arc; +use strum::VariantNames; #[derive(Debug)] pub enum InputResult { @@ -409,6 +410,7 @@ fn get_input_prompt_string() -> String { fn print_help() { let newline_key = get_newline_key().to_ascii_uppercase(); + let modes = GooseMode::VARIANTS.join(", "); println!( "Available commands: /exit or /quit - Exit the session @@ -419,7 +421,7 @@ fn print_help() { /builtin - Add builtin extensions by name (comma-separated) /prompts [--extension ] - List all available prompts, optionally filtered by extension /prompt [--info] [key=value...] - Get prompt info or execute a prompt -/mode - Set the goose mode to use ('auto', 'approve', 'chat', 'smart_approve') +/mode - Set the goose mode to use ({modes}) /plan - Enters 'plan' mode with optional message. Create a plan based on the current messages and asks user if they want to act on it. If user acts on the plan, goose mode is set to 'auto' and returns to 'normal' goose mode. To warm up goose before using '/plan', we recommend setting '/mode approve' & putting appropriate context into goose. diff --git a/crates/goose-cli/src/session/mod.rs b/crates/goose-cli/src/session/mod.rs index 67081b63..62d9736d 100644 --- a/crates/goose-cli/src/session/mod.rs +++ b/crates/goose-cli/src/session/mod.rs @@ -39,6 +39,7 @@ use input::InputResult; use rmcp::model::PromptMessage; use rmcp::model::ServerNotification; use rmcp::model::{ErrorCode, ErrorData}; +use strum::VariantNames; use goose::config::paths::Paths; use goose::conversation::message::{ActionRequiredData, Message, MessageContent}; @@ -717,14 +718,14 @@ impl CliSession { Ok(mode) => mode, Err(_) => { output::render_error(&format!( - "Invalid mode '{}'. Mode must be one of: auto, approve, chat, smart_approve", - mode + "Invalid mode '{mode}'. Mode must be one of: {}", + GooseMode::VARIANTS.join(", ") )); return Ok(()); } }; config.set_goose_mode(mode)?; - output::goose_mode_message(&format!("Goose mode set to '{:?}'", mode)); + output::goose_mode_message(&format!("Goose mode set to '{mode}'")); Ok(()) } diff --git a/crates/goose/Cargo.toml b/crates/goose/Cargo.toml index 66588b76..d4c6c287 100644 --- a/crates/goose/Cargo.toml +++ b/crates/goose/Cargo.toml @@ -66,6 +66,7 @@ keyring = { version = "3.6.2", features = [ "vendored", ] } serde_yaml = { workspace = true } +strum = { workspace = true } once_cell = { workspace = true } etcetera = { workspace = true } rand = { workspace = true } diff --git a/crates/goose/src/config/goose_mode.rs b/crates/goose/src/config/goose_mode.rs index e778aa8d..53ca6e18 100644 --- a/crates/goose/src/config/goose_mode.rs +++ b/crates/goose/src/config/goose_mode.rs @@ -1,26 +1,24 @@ -use std::str::FromStr; - use serde::{Deserialize, Serialize}; +use strum::{Display, EnumString, IntoStaticStr, VariantNames}; -#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] +#[derive( + Copy, + Clone, + Debug, + Eq, + PartialEq, + Serialize, + Deserialize, + Display, + EnumString, + IntoStaticStr, + VariantNames, +)] #[serde(rename_all = "snake_case")] +#[strum(serialize_all = "snake_case")] pub enum GooseMode { Auto, Approve, SmartApprove, Chat, } - -impl FromStr for GooseMode { - type Err = String; - - fn from_str(s: &str) -> Result { - match s { - "auto" => Ok(GooseMode::Auto), - "approve" => Ok(GooseMode::Approve), - "smart_approve" => Ok(GooseMode::SmartApprove), - "chat" => Ok(GooseMode::Chat), - _ => Err(format!("invalid mode: {}", s)), - } - } -} diff --git a/crates/goose/tests/providers.rs b/crates/goose/tests/providers.rs index 437378ba..79cc6a7e 100644 --- a/crates/goose/tests/providers.rs +++ b/crates/goose/tests/providers.rs @@ -424,14 +424,8 @@ impl ProviderTester { message: &str, label: &str, ) -> Result<()> { - let mode_str = match mode { - GooseMode::Approve => "approve", - GooseMode::SmartApprove => "smart_approve", - GooseMode::Auto => "auto", - GooseMode::Chat => "chat", - }; // Guard must live through agent.reply() — providers read GOOSE_MODE at spawn time. - let _guard = env_lock::lock_env([("GOOSE_MODE", Some(mode_str))]); + let _guard = env_lock::lock_env([("GOOSE_MODE", Some(<&str>::from(mode)))]); let provider = if self.is_cli_provider { create_with_named_model( &self.name.to_lowercase(),