Skip to main content

ag_ui/
tool.rs

1//! Tool definitions and tool calls.
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6use crate::JsonObject;
7use crate::ids::ToolCallId;
8
9/// A tool the agent may call.
10///
11/// `parameters` is a JSON Schema object describing the call arguments; it is
12/// carried verbatim and never interpreted by this crate.
13#[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    /// The name the model uses to call this tool.
18    pub name: String,
19    /// What the tool does — the model reads this to decide when to call it.
20    pub description: String,
21    /// JSON Schema for the call arguments.
22    #[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    /// Arbitrary integration-specific metadata (for example an A2UI schema).
29    #[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    /// Builds a tool definition from a name, description and parameter schema.
44    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/// Discriminator for the kind of call a [`ToolCall`] represents.
59///
60/// The protocol currently defines exactly one kind. It is modelled as an enum
61/// rather than elided so the literal `"type":"function"` stays on the wire.
62#[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    /// A call to a named function with JSON arguments.
67    #[default]
68    #[serde(rename = "function")]
69    Function,
70}
71
72/// The function being invoked, with its arguments.
73#[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    /// Name of the tool being called.
78    pub name: String,
79    /// Arguments as a JSON *string* — not a parsed object.
80    ///
81    /// Providers stream these incrementally and may emit invalid JSON until the
82    /// call is complete, so the protocol keeps them unparsed.
83    pub arguments: String,
84}
85
86/// One tool invocation requested by the assistant.
87#[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    /// Correlates this call with its `TOOL_CALL_*` events and result message.
93    pub id: ToolCallId,
94    /// Always [`ToolCallKind::Function`].
95    #[serde(rename = "type", default)]
96    pub kind: ToolCallKind,
97    /// The function and its arguments.
98    pub function: FunctionCall,
99    /// Opaque provider payload for zero-data-retention reasoning modes.
100    #[serde(default, skip_serializing_if = "Option::is_none")]
101    pub encrypted_value: Option<String>,
102    /// Extra information, open by key. A tool call is not a message, so it
103    /// carries its own rather than folding into the assistant message that
104    /// owns it: several calls can share one parent, and merging them all into
105    /// it would make the result depend on their relative order. Absent or an
106    /// object — a JSON `null` is rejected. See [`crate::metadata`].
107    #[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    /// Builds a function tool call.
122    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}