pub use aidaemon_core;
pub use async_trait;
pub use schemars;
pub use serde;
pub use serde_json;
use aidaemon_core::{ToolError, ToolOutput, ToolResult};
use serde::de::DeserializeOwned;
use serde_json::Value;
pub fn parameters<T: schemars::JsonSchema>() -> Value {
crate::schema::tool_parameters::<T>()
}
pub fn parse_arguments<T: DeserializeOwned + schemars::JsonSchema>(
arguments: &Value,
) -> Result<T, String> {
let Some(object) = arguments.as_object() else {
return Err("arguments must be a JSON object".to_string());
};
let schema = parameters::<T>();
if let Some(properties) = schema.get("properties").and_then(Value::as_object) {
let unknown: Vec<&str> = object
.keys()
.filter(|key| !properties.contains_key(key.as_str()))
.map(String::as_str)
.collect();
if !unknown.is_empty() {
return Err(format!("unknown argument(s): {}", unknown.join(", ")));
}
}
serde_json::from_value(arguments.clone()).map_err(|err| format!("invalid arguments: {err}"))
}
pub trait IntoToolResult {
fn into_tool_result(self) -> ToolResult;
}
impl IntoToolResult for ToolOutput {
fn into_tool_result(self) -> ToolResult {
Ok(self)
}
}
impl IntoToolResult for String {
fn into_tool_result(self) -> ToolResult {
Ok(ToolOutput::text(self))
}
}
impl IntoToolResult for &str {
fn into_tool_result(self) -> ToolResult {
Ok(ToolOutput::text(self))
}
}
impl IntoToolResult for Value {
fn into_tool_result(self) -> ToolResult {
Ok(ToolOutput::text(self.to_string()).with_details(self))
}
}
impl<T: IntoToolResult, E: std::fmt::Display> IntoToolResult for Result<T, E> {
fn into_tool_result(self) -> ToolResult {
match self {
Ok(value) => value.into_tool_result(),
Err(err) => Err(ToolError(err.to_string())),
}
}
}