gitai/
common.rs

1use crate::config::Config;
2use crate::core::llm::get_available_provider_names;
3use anyhow::Result;
4use clap::Args;
5use std::fmt::Write;
6use std::str::FromStr;
7
8#[derive(Clone, Copy, PartialEq, Eq, Debug)]
9pub enum DetailLevel {
10    Minimal,
11    Standard,
12    Detailed,
13}
14
15impl FromStr for DetailLevel {
16    type Err = anyhow::Error;
17
18    fn from_str(s: &str) -> Result<Self, Self::Err> {
19        match s.to_lowercase().as_str() {
20            "minimal" => Ok(Self::Minimal),
21            "standard" => Ok(Self::Standard),
22            "detailed" => Ok(Self::Detailed),
23            _ => Err(anyhow::anyhow!("Invalid detail level: {s}")),
24        }
25    }
26}
27
28impl DetailLevel {
29    pub fn as_str(&self) -> &'static str {
30        match self {
31            Self::Minimal => "minimal",
32            Self::Standard => "standard",
33            Self::Detailed => "detailed",
34        }
35    }
36}
37
38#[derive(Args, Clone, Default, Debug)]
39pub struct CommonParams {
40    /// Override default LLM provider
41    #[arg(long, help = "Override default LLM provider", value_parser = available_providers_parser)]
42    pub provider: Option<String>,
43
44    /// Custom instructions for this operation
45    #[arg(short, long, help = "Custom instructions for this operation")]
46    pub instructions: Option<String>,
47
48    /// Set the detail level
49    #[arg(
50        long,
51        help = "Set the detail level (minimal, standard, detailed)",
52        default_value = "standard"
53    )]
54    pub detail_level: String,
55
56    /// Repository URL to use instead of local repository
57    #[arg(
58        short = 'r',
59        long = "repo",
60        help = "Repository URL to use instead of local repository"
61    )]
62    pub repository_url: Option<String>,
63}
64
65impl CommonParams {
66    pub fn apply_to_config(&self, config: &mut Config) -> Result<bool> {
67        let mut changes_made = false;
68
69        if let Some(provider) = &self.provider {
70            // Convert "claude" to "anthropic" for backward compatibility
71            let provider_name = if provider.to_lowercase() == "claude" {
72                "anthropic".to_string()
73            } else if provider.to_lowercase() == "openrouter" {
74                "openrouter".to_string()
75            } else if provider.to_lowercase() == "google" {
76                "google".to_string()
77            } else {
78                provider.clone()
79            };
80
81            // Check if we need to update the default provider
82            if config.default_provider != provider_name {
83                // Ensure the provider exists in the providers HashMap
84                if !config.providers.contains_key(&provider_name) {
85                    // Import ProviderConfig here
86                    use crate::config::ProviderConfig;
87                    config.providers.insert(
88                        provider_name.clone(),
89                        ProviderConfig::default_for(&provider_name),
90                    );
91                }
92
93                config.default_provider.clone_from(&provider_name);
94                changes_made = true;
95            }
96        }
97
98        if let Some(instructions) = &self.instructions {
99            config.set_temp_instructions(Some(instructions.clone()));
100            // Note: temp instructions don't count as permanent changes
101        }
102
103        Ok(changes_made)
104    }
105}
106
107/// Validates that a provider name is available in the system
108pub fn available_providers_parser(s: &str) -> Result<String, String> {
109    let mut provider_name = s.to_lowercase();
110
111    // Handle legacy "claude" provider name by mapping it to "anthropic"
112    if provider_name == "claude" {
113        provider_name = "anthropic".to_string();
114    }
115
116    let available_providers = get_available_provider_names();
117
118    if available_providers
119        .iter()
120        .any(|p| p.to_lowercase() == provider_name)
121    {
122        Ok(provider_name)
123    } else {
124        Err(format!(
125            "Invalid provider '{}'. Available providers: {}",
126            s,
127            available_providers.join(", ")
128        ))
129    }
130}
131
132pub fn get_combined_instructions(config: &Config) -> String {
133    let mut prompt = String::from("\n\n");
134
135    if !config.instructions.is_empty() {
136        write!(
137            &mut prompt,
138            "\n\nAdditional instructions for the request:\n{}\n\n",
139            config.instructions
140        )
141        .expect("write to string should not fail");
142    }
143
144    prompt
145}