Skip to main content

ag_ui/event/
tool.rs

1//! Tool invocation: `TOOL_CALL_*`.
2
3use serde::{Deserialize, Serialize};
4
5use crate::event::BaseEvent;
6use crate::ids::{MessageId, SubagentRunId, ToolCallId};
7use crate::message::ToolContent;
8
9/// Opens a tool call. Arguments follow as `TOOL_CALL_ARGS` deltas.
10#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
11#[serde(rename_all = "camelCase")]
12#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
13#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
14pub struct ToolCallStartEvent {
15    /// Timestamp and raw provider event.
16    #[serde(flatten)]
17    pub base: BaseEvent,
18    /// Id correlating this call with its args, end and result.
19    pub tool_call_id: ToolCallId,
20    /// Name of the tool being called.
21    pub tool_call_name: String,
22    /// The assistant message that requested the call.
23    ///
24    /// A JSON `null` here deserializes to `None`: producers whose serializers
25    /// emit nulls for absent optionals (notably the .NET Microsoft Agent
26    /// Framework adapter) must not abort a run on their first tool call.
27    #[serde(default, skip_serializing_if = "Option::is_none")]
28    pub parent_message_id: Option<MessageId>,
29    /// The subagent that produced this event; absent means the parent agent.
30    /// A JSON `null` is rejected — see [`crate::event::subagent`]. A tool
31    /// call belongs to the message `parent_message_id` names, so a tag that
32    /// disagrees with that message's owner is a protocol error; an untagged
33    /// call inherits the message's owner.
34    #[serde(
35        default,
36        deserialize_with = "crate::serde_util::reject_null",
37        skip_serializing_if = "Option::is_none"
38    )]
39    pub subagent_run_id: Option<SubagentRunId>,
40}
41
42impl ToolCallStartEvent {
43    /// Opens a call to `tool_call_name`.
44    pub fn new(tool_call_id: impl Into<ToolCallId>, tool_call_name: impl Into<String>) -> Self {
45        Self {
46            base: BaseEvent::default(),
47            tool_call_id: tool_call_id.into(),
48            tool_call_name: tool_call_name.into(),
49            parent_message_id: None,
50            subagent_run_id: None,
51        }
52    }
53
54    /// Names the assistant message that requested the call.
55    #[must_use]
56    pub fn with_parent_message_id(mut self, parent_message_id: impl Into<MessageId>) -> Self {
57        self.parent_message_id = Some(parent_message_id.into());
58        self
59    }
60}
61
62/// Appends a chunk of the argument JSON for an open tool call.
63///
64/// The deltas concatenate to a JSON string; individual deltas are usually not
65/// valid JSON on their own.
66#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
67#[serde(rename_all = "camelCase")]
68#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
69#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
70pub struct ToolCallArgsEvent {
71    /// Timestamp and raw provider event.
72    #[serde(flatten)]
73    pub base: BaseEvent,
74    /// The call being appended to.
75    pub tool_call_id: ToolCallId,
76    /// The argument-JSON fragment.
77    pub delta: String,
78    /// The subagent that produced this event; absent means the parent agent.
79    /// A JSON `null` is rejected — see [`crate::event::subagent`].
80    #[serde(
81        default,
82        deserialize_with = "crate::serde_util::reject_null",
83        skip_serializing_if = "Option::is_none"
84    )]
85    pub subagent_run_id: Option<SubagentRunId>,
86}
87
88impl ToolCallArgsEvent {
89    /// Appends `delta` to the call's arguments.
90    pub fn new(tool_call_id: impl Into<ToolCallId>, delta: impl Into<String>) -> Self {
91        Self {
92            base: BaseEvent::default(),
93            tool_call_id: tool_call_id.into(),
94            delta: delta.into(),
95            subagent_run_id: None,
96        }
97    }
98}
99
100/// Closes a tool call. The arguments are complete.
101#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
102#[serde(rename_all = "camelCase")]
103#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
104#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
105pub struct ToolCallEndEvent {
106    /// Timestamp and raw provider event.
107    #[serde(flatten)]
108    pub base: BaseEvent,
109    /// The call being closed.
110    pub tool_call_id: ToolCallId,
111    /// The subagent that produced this event; absent means the parent agent.
112    /// A JSON `null` is rejected — see [`crate::event::subagent`].
113    #[serde(
114        default,
115        deserialize_with = "crate::serde_util::reject_null",
116        skip_serializing_if = "Option::is_none"
117    )]
118    pub subagent_run_id: Option<SubagentRunId>,
119}
120
121impl ToolCallEndEvent {
122    /// Closes the call.
123    pub fn new(tool_call_id: impl Into<ToolCallId>) -> Self {
124        Self {
125            base: BaseEvent::default(),
126            tool_call_id: tool_call_id.into(),
127            subagent_run_id: None,
128        }
129    }
130}
131
132/// A self-contained tool-call update: start, args and end folded into one.
133#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
134#[serde(rename_all = "camelCase")]
135#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
136#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
137pub struct ToolCallChunkEvent {
138    /// Timestamp and raw provider event.
139    #[serde(flatten)]
140    pub base: BaseEvent,
141    /// The call this chunk belongs to.
142    #[serde(default, skip_serializing_if = "Option::is_none")]
143    pub tool_call_id: Option<ToolCallId>,
144    /// Name of the tool being called.
145    #[serde(default, skip_serializing_if = "Option::is_none")]
146    pub tool_call_name: Option<String>,
147    /// The assistant message that requested the call. A JSON `null` reads as
148    /// absent, as in [`ToolCallStartEvent`].
149    #[serde(default, skip_serializing_if = "Option::is_none")]
150    pub parent_message_id: Option<MessageId>,
151    /// The argument-JSON fragment.
152    #[serde(default, skip_serializing_if = "Option::is_none")]
153    pub delta: Option<String>,
154    /// The subagent that produced this event; absent means the parent agent.
155    /// A JSON `null` is rejected — see [`crate::event::subagent`].
156    #[serde(
157        default,
158        deserialize_with = "crate::serde_util::reject_null",
159        skip_serializing_if = "Option::is_none"
160    )]
161    pub subagent_run_id: Option<SubagentRunId>,
162}
163
164impl ToolCallChunkEvent {
165    /// Builds a chunk carrying a call id, tool name and argument delta.
166    pub fn new(
167        tool_call_id: Option<ToolCallId>,
168        tool_call_name: Option<String>,
169        delta: Option<String>,
170    ) -> Self {
171        Self {
172            base: BaseEvent::default(),
173            tool_call_id,
174            tool_call_name,
175            parent_message_id: None,
176            delta,
177            subagent_run_id: None,
178        }
179    }
180}
181
182/// The result of a tool call, as a message appended to the thread.
183#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
184#[serde(rename_all = "camelCase")]
185#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
186#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
187pub struct ToolCallResultEvent {
188    /// Timestamp and raw provider event.
189    #[serde(flatten)]
190    pub base: BaseEvent,
191    /// Id of the tool message carrying the result.
192    pub message_id: MessageId,
193    /// The call this result answers.
194    pub tool_call_id: ToolCallId,
195    /// Plain text or ordered multimodal parts returned by the tool.
196    pub content: ToolContent,
197    /// Always `"tool"` when present.
198    #[serde(default, skip_serializing_if = "Option::is_none")]
199    pub role: Option<ToolResultRole>,
200    /// The subagent that *executed* the call; absent means the parent agent.
201    /// A JSON `null` is rejected — see [`crate::event::subagent`]. Attributed
202    /// independently of the call it answers, on purpose: a frontend-executed
203    /// tool, or a supervisor running a call on a subagent's behalf, produces
204    /// a result whose owner is not the caller.
205    #[serde(
206        default,
207        deserialize_with = "crate::serde_util::reject_null",
208        skip_serializing_if = "Option::is_none"
209    )]
210    pub subagent_run_id: Option<SubagentRunId>,
211}
212
213impl ToolCallResultEvent {
214    /// Reports the result of a call.
215    pub fn new(
216        message_id: impl Into<MessageId>,
217        tool_call_id: impl Into<ToolCallId>,
218        content: impl Into<ToolContent>,
219    ) -> Self {
220        Self {
221            base: BaseEvent::default(),
222            message_id: message_id.into(),
223            tool_call_id: tool_call_id.into(),
224            content: content.into(),
225            role: None,
226            subagent_run_id: None,
227        }
228    }
229}
230
231/// The single role a [`ToolCallResultEvent`] may declare.
232#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
233#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
234#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
235pub enum ToolResultRole {
236    /// `"tool"`.
237    #[default]
238    #[serde(rename = "tool")]
239    Tool,
240}