magi-code 0.96.2

Repository-aware CLI coding agent for terminal work
Documentation
use std::borrow::Cow;

pub(crate) fn next_sse_event_boundary(buffer: &str) -> Option<(usize, usize)> {
    ["\r\n\r\n", "\n\r\n", "\r\n\n", "\n\n"]
        .into_iter()
        .filter_map(|boundary| buffer.find(boundary).map(|index| (index, boundary.len())))
        .min_by_key(|(index, _)| *index)
}

pub(crate) fn sse_data(raw_event: &str) -> Option<Cow<'_, str>> {
    let mut lines = raw_event.lines().filter_map(|line| {
        line.strip_prefix("data:")
            .map(|payload| Cow::Borrowed(payload.strip_prefix(' ').unwrap_or(payload)))
    });
    let first = lines.next()?;
    let Some(second) = lines.next() else {
        return Some(first);
    };

    let mut data = String::with_capacity(first.len() + 1 + second.len());
    data.push_str(&first);
    data.push('\n');
    data.push_str(&second);
    for line in lines {
        data.push('\n');
        data.push_str(&line);
    }
    Some(Cow::Owned(data))
}

pub(crate) fn diagnostic_snippet(text: &str) -> String {
    let redacted = crate::output::redact_sensitive_text(text).replace('\n', "\\n");
    let mut snippet = redacted.chars().take(200).collect::<String>();
    if redacted.chars().count() > 200 {
        snippet.push_str("...");
    }
    snippet
}