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 Failed,
17}
18
19impl fmt::Display for FocusedReviewStatus {
20 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
21 formatter.write_str(match self {
22 Self::Pending => "Pending",
23 Self::Ready => "Ready",
24 Self::Failed => "Failed",
25 })
26 }
27}
28
29impl FromStr for FocusedReviewStatus {
30 type Err = String;
31
32 fn from_str(value: &str) -> Result<Self, Self::Err> {
33 match value {
34 "Pending" => Ok(Self::Pending),
35 "Ready" => Ok(Self::Ready),
36 "Failed" => Ok(Self::Failed),
37 _ => Err(format!("Unknown focused review status: {value}")),
38 }
39 }
40}
41
42pub fn build_apply_review_prompt(suggestions: &str) -> TurnPrompt {
48 let suggestions = suggestions.trim();
49 let fence = ag_protocol::diff_fence(suggestions);
50 let fenced_suggestions = format!("{fence}text\n{suggestions}\n{fence}");
51 let prompt = APPLY_REVIEW_PROMPT_TEMPLATE
52 .trim_end()
53 .replace("{{ fenced_suggestions }}", &fenced_suggestions);
54
55 TurnPrompt::from_text(prompt)
56}
57
58#[must_use]
63pub fn review_suggestions(review_text: &str) -> Option<String> {
64 let suggestions_header = "### Suggestions";
65 let header_start = review_text.find(suggestions_header)?;
66 let content_start = header_start + suggestions_header.len();
67 let content = &review_text[content_start..];
68 let section_end = content.find("\n### ").unwrap_or(content.len());
69 let suggestions = content[..section_end].trim();
70
71 if suggestions.is_empty() || is_no_suggestions_sentinel(suggestions) {
72 return None;
73 }
74
75 Some(suggestions.to_string())
76}
77
78#[must_use]
81pub fn has_actionable_review_suggestions(review_text: Option<&str>) -> bool {
82 review_text.and_then(review_suggestions).is_some()
83}
84
85const APPLY_REVIEW_PROMPT_TEMPLATE: &str = include_str!("template/apply_review_prompt.md");
86
87fn is_no_suggestions_sentinel(suggestions: &str) -> bool {
90 suggestions.strip_prefix("- None").is_some_and(|suffix| {
91 suffix
92 .chars()
93 .all(|character| character.is_ascii_punctuation())
94 })
95}
96
97#[cfg(test)]
98#[path = "review_test.rs"]
99mod tests;