opencrates 3.0.1

Enterprise-grade AI-powered Rust development companion with comprehensive automation, monitoring, and deployment capabilities
use crate::providers::CodexProvider;
use crate::utils::config::CodexConfig;
use anyhow::Result;
use clap::Parser;

#[derive(Parser, Debug)]
pub struct CodexCommand {
    /// The prompt to send to the Codex agent.
    #[clap(index = 1)]
    pub prompt: String,

    /// Apply the generated patch to the specified file.
    #[clap(long, value_name = "FILE_PATH")]
    pub apply_patch: Option<String>,
}

impl CodexCommand {
    pub async fn run(&self) -> Result<()> {
        let codex_config = CodexConfig::default();

        let provider = CodexProvider::new(codex_config).await?;

        println!("Sending prompt to Codex: '{}'...", self.prompt);

        let response = provider.generate_code(&self.prompt, None).await?;

        if let Some(file_path_str) = &self.apply_patch {
            let file_path = std::path::PathBuf::from(file_path_str);
            println!("\nApplying patch to '{}'...", file_path.display());
            provider.apply_patch(&file_path, &response)?;
            println!("Patch applied successfully.");
        } else {
            println!("\nCodex Response:\n{response}");
        }

        Ok(())
    }
}