use std::path::Path;
use anyhow::{Context, Result, bail};
use brok_core::Workspace;
use brok_sdk::payload::{AskRequest, SkillInvocation};
use brok_skills::Catalog;
use dialoguer::Select;
use super::ask::{self, Output};
use crate::{daemon, ui};
const PROMPT_BUDGET: usize = 14_000;
const MAX_STAT_CHARS: usize = 2_000;
#[derive(Debug, clap::Args)]
pub struct ReviewArgs {
#[arg(long)]
staged: bool,
#[arg(long, conflicts_with = "staged")]
base: Option<String>,
#[arg(short, long)]
provider: Option<String>,
}
#[derive(Debug, clap::Args)]
pub struct CommitArgs {
#[arg(short, long)]
yes: bool,
#[arg(long, conflicts_with = "yes")]
print: bool,
#[arg(short, long)]
provider: Option<String>,
}
pub async fn review(cwd: &Path, args: ReviewArgs) -> Result<()> {
let workspace = super::workspace(cwd)?;
let (diff_args, what): (Vec<String>, String) = match &args.base {
Some(base) => (vec!["diff".into(), format!("{base}...HEAD")], format!("this branch against {base}")),
None if args.staged => (vec!["diff".into(), "--cached".into()], "the staged changes".into()),
None => (vec!["diff".into(), "HEAD".into()], "the uncommitted changes".into()),
};
let diff = git(cwd, &diff_args)?;
if diff.trim().is_empty() {
ui::step(&format!("Nothing to review: {what} are empty."));
return Ok(());
}
let (diff, truncated) = truncate(&diff, PROMPT_BUDGET - 200);
let mut prompt = format!("Review {what}.\n\n```diff\n{diff}\n```");
if truncated {
prompt.push_str("\n\n(The diff was truncated; say which parts you could not see.)");
}
let skill = skill(&workspace, "code-review")?;
let (client, _inbox) = daemon::connect_or_start(&workspace).await?;
let request = AskRequest {
prompt,
provider: args.provider,
context_chunks: Some(4),
skill: Some(skill),
history: Vec::new(),
};
ask::stream(&client, &request).await.map(drop)
}
pub async fn commit(cwd: &Path, args: CommitArgs) -> Result<()> {
let workspace = super::workspace(cwd)?;
let diff = git(cwd, &["diff".into(), "--cached".into()])?;
if diff.trim().is_empty() {
bail!("nothing is staged: `git add <files>` (or `git add -A`) first");
}
let stat = git(cwd, &["diff".into(), "--cached".into(), "--stat".into()])?;
let (stat, _) = truncate(&stat, MAX_STAT_CHARS);
let (diff, _) = truncate(&diff, PROMPT_BUDGET - stat.len() - 200);
let prompt = format!("Write the commit message for this staged change.\n\n{stat}\n\n```diff\n{diff}\n```");
let skill = skill(&workspace, "commit-message")?;
let (client, _inbox) = daemon::connect_or_start(&workspace).await?;
let request = AskRequest {
prompt,
provider: args.provider,
context_chunks: Some(0),
skill: Some(skill),
history: Vec::new(),
};
let spinner = ui::spinner("Writing the commit message");
let answer = ask::answer(&client, &request, Output::Silent).await;
spinner.finish_and_clear();
let message = clean_message(&answer?);
if message.is_empty() {
bail!("the model returned an empty message");
}
println!("{}\n", console::style(&message).bold());
if args.print {
return Ok(());
}
let action = if args.yes {
0
} else if ui::interactive() {
Select::new()
.with_prompt("Commit with this message?")
.items(["Commit", "Edit in your git editor, then commit", "Cancel"])
.default(0)
.interact()?
} else {
bail!("not a terminal: pass --yes to commit or --print to only print the message");
};
if action == 2 {
ui::step("Cancelled");
return Ok(());
}
let file = tempfile_path(&workspace);
std::fs::write(&file, format!("{message}\n"))?;
let mut command = std::process::Command::new("git");
command.arg("-C").arg(cwd).arg("commit").arg("-F").arg(&file);
if action == 1 {
command.arg("--edit");
}
let status = command.status().context("cannot run git")?;
let _ = std::fs::remove_file(&file);
if !status.success() {
bail!("git commit failed");
}
Ok(())
}
fn skill(workspace: &Workspace, name: &str) -> Result<SkillInvocation> {
let catalog = Catalog::load(workspace);
let skill = catalog.require(name)?;
Ok(SkillInvocation { name: skill.name.clone(), instructions: skill.instructions_with_references()? })
}
fn git(dir: &Path, args: &[String]) -> Result<String> {
let output = std::process::Command::new("git").arg("-C").arg(dir).args(args).output().context("cannot run git")?;
if !output.status.success() {
bail!("git {} failed: {}", args.join(" "), String::from_utf8_lossy(&output.stderr).trim());
}
Ok(String::from_utf8_lossy(&output.stdout).into_owned())
}
fn truncate(diff: &str, max: usize) -> (&str, bool) {
if diff.len() <= max {
return (diff, false);
}
let mut cut = max;
while !diff.is_char_boundary(cut) {
cut -= 1;
}
let cut = diff[..cut].rfind('\n').unwrap_or(cut);
(&diff[..cut], true)
}
fn clean_message(answer: &str) -> String {
let mut text = answer.trim();
if let Some(inner) = text.strip_prefix("```") {
let inner = inner.split_once('\n').map_or(inner, |(_, rest)| rest);
text = inner.strip_suffix("```").unwrap_or(inner).trim();
}
text.trim_matches(['"', '\'', '`']).trim().to_owned()
}
fn tempfile_path(workspace: &Workspace) -> std::path::PathBuf {
workspace.paths().run.join(format!("commit-message-{}.txt", std::process::id()))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cleans_model_noise() {
assert_eq!(clean_message("```\nfeat(cli): add chat\n```"), "feat(cli): add chat");
assert_eq!(clean_message("```text\nfix: x\n\nbody\n```\n"), "fix: x\n\nbody");
assert_eq!(clean_message("\"docs: readme\""), "docs: readme");
assert_eq!(clean_message(" chore: tidy "), "chore: tidy");
}
#[test]
fn truncates_at_line_boundaries() {
let diff = "+line\n".repeat(5_000);
let (cut, truncated) = truncate(&diff, PROMPT_BUDGET);
assert!(truncated);
assert!(cut.len() <= PROMPT_BUDGET);
assert!(cut.ends_with("+line"));
assert_eq!(truncate("small", 10), ("small", false));
}
}