use std::collections::{BTreeMap, HashMap};
use adk_core::{DRAW_TOOL_NAMES, Event};
use serde_json::{Map, Value};
use super::a2ui::DataModel;
use super::catalog::{Catalog, HomeNode};
use super::draw::{DRAWN_STATE_KEY, DrawnEnvelope, DrawnRecord, REFUSED_STATE_KEY};
use super::drawn::{
DrawnAccessibility, DrawnActionView, DrawnArg, DrawnCheck, DrawnExtension, DrawnField,
DrawnInput, DrawnKind, DrawnNode, DrawnOption, DrawnSurface, RefusalKind, RefusedDrawing,
};
use super::scene::ScenePanel;
use super::validate::{
ActionArg, Child, DrawRefusal, ExtensionView, ValidatedSurface, Validator, children,
};
use crate::model::ThreadItem;
use crate::project::Projector;
use crate::words;
pub const SHOWN_ROWS: usize = 10;
pub(crate) fn project(
projector: &Projector,
event: &Event,
tool: &str,
response: &Value,
call_id: Option<&String>,
) -> Option<ThreadItem> {
#[cfg(feature = "genui-apps")]
if let Some(item) = super::mcp_apps::project(projector, event, response, call_id) {
return Some(item);
}
if !DRAW_TOOL_NAMES.contains(&tool) {
return None;
}
if response.get("error").is_some() {
let refusal = event.actions.state_delta.get(REFUSED_STATE_KEY)?;
let refusal: DrawRefusal = serde_json::from_value(refusal.clone()).ok()?;
return Some(refused(projector, event, tool, call_id, &refusal));
}
let Some(envelope) = proven(event, response) else {
tracing::debug!(
tool.name = %tool,
"a render tool's result without its drawing's record is shown as a tool result"
);
return None;
};
Some(match drawn(projector, event, call_id, &envelope) {
Ok(surface) => ThreadItem::Drawn { surface: Box::new(surface) },
Err(refusal) => refused(projector, event, tool, call_id, &refusal),
})
}
pub(crate) fn proven(event: &Event, response: &Value) -> Option<DrawnEnvelope> {
let record = event.actions.state_delta.get(DRAWN_STATE_KEY)?;
let record: DrawnRecord = serde_json::from_value(record.clone()).ok()?;
let envelope = DrawnEnvelope::from_response(response)?;
(record.surface_id == envelope.surface_id
&& record.version == envelope.version
&& record.digest == envelope.digest())
.then_some(envelope)
}
fn drawn(
projector: &Projector,
event: &Event,
call_id: Option<&String>,
envelope: &DrawnEnvelope,
) -> Result<DrawnSurface, DrawRefusal> {
let known = projector.catalogs.get(&envelope.catalog_id);
let catalog = known.unwrap_or_else(Catalog::forms);
let agent = projector.agent(&event.author);
let at = projector.hh_mm(event.timestamp);
let asks = |envelope: &DrawnEnvelope| {
!envelope.actions.is_empty()
|| envelope.draft().is_some_and(|draft| {
draft.components.iter().any(|component| {
component.get("component").and_then(Value::as_str).is_some_and(|name| {
matches!(
name,
"TextField" | "CheckBox" | "ChoicePicker" | "Slider" | "DateTimeInput"
)
})
})
})
};
let changed_text = match (envelope.deleted, envelope.kind) {
(true, _) => words::removed_this(&agent.display_name, &at),
(false, DrawnKind::Form) => words::changed_this_form(&agent.display_name, &at),
(false, DrawnKind::Screen) if asks(envelope) => {
words::changed_this_form(&agent.display_name, &at)
}
(false, _) => words::changed_this(&agent.display_name, &at),
};
let mut surface = DrawnSurface {
surface_id: envelope.surface_id.clone(),
version: envelope.version,
kind: envelope.kind,
catalog: catalog.label(),
drawn_text: words::drawn_by(&agent.display_name, &at),
changed_text,
earlier_text: words::earlier_version(&at),
agent,
drawn_at: event.timestamp,
at_text: at.clone(),
title: None,
root: None,
actions: Vec::new(),
deleted: envelope.deleted,
call_id: call_id.cloned(),
};
if envelope.deleted {
return Ok(surface);
}
let unknown = || DrawRefusal {
kind: RefusalKind::CannotShow,
catalog: catalog.label(),
component: None,
detail: format!("this home does not render the catalog {}", envelope.catalog_id),
};
if known.is_none() {
return Err(unknown());
}
let draft = envelope.draft().ok_or_else(|| DrawRefusal {
kind: RefusalKind::CannotShow,
catalog: catalog.label(),
component: None,
detail: "its messages do not draw one surface".to_string(),
})?;
let validated = Validator::new(catalog).validate(&draft)?;
surface.title = validated.draft.title.clone();
surface.actions = validated
.draft
.actions
.iter()
.map(|action| DrawnActionView {
id: action.id.clone(),
label: action.label.clone(),
tool: action.tool.clone(),
note: words::sending_runs(&action.tool),
args: action
.args
.iter()
.map(|(name, arg)| match arg {
ActionArg::Field(field) => {
DrawnArg { name: name.clone(), path: Some(field.path.clone()), value: None }
}
ActionArg::Constant(value) => {
DrawnArg { name: name.clone(), path: None, value: Some(value.clone()) }
}
})
.collect(),
})
.collect();
surface.root = Some(tree(catalog, &validated));
Ok(surface)
}
fn refused(
projector: &Projector,
event: &Event,
tool: &str,
call_id: Option<&String>,
refusal: &DrawRefusal,
) -> ThreadItem {
let agent = projector.agent(&event.author);
let text = match refusal.kind {
RefusalKind::LooksLikeDecision => words::looked_like_a_decision(&agent.display_name),
RefusalKind::AsksForSecret => words::asked_for_a_password(&agent.display_name),
RefusalKind::CannotShow => words::cant_show_here(&agent.display_name),
};
ThreadItem::DrawnRefused {
refusal: RefusedDrawing {
agent,
kind: refusal.kind,
text,
reason: refusal.technical(),
component: refusal.component.as_ref().map(|component| component.id.clone()),
at: event.timestamp,
at_text: event
.timestamp
.with_timezone(&projector.offset)
.format("%H:%M:%S")
.to_string(),
tool: tool.to_string(),
call_id: call_id.cloned(),
},
}
}
type Texts<'a> = HashMap<(&'a str, &'a str, &'a str), &'a str>;
pub(crate) fn tree(catalog: &'static Catalog, validated: &ValidatedSurface) -> DrawnNode {
drawn_tree(catalog, validated)
}
pub fn drawn_tree(catalog: &'static Catalog, validated: &ValidatedSurface) -> DrawnNode {
let mut texts: Texts<'_> = HashMap::new();
for text in &validated.texts {
if let Some(id) = &text.component_id {
texts
.entry((id.as_str(), text.item.as_deref().unwrap_or(""), text.field.as_str()))
.or_insert(text.text.as_str());
}
}
let components: HashMap<&str, (&str, &Map<String, Value>)> = validated
.draft
.components
.iter()
.filter_map(|value| {
let props = value.as_object()?;
Some((props.get("id")?.as_str()?, (props.get("component")?.as_str()?, props)))
})
.collect();
let builder = Builder {
catalog,
extensions: &validated.extensions,
texts,
components,
model: DataModel::new(validated.draft.data_model.clone()),
title: validated.draft.title.as_deref(),
};
builder.node("root", "", 0).unwrap_or_else(|| DrawnNode {
key: "root".to_string(),
component: "Column".to_string(),
text: None,
variant: None,
field: None,
action: None,
tabs: Vec::new(),
show_all_text: None,
children: Vec::new(),
chart: None,
scene: None,
depth: None,
extension: None,
accessibility: None,
})
}
struct Builder<'a> {
catalog: &'static Catalog,
extensions: &'a BTreeMap<(String, String), ExtensionView>,
texts: Texts<'a>,
components: HashMap<&'a str, (&'a str, &'a Map<String, Value>)>,
model: DataModel,
title: Option<&'a str>,
}
impl Builder<'_> {
fn text(&self, id: &str, scope: &str, field: &str) -> Option<String> {
self.texts.get(&(id, scope, field)).map(|text| (*text).to_string())
}
fn accessibility(
&self,
id: &str,
scope: &str,
props: &Map<String, Value>,
) -> Option<DrawnAccessibility> {
let attributes = props.get("accessibility")?.as_object()?;
let hidden = match attributes.get("hidden") {
Some(Value::Bool(hidden)) => *hidden,
Some(Value::Object(binding)) => binding
.get("path")
.and_then(Value::as_str)
.and_then(|path| self.model.get(&Self::absolute(scope, path)))
.and_then(Value::as_bool)
.unwrap_or(false),
_ => false,
};
let drawn = DrawnAccessibility {
label: self.text(id, scope, "accessibility/label"),
description: self.text(id, scope, "accessibility/description"),
live: attributes
.get("live")
.and_then(Value::as_str)
.filter(|live| matches!(*live, "polite" | "assertive"))
.map(str::to_string),
hidden,
};
(drawn != DrawnAccessibility::default()).then_some(drawn)
}
fn absolute(scope: &str, path: &str) -> String {
if path.starts_with('/') { path.to_string() } else { format!("{scope}/{path}") }
}
fn node(&self, id: &str, scope: &str, depth: usize) -> Option<DrawnNode> {
if depth > super::validate::MAX_DEPTH + 1 {
return None;
}
let (component, props) = *self.components.get(id)?;
let key = if scope.is_empty() { id.to_string() } else { format!("{id}@{scope}") };
let string = |name: &str| props.get(name).and_then(Value::as_str).map(str::to_string);
let mut node = DrawnNode {
key,
component: component.to_string(),
text: None,
variant: None,
field: None,
action: None,
tabs: Vec::new(),
show_all_text: None,
children: Vec::new(),
chart: None,
scene: None,
depth: None,
extension: None,
accessibility: None,
};
node.accessibility = self.accessibility(id, scope, props);
if let Some(view) = self.extensions.get(&(id.to_string(), scope.to_string())) {
let keys: Vec<String> = view
.children
.iter()
.map(
|child| {
if scope.is_empty() { child.clone() } else { format!("{child}@{scope}") }
},
)
.collect();
let mut held: Vec<Option<DrawnNode>> =
view.children.iter().map(|child| self.node(child, scope, depth + 1)).collect();
let mut parts = 0;
node.children.extend(Self::home(&view.home, &node.key, &mut parts, &mut held));
node.extension = Some(DrawnExtension {
catalog: self.catalog.id().to_string(),
component: view.component.clone(),
props: view.props.clone(),
children: keys,
});
return Some(node);
}
match component {
"Text" => {
node.text = self.text(id, scope, "text");
node.variant = string("variant");
}
"Row" | "Column" => node.variant = string("justify"),
"List" => node.variant = string("direction"),
"Divider" => node.variant = string("axis"),
"Button" => {
node.variant = string("variant");
node.action = props
.get("action")
.and_then(|action| action.get("event"))
.and_then(|event| event.get("name"))
.and_then(Value::as_str)
.map(str::to_string);
if let Some(child) = props.get("child").and_then(Value::as_str)
&& let Some(("Text", _)) = self.components.get(child)
{
node.text = self.text(child, scope, "text");
return Some(node);
}
}
"Tabs" => {
node.tabs = props
.get("tabs")
.and_then(Value::as_array)
.into_iter()
.flatten()
.enumerate()
.map(|(index, _)| {
self.text(id, scope, &format!("tabs/{index}/title")).unwrap_or_default()
})
.collect();
}
"TextField" | "DateTimeInput" | "CheckBox" | "ChoicePicker" | "Slider" => {
node.text = self.text(id, scope, "label");
node.variant = string("variant");
node.field = self.field(id, component, props, scope);
}
_ => {}
}
for child in children(self.catalog, component, props) {
match child {
Child::Id(child) => {
if child == "title"
&& scope.is_empty()
&& self.title.is_some()
&& self.text(child, "", "text").as_deref() == self.title
{
continue;
}
node.children.extend(self.node(child, scope, depth + 1));
}
Child::Template { component: child, list } => {
let list = Self::absolute(scope, list);
let items = self.model.get(&list).and_then(Value::as_array).map_or(0, Vec::len);
for index in 0..items {
node.children.extend(self.node(
child,
&format!("{list}/{index}"),
depth + 1,
));
}
if items > SHOWN_ROWS {
node.show_all_text = Some(words::show_all(items));
}
}
}
}
Some(node)
}
fn home(
view: &HomeNode,
key: &str,
parts: &mut usize,
held: &mut [Option<DrawnNode>],
) -> Option<DrawnNode> {
if let HomeNode::Child(index) = view {
return held.get_mut(*index).and_then(Option::take);
}
*parts += 1;
let mut node = DrawnNode {
key: format!("{key}#{parts}"),
component: String::new(),
text: None,
variant: None,
field: None,
action: None,
tabs: Vec::new(),
show_all_text: None,
children: Vec::new(),
chart: None,
scene: None,
depth: None,
extension: None,
accessibility: None,
};
let mut nested = |children: &[HomeNode], parts: &mut usize| -> Vec<DrawnNode> {
children.iter().filter_map(|child| Self::home(child, key, parts, held)).collect()
};
match view {
HomeNode::Text { text, variant } => {
node.component = "Text".to_string();
node.text = Some(text.clone());
node.variant = Some(variant.variant().to_string());
}
HomeNode::Row(children) => {
node.component = "Row".to_string();
node.children = nested(children, parts);
}
HomeNode::Column(children) => {
node.component = "Column".to_string();
node.children = nested(children, parts);
}
HomeNode::Card(children) => {
node.component = "Card".to_string();
node.children = nested(children, parts);
}
HomeNode::Tabs(tabs) => {
node.component = "Tabs".to_string();
node.tabs = tabs.iter().map(|(title, _)| title.clone()).collect();
let views: Vec<HomeNode> = tabs.iter().map(|(_, view)| view.clone()).collect();
node.children = nested(&views, parts);
}
HomeNode::Divider => node.component = "Divider".to_string(),
HomeNode::Chart(chart) => {
node.component = "Chart".to_string();
node.chart = Some(chart.clone());
}
HomeNode::Scene(home) => {
node.component = "Scene".to_string();
let mut scene = home.scene.clone();
for panel in &home.panels {
if let Some(child) = Self::home(&panel.content, key, parts, held) {
scene.panels.push(ScenePanel {
anchor: panel.anchor.clone(),
key: child.key.clone(),
});
node.children.push(child);
}
}
if let Some(text) = &home.text
&& let Some(child) = Self::home(text, key, parts, held)
{
scene.text_view = Some(child.key.clone());
node.children.push(child);
}
node.scene = Some(scene);
}
HomeNode::Depth { depth, children } => {
node.component = "Depth".to_string();
node.depth = Some(depth.clone());
node.children = nested(children, parts);
}
HomeNode::Child(_) => return None,
}
Some(node)
}
fn field(
&self,
id: &str,
component: &str,
props: &Map<String, Value>,
scope: &str,
) -> Option<DrawnField> {
let path = props.get("value")?.get("path")?.as_str()?;
let path = Self::absolute(scope, path);
let flag = |name: &str| props.get(name).and_then(Value::as_bool).unwrap_or(false);
let input = match component {
"TextField" => match props.get("variant").and_then(Value::as_str) {
Some("longText") => DrawnInput::LongText,
Some("number") => DrawnInput::Number,
_ => DrawnInput::ShortText,
},
"DateTimeInput" => match (flag("enableDate"), flag("enableTime")) {
(true, true) => DrawnInput::DateTime,
(false, true) => DrawnInput::Time,
_ => DrawnInput::Date,
},
"CheckBox" => DrawnInput::Checkbox,
"ChoicePicker" => match props.get("variant").and_then(Value::as_str) {
Some("multipleSelection") => DrawnInput::Choices,
_ => DrawnInput::Choice,
},
_ => DrawnInput::Slider,
};
let value = self.model.get(&path).cloned().unwrap_or_else(|| match input {
DrawnInput::Checkbox => Value::Bool(false),
DrawnInput::Choice | DrawnInput::Choices => Value::Array(Vec::new()),
DrawnInput::Number | DrawnInput::Slider => Value::Null,
_ => Value::String(String::new()),
});
let options = props
.get("options")
.and_then(Value::as_array)
.into_iter()
.flatten()
.enumerate()
.filter_map(|(index, option)| {
Some(DrawnOption {
label: self.text(id, scope, &format!("options/{index}/label"))?,
value: option.get("value")?.as_str()?.to_string(),
})
})
.collect();
let bound = |name: &str| {
props.get(name).filter(|value| value.is_number() || value.is_string()).cloned()
};
let (min, max) = match input {
DrawnInput::Slider => (Some(bound("min").unwrap_or(Value::from(0))), bound("max")),
_ => (bound("min"), bound("max")),
};
let checks = props
.get("checks")
.and_then(Value::as_array)
.into_iter()
.flatten()
.enumerate()
.filter_map(|(index, check)| {
Some(DrawnCheck {
condition: scoped(check.get("condition")?, scope),
message: self.text(id, scope, &format!("checks/{index}/message"))?,
})
})
.collect();
Some(DrawnField { path, input, value, options, min, max, checks })
}
}
fn scoped(condition: &Value, scope: &str) -> Value {
match condition {
Value::Object(map) => Value::Object(
map.iter()
.map(|(key, value)| match (key.as_str(), value) {
("path", Value::String(path)) if !path.starts_with('/') => {
(key.clone(), Value::String(Builder::absolute(scope, path)))
}
_ => (key.clone(), scoped(value, scope)),
})
.collect(),
),
Value::Array(items) => Value::Array(items.iter().map(|item| scoped(item, scope)).collect()),
other => other.clone(),
}
}