1use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6use crate::event::BaseEvent;
7use crate::ids::{RunId, StepName, SubagentRunId, ThreadId};
8use crate::input::PROTOCOL_VERSION;
9use crate::input::RunAgentInput;
10use crate::outcome::RunOutcome;
11use crate::token_usage::TokenUsage;
12
13#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
15#[serde(rename_all = "camelCase")]
16#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
17#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
18pub struct RunStartedEvent {
19 #[serde(flatten)]
21 pub base: BaseEvent,
22 pub thread_id: ThreadId,
24 pub run_id: RunId,
26 #[serde(default, skip_serializing_if = "Option::is_none")]
29 pub protocol_version: Option<String>,
30 #[serde(default, skip_serializing_if = "Option::is_none")]
32 pub parent_run_id: Option<RunId>,
33 #[serde(default, skip_serializing_if = "Option::is_none")]
39 pub input: Option<Box<RunAgentInput>>,
40}
41
42impl RunStartedEvent {
43 pub fn new(thread_id: impl Into<ThreadId>, run_id: impl Into<RunId>) -> Self {
45 Self {
46 base: BaseEvent::default(),
47 thread_id: thread_id.into(),
48 run_id: run_id.into(),
49 protocol_version: Some(PROTOCOL_VERSION.to_owned()),
50 parent_run_id: None,
51 input: None,
52 }
53 }
54}
55
56#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
61#[serde(rename_all = "camelCase")]
62#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
63#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
64pub struct RunFinishedEvent {
65 #[serde(flatten)]
67 pub base: BaseEvent,
68 pub thread_id: ThreadId,
70 pub run_id: RunId,
72 #[serde(default, skip_serializing_if = "crate::serde_util::is_none_or_null")]
74 pub result: Option<Value>,
75 #[serde(default, skip_serializing_if = "Option::is_none")]
79 pub outcome: Option<RunOutcome>,
80 #[serde(default, skip_serializing_if = "Option::is_none")]
82 pub usage: Option<Vec<TokenUsage>>,
83}
84
85impl RunFinishedEvent {
86 pub fn new(thread_id: impl Into<ThreadId>, run_id: impl Into<RunId>) -> Self {
88 Self {
89 base: BaseEvent::default(),
90 thread_id: thread_id.into(),
91 run_id: run_id.into(),
92 result: None,
93 outcome: None,
94 usage: None,
95 }
96 }
97
98 #[must_use]
100 pub fn with_outcome(mut self, outcome: RunOutcome) -> Self {
101 self.outcome = Some(outcome);
102 self
103 }
104
105 #[must_use]
107 pub fn with_result(mut self, result: impl Into<Value>) -> Self {
108 let result = result.into();
109 self.result = (!result.is_null()).then_some(result);
110 self
111 }
112
113 #[must_use]
115 pub fn with_usage(mut self, usage: impl Into<Vec<TokenUsage>>) -> Self {
116 self.usage = Some(usage.into());
117 self
118 }
119}
120
121#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
123#[serde(rename_all = "camelCase")]
124#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
125#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
126pub struct RunErrorEvent {
127 #[serde(flatten)]
129 pub base: BaseEvent,
130 pub message: String,
132 #[serde(default, skip_serializing_if = "Option::is_none")]
134 pub code: Option<String>,
135 #[serde(default, skip_serializing_if = "Option::is_none")]
137 pub usage: Option<Vec<TokenUsage>>,
138}
139
140impl RunErrorEvent {
141 pub fn new(message: impl Into<String>) -> Self {
143 Self {
144 base: BaseEvent::default(),
145 message: message.into(),
146 code: None,
147 usage: None,
148 }
149 }
150
151 #[must_use]
153 pub fn with_code(mut self, code: impl Into<String>) -> Self {
154 self.code = Some(code.into());
155 self
156 }
157}
158
159#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
161#[serde(rename_all = "camelCase")]
162#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
163#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
164pub struct StepStartedEvent {
165 #[serde(flatten)]
167 pub base: BaseEvent,
168 pub step_name: StepName,
170 #[serde(
176 default,
177 deserialize_with = "crate::serde_util::reject_null",
178 skip_serializing_if = "Option::is_none"
179 )]
180 pub subagent_run_id: Option<SubagentRunId>,
181}
182
183impl StepStartedEvent {
184 pub fn new(step_name: impl Into<StepName>) -> Self {
186 Self {
187 base: BaseEvent::default(),
188 step_name: step_name.into(),
189 subagent_run_id: None,
190 }
191 }
192}
193
194#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
196#[serde(rename_all = "camelCase")]
197#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
198#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
199pub struct StepFinishedEvent {
200 #[serde(flatten)]
202 pub base: BaseEvent,
203 pub step_name: StepName,
205 #[serde(
209 default,
210 deserialize_with = "crate::serde_util::reject_null",
211 skip_serializing_if = "Option::is_none"
212 )]
213 pub subagent_run_id: Option<SubagentRunId>,
214}
215
216impl StepFinishedEvent {
217 pub fn new(step_name: impl Into<StepName>) -> Self {
219 Self {
220 base: BaseEvent::default(),
221 step_name: step_name.into(),
222 subagent_run_id: None,
223 }
224 }
225}