1use serde::{Deserialize, Serialize};
4
5use crate::role::Role;
6use crate::usage::Usage;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
10#[serde(rename_all = "snake_case")]
11#[non_exhaustive]
12pub enum FinishReason {
13 Stop,
15 Length,
17 ToolUse,
19 ContentFilter,
21 Error,
23 Cancelled,
25}
26
27pub trait StreamEvent: Send + Sync + std::fmt::Debug {
29 fn event_type(&self) -> &'static str;
31}
32
33pub type StreamEventRef = std::sync::Arc<dyn StreamEvent>;
35
36#[derive(Debug, Clone, PartialEq)]
38pub struct MessageStart {
39 pub role: Role,
41 pub model: String,
43 pub request_id: Option<String>,
45}
46
47impl StreamEvent for MessageStart {
48 fn event_type(&self) -> &'static str {
49 "message.start"
50 }
51}
52
53#[derive(Debug, Clone, PartialEq)]
55pub struct TextDelta {
56 pub text: String,
58}
59
60impl StreamEvent for TextDelta {
61 fn event_type(&self) -> &'static str {
62 "text.delta"
63 }
64}
65
66#[derive(Debug, Clone, PartialEq)]
68pub struct ReasoningDelta {
69 pub text: String,
71}
72
73impl StreamEvent for ReasoningDelta {
74 fn event_type(&self) -> &'static str {
75 "reasoning.delta"
76 }
77}
78
79#[derive(Debug, Clone, PartialEq)]
81pub struct ToolUseStart {
82 pub id: String,
84 pub name: String,
86}
87
88impl StreamEvent for ToolUseStart {
89 fn event_type(&self) -> &'static str {
90 "tool_use.start"
91 }
92}
93
94#[derive(Debug, Clone, PartialEq)]
96pub struct ToolUseDelta {
97 pub id: String,
99 pub input_delta: String,
101}
102
103impl StreamEvent for ToolUseDelta {
104 fn event_type(&self) -> &'static str {
105 "tool_use.delta"
106 }
107}
108
109#[derive(Debug, Clone, PartialEq)]
111pub struct ToolUseStop {
112 pub id: String,
114}
115
116impl StreamEvent for ToolUseStop {
117 fn event_type(&self) -> &'static str {
118 "tool_use.stop"
119 }
120}
121
122#[derive(Debug, Clone, PartialEq)]
124pub struct MessageStop {
125 pub finish_reason: FinishReason,
127}
128
129impl StreamEvent for MessageStop {
130 fn event_type(&self) -> &'static str {
131 "message.stop"
132 }
133}
134
135#[derive(Debug, Clone, PartialEq)]
137pub struct UsageDelta {
138 pub usage: Usage,
140}
141
142impl StreamEvent for UsageDelta {
143 fn event_type(&self) -> &'static str {
144 "usage.delta"
145 }
146}
147
148#[derive(Debug, Clone, PartialEq)]
150pub struct ErrorEvent {
151 pub message: String,
153 pub code: Option<String>,
155}
156
157impl StreamEvent for ErrorEvent {
158 fn event_type(&self) -> &'static str {
159 "error"
160 }
161}