use std::collections::HashSet;
use adk_core::{AdkError, ErrorCategory, ErrorComponent, Result, TOOL_INVALID_ARGUMENTS_CODE};
use serde_json::{Map, Value};
const MAX_REPORTED_VIOLATIONS: usize = 8;
const MAX_VIOLATION_CHARS: usize = 200;
pub const TOOL_INVALID_PARAMETERS_SCHEMA_CODE: &str = "tool.invalid_parameters_schema";
pub struct ToolArgumentValidator {
validator: jsonschema::Validator,
}
impl std::fmt::Debug for ToolArgumentValidator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ToolArgumentValidator").finish_non_exhaustive()
}
}
impl ToolArgumentValidator {
pub fn new(schema: &Value) -> Result<Self> {
let validator = jsonschema::validator_for(schema).map_err(|error| {
AdkError::new(
ErrorComponent::Tool,
ErrorCategory::Internal,
TOOL_INVALID_PARAMETERS_SCHEMA_CODE,
format!(
"the tool's parameters schema does not compile: {error}. Fix the schema the \
tool declares."
),
)
})?;
Ok(Self { validator })
}
pub fn validate(&self, tool_name: &str, args: &Value) -> Result<()> {
let mut violations = Vec::new();
let mut total = 0usize;
for error in self.validator.iter_errors(args) {
total += 1;
if violations.len() == MAX_REPORTED_VIOLATIONS {
continue;
}
let path = error.instance_path().as_str();
let location = if path.is_empty() { "arguments" } else { path };
violations.push(format!("{location}: {}", truncate(&error.to_string())));
}
if total == 0 {
return Ok(());
}
if total > violations.len() {
violations.push(format!("and {} more", total - violations.len()));
}
Err(AdkError::new(
ErrorComponent::Tool,
ErrorCategory::InvalidInput,
TOOL_INVALID_ARGUMENTS_CODE,
format!(
"invalid arguments for '{tool_name}': {}. Correct the arguments to match the \
tool's parameters schema and call it again.",
violations.join("; ")
),
))
}
}
fn truncate(message: &str) -> String {
match message.char_indices().nth(MAX_VIOLATION_CHARS) {
Some((end, _)) => format!("{}…", &message[..end]),
None => message.to_string(),
}
}
pub(crate) struct ArgumentCheck {
schema: Value,
validator: std::result::Result<ToolArgumentValidator, String>,
}
impl ArgumentCheck {
pub(crate) fn for_type<T: schemars::JsonSchema>() -> Self {
let schema = serde_json::to_value(schemars::schema_for!(T)).unwrap_or_default();
let mut closed = schema.clone();
close_object_schemas(&mut closed);
let validator = ToolArgumentValidator::new(&closed).map_err(|error| error.message);
Self { schema, validator }
}
pub(crate) fn apply(&self, tool_name: &str, lenient: bool, args: &mut Value) -> Result<()> {
coerce_integral_floats(&self.schema, args);
if lenient {
return Ok(());
}
match &self.validator {
Ok(validator) => validator.validate(tool_name, args),
Err(message) => Err(AdkError::new(
ErrorComponent::Tool,
ErrorCategory::Internal,
TOOL_INVALID_PARAMETERS_SCHEMA_CODE,
message.clone(),
)),
}
}
}
const EXACT_F64_INTEGERS: f64 = 9_007_199_254_740_992.0;
pub fn coerce_integral_floats(schema: &Value, args: &mut Value) {
coerce_at(schema, schema, args, 0);
}
fn coerce_at(root: &Value, schema: &Value, value: &mut Value, depth: usize) {
if depth > 64 {
return;
}
let Some(obj) = schema.as_object() else {
return;
};
if let Some(target) = obj
.get("$ref")
.and_then(Value::as_str)
.and_then(|reference| reference.strip_prefix('#'))
.and_then(|pointer| root.pointer(pointer))
{
coerce_at(root, target, value, depth + 1);
}
let integer_only = match obj.get("type") {
Some(Value::String(kind)) => kind == "integer",
Some(Value::Array(kinds)) => {
kinds.iter().any(|kind| kind == "integer") && !kinds.iter().any(|kind| kind == "number")
}
_ => false,
};
match value {
Value::Number(number) if integer_only && number.is_f64() => {
if let Some(float) = number.as_f64()
&& float.fract() == 0.0
&& float.abs() < EXACT_F64_INTEGERS
{
*value =
if float < 0.0 { Value::from(float as i64) } else { Value::from(float as u64) };
}
}
Value::Object(members) => {
let properties = obj.get("properties").and_then(Value::as_object);
for (name, member) in members.iter_mut() {
match properties.and_then(|properties| properties.get(name)) {
Some(sub) => coerce_at(root, sub, member, depth + 1),
None => {
if let Some(sub @ Value::Object(_)) = obj.get("additionalProperties") {
coerce_at(root, sub, member, depth + 1);
}
}
}
}
}
Value::Array(items) => {
let prefix =
obj.get("prefixItems").and_then(Value::as_array).map_or(&[][..], Vec::as_slice);
for (index, item) in items.iter_mut().enumerate() {
match prefix.get(index) {
Some(sub) => coerce_at(root, sub, item, depth + 1),
None => {
if let Some(sub @ Value::Object(_)) = obj.get("items") {
coerce_at(root, sub, item, depth + 1);
}
}
}
}
}
_ => {}
}
for keyword in ["allOf", "anyOf", "oneOf"] {
if let Some(Value::Array(branches)) = obj.get(keyword) {
for branch in branches {
coerce_at(root, branch, value, depth + 1);
}
}
}
}
pub fn close_object_schemas(schema: &mut Value) {
let mut partial_definitions = HashSet::new();
collect_all_of_references(schema, &mut partial_definitions);
close(schema, true, &partial_definitions);
}
const COMBINATORS: [&str; 3] = ["allOf", "anyOf", "oneOf"];
fn close(schema: &mut Value, complete: bool, partial_definitions: &HashSet<String>) {
let Some(obj) = schema.as_object_mut() else {
return;
};
let has_properties = obj.get("properties").is_some_and(Value::is_object);
let extendable = ["additionalProperties", "patternProperties", "unevaluatedProperties"]
.iter()
.chain(&["$ref", "if"])
.chain(&COMBINATORS)
.any(|keyword| obj.contains_key(*keyword));
if complete && has_properties && !extendable {
obj.insert("additionalProperties".to_string(), Value::Bool(false));
}
for keyword in ["properties", "patternProperties"] {
if let Some(Value::Object(children)) = obj.get_mut(keyword) {
children.values_mut().for_each(|child| close(child, true, partial_definitions));
}
}
for keyword in ["$defs", "definitions"] {
if let Some(Value::Object(definitions)) = obj.get_mut(keyword) {
for (name, definition) in definitions.iter_mut() {
close(definition, !partial_definitions.contains(name), partial_definitions);
}
}
}
for keyword in ["items", "additionalProperties", "unevaluatedProperties", "contains"] {
match obj.get_mut(keyword) {
Some(child @ Value::Object(_)) => close(child, true, partial_definitions),
Some(Value::Array(children)) => {
children.iter_mut().for_each(|child| close(child, true, partial_definitions));
}
_ => {}
}
}
if let Some(Value::Array(children)) = obj.get_mut("prefixItems") {
children.iter_mut().for_each(|child| close(child, true, partial_definitions));
}
for keyword in COMBINATORS {
let branch_complete = keyword != "allOf" && !has_properties;
if let Some(Value::Array(branches)) = obj.get_mut(keyword) {
for branch in branches {
close(branch, branch_complete, partial_definitions);
}
}
}
for keyword in ["not", "if", "then", "else"] {
if let Some(child) = obj.get_mut(keyword) {
close(child, false, partial_definitions);
}
}
if let Some(Value::Object(children)) = obj.get_mut("dependentSchemas") {
children.values_mut().for_each(|child| close(child, false, partial_definitions));
}
}
fn collect_all_of_references(schema: &Value, names: &mut HashSet<String>) {
match schema {
Value::Object(obj) => {
if let Some(Value::Array(branches)) = obj.get("allOf") {
for branch in branches {
if let Some(name) = branch.get("$ref").and_then(Value::as_str).and_then(|r| {
r.strip_prefix("#/$defs/").or_else(|| r.strip_prefix("#/definitions/"))
}) {
names.insert(name.to_string());
}
}
}
obj.values().for_each(|child| collect_all_of_references(child, names));
}
Value::Array(children) => {
children.iter().for_each(|child| collect_all_of_references(child, names));
}
_ => {}
}
}
pub fn model_facing_schema(mut schema: Value) -> Value {
if let Some(obj) = schema.as_object_mut() {
obj.remove("$schema");
obj.remove("title");
}
simplify_nullable(&mut schema);
schema
}
fn simplify_nullable(schema: &mut Value) {
match schema {
Value::Object(map) => {
if let Some(Value::Array(types)) = map.get("type") {
let non_null: Vec<_> =
types.iter().filter(|t| t.as_str() != Some("null")).cloned().collect();
if let [only] = non_null.as_slice() {
map.insert("type".to_string(), only.clone());
}
}
if let Some(Value::Array(any_of)) = map.remove("anyOf") {
merge_first_non_null(map, &any_of);
}
map.values_mut().for_each(simplify_nullable);
}
Value::Array(items) => items.iter_mut().for_each(simplify_nullable),
_ => {}
}
}
fn merge_first_non_null(map: &mut Map<String, Value>, any_of: &[Value]) {
let variant = any_of
.iter()
.filter_map(Value::as_object)
.find(|variant| variant.get("type").and_then(Value::as_str) != Some("null"));
if let Some(variant) = variant {
for (key, value) in variant {
map.insert(key.clone(), value.clone());
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn closed(mut schema: Value) -> Value {
close_object_schemas(&mut schema);
schema
}
#[test]
fn closes_complete_objects_and_their_definitions() {
let schema = closed(json!({
"type": "object",
"properties": {
"address": { "$ref": "#/$defs/Address", "description": "where" },
"tags": { "type": "array", "items": { "type": "object", "properties": { "k": {} } } }
},
"$defs": { "Address": { "type": "object", "properties": { "city": {} } } }
}));
assert_eq!(schema["additionalProperties"], false);
assert_eq!(schema["$defs"]["Address"]["additionalProperties"], false);
assert_eq!(schema["properties"]["tags"]["items"]["additionalProperties"], false);
assert!(schema["properties"]["address"].get("additionalProperties").is_none());
}
#[test]
fn leaves_declared_and_open_ended_objects_alone() {
let schema = closed(json!({
"type": "object",
"properties": {
"open": { "type": "object", "properties": { "a": {} }, "additionalProperties": true },
"map": { "type": "object", "additionalProperties": { "type": "string" } },
"free": { "type": "object" }
}
}));
assert_eq!(schema["properties"]["open"]["additionalProperties"], true);
assert_eq!(
schema["properties"]["map"]["additionalProperties"],
json!({ "type": "string" })
);
assert!(schema["properties"]["free"].get("additionalProperties").is_none());
}
#[test]
fn leaves_partial_schemas_open() {
let flattened = closed(json!({
"type": "object",
"properties": { "name": {} },
"oneOf": [
{ "type": "object", "properties": { "a": {} } },
{ "type": "object", "properties": { "b": {} } }
]
}));
assert!(flattened.get("additionalProperties").is_none());
assert!(flattened["oneOf"][0].get("additionalProperties").is_none());
let composed = closed(json!({
"allOf": [
{ "$ref": "#/$defs/Base" },
{ "type": "object", "properties": { "extra": {} } }
],
"if": { "properties": { "kind": { "const": "x" } } },
"$defs": { "Base": { "type": "object", "properties": { "id": {} } } }
}));
assert!(composed["allOf"][1].get("additionalProperties").is_none());
assert!(composed["if"].get("additionalProperties").is_none());
assert!(composed["$defs"]["Base"].get("additionalProperties").is_none());
}
#[test]
fn closes_whole_value_branches_of_a_tagged_enum() {
let schema = closed(json!({
"oneOf": [
{ "type": "object", "properties": { "kind": { "const": "a" }, "a": {} } },
{ "type": "null" }
]
}));
assert_eq!(schema["oneOf"][0]["additionalProperties"], false);
}
#[test]
fn validation_lists_every_violation_by_pointer() {
let validator = ToolArgumentValidator::new(&closed(json!({
"type": "object",
"properties": {
"count": { "type": "integer", "minimum": 1, "maximum": 10 },
"room": { "type": "string", "pattern": "^[A-Z][0-9]$" },
"address": { "$ref": "#/$defs/Address" },
"note": { "type": ["string", "null"] }
},
"required": ["count", "room"],
"$defs": { "Address": { "type": "object", "properties": { "city": { "type": "string" } } } }
})))
.unwrap();
assert!(
validator.validate("book", &json!({ "count": 2, "room": "A1", "note": null })).is_ok()
);
let err = validator
.validate(
"book",
&json!({ "count": 50, "room": "lobby", "dry_run": true, "address": { "zip": "1" } }),
)
.unwrap_err();
assert_eq!(err.code, TOOL_INVALID_ARGUMENTS_CODE);
assert_eq!(err.category, ErrorCategory::InvalidInput);
for expected in
["'book'", "/count: 50", "/room:", "arguments: ", "dry_run", "/address:", "zip"]
{
assert!(err.message.contains(expected), "{expected} missing from {}", err.message);
}
}
#[test]
fn long_violation_lists_are_capped() {
let properties: Map<String, Value> =
(0..12).map(|i| (format!("f{i}"), json!({ "type": "integer" }))).collect();
let validator =
ToolArgumentValidator::new(&json!({ "type": "object", "properties": properties }))
.unwrap();
let args: Map<String, Value> =
(0..12).map(|i| (format!("f{i}"), json!("x".repeat(500)))).collect();
let err = validator.validate("t", &Value::Object(args)).unwrap_err();
assert!(err.message.contains("and 4 more"), "{}", err.message);
assert!(err.message.len() < 8 * (MAX_VIOLATION_CHARS + 16) + 200, "{}", err.message);
}
#[test]
fn an_invalid_schema_is_reported() {
let err = ToolArgumentValidator::new(&json!({ "type": 12 })).unwrap_err();
assert_eq!(err.code, TOOL_INVALID_PARAMETERS_SCHEMA_CODE);
}
#[test]
fn model_facing_schema_reduces_nullable_types() {
let schema = model_facing_schema(json!({
"$schema": "x",
"title": "T",
"properties": {
"a": { "anyOf": [{ "$ref": "#/$defs/A" }, { "type": "null" }], "description": "d" },
"b": { "type": ["integer", "null"] }
}
}));
assert_eq!(
schema,
json!({
"properties": {
"a": { "$ref": "#/$defs/A", "description": "d" },
"b": { "type": "integer" }
}
})
);
}
#[test]
fn integral_floats_become_integers_only_where_the_schema_says_integer() {
let schema = json!({
"type": "object",
"properties": {
"count": { "type": "integer" },
"maybe": { "type": ["integer", "null"] },
"ratio": { "type": "number" },
"seats": { "type": "array", "items": { "$ref": "#/$defs/Seat" } },
"pick": { "anyOf": [{ "type": "integer" }, { "type": "string" }] },
"extra": { "type": "object", "additionalProperties": { "type": "integer" } }
},
"$defs": {
"Seat": { "type": "object", "properties": { "row": { "type": "integer" } } }
}
});
let mut args = json!({
"count": 3.0,
"maybe": -2.0,
"ratio": 2.0,
"seats": [{ "row": 7.0 }, { "row": 7.5 }],
"pick": 4.0,
"extra": { "a": 1.0 },
"undeclared": 5.0
});
coerce_integral_floats(&schema, &mut args);
assert_eq!(
args,
json!({
"count": 3,
"maybe": -2,
"ratio": 2.0,
"seats": [{ "row": 7 }, { "row": 7.5 }],
"pick": 4,
"extra": { "a": 1 },
"undeclared": 5.0
})
);
}
#[test]
fn floats_beyond_exact_integers_are_left_alone() {
let schema = json!({ "type": "integer" });
let mut args = json!(1.0e300);
coerce_integral_floats(&schema, &mut args);
assert_eq!(args, json!(1.0e300));
}
}