use super::{Surface, bound_value_at, display_value, validate_surface_composition}; use pulldown_cmark::{Event, Options, Parser, Tag, TagEnd, html}; use serde_json::{Map, Value}; use std::collections::{BTreeSet, HashMap}; use std::fmt::Write; const WIDTH: usize = 900; const HEIGHT: usize = 720; const COLORS: [&str; 6] = [ "#75beff", "#89d185", "#d18616", "#f14c4c", "#b180d7", "#e2c440", ]; pub(crate) fn surface_svg( surface: &Surface, images: &HashMap, ) -> Result { validate_surface_composition(surface)?; let renderer = Renderer { surface, images }; let content = renderer.component("root", &surface.data, BTreeSet::new()); let agent = surface .surface_properties .get("agentDisplayName") .and_then(Value::as_str) .unwrap_or("A2UI"); let label = format!("{agent} surface {}", surface.id); let mut svg = format!( r#"
{}{}
"#, escaped(&label), STYLE, escaped(agent), escaped(&surface.id), ); svg.push_str(&content); svg.push_str("
"); Ok(svg) } const STYLE: &str = r#" *{box-sizing:border-box} .a2ui-document{height:100%;overflow:auto;background:#17181b;color:#e8e8ea;font:14px/1.45 -apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif;border:1px solid #34363d;border-radius:12px}.a2ui-document>header{position:sticky;top:0;z-index:2;display:flex;justify-content:space-between;padding:11px 14px;background:#202125;border-bottom:1px solid #34363d;color:#aeb0b6;font-size:12px}.a2ui-document>main{display:flex;flex-direction:column;gap:10px;padding:16px}.row,.column,.list{display:flex;gap:10px}.row,.list.horizontal{flex-direction:row;align-items:flex-start}.column,.list{flex-direction:column}.card,.map,.table{padding:13px;background:#202125;border:1px solid #34363d;border-radius:9px}.button,.chip{display:inline-block;padding:7px 11px;background:#30323a;border:1px solid #464951;border-radius:7px}.button.primary{background:#416a91}.button.borderless{background:transparent;border-color:transparent}.field{display:flex;flex-direction:column;gap:5px}.field-label,.muted,.chart-kind,.caption{color:#999ba3;font-size:12px}.input{min-height:34px;padding:7px 9px;background:#101114;border:1px solid #393b42;border-radius:6px;white-space:pre-wrap}.check{display:flex;gap:7px;align-items:center}.check-mark{width:18px;height:18px;text-align:center;border:1px solid #555862;border-radius:5px}.divider{height:1px;background:#3a3c43}.divider.vertical{width:1px;height:auto;min-height:32px}.metric-value{font-size:28px}.error{color:#ff8585}.bar{display:grid;grid-template-columns:minmax(80px,150px) 1fr auto;gap:8px;align-items:center}.bar-track{height:9px;overflow:hidden;background:#30323a;border-radius:5px}.bar-fill{height:100%;background:#75beff}.pie{width:150px;height:150px;border-radius:50%}.donut{position:relative}.donut:after{content:"";position:absolute;inset:34px;background:#17181b;border-radius:50%}.legend{display:flex;flex-wrap:wrap;gap:8px 14px}.swatch{display:inline-block;width:10px;height:10px;margin-right:5px;border-radius:2px}.table{width:100%;border-collapse:collapse}.table th,.table td{padding:7px 9px;border:1px solid #3a3c43;text-align:left}.timeline-item,.location,.mind-node{display:flex;gap:8px}.tabs>details{margin-top:7px;padding:7px 9px;border:1px solid #34363d;border-radius:7px}.tabs summary{cursor:pointer}.media{padding:10px;border:1px solid #34363d;border-radius:7px}.surface-image{display:block;max-width:100%;max-height:260px;margin:auto;border-radius:7px}.chart,.timeline,.mindmap{display:flex;flex-direction:column;gap:8px}h1,h2,h3,p{margin-top:0}pre{overflow:auto}a{color:#8bbceb} "#; struct Renderer<'a> { surface: &'a Surface, images: &'a HashMap, } impl Renderer<'_> { fn component(&self, id: &str, context: &Value, mut ancestors: BTreeSet) -> String { if !ancestors.insert(id.to_owned()) { return format!( "
Cyclic component {}
", escaped(id) ); } let Some(component) = self.surface.components.get(id).and_then(Value::as_object) else { return format!( "
Missing component {}
", escaped(id) ); }; let kind = component .get("component") .and_then(Value::as_str) .unwrap_or("Unknown"); match kind { "Text" => { let content = safe_markdown(&self.value(component, "text", context)); if component.get("variant").and_then(Value::as_str) == Some("caption") { format!("
{content}
") } else { content } } "Image" => self.image(component, context), "Video" | "AudioPlayer" => self.media(component, context, kind), "Icon" => format!( "{}", escaped(&self.value(component, "name", context)), icon(&self.value(component, "name", context)) ), "Divider" => format!( "
", if component.get("axis").and_then(Value::as_str) == Some("vertical") { " vertical" } else { "" } ), "Row" | "Column" | "List" => { let horizontal = kind == "Row" || (kind == "List" && component.get("direction").and_then(Value::as_str) == Some("horizontal")); let class = if kind == "List" { if horizontal { "list horizontal" } else { "list" } } else if kind == "Row" { "row" } else { "column" }; format!( "
{}
", layout_style(component), self.children(component.get("children"), context, &ancestors) ) } "Card" => format!( "
{}
", self.child(component, "child", context, &ancestors) ), "Tabs" => self.tabs(component, context, &ancestors), "Modal" => format!( "
{}
{}
", self.child(component, "trigger", context, &ancestors), self.child(component, "content", context, &ancestors) ), "Button" => self.with_error( component, format!( "{}", component .get("variant") .and_then(Value::as_str) .unwrap_or("default"), self.child(component, "child", context, &ancestors) ), ), "TextField" => self.text_field(component, context), "CheckBox" => self.checkbox(component, context), "Slider" => self.slider(component, context), "DateTimeInput" => self.with_error( component, self.field( &self.value(component, "label", context), &self.value(component, "value", context), ), ), "ChoicePicker" => self.choices(component, context), "Chart" => self.chart(component, context), "Table" => self.table(component, context), "Metric" => self.metric(component, context), "Timeline" => self.timeline(component, context), "Map" => self.map(component, context), "MindMap" => self.mind_map(component, context), "Form" => format!( "
{}{}{}
", escaped(&self.value(component, "title", context)), self.children(component.get("children"), context, &ancestors), escaped( component .get("submitLabel") .and_then(Value::as_str) .unwrap_or("Submit") ) ), _ => format!( "
Unsupported component {}
", escaped(kind) ), } } fn value(&self, component: &Map, key: &str, context: &Value) -> String { display_value(&bound_value_at( component.get(key), &self.surface.data, context, )) } fn with_error(&self, component: &Map, mut content: String) -> String { if let Some(error) = super::first_failed_check(component, &self.surface.data) { let _ = write!(content, "
{}
", escaped(&error)); } content } fn child( &self, component: &Map, key: &str, context: &Value, ancestors: &BTreeSet, ) -> String { component .get(key) .and_then(Value::as_str) .map(|id| self.component(id, context, ancestors.clone())) .unwrap_or_default() } fn children( &self, children: Option<&Value>, context: &Value, ancestors: &BTreeSet, ) -> String { if let Some(children) = children.and_then(Value::as_array) { return children .iter() .filter_map(Value::as_str) .map(|id| self.weighted_component(id, context, ancestors)) .collect(); } let Some(template) = children.and_then(Value::as_object) else { return String::new(); }; let id = template .get("componentId") .and_then(Value::as_str) .unwrap_or(""); let path = template.get("path").and_then(Value::as_str).unwrap_or("/"); self.surface .data .pointer(path) .and_then(Value::as_array) .into_iter() .flatten() .enumerate() .map(|(index, item)| { super::with_template_index(index, || self.weighted_component(id, item, ancestors)) }) .collect() } fn weighted_component( &self, id: &str, context: &Value, ancestors: &BTreeSet, ) -> String { let content = self.component(id, context, ancestors.clone()); self.surface .components .get(id) .and_then(|component| component.get("weight")) .and_then(Value::as_f64) .filter(|weight| weight.is_finite() && *weight > 0.0) .map_or(content.clone(), |weight| { format!("
{content}
") }) } fn tabs( &self, component: &Map, context: &Value, ancestors: &BTreeSet, ) -> String { let mut output = String::from("
"); for (index, tab) in component .get("tabs") .and_then(Value::as_array) .into_iter() .flatten() .enumerate() { let title = display_value(&bound_value_at( tab.get("title"), &self.surface.data, context, )); let child = tab.get("child").and_then(Value::as_str).unwrap_or(""); let _ = write!( output, "{}{}", if index == 0 { " open" } else { "" }, escaped(&title), self.component(child, context, ancestors.clone()) ); } output.push_str("
"); output } fn image(&self, component: &Map, context: &Value) -> String { let url = self.value(component, "url", context); let description = self.value(component, "description", context); let source = self .images .get(&url) .map(String::as_str) .or_else(|| url.starts_with("data:image/").then_some(url.as_str())); source.map_or_else( || { format!( "
▧ {}
{}
", escaped(&description), escaped(&url) ) }, |source| { format!( "\"{}\"/", escaped(source), escaped(&description) ) }, ) } fn media(&self, component: &Map, context: &Value, kind: &str) -> String { format!( "
{} {}
{}
", if kind == "Video" { "▶" } else { "♫" }, escaped(&self.value(component, "description", context)), escaped(&self.value(component, "url", context)) ) } fn text_field(&self, component: &Map, context: &Value) -> String { let mut value = self.value(component, "value", context); if component.get("variant").and_then(Value::as_str) == Some("obscured") { value = "••••••••".to_owned(); } else if value.is_empty() { value = self.value(component, "placeholder", context); } self.with_error( component, self.field(&self.value(component, "label", context), &value), ) } fn field(&self, label: &str, value: &str) -> String { format!( "
{}
{}
", escaped(label), escaped(value) ) } fn checkbox(&self, component: &Map, context: &Value) -> String { let checked = bound_value_at(component.get("value"), &self.surface.data, context) .as_bool() .unwrap_or(false); self.with_error( component, format!( "
{}{}
", if checked { "✓" } else { "" }, escaped(&self.value(component, "label", context)) ), ) } fn slider(&self, component: &Map, context: &Value) -> String { let value = bound_value_at(component.get("value"), &self.surface.data, context) .as_f64() .unwrap_or(0.0); let min = component.get("min").and_then(Value::as_f64).unwrap_or(0.0); let max = component .get("max") .and_then(Value::as_f64) .unwrap_or(100.0); let percent = if max > min { ((value - min) / (max - min) * 100.0).clamp(0.0, 100.0) } else { 0.0 }; self.with_error(component, format!( "
{}
{}
", escaped(&self.value(component, "label", context)), escaped(&display_value(&Value::from(value))) )) } fn choices(&self, component: &Map, context: &Value) -> String { let selected = bound_value_at(component.get("value"), &self.surface.data, context); let multiple = component.get("variant").and_then(Value::as_str) == Some("multipleSelection"); let mut output = format!( "
{}
", escaped(&self.value(component, "label", context)) ); for option in component .get("options") .and_then(Value::as_array) .into_iter() .flatten() { let value = option.get("value").unwrap_or(&Value::Null); let active = selected .as_array() .is_some_and(|values| values.contains(value)) || (!multiple && &selected == value); let _ = write!( output, "{} {}", if active { "✓" } else { "○" }, escaped(&option.get("label").map(display_value).unwrap_or_default()) ); } output.push_str("
"); self.with_error(component, output) } fn chart(&self, component: &Map, context: &Value) -> String { let series = bound_value_at(component.get("series"), &self.surface.data, context); let series = series.as_array().map(Vec::as_slice).unwrap_or(&[]); let chart_type = component .get("chartType") .and_then(Value::as_str) .unwrap_or("bar"); let mut output = format!( "
{}{}", escaped(&self.value(component, "title", context)), escaped(&chart_type.to_uppercase()) ); if matches!(chart_type, "pie" | "donut") { let total = series .iter() .filter_map(|point| point.get("value")?.as_f64()) .filter(|value| *value > 0.0) .sum::(); let mut position = 0.0; let mut stops = Vec::new(); for (index, point) in series.iter().enumerate() { let value = point .get("value") .and_then(Value::as_f64) .unwrap_or(0.0) .max(0.0); let end = if total > 0.0 { position + value / total * 100.0 } else { position }; stops.push(format!( "{} {position:.3}% {end:.3}%", COLORS[index % COLORS.len()] )); position = end; } let _ = write!( output, "
", if chart_type == "donut" { "donut" } else { "" }, stops.join(",") ); for (index, point) in series.iter().enumerate() { let _ = write!( output, "{} {}", COLORS[index % COLORS.len()], escaped(&point.get("label").map(display_value).unwrap_or_default()), escaped(&point.get("value").map(display_value).unwrap_or_default()) ); } output.push_str("
"); } else if chart_type == "heatmap" { let columns = series .iter() .flat_map(|row| { row.get("segments") .and_then(Value::as_array) .into_iter() .flatten() }) .filter_map(|segment| segment.get("label").and_then(Value::as_str)) .fold(Vec::::new(), |mut columns, label| { if !columns.iter().any(|column| column == label) { columns.push(label.to_owned()); } columns }); let max = series .iter() .flat_map(|row| { row.get("segments") .and_then(Value::as_array) .into_iter() .flatten() }) .filter_map(|segment| segment.get("value").and_then(Value::as_f64)) .fold(0.0_f64, f64::max) .max(1.0); output.push_str(""); for column in &columns { let _ = write!(output, "", escaped(column)); } output.push_str(""); for row in series { let segments = row .get("segments") .and_then(Value::as_array) .map(Vec::as_slice) .unwrap_or(&[]); let _ = write!( output, "", escaped(&row.get("label").map(display_value).unwrap_or_default()) ); for column in &columns { let value = segments .iter() .find(|segment| { segment.get("label").and_then(Value::as_str) == Some(column) }) .and_then(|segment| segment.get("value").and_then(Value::as_f64)) .unwrap_or(0.0); let alpha = (value / max).clamp(0.0, 1.0); let _ = write!( output, "", escaped(&display_value(&Value::from(value))) ); } output.push_str(""); } output.push_str("
{}
{}{}
"); } else { let max = series .iter() .filter_map(|point| point.get("value")?.as_f64()) .fold(0.0_f64, f64::max) .max(1.0); for point in series { let value = point.get("value").and_then(Value::as_f64).unwrap_or(0.0); let percent = (value / max * 100.0).clamp(0.0, 100.0); let _ = write!( output, "
{}
{}
", escaped(&point.get("label").map(display_value).unwrap_or_default()), escaped(&point.get("value").map(display_value).unwrap_or_default()) ); if let Some(segments) = point.get("segments").and_then(Value::as_array) { let details = segments .iter() .map(|segment| { format!( "{} {}", segment.get("label").map(display_value).unwrap_or_default(), segment.get("value").map(display_value).unwrap_or_default() ) }) .collect::>() .join(" · "); let _ = write!(output, "
{}
", escaped(&details)); } } } output.push_str("
"); output } fn table(&self, component: &Map, context: &Value) -> String { let columns = bound_value_at(component.get("columns"), &self.surface.data, context); let rows = bound_value_at(component.get("rows"), &self.surface.data, context); let mut output = format!( "
{}", escaped(&self.value(component, "title", context)) ); for column in columns.as_array().into_iter().flatten() { let _ = write!(output, "", escaped(&display_value(column))); } output.push_str(""); for row in rows .as_array() .into_iter() .flatten() .filter_map(Value::as_array) { output.push_str(""); for cell in row { let _ = write!(output, "", escaped(&display_value(cell))); } output.push_str(""); } output.push_str("
{}
{}
"); output } fn metric(&self, component: &Map, context: &Value) -> String { format!( "
{}
{}
{} {}
", escaped(&self.value(component, "label", context)), escaped(&self.value(component, "value", context)), escaped(&self.value(component, "detail", context)), escaped(&self.value(component, "trend", context)) ) } fn timeline(&self, component: &Map, context: &Value) -> String { let events = bound_value_at(component.get("events"), &self.surface.data, context); let mut output = format!( "
{}", escaped(&self.value(component, "title", context)) ); for event in events.as_array().into_iter().flatten() { let _ = write!( output, "
●
{}
{} · {} · {}
", escaped(&event.get("title").map(display_value).unwrap_or_default()), escaped(&event.get("time").map(display_value).unwrap_or_default()), escaped( &event .get("description") .map(display_value) .unwrap_or_default() ), escaped(&event.get("status").map(display_value).unwrap_or_default()) ); } output.push_str("
"); output } fn map(&self, component: &Map, context: &Value) -> String { let locations = bound_value_at(component.get("locations"), &self.surface.data, context); let mut output = format!( "
{}", escaped(&self.value(component, "title", context)) ); for location in locations.as_array().into_iter().flatten() { let _ = write!( output, "
⌖
{}
{}, {} · {}
", escaped(&location.get("label").map(display_value).unwrap_or_default()), escaped( &location .get("latitude") .map(display_value) .unwrap_or_default() ), escaped( &location .get("longitude") .map(display_value) .unwrap_or_default() ), escaped( &location .get("detail") .map(display_value) .unwrap_or_default() ) ); } output.push_str("
"); output } fn mind_map(&self, component: &Map, context: &Value) -> String { let nodes = bound_value_at(component.get("nodes"), &self.surface.data, context); let nodes = nodes.as_array().map(Vec::as_slice).unwrap_or(&[]); let mut output = format!( "
{}", escaped(&self.value(component, "title", context)) ); if let Some(root) = nodes.first() { output.push_str(&mind_node(nodes, root, 0, BTreeSet::new())); } output.push_str("
"); output } } fn mind_node(nodes: &[Value], node: &Value, depth: usize, mut seen: BTreeSet) -> String { let id = node.get("id").map(display_value).unwrap_or_default(); if !seen.insert(id) { return "
Cycle
".to_owned(); } let mut output = format!( "
{}{}
", depth * 18, if depth == 0 { "◆" } else { "↳" }, escaped(&node.get("label").map(display_value).unwrap_or_default()) ); for child in node .get("children") .and_then(Value::as_array) .into_iter() .flatten() { if let Some(child) = nodes .iter() .find(|candidate| candidate.get("id") == Some(child)) { output.push_str(&mind_node(nodes, child, depth + 1, seen.clone())); } } output } fn layout_style(component: &Map) -> String { let justify = match component.get("justify").and_then(Value::as_str) { Some("center") => "center", Some("end") => "flex-end", Some("spaceBetween") => "space-between", Some("spaceAround") => "space-around", Some("spaceEvenly") => "space-evenly", _ => "flex-start", }; let align = match component.get("align").and_then(Value::as_str) { Some("center") => "center", Some("end") => "flex-end", Some("stretch") => "stretch", _ => "flex-start", }; format!(" style=\"justify-content:{justify};align-items:{align}\"") } fn safe_markdown(markdown: &str) -> String { let options = Options::ENABLE_TABLES | Options::ENABLE_FOOTNOTES | Options::ENABLE_STRIKETHROUGH | Options::ENABLE_TASKLISTS; let parser = Parser::new_ext(markdown, options).scan(Vec::new(), |images, event| { Some(match event { Event::Html(html) | Event::InlineHtml(html) => Event::Text(html), Event::Start(tag @ Tag::Image { .. }) => { let embedded = matches!( &tag, Tag::Image { dest_url, .. } if dest_url.starts_with("data:image/") ); images.push(embedded); if embedded { Event::Start(tag) } else { Event::Text("[Image: ".into()) } } Event::End(TagEnd::Image) => { if images.pop().unwrap_or(false) { Event::End(TagEnd::Image) } else { Event::Text("]".into()) } } event => event, }) }); let mut output = String::new(); html::push_html(&mut output, parser); output } fn escaped(text: &str) -> String { let mut output = String::with_capacity(text.len()); for character in text.chars() { match character { '&' => output.push_str("&"), '<' => output.push_str("<"), '>' => output.push_str(">"), '"' => output.push_str("""), '\'' => output.push_str("'"), _ => output.push(character), } } output } fn icon(name: &str) -> &'static str { match name { "accountCircle" | "person" => "●", "add" => "+", "arrowBack" => "←", "arrowForward" => "→", "attachFile" => "⌕", "calendarToday" | "event" => "▣", "call" | "phone" => "☎", "camera" => "◉", "check" => "✓", "close" => "×", "delete" => "⌫", "download" => "⇩", "edit" => "✎", "error" => "⊗", "fastForward" => "≫", "favorite" => "♥", "favoriteOff" => "♡", "folder" => "▰", "help" => "?", "home" => "⌂", "info" => "ⓘ", "locationOn" => "⌖", "lock" => "▣", "lockOpen" => "□", "mail" => "✉", "menu" => "☰", "moreVert" => "⋮", "moreHoriz" => "…", "notifications" => "◈", "notificationsOff" => "◇", "pause" => "Ⅱ", "payment" => "¤", "photo" => "▧", "play" => "▶", "print" => "▤", "refresh" => "↻", "rewind" => "≪", "search" => "⌕", "send" => "➤", "settings" => "⚙", "share" => "↗", "shoppingCart" => "⌑", "skipNext" => "▸|", "skipPrevious" => "|◂", "star" => "★", "starHalf" | "starOff" => "☆", "stop" => "■", "upload" => "⇧", "visibility" => "◉", "visibilityOff" => "○", "volumeDown" => "◖", "volumeMute" => "◁", "volumeOff" => "×", "volumeUp" => "◀", "warning" => "⚠", _ => "•", } } #[cfg(test)] mod tests { use super::*; use serde_json::json; use std::collections::BTreeMap; #[test] fn static_svg_renderer_covers_the_complete_catalog() { let entries = [ json!({"id":"text","component":"Text","text":"Hello"}), json!({"id":"image","component":"Image","url":"https://example.com/a.png","description":"Preview"}), json!({"id":"icon","component":"Icon","name":"settings"}), json!({"id":"video","component":"Video","url":"https://example.com/a.mp4"}), json!({"id":"audio","component":"AudioPlayer","url":"https://example.com/a.mp3"}), json!({"id":"divider","component":"Divider"}), json!({"id":"row","component":"Row","children":[]}), json!({"id":"list","component":"List","children":[]}), json!({"id":"card","component":"Card","child":"text"}), json!({"id":"modal","component":"Modal","trigger":"text","content":"text"}), json!({"id":"tabs","component":"Tabs","tabs":[{"title":"One","child":"text"}]}), json!({"id":"button","component":"Button","child":"text"}), json!({"id":"field","component":"TextField","label":"Name","value":"Ada"}), json!({"id":"check","component":"CheckBox","label":"Ready","value":true}), json!({"id":"slider","component":"Slider","label":"Amount","value":4,"max":10}), json!({"id":"date","component":"DateTimeInput","label":"When","value":"2026-08-31"}), json!({"id":"choice","component":"ChoicePicker","label":"Pick","options":[{"label":"One","value":"one"}],"value":["one"]}), json!({"id":"chart","component":"Chart","title":"Chart","chartType":"heatmap","series":[{"label":"A","segments":[{"label":"B","value":2}]}]}), json!({"id":"table","component":"Table","title":"Table","columns":["A"],"rows":[["B"]]}), json!({"id":"metric","component":"Metric","label":"Count","value":"2"}), json!({"id":"timeline","component":"Timeline","title":"Timeline","events":[{"time":"Now","title":"Done"}]}), json!({"id":"map","component":"Map","title":"Map","locations":[{"label":"Here","latitude":1,"longitude":2}]}), json!({"id":"mind","component":"MindMap","title":"Mind","nodes":[{"id":"n","label":"Root","children":[]}]}), json!({"id":"form","component":"Form","title":"Form","children":[]}), ]; let ids = entries .iter() .filter_map(|entry| entry.get("id").and_then(Value::as_str)) .map(str::to_owned) .collect::>(); let mut components = entries .into_iter() .map(|entry| (entry["id"].as_str().unwrap().to_owned(), entry)) .collect::>(); components.insert( "root".into(), json!({"id":"root","component":"Column","children":ids}), ); let surface = Surface { id: "catalog".into(), catalog_id: super::super::CATALOG_ID.into(), surface_properties: json!({}), send_data_model: false, components, data: json!({}), owner_message_id: 1, }; let svg = surface_svg(&surface, &HashMap::new()).unwrap(); assert!(svg.starts_with("