magi-code 0.97.4

Repository-aware CLI coding agent for terminal work
Documentation
use super::*;

pub(super) fn web_card(input: &ToolCardInput<'_>) -> CardDraft {
    let detail = input.detail;
    let status = input_status(input);
    let operation = detail
        .and_then(|detail| detail.params.get("operation"))
        .and_then(serde_json::Value::as_str)
        .unwrap_or("web");
    let target = detail.and_then(|detail| {
        ["query", "url", "reference"]
            .iter()
            .find_map(|key| detail.params.get(key).and_then(serde_json::Value::as_str))
    });
    let mut metadata = Vec::new();
    if let Some(results) = detail
        .and_then(|detail| detail.metadata.get("results"))
        .and_then(serde_json::Value::as_u64)
    {
        metadata.push(format!("{results} results"));
    }
    if detail
        .and_then(|detail| detail.metadata.get("truncated"))
        .and_then(serde_json::Value::as_bool)
        == Some(true)
    {
        metadata.push("output truncated".to_string());
    }
    let mut lines = target
        .map(sanitized_single_line)
        .into_iter()
        .collect::<Vec<_>>();
    match status {
        CardStatus::Success => {
            if let Some(detail) = detail {
                let preview = bounded_preview_lines(detail.output.as_ref());
                if !preview.is_empty() {
                    lines.push(preview);
                }
            }
        }
        CardStatus::Failed => lines.push(bounded_error_line(
            detail
                .map(|detail| detail.output.as_ref())
                .unwrap_or("web request failed"),
        )),
        CardStatus::Canceled => lines.push(bounded_error_line(
            detail
                .map(|detail| detail.output.as_ref())
                .filter(|output| !output.trim().is_empty())
                .unwrap_or("canceled"),
        )),
        CardStatus::Running | CardStatus::None => {}
    }
    CardDraft {
        role: ToolCardRole::Web,
        status,
        title: match operation {
            "search" => "Web Search",
            "open" => "Web Open",
            _ => "Web",
        }
        .to_string(),
        metadata,
        body: lines.join("\n"),
        body_lines: Vec::new(),
        activity_rows: Vec::new(),
    }
}