1use serde::{Deserialize, Serialize};
4
5use crate::event::BaseEvent;
6use crate::ids::{MessageId, SubagentRunId, ToolCallId};
7
8#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
10#[serde(rename_all = "camelCase")]
11#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
12#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
13pub struct ToolCallStartEvent {
14 #[serde(flatten)]
16 pub base: BaseEvent,
17 pub tool_call_id: ToolCallId,
19 pub tool_call_name: String,
21 #[serde(default, skip_serializing_if = "Option::is_none")]
27 pub parent_message_id: Option<MessageId>,
28 #[serde(
34 default,
35 deserialize_with = "crate::serde_util::reject_null",
36 skip_serializing_if = "Option::is_none"
37 )]
38 pub subagent_run_id: Option<SubagentRunId>,
39}
40
41impl ToolCallStartEvent {
42 pub fn new(tool_call_id: impl Into<ToolCallId>, tool_call_name: impl Into<String>) -> Self {
44 Self {
45 base: BaseEvent::default(),
46 tool_call_id: tool_call_id.into(),
47 tool_call_name: tool_call_name.into(),
48 parent_message_id: None,
49 subagent_run_id: None,
50 }
51 }
52}
53
54#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
59#[serde(rename_all = "camelCase")]
60#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
61#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
62pub struct ToolCallArgsEvent {
63 #[serde(flatten)]
65 pub base: BaseEvent,
66 pub tool_call_id: ToolCallId,
68 pub delta: String,
70 #[serde(
73 default,
74 deserialize_with = "crate::serde_util::reject_null",
75 skip_serializing_if = "Option::is_none"
76 )]
77 pub subagent_run_id: Option<SubagentRunId>,
78}
79
80impl ToolCallArgsEvent {
81 pub fn new(tool_call_id: impl Into<ToolCallId>, delta: impl Into<String>) -> Self {
83 Self {
84 base: BaseEvent::default(),
85 tool_call_id: tool_call_id.into(),
86 delta: delta.into(),
87 subagent_run_id: None,
88 }
89 }
90}
91
92#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
94#[serde(rename_all = "camelCase")]
95#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
96#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
97pub struct ToolCallEndEvent {
98 #[serde(flatten)]
100 pub base: BaseEvent,
101 pub tool_call_id: ToolCallId,
103 #[serde(
106 default,
107 deserialize_with = "crate::serde_util::reject_null",
108 skip_serializing_if = "Option::is_none"
109 )]
110 pub subagent_run_id: Option<SubagentRunId>,
111}
112
113impl ToolCallEndEvent {
114 pub fn new(tool_call_id: impl Into<ToolCallId>) -> Self {
116 Self {
117 base: BaseEvent::default(),
118 tool_call_id: tool_call_id.into(),
119 subagent_run_id: None,
120 }
121 }
122}
123
124#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
126#[serde(rename_all = "camelCase")]
127#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
128#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
129pub struct ToolCallChunkEvent {
130 #[serde(flatten)]
132 pub base: BaseEvent,
133 #[serde(default, skip_serializing_if = "Option::is_none")]
135 pub tool_call_id: Option<ToolCallId>,
136 #[serde(default, skip_serializing_if = "Option::is_none")]
138 pub tool_call_name: Option<String>,
139 #[serde(default, skip_serializing_if = "Option::is_none")]
142 pub parent_message_id: Option<MessageId>,
143 #[serde(default, skip_serializing_if = "Option::is_none")]
145 pub delta: Option<String>,
146 #[serde(
149 default,
150 deserialize_with = "crate::serde_util::reject_null",
151 skip_serializing_if = "Option::is_none"
152 )]
153 pub subagent_run_id: Option<SubagentRunId>,
154}
155
156impl ToolCallChunkEvent {
157 pub fn new(
159 tool_call_id: Option<ToolCallId>,
160 tool_call_name: Option<String>,
161 delta: Option<String>,
162 ) -> Self {
163 Self {
164 base: BaseEvent::default(),
165 tool_call_id,
166 tool_call_name,
167 parent_message_id: None,
168 delta,
169 subagent_run_id: None,
170 }
171 }
172}
173
174#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
176#[serde(rename_all = "camelCase")]
177#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
178#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
179pub struct ToolCallResultEvent {
180 #[serde(flatten)]
182 pub base: BaseEvent,
183 pub message_id: MessageId,
185 pub tool_call_id: ToolCallId,
187 pub content: String,
189 #[serde(default, skip_serializing_if = "Option::is_none")]
191 pub role: Option<ToolResultRole>,
192 #[serde(
198 default,
199 deserialize_with = "crate::serde_util::reject_null",
200 skip_serializing_if = "Option::is_none"
201 )]
202 pub subagent_run_id: Option<SubagentRunId>,
203}
204
205impl ToolCallResultEvent {
206 pub fn new(
208 message_id: impl Into<MessageId>,
209 tool_call_id: impl Into<ToolCallId>,
210 content: impl Into<String>,
211 ) -> Self {
212 Self {
213 base: BaseEvent::default(),
214 message_id: message_id.into(),
215 tool_call_id: tool_call_id.into(),
216 content: content.into(),
217 role: None,
218 subagent_run_id: None,
219 }
220 }
221}
222
223#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
225#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
226#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
227pub enum ToolResultRole {
228 #[default]
230 #[serde(rename = "tool")]
231 Tool,
232}