1use std::fmt;
4use std::str::FromStr;
5
6use ag_protocol::TurnPrompt;
7
8#[derive(Clone, Copy, Debug, Eq, PartialEq)]
10pub enum FocusedReviewStatus {
11 Pending,
13 Ready,
15 Partial,
18 Failed,
20}
21
22impl FocusedReviewStatus {
23 #[must_use]
27 pub fn for_text(text: &str) -> Self {
28 if text
29 .rsplit_once("\n### Coverage\n")
30 .is_some_and(|(_, coverage)| coverage.trim_start().starts_with("Partial review:"))
31 {
32 Self::Partial
33 } else {
34 Self::Ready
35 }
36 }
37}
38
39impl fmt::Display for FocusedReviewStatus {
40 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
41 formatter.write_str(match self {
42 Self::Pending => "Pending",
43 Self::Ready => "Ready",
44 Self::Partial => "Partial",
45 Self::Failed => "Failed",
46 })
47 }
48}
49
50impl FromStr for FocusedReviewStatus {
51 type Err = String;
52
53 fn from_str(value: &str) -> Result<Self, Self::Err> {
54 match value {
55 "Pending" => Ok(Self::Pending),
56 "Ready" => Ok(Self::Ready),
57 "Partial" => Ok(Self::Partial),
58 "Failed" => Ok(Self::Failed),
59 _ => Err(format!("Unknown focused review status: {value}")),
60 }
61 }
62}
63
64pub fn build_apply_review_prompt(suggestions: &str) -> TurnPrompt {
70 let suggestions = suggestions.trim();
71 let fence = ag_protocol::diff_fence(suggestions);
72 let fenced_suggestions = format!("{fence}text\n{suggestions}\n{fence}");
73 let prompt = APPLY_REVIEW_PROMPT_TEMPLATE
74 .trim_end()
75 .replace("{{ fenced_suggestions }}", &fenced_suggestions);
76
77 TurnPrompt::from_text(prompt)
78}
79
80#[must_use]
85pub fn review_suggestions(review_text: &str) -> Option<String> {
86 let suggestions_header = "### Suggestions";
87 let header_start = review_text.find(suggestions_header)?;
88 let content_start = header_start + suggestions_header.len();
89 let content = &review_text[content_start..];
90 let section_end = content.find("\n### ").unwrap_or(content.len());
91 let suggestions = content[..section_end].trim();
92
93 if suggestions.is_empty() || is_no_suggestions_sentinel(suggestions) {
94 return None;
95 }
96
97 Some(suggestions.to_string())
98}
99
100#[must_use]
103pub fn has_actionable_review_suggestions(review_text: Option<&str>) -> bool {
104 review_text.and_then(review_suggestions).is_some()
105}
106
107const APPLY_REVIEW_PROMPT_TEMPLATE: &str = include_str!("template/apply_review_prompt.md");
108
109fn is_no_suggestions_sentinel(suggestions: &str) -> bool {
112 suggestions.strip_prefix("- None").is_some_and(|suffix| {
113 suffix
114 .chars()
115 .all(|character| character.is_ascii_punctuation())
116 })
117}
118
119#[cfg(test)]
120#[path = "review_test.rs"]
121mod tests;