use std::fmt;
use std::str::FromStr;
use ag_protocol::TurnPrompt;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum FocusedReviewStatus {
Pending,
Ready,
Partial,
Failed,
}
impl FocusedReviewStatus {
#[must_use]
pub fn for_text(text: &str) -> Self {
if text
.rsplit_once("\n### Coverage\n")
.is_some_and(|(_, coverage)| coverage.trim_start().starts_with("Partial review:"))
{
Self::Partial
} else {
Self::Ready
}
}
}
impl fmt::Display for FocusedReviewStatus {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(match self {
Self::Pending => "Pending",
Self::Ready => "Ready",
Self::Partial => "Partial",
Self::Failed => "Failed",
})
}
}
impl FromStr for FocusedReviewStatus {
type Err = String;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value {
"Pending" => Ok(Self::Pending),
"Ready" => Ok(Self::Ready),
"Partial" => Ok(Self::Partial),
"Failed" => Ok(Self::Failed),
_ => Err(format!("Unknown focused review status: {value}")),
}
}
}
pub fn build_apply_review_prompt(suggestions: &str) -> TurnPrompt {
let suggestions = suggestions.trim();
let fence = ag_protocol::diff_fence(suggestions);
let fenced_suggestions = format!("{fence}text\n{suggestions}\n{fence}");
let prompt = APPLY_REVIEW_PROMPT_TEMPLATE
.trim_end()
.replace("{{ fenced_suggestions }}", &fenced_suggestions);
TurnPrompt::from_text(prompt)
}
#[must_use]
pub fn review_suggestions(review_text: &str) -> Option<String> {
let suggestions_header = "### Suggestions";
let header_start = review_text.find(suggestions_header)?;
let content_start = header_start + suggestions_header.len();
let content = &review_text[content_start..];
let section_end = content.find("\n### ").unwrap_or(content.len());
let suggestions = content[..section_end].trim();
if suggestions.is_empty() || is_no_suggestions_sentinel(suggestions) {
return None;
}
Some(suggestions.to_string())
}
#[must_use]
pub fn has_actionable_review_suggestions(review_text: Option<&str>) -> bool {
review_text.and_then(review_suggestions).is_some()
}
const APPLY_REVIEW_PROMPT_TEMPLATE: &str = include_str!("template/apply_review_prompt.md");
fn is_no_suggestions_sentinel(suggestions: &str) -> bool {
suggestions.strip_prefix("- None").is_some_and(|suffix| {
suffix
.chars()
.all(|character| character.is_ascii_punctuation())
})
}
#[cfg(test)]
#[path = "review_test.rs"]
mod tests;