1use serde_json::Value;
19
20use crate::JsonObject;
21use crate::event::Event;
22use crate::event::activity::{ActivityDeltaEvent, ActivitySnapshotEvent};
23use crate::event::lifecycle::{
24 RunErrorEvent, RunFinishedEvent, RunStartedEvent, StepFinishedEvent, StepStartedEvent,
25};
26use crate::event::reasoning::{
27 ReasoningEncryptedValueEvent, ReasoningEncryptedValueSubtype, ReasoningEndEvent,
28 ReasoningMessageChunkEvent, ReasoningMessageContentEvent, ReasoningMessageEndEvent,
29 ReasoningMessageStartEvent, ReasoningStartEvent, ThinkingEndEvent, ThinkingStartEvent,
30 ThinkingTextMessageContentEvent, ThinkingTextMessageEndEvent, ThinkingTextMessageStartEvent,
31};
32use crate::event::special::{CustomEvent, RawEvent};
33use crate::event::state::{MessagesSnapshotEvent, StateDeltaEvent, StateSnapshotEvent};
34use crate::event::subagent::{
35 SubagentErrorEvent, SubagentFinishedEvent, SubagentOutcome, SubagentStartedEvent,
36};
37use crate::event::text::{
38 TextMessageChunkEvent, TextMessageContentEvent, TextMessageEndEvent, TextMessageRole,
39 TextMessageStartEvent,
40};
41use crate::event::tool::{
42 ToolCallArgsEvent, ToolCallChunkEvent, ToolCallEndEvent, ToolCallResultEvent,
43 ToolCallStartEvent,
44};
45use crate::ids::{MessageId, RunId, StepName, SubagentRunId, ThreadId, ToolCallId};
46use crate::message::Message;
47use crate::outcome::{Interrupt, RunOutcome};
48use crate::patch::PatchOperation;
49
50impl Event {
51 pub fn text_message_start(message_id: impl Into<MessageId>, role: TextMessageRole) -> Self {
53 TextMessageStartEvent::new(message_id, role).into()
54 }
55
56 pub fn text_message_content(
58 message_id: impl Into<MessageId>,
59 delta: impl Into<String>,
60 ) -> Self {
61 TextMessageContentEvent::new(message_id, delta).into()
62 }
63
64 pub fn text_message_end(message_id: impl Into<MessageId>) -> Self {
66 TextMessageEndEvent::new(message_id).into()
67 }
68
69 pub fn text_message_chunk(message_id: Option<MessageId>, delta: Option<String>) -> Self {
71 TextMessageChunkEvent::new(message_id, delta).into()
72 }
73
74 pub fn tool_call_start(
76 tool_call_id: impl Into<ToolCallId>,
77 tool_call_name: impl Into<String>,
78 ) -> Self {
79 ToolCallStartEvent::new(tool_call_id, tool_call_name).into()
80 }
81
82 pub fn tool_call_args(tool_call_id: impl Into<ToolCallId>, delta: impl Into<String>) -> Self {
84 ToolCallArgsEvent::new(tool_call_id, delta).into()
85 }
86
87 pub fn tool_call_end(tool_call_id: impl Into<ToolCallId>) -> Self {
89 ToolCallEndEvent::new(tool_call_id).into()
90 }
91
92 pub fn tool_call_chunk(
94 tool_call_id: Option<ToolCallId>,
95 tool_call_name: Option<String>,
96 delta: Option<String>,
97 ) -> Self {
98 ToolCallChunkEvent::new(tool_call_id, tool_call_name, delta).into()
99 }
100
101 pub fn tool_call_result(
103 message_id: impl Into<MessageId>,
104 tool_call_id: impl Into<ToolCallId>,
105 content: impl Into<String>,
106 ) -> Self {
107 ToolCallResultEvent::new(message_id, tool_call_id, content).into()
108 }
109
110 #[deprecated(note = "use Event::reasoning_start")]
112 pub fn thinking_start(title: Option<String>) -> Self {
113 ThinkingStartEvent::new(title).into()
114 }
115
116 #[deprecated(note = "use Event::reasoning_end")]
118 pub fn thinking_end() -> Self {
119 ThinkingEndEvent::default().into()
120 }
121
122 #[deprecated(note = "use Event::reasoning_message_start")]
124 pub fn thinking_text_message_start() -> Self {
125 ThinkingTextMessageStartEvent::default().into()
126 }
127
128 #[deprecated(note = "use Event::reasoning_message_content")]
130 pub fn thinking_text_message_content(delta: impl Into<String>) -> Self {
131 ThinkingTextMessageContentEvent::new(delta).into()
132 }
133
134 #[deprecated(note = "use Event::reasoning_message_end")]
136 pub fn thinking_text_message_end() -> Self {
137 ThinkingTextMessageEndEvent::default().into()
138 }
139
140 pub fn state_snapshot(snapshot: impl Into<Value>) -> Self {
142 StateSnapshotEvent::new(snapshot).into()
143 }
144
145 pub fn state_delta(delta: impl Into<Vec<PatchOperation>>) -> Self {
147 StateDeltaEvent::new(delta).into()
148 }
149
150 pub fn messages_snapshot(messages: impl Into<Vec<Message>>) -> Self {
152 MessagesSnapshotEvent::new(messages).into()
153 }
154
155 pub fn activity_snapshot(
157 message_id: impl Into<MessageId>,
158 activity_type: impl Into<String>,
159 content: JsonObject,
160 ) -> Self {
161 ActivitySnapshotEvent::new(message_id, activity_type, content).into()
162 }
163
164 pub fn activity_delta(
166 message_id: impl Into<MessageId>,
167 activity_type: impl Into<String>,
168 patch: impl Into<Vec<PatchOperation>>,
169 ) -> Self {
170 ActivityDeltaEvent::new(message_id, activity_type, patch).into()
171 }
172
173 pub fn raw(event: impl Into<Value>) -> Self {
175 RawEvent::new(event).into()
176 }
177
178 pub fn custom(name: impl Into<String>, value: impl Into<Value>) -> Self {
180 CustomEvent::new(name, value).into()
181 }
182
183 pub fn run_started(thread_id: impl Into<ThreadId>, run_id: impl Into<RunId>) -> Self {
185 RunStartedEvent::new(thread_id, run_id).into()
186 }
187
188 pub fn run_finished(thread_id: impl Into<ThreadId>, run_id: impl Into<RunId>) -> Self {
191 RunFinishedEvent::new(thread_id, run_id).into()
192 }
193
194 pub fn run_finished_success(thread_id: impl Into<ThreadId>, run_id: impl Into<RunId>) -> Self {
196 RunFinishedEvent::new(thread_id, run_id)
197 .with_outcome(RunOutcome::Success)
198 .into()
199 }
200
201 pub fn run_finished_interrupt(
204 thread_id: impl Into<ThreadId>,
205 run_id: impl Into<RunId>,
206 interrupts: impl Into<Vec<Interrupt>>,
207 ) -> Self {
208 RunFinishedEvent::new(thread_id, run_id)
209 .with_outcome(RunOutcome::interrupt(interrupts))
210 .into()
211 }
212
213 pub fn run_error(message: impl Into<String>) -> Self {
215 RunErrorEvent::new(message).into()
216 }
217
218 pub fn step_started(step_name: impl Into<StepName>) -> Self {
220 StepStartedEvent::new(step_name).into()
221 }
222
223 pub fn step_finished(step_name: impl Into<StepName>) -> Self {
225 StepFinishedEvent::new(step_name).into()
226 }
227
228 pub fn reasoning_start(message_id: impl Into<MessageId>) -> Self {
230 ReasoningStartEvent::new(message_id).into()
231 }
232
233 pub fn reasoning_message_start(message_id: impl Into<MessageId>) -> Self {
235 ReasoningMessageStartEvent::new(message_id).into()
236 }
237
238 pub fn reasoning_message_content(
240 message_id: impl Into<MessageId>,
241 delta: impl Into<String>,
242 ) -> Self {
243 ReasoningMessageContentEvent::new(message_id, delta).into()
244 }
245
246 pub fn reasoning_message_end(message_id: impl Into<MessageId>) -> Self {
248 ReasoningMessageEndEvent::new(message_id).into()
249 }
250
251 pub fn reasoning_message_chunk(message_id: Option<MessageId>, delta: Option<String>) -> Self {
253 ReasoningMessageChunkEvent::new(message_id, delta).into()
254 }
255
256 pub fn reasoning_end(message_id: impl Into<MessageId>) -> Self {
258 ReasoningEndEvent::new(message_id).into()
259 }
260
261 pub fn reasoning_encrypted_value(
263 subtype: ReasoningEncryptedValueSubtype,
264 entity_id: impl Into<String>,
265 encrypted_value: impl Into<String>,
266 ) -> Self {
267 ReasoningEncryptedValueEvent::new(subtype, entity_id, encrypted_value).into()
268 }
269
270 pub fn subagent_started(
272 subagent_run_id: impl Into<SubagentRunId>,
273 name: impl Into<String>,
274 ) -> Self {
275 SubagentStartedEvent::new(subagent_run_id, name).into()
276 }
277
278 pub fn subagent_finished(subagent_run_id: impl Into<SubagentRunId>) -> Self {
281 SubagentFinishedEvent::new(subagent_run_id).into()
282 }
283
284 pub fn subagent_finished_success(subagent_run_id: impl Into<SubagentRunId>) -> Self {
286 SubagentFinishedEvent::new(subagent_run_id)
287 .with_outcome(SubagentOutcome::Success)
288 .into()
289 }
290
291 pub fn subagent_finished_suspended(
294 subagent_run_id: impl Into<SubagentRunId>,
295 interrupt_ids: impl Into<Vec<String>>,
296 ) -> Self {
297 SubagentFinishedEvent::new(subagent_run_id)
298 .with_outcome(SubagentOutcome::suspended(interrupt_ids))
299 .into()
300 }
301
302 pub fn subagent_error(
304 subagent_run_id: impl Into<SubagentRunId>,
305 message: impl Into<String>,
306 ) -> Self {
307 SubagentErrorEvent::new(subagent_run_id, message).into()
308 }
309}