use gadk_agent::go_quote;
use std::sync::Arc;
use gadk_genai::{Content, FunctionCall, FunctionResponse, Schema};
use gadk_platform::Platform;
use schemars::generate::SchemaSettings;
use schemars::transform::RecursiveTransform;
use serde_json::{Map, Value};
use crate::llm::processors::{as_go_floats, go_sprint};
pub(crate) const AF_FUNCTION_CALL_ID_PREFIX: &str = "adk-";
pub(crate) fn schema_expects_object(schema: &Value) -> bool {
match schema.get("type") {
Some(Value::String(t)) => t == "object",
Some(Value::Array(ts)) => ts.iter().any(|t| t == "object"),
_ => false,
}
}
pub(crate) fn convert_with_schema<In: serde::Serialize, Out: serde::de::DeserializeOwned>(
v: &In,
schema: Option<&CompiledSchema>,
) -> Result<Out, String> {
let mut decoded = serde_json::to_value(v).map_err(|e| e.to_string())?;
if let Some(s) = schema {
if decoded.is_null() && schema_expects_object(s.schema()) {
decoded = Value::Object(Map::new());
}
s.validate(&decoded)?;
}
serde_json::from_value(decoded).map_err(|e| e.to_string())
}
pub(crate) fn convert_args_with_schema<Out: serde::de::DeserializeOwned>(
args: &Value,
schema: Option<&CompiledSchema>,
) -> Result<Out, String> {
convert_with_schema(&go_marshaled_numbers(args.clone()), schema)
}
fn go_marshaled_numbers(v: Value) -> Value {
const GO_EXPONENT_FROM: f64 = 1e21;
match v {
Value::Number(n) if !n.is_i64() && !n.is_u64() => match n.as_f64() {
Some(f) if f.fract() == 0.0 && f.abs() < GO_EXPONENT_FROM => {
if f >= i64::MIN as f64 && f < i64::MAX as f64 {
#[allow(clippy::cast_possible_truncation)] Value::from(f as i64)
} else if f >= 0.0 && f < u64::MAX as f64 {
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
Value::from(f as u64)
} else {
Value::Number(n)
}
}
_ => Value::Number(n),
},
Value::Array(items) => Value::Array(items.into_iter().map(go_marshaled_numbers).collect()),
Value::Object(m) => Value::Object(
m.into_iter()
.map(|(k, v)| (k, go_marshaled_numbers(v)))
.collect(),
),
other => other,
}
}
pub(crate) fn validate_with_schema<In: serde::Serialize>(
v: &In,
schema: Option<&CompiledSchema>,
) -> Result<(), String> {
let Some(s) = schema else { return Ok(()) };
let mut decoded = serde_json::to_value(v).map_err(|e| e.to_string())?;
if decoded.is_null() && schema_expects_object(s.schema()) {
decoded = Value::Object(Map::new());
}
s.validate(&decoded)
}
fn settings() -> SchemaSettings {
SchemaSettings::draft2020_12()
.with(|s| {
s.meta_schema = None; s.inline_subschemas = true; })
.with_transform(RecursiveTransform(
go_schema_shape as fn(&mut schemars::Schema),
))
}
fn go_schema_shape(schema: &mut schemars::Schema) {
let Some(obj) = schema.as_object_mut() else {
return;
};
let is_numeric = |t: &Value| t == "integer" || t == "number";
let numeric = match obj.get("type") {
Some(Value::Array(ts)) => ts.iter().any(is_numeric),
Some(t) => is_numeric(t),
None => false,
};
if numeric {
obj.remove("format");
}
let is_struct =
obj.contains_key("properties") || obj.get("type").is_some_and(|t| t == "object");
if is_struct && !obj.contains_key("additionalProperties") {
obj.insert("additionalProperties".to_string(), Value::Bool(false));
}
}
pub(crate) fn infer_schema<T: schemars::JsonSchema>() -> Value {
let mut schema = settings().into_generator().into_root_schema_for::<T>();
schema.remove("title");
schema.to_value()
}
pub(crate) fn infer_output_schema<T: schemars::JsonSchema>() -> Value {
let mut schema = settings()
.for_serialize()
.into_generator()
.into_root_schema_for::<T>();
schema.remove("title");
schema.to_value()
}
#[derive(Debug, Clone)]
pub(crate) struct CompiledSchema {
schema: Value,
validator: Arc<jsonschema::Validator>,
}
impl CompiledSchema {
pub(crate) fn new(schema: Value) -> Result<Self, String> {
let validator = jsonschema::validator_for(&schema).map_err(|e| e.to_string())?;
Ok(Self {
schema,
validator: Arc::new(validator),
})
}
pub(crate) fn schema(&self) -> &Value {
&self.schema
}
pub(crate) fn validate(&self, v: &Value) -> Result<(), String> {
self.validator.validate(v).map_err(|e| e.to_string())
}
}
const UNORDERED_ARRAY_KEYWORDS: [&str; 2] = ["required", "type"];
pub(crate) fn canonical_schema_json(schema: &Value) -> String {
canonicalize(schema)
}
pub(crate) fn canonicalize(v: &Value) -> String {
match v {
Value::Object(map) => {
let mut keys: Vec<&String> = map.keys().collect();
keys.sort();
let mut out = String::from("{");
for (i, k) in keys.into_iter().enumerate() {
if i > 0 {
out.push(',');
}
out.push_str(&encode_string(k));
out.push(':');
match &map[k] {
Value::Array(items) if UNORDERED_ARRAY_KEYWORDS.contains(&k.as_str()) => {
out.push_str(&canonicalize_unordered_array(items));
}
other => out.push_str(&canonicalize(other)),
}
}
out.push('}');
out
}
Value::Array(items) => {
let encoded: Vec<String> = items.iter().map(canonicalize).collect();
format!("[{}]", encoded.join(","))
}
Value::Number(n) => encode_number(n),
Value::String(s) => encode_string(s),
Value::Bool(b) => b.to_string(),
Value::Null => "null".to_string(),
}
}
fn canonicalize_unordered_array(items: &[Value]) -> String {
let mut encoded: Vec<String> = items.iter().map(canonicalize).collect();
encoded.sort();
format!("[{}]", encoded.join(","))
}
fn encode_string(s: &str) -> String {
Value::String(s.to_string()).to_string()
}
fn encode_number(n: &serde_json::Number) -> String {
if n.is_i64() || n.is_u64() {
return n.to_string();
}
match n.as_f64() {
Some(f) if f.fract() == 0.0 && f.abs() < 1e21 => format!("{f:.0}"),
_ => n.to_string(),
}
}
pub(crate) use gadk_platform::go_type_name;
fn type_str(schema: &Schema) -> &str {
schema.r#type.as_ref().map(|t| t.as_str()).unwrap_or("")
}
pub(crate) fn match_type(
value: &Value,
schema: Option<&Schema>,
is_input: bool,
) -> Result<bool, String> {
let Some(schema) = schema else {
return Err("schema is nil".to_string());
};
if value.is_null() {
return Ok(schema.nullable == Some(true));
}
match type_str(schema).to_uppercase().as_str() {
"STRING" => Ok(value.is_string()),
"INTEGER" => Ok(value.as_f64().is_some_and(|f| f == f.trunc())),
"BOOLEAN" => Ok(value.is_boolean()),
"NUMBER" => Ok(value.is_number()),
"ARRAY" => {
let Value::Array(items) = value else {
return Ok(false);
};
let Some(item_schema) = schema.items.as_deref() else {
return Err("array schema missing items definition".to_string());
};
for (i, item) in items.iter().enumerate() {
match match_type(item, Some(item_schema), is_input) {
Err(e) => return Err(format!("array item {i}: {e}")),
Ok(false) => return Ok(false),
Ok(true) => {}
}
}
Ok(true)
}
"OBJECT" => {
let Value::Object(obj) = value else {
return Ok(false);
};
validate_map_on_schema(obj, schema, is_input).map(|()| true)
}
_ => Err(format!("unsupported type: {}", type_str(schema))),
}
}
pub(crate) fn validate_map_on_schema(
args: &Map<String, Value>,
schema: &Schema,
is_input: bool,
) -> Result<(), String> {
let arg_type = if is_input { "input" } else { "output" };
for (key, value) in args {
let Some(prop_schema) = schema.properties.get(key) else {
return Err(format!(
"{arg_type} arg: '{}' does not exist in schema properties",
go_quote(key)
));
};
match match_type(value, Some(prop_schema), is_input) {
Err(e) => {
return Err(format!(
"{arg_type} arg: '{}' validation failed: {e}",
go_quote(key)
));
}
Ok(false) => {
return Err(format!(
"{arg_type} arg: '{}' type mismatch, expected schema type {}, got value {} of type {}",
go_quote(key),
type_str(prop_schema),
go_sprint(&as_go_floats(value)),
go_type_name(value)
));
}
Ok(true) => {}
}
}
for required_key in &schema.required {
if !args.contains_key(required_key) {
return Err(format!(
"{} args does not contain required key: '{}'",
go_quote(arg_type),
go_quote(required_key)
));
}
}
Ok(())
}
pub(crate) fn validate_output_schema(
output: &str,
schema: &Schema,
) -> Result<Map<String, Value>, String> {
let parsed: Value =
serde_json::from_str(output).map_err(|e| format!("failed to parse output JSON: {e}"))?;
let map = match parsed {
Value::Object(m) => m,
Value::Null => Map::new(),
other => {
let kind = match other {
Value::Array(_) => "array",
Value::String(_) => "string",
Value::Number(_) => "number",
_ => "bool",
};
return Err(format!(
"failed to parse output JSON: json: cannot unmarshal {kind} into Go value of type map[string]interface {{}}"
));
}
};
validate_map_on_schema(&map, schema, false)?;
Ok(map)
}
pub(crate) fn populate_client_function_call_id(platform: &Platform, c: Option<&mut Content>) {
let Some(c) = c else { return };
for fc in c.parts.iter_mut().filter_map(|p| p.function_call.as_mut()) {
if fc.id.is_empty() {
fc.id = generate_function_call_id(platform);
}
}
}
pub(crate) fn generate_function_call_id(platform: &Platform) -> String {
format!("{AF_FUNCTION_CALL_ID_PREFIX}{}", platform.new_uuid())
}
pub(crate) fn remove_client_function_call_id(c: &mut Content) {
for p in &mut c.parts {
if let Some(fc) = p.function_call.as_mut()
&& fc.id.starts_with(AF_FUNCTION_CALL_ID_PREFIX)
{
fc.id.clear();
}
if let Some(fr) = p.function_response.as_mut()
&& fr.id.starts_with(AF_FUNCTION_CALL_ID_PREFIX)
{
fr.id.clear();
}
}
}
pub(crate) use gadk_session::compaction::content;
pub fn function_calls(c: Option<&Content>) -> Vec<&FunctionCall> {
c.map(|c| {
c.parts
.iter()
.filter_map(|p| p.function_call.as_ref())
.collect()
})
.unwrap_or_default()
}
pub(crate) fn function_responses(c: Option<&Content>) -> Vec<&FunctionResponse> {
c.map(|c| {
c.parts
.iter()
.filter_map(|p| p.function_response.as_ref())
.collect()
})
.unwrap_or_default()
}
pub(crate) fn text_parts(c: Option<&Content>) -> Vec<String> {
c.map(|c| {
c.parts
.iter()
.filter(|p| !p.text.is_empty())
.map(|p| p.text.clone())
.collect()
})
.unwrap_or_default()
}
#[allow(dead_code)] pub(crate) fn content_text(c: &Content) -> String {
text_parts(Some(c)).concat()
}
#[cfg(test)]
pub(crate) fn function_decls(
c: Option<&gadk_genai::GenerateContentConfig>,
) -> Vec<&gadk_genai::FunctionDeclaration> {
c.map(|c| {
c.tools
.iter()
.flat_map(|t| t.function_declarations.iter())
.collect()
})
.unwrap_or_default()
}
pub(crate) fn event_belongs_to_branch(invocation_branch: &str, event_branch: &str) -> bool {
invocation_branch.is_empty()
|| event_branch.is_empty()
|| event_branch == invocation_branch
|| invocation_branch.starts_with(&format!("{event_branch}."))
}
pub(crate) fn panic_message(payload: &(dyn std::any::Any + Send)) -> String {
if let Some(s) = payload.downcast_ref::<&'static str>() {
(*s).to_string()
} else if let Some(s) = payload.downcast_ref::<String>() {
s.clone()
} else {
"Box<dyn Any>".to_string()
}
}
pub(crate) fn model_error_text(tool_name: &str, err: &gadk_agent::ToolError) -> (String, bool) {
if chain_has_panic(err) {
return (
format!("Invoking `{tool_name}()` failed with an internal error."),
true,
);
}
(err.to_string(), false)
}
fn chain_has_panic(err: &(dyn std::error::Error + 'static)) -> bool {
use gadk_agent::{AgentError, SharedToolError, ToolError};
let mut cur: Option<&(dyn std::error::Error + 'static)> = Some(err);
while let Some(e) = cur {
cur = if let Some(te) = e.downcast_ref::<ToolError>() {
match te {
ToolError::Panic(_) => return true,
ToolError::Execution(inner) => Some(&**inner),
_ => std::error::Error::source(te),
}
} else if let Some(shared) = e.downcast_ref::<SharedToolError>() {
Some(&*shared.0)
} else if let Some(AgentError::Other(inner) | AgentError::Workflow(inner)) =
e.downcast_ref::<AgentError>()
{
Some(&**inner)
} else {
e.source()
};
}
false
}
#[cfg(test)]
mod tests {
use std::collections::BTreeMap;
use gadk_genai::Type;
use pretty_assertions::assert_eq;
use serde_json::json;
use super::*;
#[test]
fn type_mismatch_formats_value_like_go_percent_v() {
let schema = Schema {
r#type: Some(Type::Object),
properties: BTreeMap::from([(
"answer".to_owned(),
Schema {
r#type: Some(Type::Integer),
..Default::default()
},
)]),
..Default::default()
};
let check = |args: Value| {
let Value::Object(m) = args else {
unreachable!()
};
validate_map_on_schema(&m, &schema, false).unwrap_err()
};
assert_eq!(
check(json!({"answer": "42"})),
"output arg: '\"answer\"' type mismatch, expected schema type INTEGER, got value 42 of type string"
);
assert_eq!(
check(json!({"answer": {"a": 1, "b": 1_000_000}})),
"output arg: '\"answer\"' type mismatch, expected schema type INTEGER, got value map[a:1 b:1e+06] of type map[string]interface {}"
);
}
}