pub mod commit;
pub mod diff;
pub mod prompt;
pub mod providers;
pub mod types;
use anyhow::Result;
use providers::{create_provider, AIProvider, ProviderConfig};
use std::env;
use std::time::Duration;
pub struct Config {
pub provider_config: ProviderConfig,
pub count: u8,
pub auto_commit: bool,
pub show_diff: bool,
}
impl Config {
pub fn new() -> Result<Self> {
let api_key = env::var("OPENAI_API_KEY")
.map_err(|_| anyhow::anyhow!("OPENAI_API_KEY environment variable not set"))?;
Ok(Config {
provider_config: ProviderConfig::openai(api_key, "gpt-4".to_string()),
count: 3,
auto_commit: false,
show_diff: false,
})
}
pub fn with_openai(
api_key: String,
model: String,
count: u8,
auto_commit: bool,
show_diff: bool,
) -> Self {
Config {
provider_config: ProviderConfig::openai(api_key, model),
count,
auto_commit,
show_diff,
}
}
pub fn with_ollama(
base_url: String,
model: String,
count: u8,
auto_commit: bool,
show_diff: bool,
) -> Self {
Config {
provider_config: ProviderConfig::ollama(base_url, model),
count,
auto_commit,
show_diff,
}
}
pub fn with_ollama_timeout(
base_url: String,
model: String,
timeout: Duration,
count: u8,
auto_commit: bool,
show_diff: bool,
) -> Self {
Config {
provider_config: ProviderConfig::ollama_with_timeout(base_url, model, timeout),
count,
auto_commit,
show_diff,
}
}
}
pub struct Commitor {
config: Config,
provider: Box<dyn AIProvider>,
}
impl Commitor {
pub fn new(config: Config) -> Result<Self> {
let provider = create_provider(config.provider_config.clone())?;
Ok(Self { config, provider })
}
pub async fn generate_commit_messages(&self, diff: &str) -> Result<Vec<String>> {
commit::generate_commit_messages(diff, &*self.provider, self.config.count).await
}
pub fn get_staged_diff(&self) -> Result<String> {
diff::get_staged_diff()
}
pub fn commit_with_message(&self, message: &str) -> Result<()> {
commit::commit_with_message(message)
}
}
impl Default for Config {
fn default() -> Self {
Self::new().unwrap_or_else(|_| Config {
provider_config: ProviderConfig::openai(String::new(), "gpt-4".to_string()),
count: 3,
auto_commit: false,
show_diff: false,
})
}
}