feat(ai): add AI-assisted commit splitting flow
Introduce a split-planning path for staged changes that asks supported AI providers to group files into ordered Conventional Commit messages. The plan is validated before execution so every staged file is assigned once and unsafe states are rejected. A new dialog lets users review and adjust the proposed groups before creating the commits in sequence, with safeguards to preserve remaining changes if something fails.
This commit is contained in:
@@ -1643,6 +1643,127 @@ pub struct AiReviewResult {
|
||||
pub findings: Vec<AiReviewFinding>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AiCommitGroup {
|
||||
pub message: String,
|
||||
pub reason: String,
|
||||
pub files: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AiCommitPlan {
|
||||
pub summary: String,
|
||||
pub groups: Vec<AiCommitGroup>,
|
||||
}
|
||||
|
||||
fn parse_ai_commit_plan(raw: &str, staged_files: &[String]) -> Result<AiCommitPlan, String> {
|
||||
use std::collections::HashSet;
|
||||
let trimmed = raw.trim().trim_matches('`').trim();
|
||||
let json = match (trimmed.find('{'), trimmed.rfind('}')) {
|
||||
(Some(start), Some(end)) if start <= end => &trimmed[start..=end],
|
||||
_ => return Err("The AI response did not contain a valid commit plan.".to_string()),
|
||||
};
|
||||
let mut plan: AiCommitPlan = serde_json::from_str(json)
|
||||
.map_err(|error| format!("Could not process the commit plan: {error}"))?;
|
||||
plan.groups
|
||||
.retain(|group| !group.message.trim().is_empty() && !group.files.is_empty());
|
||||
if plan.groups.len() < 2 {
|
||||
return Err("The staged changes do not appear to benefit from splitting.".to_string());
|
||||
}
|
||||
if plan.groups.len() > 12 {
|
||||
return Err("The AI proposed too many commit groups.".to_string());
|
||||
}
|
||||
let expected = staged_files.iter().cloned().collect::<HashSet<_>>();
|
||||
let mut seen = HashSet::new();
|
||||
for group in &mut plan.groups {
|
||||
group.message = group.message.trim().to_string();
|
||||
group.reason = group.reason.trim().to_string();
|
||||
group
|
||||
.files
|
||||
.retain(|file| expected.contains(file) && seen.insert(file.clone()));
|
||||
if group.files.is_empty() {
|
||||
return Err("The AI returned an empty or duplicate commit group.".to_string());
|
||||
}
|
||||
}
|
||||
if seen != expected {
|
||||
return Err("The AI plan did not assign every staged file exactly once.".to_string());
|
||||
}
|
||||
plan.summary = plan.summary.trim().to_string();
|
||||
Ok(plan)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn commit_ai_split(
|
||||
path: String,
|
||||
provider: String,
|
||||
model: Option<String>,
|
||||
api_key: Option<String>,
|
||||
base_url: Option<String>,
|
||||
) -> Result<AiCommitPlan, String> {
|
||||
let repo = resolve_repo(&path)?;
|
||||
let status = status_for_repo(&repo)?;
|
||||
let staged_files = status
|
||||
.files
|
||||
.iter()
|
||||
.filter(|file| file.staged.is_some())
|
||||
.map(|file| file.path.clone())
|
||||
.collect::<Vec<_>>();
|
||||
if staged_files.len() < 2 {
|
||||
return Err("Stage at least two files before creating a split plan.".to_string());
|
||||
}
|
||||
if status
|
||||
.files
|
||||
.iter()
|
||||
.any(|file| file.staged.is_some() && file.unstaged.is_some())
|
||||
{
|
||||
return Err("Files with both staged and unstaged changes cannot be split safely. Stage or discard the remaining changes first.".to_string());
|
||||
}
|
||||
let diff = staged_diff(&repo)?;
|
||||
let model = model.filter(|value| !value.trim().is_empty());
|
||||
let api_key = api_key.filter(|value| !value.trim().is_empty());
|
||||
let base_url = base_url.filter(|value| !value.trim().is_empty());
|
||||
let raw = match provider.as_str() {
|
||||
"openai" => {
|
||||
commit_ai::split_openai(
|
||||
api_key
|
||||
.as_deref()
|
||||
.ok_or_else(|| "OpenAI API key is missing.".to_string())?,
|
||||
model.as_deref().unwrap_or("gpt-4o-mini"),
|
||||
&diff,
|
||||
)
|
||||
.await?
|
||||
}
|
||||
"anthropic" => {
|
||||
commit_ai::split_anthropic(
|
||||
api_key
|
||||
.as_deref()
|
||||
.ok_or_else(|| "Anthropic API key is missing.".to_string())?,
|
||||
model.as_deref().unwrap_or("claude-3-5-haiku-latest"),
|
||||
&diff,
|
||||
)
|
||||
.await?
|
||||
}
|
||||
"custom" => {
|
||||
commit_ai::split_custom(
|
||||
base_url
|
||||
.as_deref()
|
||||
.ok_or_else(|| "Endpoint URL is missing.".to_string())?,
|
||||
api_key.as_deref(),
|
||||
model
|
||||
.as_deref()
|
||||
.ok_or_else(|| "Model name is missing.".to_string())?,
|
||||
&diff,
|
||||
)
|
||||
.await?
|
||||
}
|
||||
"local" => return Err("Commit splitting currently requires an API provider.".to_string()),
|
||||
other => return Err(format!("Unknown AI provider: {other}")),
|
||||
};
|
||||
parse_ai_commit_plan(&raw, &staged_files)
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct AiReviewWireFinding {
|
||||
severity: String,
|
||||
@@ -7105,4 +7226,15 @@ mod tests {
|
||||
assert_eq!(review.findings[0].file.as_deref(), Some("src/main.rs"));
|
||||
assert_eq!(review.findings[0].line, Some(42));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_ai_commit_plan_requires_each_staged_file_exactly_once() {
|
||||
let files = vec!["src/app.ts".to_string(), "tests/app.test.ts".to_string()];
|
||||
let raw = r#"{"summary":"Separate behavior and coverage","groups":[{"message":"feat(app): add behavior","reason":"Production code","files":["src/app.ts"]},{"message":"test(app): cover behavior","reason":"Tests","files":["tests/app.test.ts"]}]}"#;
|
||||
let plan = parse_ai_commit_plan(raw, &files).expect("complete plan should parse");
|
||||
assert_eq!(plan.groups.len(), 2);
|
||||
|
||||
let duplicate = r#"{"summary":"Bad plan","groups":[{"message":"feat: one","reason":"","files":["src/app.ts"]},{"message":"test: two","reason":"","files":["src/app.ts"]}]}"#;
|
||||
assert!(parse_ai_commit_plan(duplicate, &files).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user