refactor: derive GooseMode string conversions with strum (#7706)

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Adrian Cole
2026-03-09 10:01:54 +08:00
committed by GitHub
parent 55d2f4dac0
commit 90d34853de
9 changed files with 32 additions and 30 deletions
Generated
+2
View File
@@ -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",
+1
View File
@@ -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"] }
+1
View File
@@ -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 }
+3 -1
View File
@@ -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<Pair>)> {
let modes = ["auto", "approve", "smart_approve", "chat"];
let modes = GooseMode::VARIANTS;
let parts: Vec<&str> = line.split_whitespace().collect();
+4 -2
View File
@@ -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 <names> - Add builtin extensions by name (comma-separated)
/prompts [--extension <name>] - List all available prompts, optionally filtered by extension
/prompt <n> [--info] [key=value...] - Get prompt info or execute a prompt
/mode <name> - Set the goose mode to use ('auto', 'approve', 'chat', 'smart_approve')
/mode <name> - Set the goose mode to use ({modes})
/plan <message_text> - 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.
+4 -3
View File
@@ -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(())
}
+1
View File
@@ -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 }
+15 -17
View File
@@ -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<Self, Self::Err> {
match s {
"auto" => Ok(GooseMode::Auto),
"approve" => Ok(GooseMode::Approve),
"smart_approve" => Ok(GooseMode::SmartApprove),
"chat" => Ok(GooseMode::Chat),
_ => Err(format!("invalid mode: {}", s)),
}
}
}
+1 -7
View File
@@ -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(),