1use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6use crate::JsonObject;
7use crate::ids::ToolCallId;
8
9#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
14#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
15#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
16pub struct Tool {
17 pub name: String,
19 pub description: String,
21 #[serde(
23 default,
24 deserialize_with = "crate::serde_util::reject_null_value",
25 skip_serializing_if = "Value::is_null"
26 )]
27 pub parameters: Value,
28 #[serde(
30 default,
31 deserialize_with = "crate::serde_util::reject_null",
32 skip_serializing_if = "Option::is_none"
33 )]
34 #[cfg_attr(
35 feature = "schemars",
36 schemars(with = "Option<std::collections::BTreeMap<String, serde_json::Value>>")
37 )]
38 #[cfg_attr(feature = "utoipa", schema(value_type = Option<Object>))]
39 pub metadata: Option<JsonObject>,
40}
41
42impl Tool {
43 pub fn new(
45 name: impl Into<String>,
46 description: impl Into<String>,
47 parameters: impl Into<Value>,
48 ) -> Self {
49 Self {
50 name: name.into(),
51 description: description.into(),
52 parameters: parameters.into(),
53 metadata: None,
54 }
55 }
56}
57
58#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
63#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
64#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
65pub enum ToolCallKind {
66 #[default]
68 #[serde(rename = "function")]
69 Function,
70}
71
72#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
74#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
75#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
76pub struct FunctionCall {
77 pub name: String,
79 pub arguments: String,
84}
85
86#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
88#[serde(rename_all = "camelCase")]
89#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
90#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
91pub struct ToolCall {
92 pub id: ToolCallId,
94 #[serde(rename = "type", default)]
96 pub kind: ToolCallKind,
97 pub function: FunctionCall,
99 #[serde(default, skip_serializing_if = "Option::is_none")]
101 pub encrypted_value: Option<String>,
102 #[serde(
108 default,
109 deserialize_with = "crate::serde_util::reject_null",
110 skip_serializing_if = "Option::is_none"
111 )]
112 #[cfg_attr(
113 feature = "schemars",
114 schemars(with = "Option<std::collections::BTreeMap<String, serde_json::Value>>")
115 )]
116 #[cfg_attr(feature = "utoipa", schema(value_type = Option<Object>))]
117 pub metadata: Option<JsonObject>,
118}
119
120impl ToolCall {
121 pub fn new(
123 id: impl Into<ToolCallId>,
124 name: impl Into<String>,
125 arguments: impl Into<String>,
126 ) -> Self {
127 Self {
128 id: id.into(),
129 kind: ToolCallKind::Function,
130 function: FunctionCall {
131 name: name.into(),
132 arguments: arguments.into(),
133 },
134 encrypted_value: None,
135 metadata: None,
136 }
137 }
138}