use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct FunctionCall {
#[serde(default)]
pub name: String,
#[serde(default)]
pub arguments: String,
}
impl FunctionCall {
pub fn parse_arguments(&self) -> Result<Value, serde_json::Error> {
serde_json::from_str(&self.arguments)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCall {
#[serde(skip_serializing_if = "Option::is_none")]
pub index: Option<u32>,
#[serde(default)]
pub id: String,
#[serde(rename = "type", default)]
pub call_type: String,
#[serde(default)]
pub function: FunctionCall,
}
impl ToolCall {
pub fn new(
id: impl Into<String>,
name: impl Into<String>,
arguments: impl Into<String>,
) -> Self {
Self {
index: None,
id: id.into(),
call_type: "function".to_string(),
function: FunctionCall {
name: name.into(),
arguments: arguments.into(),
},
}
}
pub fn parse_arguments(&self) -> Result<Value, serde_json::Error> {
self.function.parse_arguments()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolParameters {
#[serde(rename = "type")]
pub schema_type: String,
pub properties: serde_json::Map<String, Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub required: Option<Vec<String>>,
}
impl Default for ToolParameters {
fn default() -> Self {
Self {
schema_type: "object".to_string(),
properties: serde_json::Map::new(),
required: None,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FunctionDefinition {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub parameters: Option<ToolParameters>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolDefinition {
#[serde(rename = "type")]
pub tool_type: String,
pub function: FunctionDefinition,
}
impl ToolDefinition {
pub fn new(name: impl Into<String>) -> Self {
Self {
tool_type: "function".to_string(),
function: FunctionDefinition {
name: name.into(),
description: None,
parameters: None,
},
}
}
pub fn description(mut self, description: impl Into<String>) -> Self {
self.function.description = Some(description.into());
self
}
pub fn parameters(mut self, parameters: ToolParameters) -> Self {
self.function.parameters = Some(parameters);
self
}
}