1use serde::{Deserialize, Serialize};
4
5use crate::event::BaseEvent;
6use crate::ids::{MessageId, SubagentRunId, ToolCallId};
7use crate::message::ToolContent;
8
9#[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 #[serde(flatten)]
17 pub base: BaseEvent,
18 pub tool_call_id: ToolCallId,
20 pub tool_call_name: String,
22 #[serde(default, skip_serializing_if = "Option::is_none")]
28 pub parent_message_id: Option<MessageId>,
29 #[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 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 #[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#[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 #[serde(flatten)]
73 pub base: BaseEvent,
74 pub tool_call_id: ToolCallId,
76 pub delta: String,
78 #[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 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#[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 #[serde(flatten)]
108 pub base: BaseEvent,
109 pub tool_call_id: ToolCallId,
111 #[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 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#[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 #[serde(flatten)]
140 pub base: BaseEvent,
141 #[serde(default, skip_serializing_if = "Option::is_none")]
143 pub tool_call_id: Option<ToolCallId>,
144 #[serde(default, skip_serializing_if = "Option::is_none")]
146 pub tool_call_name: Option<String>,
147 #[serde(default, skip_serializing_if = "Option::is_none")]
150 pub parent_message_id: Option<MessageId>,
151 #[serde(default, skip_serializing_if = "Option::is_none")]
153 pub delta: Option<String>,
154 #[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 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#[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 #[serde(flatten)]
190 pub base: BaseEvent,
191 pub message_id: MessageId,
193 pub tool_call_id: ToolCallId,
195 pub content: ToolContent,
197 #[serde(default, skip_serializing_if = "Option::is_none")]
199 pub role: Option<ToolResultRole>,
200 #[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 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#[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 #[default]
238 #[serde(rename = "tool")]
239 Tool,
240}