Something went wrong. Try again.
A semantic stack slicer for Jujutsu. crates.io/crates/jj-slice
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283use anyhow::Context;
use crate::config::Prompts;use crate::provider::Provider;use crate::revisions::Revision;use crate::slices::Slices;
const DEFAULT_GROUP_PROMPT: &str = "\Inspect the following numbered commits to be submitted as one or more stackedPRs. Choose logical groups of atomic changes to be submitted together. Respondwith ONLY a raw JSON array of the 1-based indices of the last commit in eachgroup except the final one, e.g. [2, 4] to split 5 commits into groups [1-2],[3-4], [5]. No markdown, no code fences, no prose; just the raw JSON array.
{revisions}";
const DEFAULT_NAME_PROMPT: &str = "\Emit ONLY a branch name, with no extra fluff or prose, that summarizes theintent of this series of commits as an action phrase in 3-5 words, alllowercase, hyphenated, without 'generic' words like implement or feature.
{revisions}";
const PLACEHOLDER: &str = "{revisions}";
pub struct Agent { group_prompt: String, name_prompt: String, provider: Provider,}
impl Agent { pub fn new(provider: Provider, prompts: &Prompts) -> anyhow::Result<Self> { let group_prompt = prompts.group.clone().unwrap_or_else(|| DEFAULT_GROUP_PROMPT.into()); let name_prompt = prompts.name.clone().unwrap_or_else(|| DEFAULT_NAME_PROMPT.into()); for (key, prompt) in [("group", &group_prompt), ("name", &name_prompt)] { anyhow::ensure!( prompt.contains(PLACEHOLDER), "tools.jj-slice.prompts.{key} must contain {PLACEHOLDER}" ); } Ok(Self { provider, group_prompt, name_prompt }) }
pub fn slice_revisions(&self, slices: &Slices) -> anyhow::Result<Vec<usize>> { let revisions = slices.0.iter().flat_map(|s| &s.revisions); let prompt = self.group_prompt.replace(PLACEHOLDER, &format_numbered(revisions)); let response = self.provider.prompt(&prompt)?; let one_based: Vec<usize> = serde_json::from_str(response.trim()) .with_context(|| format!("failed to parse slice indices from response: {response:?}"))?; Ok(one_based.into_iter().filter_map(|i| i.checked_sub(1)).collect()) }
pub fn name_slice(&self, revisions: &[Revision]) -> anyhow::Result<String> { let prompt = self.name_prompt.replace(PLACEHOLDER, &format_numbered(revisions)); let response = self.provider.prompt(&prompt)?; let name = response .lines() .map(|l| l.trim().trim_matches(['`', '"', '\''])) .find(|l| !l.is_empty()) .ok_or_else(|| anyhow::anyhow!("provider returned no branch name"))?; Ok(name.to_string()) }}
fn format_numbered<'a>(revisions: impl IntoIterator<Item = &'a Revision>) -> String { let mut out = String::new(); for (i, r) in revisions.into_iter().enumerate() { out.push_str(&format!("{}.\n", i + 1));
for line in r.description.lines() { out.push_str(&format!(" {line}\n")); }
for f in &r.stat.files { out.push_str(&format!(" {} (+{} -{})\n", f.path, f.added, f.removed)); } } out}