1use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6use crate::event::BaseEvent;
7use crate::ids::{RunId, StepName, ThreadId};
8use crate::input::RunAgentInput;
9use crate::outcome::RunOutcome;
10use crate::token_usage::TokenUsage;
11
12#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
14#[serde(rename_all = "camelCase")]
15#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
16#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
17pub struct RunStartedEvent {
18 #[serde(flatten)]
20 pub base: BaseEvent,
21 pub thread_id: ThreadId,
23 pub run_id: RunId,
25 #[serde(default, skip_serializing_if = "Option::is_none")]
27 pub parent_run_id: Option<RunId>,
28 #[serde(default, skip_serializing_if = "Option::is_none")]
34 pub input: Option<Box<RunAgentInput>>,
35}
36
37impl RunStartedEvent {
38 pub fn new(thread_id: impl Into<ThreadId>, run_id: impl Into<RunId>) -> Self {
40 Self {
41 base: BaseEvent::default(),
42 thread_id: thread_id.into(),
43 run_id: run_id.into(),
44 parent_run_id: None,
45 input: None,
46 }
47 }
48}
49
50#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
55#[serde(rename_all = "camelCase")]
56#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
57#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
58pub struct RunFinishedEvent {
59 #[serde(flatten)]
61 pub base: BaseEvent,
62 pub thread_id: ThreadId,
64 pub run_id: RunId,
66 #[serde(default, skip_serializing_if = "Option::is_none")]
68 pub result: Option<Value>,
69 #[serde(default, skip_serializing_if = "Option::is_none")]
73 pub outcome: Option<RunOutcome>,
74 #[serde(default, skip_serializing_if = "Option::is_none")]
76 pub usage: Option<Vec<TokenUsage>>,
77}
78
79impl RunFinishedEvent {
80 pub fn new(thread_id: impl Into<ThreadId>, run_id: impl Into<RunId>) -> Self {
82 Self {
83 base: BaseEvent::default(),
84 thread_id: thread_id.into(),
85 run_id: run_id.into(),
86 result: None,
87 outcome: None,
88 usage: None,
89 }
90 }
91
92 #[must_use]
94 pub fn with_outcome(mut self, outcome: RunOutcome) -> Self {
95 self.outcome = Some(outcome);
96 self
97 }
98
99 #[must_use]
101 pub fn with_result(mut self, result: impl Into<Value>) -> Self {
102 self.result = Some(result.into());
103 self
104 }
105
106 #[must_use]
108 pub fn with_usage(mut self, usage: impl Into<Vec<TokenUsage>>) -> Self {
109 self.usage = Some(usage.into());
110 self
111 }
112}
113
114#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
116#[serde(rename_all = "camelCase")]
117#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
118#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
119pub struct RunErrorEvent {
120 #[serde(flatten)]
122 pub base: BaseEvent,
123 pub message: String,
125 #[serde(default, skip_serializing_if = "Option::is_none")]
127 pub code: Option<String>,
128 #[serde(default, skip_serializing_if = "Option::is_none")]
130 pub usage: Option<Vec<TokenUsage>>,
131}
132
133impl RunErrorEvent {
134 pub fn new(message: impl Into<String>) -> Self {
136 Self {
137 base: BaseEvent::default(),
138 message: message.into(),
139 code: None,
140 usage: None,
141 }
142 }
143
144 #[must_use]
146 pub fn with_code(mut self, code: impl Into<String>) -> Self {
147 self.code = Some(code.into());
148 self
149 }
150}
151
152#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
154#[serde(rename_all = "camelCase")]
155#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
156#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
157pub struct StepStartedEvent {
158 #[serde(flatten)]
160 pub base: BaseEvent,
161 pub step_name: StepName,
163}
164
165impl StepStartedEvent {
166 pub fn new(step_name: impl Into<StepName>) -> Self {
168 Self {
169 base: BaseEvent::default(),
170 step_name: step_name.into(),
171 }
172 }
173}
174
175#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
177#[serde(rename_all = "camelCase")]
178#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
179#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
180pub struct StepFinishedEvent {
181 #[serde(flatten)]
183 pub base: BaseEvent,
184 pub step_name: StepName,
186}
187
188impl StepFinishedEvent {
189 pub fn new(step_name: impl Into<StepName>) -> Self {
191 Self {
192 base: BaseEvent::default(),
193 step_name: step_name.into(),
194 }
195 }
196}