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