use crate::config::Config;
use crate::core::llm::get_available_provider_names;
use anyhow::Result;
use clap::Args;
use std::fmt::Write;
use std::str::FromStr;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum DetailLevel {
Minimal,
Standard,
Detailed,
}
impl FromStr for DetailLevel {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"minimal" => Ok(Self::Minimal),
"standard" => Ok(Self::Standard),
"detailed" => Ok(Self::Detailed),
_ => Err(anyhow::anyhow!("Invalid detail level: {s}")),
}
}
}
impl DetailLevel {
pub fn as_str(&self) -> &'static str {
match self {
Self::Minimal => "minimal",
Self::Standard => "standard",
Self::Detailed => "detailed",
}
}
}
#[derive(Args, Clone, Debug)]
pub struct CommonParams {
#[cfg_attr(
debug_assertions,
arg(
long = "debug-llm",
help = "Dump exact raw LLM prompt sent and raw JSON response received to ./target/debug/gait-llm-debug.jsonl (debug builds only)"
)
)]
#[cfg(debug_assertions)]
pub debug_llm: bool,
#[arg(long, help = "Override default LLM provider", value_parser = available_providers_parser)]
pub provider: Option<String>,
#[arg(short, long, help = "Custom instructions for this operation")]
pub instructions: Option<String>,
#[arg(
long,
help = "Set the detail level (minimal, standard, detailed)",
default_value = "standard"
)]
pub detail_level: String,
#[arg(
short = 'r',
long = "repo",
help = "Repository URL to use instead of local repository"
)]
pub repository_url: Option<String>,
}
impl Default for CommonParams {
fn default() -> Self {
Self {
#[cfg(debug_assertions)]
debug_llm: false,
provider: None,
instructions: None,
detail_level: "standard".to_string(),
repository_url: None,
}
}
}
impl CommonParams {
pub fn apply_to_config(&self, config: &mut Config) -> Result<bool> {
#[cfg(debug_assertions)]
{
config.debug_llm = self.debug_llm;
}
let mut changes_made = false;
if let Some(provider) = &self.provider {
let provider_name = if provider.to_lowercase() == "claude" {
"anthropic".to_string()
} else if provider.to_lowercase() == "openrouter" {
"openrouter".to_string()
} else if provider.to_lowercase() == "google" {
"google".to_string()
} else {
provider.clone()
};
if config.default_provider != provider_name {
if !config.providers.contains_key(&provider_name) {
use crate::config::ProviderConfig;
config.providers.insert(
provider_name.clone(),
ProviderConfig::default_for(&provider_name),
);
}
config.default_provider.clone_from(&provider_name);
changes_made = true;
}
}
if let Some(instructions) = &self.instructions {
config.set_temp_instructions(Some(instructions.clone()));
}
Ok(changes_made)
}
}
pub fn available_providers_parser(s: &str) -> Result<String, String> {
let mut provider_name = s.to_lowercase();
if provider_name == "claude" {
provider_name = "anthropic".to_string();
}
let available_providers = get_available_provider_names();
if available_providers
.iter()
.any(|p| p.to_lowercase() == provider_name)
{
Ok(provider_name)
} else {
Err(format!(
"Invalid provider '{}'. Available providers: {}",
s,
available_providers.join(", ")
))
}
}
pub fn get_combined_instructions(config: &Config) -> String {
let mut prompt = String::from("\n\n");
if !config.instructions.is_empty() {
write!(
&mut prompt,
"\n\nAdditional instructions for the request:\n{}\n\n",
config.instructions
)
.expect("write to string should not fail");
}
prompt
}