ag_protocol/fence.rs
1/// Builds a Markdown code-fence delimiter long enough to safely wrap an
2/// arbitrary prompt payload.
3///
4/// Returns a string of backticks whose length exceeds the longest run of
5/// consecutive backticks found anywhere in `content`, with a minimum length
6/// of three. This prevents a triple-backtick fence from being terminated
7/// prematurely when the payload itself contains Markdown fences (for example,
8/// when reviewing changes to Markdown or prompt-template files).
9pub fn diff_fence(content: &str) -> String {
10 let mut max_run = 0usize;
11 let mut current_run = 0usize;
12 for character in content.chars() {
13 if character == '`' {
14 current_run += 1;
15 if current_run > max_run {
16 max_run = current_run;
17 }
18 } else {
19 current_run = 0;
20 }
21 }
22
23 let fence_length = std::cmp::max(3, max_run + 1);
24
25 "`".repeat(fence_length)
26}