use serde_json::{Map, Value, json};
use super::catalog::Catalog;
use super::wire::{A2uiVersion, WireError};
pub const INVALID_FUNCTION_CALL: &str = "INVALID_FUNCTION_CALL";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FunctionCaller {
Agent,
Renderer,
}
#[derive(Debug, Clone, PartialEq)]
pub struct FunctionInvocation {
pub caller: FunctionCaller,
pub function_call_id: String,
pub surface_id: Option<String>,
pub name: String,
pub catalog_id: Option<String>,
pub args: Value,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FunctionError {
pub code: String,
pub message: String,
}
#[derive(Debug, Clone, PartialEq)]
pub struct FunctionResponse {
pub caller: FunctionCaller,
pub function_call_id: String,
pub outcome: Result<Value, FunctionError>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum FunctionMessage {
Call(FunctionInvocation),
Response(FunctionResponse),
}
pub(crate) const FUNCTION_MESSAGES: [&str; 4] = [
"callRendererFunction",
"callAgentFunction",
"rendererFunctionResponse",
"agentFunctionResponse",
];
impl FunctionInvocation {
pub fn refusal(&self, catalog: &Catalog) -> FunctionResponse {
let message = match self.caller {
FunctionCaller::Renderer => {
format!("this agent offers no functions to call; {} is not one", self.name)
}
FunctionCaller::Agent => {
let named = self.catalog_id.as_deref().unwrap_or_default();
if named != catalog.id() {
format!("{named} is not a catalog this renderer registers")
} else if catalog.functions().any(|function| function == self.name) {
format!(
"{} is rendererOnly in {named}: the renderer evaluates it while it draws, \
and an agent cannot call it",
self.name
)
} else {
format!("{} is not a function of {named}", self.name)
}
}
};
FunctionResponse {
caller: self.caller,
function_call_id: self.function_call_id.clone(),
outcome: Err(FunctionError { code: INVALID_FUNCTION_CALL.to_string(), message }),
}
}
pub fn to_wire(&self) -> Value {
let mut call = Map::new();
call.insert("@call".to_string(), json!(self.name));
if let Some(catalog_id) = &self.catalog_id {
call.insert("catalogId".to_string(), json!(catalog_id));
}
if self.args.as_object().is_some_and(|args| !args.is_empty()) {
call.insert("args".to_string(), self.args.clone());
}
let mut body = Map::new();
if let Some(surface_id) = &self.surface_id {
body.insert("surfaceId".to_string(), json!(surface_id));
}
body.insert("functionCallId".to_string(), json!(self.function_call_id));
body.insert("callFunction".to_string(), Value::Object(call));
let key = match self.caller {
FunctionCaller::Agent => "callRendererFunction",
FunctionCaller::Renderer => "callAgentFunction",
};
json!({ "version": A2uiVersion::V1_0.wire(), key: body })
}
}
impl FunctionResponse {
pub fn to_wire(&self) -> Value {
let mut body = Map::new();
body.insert("functionCallId".to_string(), json!(self.function_call_id));
match &self.outcome {
Ok(value) => body.insert("value".to_string(), value.clone()),
Err(error) => body.insert(
"error".to_string(),
json!({ "code": error.code, "message": error.message }),
),
};
let key = match self.caller {
FunctionCaller::Agent => "rendererFunctionResponse",
FunctionCaller::Renderer => "agentFunctionResponse",
};
json!({ "version": A2uiVersion::V1_0.wire(), key: body })
}
}
pub fn read_function_message(message: &Value) -> Result<Option<FunctionMessage>, WireError> {
let Some(kind) = FUNCTION_MESSAGES.into_iter().find(|kind| message.get(*kind).is_some()) else {
return Ok(None);
};
let version = message.get("version").and_then(Value::as_str);
if version != Some(A2uiVersion::V1_0.wire()) {
return Err(WireError::new(
"/version",
format!("{kind} is A2UI 1.0; this message says version {}", version.unwrap_or("none")),
));
}
let at = format!("/{kind}");
let body = message[kind]
.as_object()
.ok_or_else(|| WireError::new(&at, format!("{kind} is an object")))?;
let text = |key: &str| body.get(key).and_then(Value::as_str).map(str::to_string);
let function_call_id = text("functionCallId").ok_or_else(|| {
WireError::new(&format!("{at}/functionCallId"), "a functionCallId is required")
})?;
match kind {
"callRendererFunction" | "callAgentFunction" => {
let caller = if kind == "callRendererFunction" {
FunctionCaller::Agent
} else {
FunctionCaller::Renderer
};
let call_at = format!("{at}/callFunction");
let call = body
.get("callFunction")
.and_then(Value::as_object)
.ok_or_else(|| WireError::new(&call_at, "a callFunction object is required"))?;
let name = call
.get("@call")
.and_then(Value::as_str)
.ok_or_else(|| {
WireError::new(&format!("{call_at}/@call"), "@call names the function")
})?
.to_string();
let catalog_id = call.get("catalogId").and_then(Value::as_str).map(str::to_string);
if caller == FunctionCaller::Agent && catalog_id.is_none() {
return Err(WireError::new(
&format!("{call_at}/catalogId"),
"callRendererFunction names the catalog of its function",
));
}
let surface_id = text("surfaceId");
if caller == FunctionCaller::Renderer && surface_id.is_none() {
return Err(WireError::new(
&format!("{at}/surfaceId"),
"callAgentFunction names the surface it came from",
));
}
let args = call.get("args").cloned().unwrap_or_else(|| json!({}));
if !args.is_object() {
return Err(WireError::new(&format!("{call_at}/args"), "args is an object"));
}
Ok(Some(FunctionMessage::Call(FunctionInvocation {
caller,
function_call_id,
surface_id,
name,
catalog_id,
args,
})))
}
_ => {
let caller = if kind == "rendererFunctionResponse" {
FunctionCaller::Agent
} else {
FunctionCaller::Renderer
};
let outcome = match (body.get("value"), body.get("error")) {
(Some(value), None) => Ok(value.clone()),
(None, Some(error)) => {
let field = |key: &str| {
error.get(key).and_then(Value::as_str).map(str::to_string).ok_or_else(
|| {
WireError::new(
&format!("{at}/error/{key}"),
format!("an error has a {key}"),
)
},
)
};
Err(FunctionError { code: field("code")?, message: field("message")? })
}
_ => {
return Err(WireError::new(
&at,
"an answer has exactly one of value and error",
));
}
};
Ok(Some(FunctionMessage::Response(FunctionResponse {
caller,
function_call_id,
outcome,
})))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn call(message: Value) -> FunctionInvocation {
match read_function_message(&message).unwrap() {
Some(FunctionMessage::Call(call)) => call,
other => panic!("not a call: {other:?}"),
}
}
#[test]
fn every_call_is_answered_invalid_with_its_reason() {
let forms = Catalog::forms();
for (message, says) in [
(
json!({ "version": "1.0", "callRendererFunction": { "functionCallId": "a",
"callFunction": { "@call": "formatString", "catalogId": forms.id() } } }),
"rendererOnly",
),
(
json!({ "version": "1.0", "callRendererFunction": { "functionCallId": "b",
"callFunction": { "@call": "getScreenResolution", "catalogId": forms.id() } } }),
"not a function of",
),
(
json!({ "version": "1.0", "callRendererFunction": { "functionCallId": "c",
"callFunction": { "@call": "x", "catalogId": "https://x.test/c.json" } } }),
"not a catalog this renderer registers",
),
(
json!({ "version": "1.0", "callAgentFunction": { "surfaceId": "s", "functionCallId": "d",
"callFunction": { "@call": "lookupProvider", "args": { "id": 1 } } } }),
"offers no functions",
),
] {
let call = call(message.clone());
let answer = call.refusal(forms);
assert_eq!(answer.function_call_id, call.function_call_id);
let error = answer.outcome.as_ref().unwrap_err();
assert_eq!(error.code, INVALID_FUNCTION_CALL);
assert!(error.message.contains(says), "{message}: {}", error.message);
assert_eq!(
read_function_message(&answer.to_wire()).unwrap(),
Some(FunctionMessage::Response(answer))
);
assert_eq!(call.to_wire(), message, "a call writes back as read");
}
}
#[test]
fn what_1_0_requires_of_a_function_message_is_checked() {
for (message, says) in [
(json!({ "version": "v0.9.1", "callRendererFunction": {} }), "A2UI 1.0"),
(
json!({ "version": "1.0", "callRendererFunction": { "callFunction": {} } }),
"functionCallId",
),
(
json!({ "version": "1.0", "callRendererFunction": { "functionCallId": "a",
"callFunction": { "@call": "formatNumber" } } }),
"catalog",
),
(
json!({ "version": "1.0", "callAgentFunction": { "functionCallId": "a",
"callFunction": { "@call": "f" } } }),
"surface",
),
(
json!({ "version": "1.0", "agentFunctionResponse": { "functionCallId": "a" } }),
"exactly one",
),
(
json!({ "version": "1.0", "rendererFunctionResponse": { "functionCallId": "a",
"value": 1, "error": { "code": "X", "message": "y" } } }),
"exactly one",
),
(
json!({ "version": "1.0", "rendererFunctionResponse": { "functionCallId": "a",
"error": { "code": "X" } } }),
"message",
),
] {
let error = read_function_message(&message).unwrap_err();
assert!(error.to_string().contains(says), "{message}: {error}");
}
}
#[test]
fn a_success_answer_reads_and_writes() {
let answer = json!({ "version": "1.0", "rendererFunctionResponse": {
"functionCallId": "r", "value": [1920, 1080] } });
let Some(FunctionMessage::Response(read)) = read_function_message(&answer).unwrap() else {
panic!("an answer")
};
assert_eq!(read.caller, FunctionCaller::Agent);
assert_eq!(read.outcome, Ok(json!([1920, 1080])));
assert_eq!(read.to_wire(), answer);
}
}