1use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
26pub enum Event {
27 Ping(PingEvent),
29 Usage(UsageEvent),
31 Status(StatusEvent),
33 Error(ErrorEvent),
35
36 BlockStart(BlockStart),
38 BlockDelta(BlockDelta),
40 BlockStop(BlockStop),
42 BlockAbort(BlockAbort),
44}
45
46#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
52pub struct PingEvent {
53 pub timestamp: Option<u64>,
54}
55
56#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
58pub struct UsageEvent {
59 pub input_tokens: Option<u64>,
61 pub output_tokens: Option<u64>,
63 pub total_tokens: Option<u64>,
65 pub cache_read_input_tokens: Option<u64>,
67 pub cache_creation_input_tokens: Option<u64>,
69}
70
71#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
73pub struct StatusEvent {
74 pub status: ResponseStatus,
75}
76
77#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
79pub enum ResponseStatus {
80 Started,
82 Completed,
84 Cancelled,
86 Failed,
88}
89
90#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
92pub struct ErrorEvent {
93 pub code: Option<String>,
94 pub message: String,
95}
96
97#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
103pub enum BlockType {
104 Text,
106 Thinking,
108 ToolUse,
110 ToolResult,
112}
113
114#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
116pub struct BlockStart {
117 pub index: usize,
119 pub block_type: BlockType,
121 pub metadata: BlockMetadata,
123}
124
125impl BlockStart {
126 pub fn block_type(&self) -> BlockType {
127 self.block_type
128 }
129}
130
131#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
133pub enum BlockMetadata {
134 Text,
135 Thinking,
136 ToolUse { id: String, name: String },
137 ToolResult { tool_use_id: String },
138}
139
140#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
142pub struct BlockDelta {
143 pub index: usize,
145 pub delta: DeltaContent,
147}
148
149#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
151pub enum DeltaContent {
152 Text(String),
154 Thinking(String),
156 InputJson(String),
158}
159
160impl DeltaContent {
161 pub fn block_type(&self) -> BlockType {
163 match self {
164 DeltaContent::Text(_) => BlockType::Text,
165 DeltaContent::Thinking(_) => BlockType::Thinking,
166 DeltaContent::InputJson(_) => BlockType::ToolUse,
167 }
168 }
169}
170
171#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
173pub struct BlockStop {
174 pub index: usize,
176 pub block_type: BlockType,
178 pub stop_reason: Option<StopReason>,
180}
181
182impl BlockStop {
183 pub fn block_type(&self) -> BlockType {
184 self.block_type
185 }
186}
187
188#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
190pub struct BlockAbort {
191 pub index: usize,
193 pub block_type: BlockType,
195 pub reason: String,
197}
198
199impl BlockAbort {
200 pub fn block_type(&self) -> BlockType {
201 self.block_type
202 }
203}
204
205#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
207pub enum StopReason {
208 EndTurn,
210 MaxTokens,
212 StopSequence,
214 ToolUse,
216}
217
218impl Event {
223 pub fn text_block_start(index: usize) -> Self {
225 Event::BlockStart(BlockStart {
226 index,
227 block_type: BlockType::Text,
228 metadata: BlockMetadata::Text,
229 })
230 }
231
232 pub fn text_delta(index: usize, text: impl Into<String>) -> Self {
234 Event::BlockDelta(BlockDelta {
235 index,
236 delta: DeltaContent::Text(text.into()),
237 })
238 }
239
240 pub fn text_block_stop(index: usize, stop_reason: Option<StopReason>) -> Self {
242 Event::BlockStop(BlockStop {
243 index,
244 block_type: BlockType::Text,
245 stop_reason,
246 })
247 }
248
249 pub fn tool_use_start(index: usize, id: impl Into<String>, name: impl Into<String>) -> Self {
251 Event::BlockStart(BlockStart {
252 index,
253 block_type: BlockType::ToolUse,
254 metadata: BlockMetadata::ToolUse {
255 id: id.into(),
256 name: name.into(),
257 },
258 })
259 }
260
261 pub fn tool_input_delta(index: usize, json: impl Into<String>) -> Self {
263 Event::BlockDelta(BlockDelta {
264 index,
265 delta: DeltaContent::InputJson(json.into()),
266 })
267 }
268
269 pub fn tool_use_stop(index: usize) -> Self {
271 Event::BlockStop(BlockStop {
272 index,
273 block_type: BlockType::ToolUse,
274 stop_reason: Some(StopReason::ToolUse),
275 })
276 }
277
278 pub fn usage(input_tokens: u64, output_tokens: u64) -> Self {
280 Event::Usage(UsageEvent {
281 input_tokens: Some(input_tokens),
282 output_tokens: Some(output_tokens),
283 total_tokens: Some(input_tokens + output_tokens),
284 cache_read_input_tokens: None,
285 cache_creation_input_tokens: None,
286 })
287 }
288
289 pub fn ping() -> Self {
291 Event::Ping(PingEvent { timestamp: None })
292 }
293}