Skip to main content

sr_ai/commands/
explain.rs

1use crate::ai::{AiRequest, BackendConfig, resolve_backend};
2use crate::git::GitRepo;
3use crate::ui;
4use anyhow::Result;
5
6#[derive(Debug, clap::Args)]
7pub struct ExplainArgs {
8    /// Commit ref to explain (default: HEAD)
9    #[arg(default_value = "HEAD")]
10    pub rev: String,
11}
12
13const SYSTEM_PROMPT: &str = "You are an expert at explaining git commits. Given a git show output, \
14explain what the commit does, why the changes were made, and any notable patterns or concerns. \
15Be concise but thorough.";
16
17pub async fn run(args: &ExplainArgs, backend_config: &BackendConfig) -> Result<()> {
18    let repo = GitRepo::discover()?;
19    let backend = resolve_backend(backend_config).await?;
20
21    let show = repo.show(&args.rev)?;
22
23    let spinner = ui::spinner(&format!("Explaining commit with {}...", backend.name()));
24
25    let request = AiRequest {
26        system_prompt: SYSTEM_PROMPT.to_string(),
27        user_prompt: format!("Explain this commit:\n\n{show}"),
28        json_schema: None,
29        working_dir: repo.root().to_string_lossy().to_string(),
30    };
31
32    let response = backend.request(&request, None).await?;
33    spinner.finish_and_clear();
34
35    println!("{}", response.text);
36
37    Ok(())
38}