use std::io;
use serde::{Serialize, Deserialize};
use serde_json::Value;
pub(crate) mod http;
pub mod web_search;
pub mod web_fetch;
pub(crate) fn arg_str<'a>(data: &'a Value, key: &str) -> Result<&'a str, io::Error> {
data.get(key)
.and_then(|v| v.as_str())
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, format!("Missing or invalid argument '{}'", key)))
}
pub(crate) fn arg_usize(data: &Value, key: &str, default: usize) -> usize {
data.get(key).and_then(|v| v.as_u64()).map(|v| v as usize).unwrap_or(default)
}
pub trait Tool {
fn name(&self) -> &str;
fn description(&self) -> &str;
fn execute(&self, arguments: &str) -> Result<String, io::Error>;
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCall {
pub id: String,
pub name: String,
pub arguments: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolResult {
pub tool_call_id: String,
pub content: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolDefinition {
#[serde(rename = "type")]
pub tool_type: String,
pub function: FunctionDefinition,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FunctionDefinition {
pub name: String,
pub description: String,
pub parameters: serde_json::Value,
}
impl ToolDefinition {
pub fn from_tool<T: Tool + ?Sized>(tool: &T) -> Self {
ToolDefinition {
tool_type: "function".to_string(),
function: FunctionDefinition {
name: tool.name().to_string(),
description: tool.description().to_string(),
parameters: serde_json::json!({
"type": "object",
"properties": {},
"required": []
}),
},
}
}
}