1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//! Tool definitions and tool calls.
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::JsonObject;
use crate::ids::ToolCallId;
/// A tool the agent may call.
///
/// `parameters` is a JSON Schema object describing the call arguments; it is
/// carried verbatim and never interpreted by this crate.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct Tool {
/// The name the model uses to call this tool.
pub name: String,
/// What the tool does — the model reads this to decide when to call it.
pub description: String,
/// JSON Schema for the call arguments.
#[serde(
default,
deserialize_with = "crate::serde_util::reject_null_value",
skip_serializing_if = "Value::is_null"
)]
pub parameters: Value,
/// Arbitrary integration-specific metadata (for example an A2UI schema).
#[serde(
default,
deserialize_with = "crate::serde_util::reject_null",
skip_serializing_if = "Option::is_none"
)]
#[cfg_attr(
feature = "schemars",
schemars(with = "Option<std::collections::BTreeMap<String, serde_json::Value>>")
)]
#[cfg_attr(feature = "utoipa", schema(value_type = Option<Object>))]
pub metadata: Option<JsonObject>,
}
impl Tool {
/// Builds a tool definition from a name, description and parameter schema.
pub fn new(
name: impl Into<String>,
description: impl Into<String>,
parameters: impl Into<Value>,
) -> Self {
Self {
name: name.into(),
description: description.into(),
parameters: parameters.into(),
metadata: None,
}
}
}
/// Discriminator for the kind of call a [`ToolCall`] represents.
///
/// The protocol currently defines exactly one kind. It is modelled as an enum
/// rather than elided so the literal `"type":"function"` stays on the wire.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub enum ToolCallKind {
/// A call to a named function with JSON arguments.
#[default]
#[serde(rename = "function")]
Function,
}
/// The function being invoked, with its arguments.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct FunctionCall {
/// Name of the tool being called.
pub name: String,
/// Arguments as a JSON *string* — not a parsed object.
///
/// Providers stream these incrementally and may emit invalid JSON until the
/// call is complete, so the protocol keeps them unparsed.
pub arguments: String,
}
/// One tool invocation requested by the assistant.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct ToolCall {
/// Correlates this call with its `TOOL_CALL_*` events and result message.
pub id: ToolCallId,
/// Always [`ToolCallKind::Function`].
#[serde(rename = "type", default)]
pub kind: ToolCallKind,
/// The function and its arguments.
pub function: FunctionCall,
/// Opaque provider payload for zero-data-retention reasoning modes.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub encrypted_value: Option<String>,
/// Extra information, open by key. A tool call is not a message, so it
/// carries its own rather than folding into the assistant message that
/// owns it: several calls can share one parent, and merging them all into
/// it would make the result depend on their relative order. Absent or an
/// object — a JSON `null` is rejected. See [`crate::metadata`].
#[serde(
default,
deserialize_with = "crate::serde_util::reject_null",
skip_serializing_if = "Option::is_none"
)]
#[cfg_attr(
feature = "schemars",
schemars(with = "Option<std::collections::BTreeMap<String, serde_json::Value>>")
)]
#[cfg_attr(feature = "utoipa", schema(value_type = Option<Object>))]
pub metadata: Option<JsonObject>,
}
impl ToolCall {
/// Builds a function tool call.
pub fn new(
id: impl Into<ToolCallId>,
name: impl Into<String>,
arguments: impl Into<String>,
) -> Self {
Self {
id: id.into(),
kind: ToolCallKind::Function,
function: FunctionCall {
name: name.into(),
arguments: arguments.into(),
},
encrypted_value: None,
metadata: None,
}
}
}