1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum DisconnectReason {
8 Normal,
10 Error,
12 Timeout,
14 ServerClose,
16 ReconnectFailed,
18}
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
22#[serde(rename_all = "snake_case")]
23pub enum ErrorCode {
24 ConnectionFailed,
25 ConnectionTimeout,
26 AuthenticationFailed,
27 SessionError,
28 AudioError,
29 StreamingError,
30 SttError,
31 LlmError,
32 TtsError,
33 RateLimit,
34 InternalError,
35 InvalidMessage,
36 UserIdUpdateError,
37}
38
39impl std::fmt::Display for ErrorCode {
40 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41 let s = match self {
42 ErrorCode::ConnectionFailed => "connection_failed",
43 ErrorCode::ConnectionTimeout => "connection_timeout",
44 ErrorCode::AuthenticationFailed => "authentication_failed",
45 ErrorCode::SessionError => "session_error",
46 ErrorCode::AudioError => "audio_error",
47 ErrorCode::StreamingError => "streaming_error",
48 ErrorCode::SttError => "stt_error",
49 ErrorCode::LlmError => "llm_error",
50 ErrorCode::TtsError => "tts_error",
51 ErrorCode::RateLimit => "rate_limit",
52 ErrorCode::InternalError => "internal_error",
53 ErrorCode::InvalidMessage => "invalid_message",
54 ErrorCode::UserIdUpdateError => "user_id_update_error",
55 };
56 write!(f, "{}", s)
57 }
58}
59
60#[derive(Debug, Clone)]
62pub struct ConnectedEvent {
63 pub connection_id: String,
64 pub timestamp: String,
65}
66
67#[derive(Debug, Clone)]
69pub struct DisconnectedEvent {
70 pub reason: DisconnectReason,
71 pub code: Option<u16>,
72 pub timestamp: String,
73}
74
75#[derive(Debug, Clone)]
77pub struct SessionStartedEvent {
78 pub session_id: String,
79 pub timestamp: String,
80}
81
82#[derive(Debug, Clone)]
84pub struct SessionEndedEvent {
85 pub session_id: String,
86 pub timestamp: String,
87}
88
89#[derive(Debug, Clone)]
91pub struct ReadyEvent {
92 pub timestamp: String,
93}
94
95#[derive(Debug, Clone)]
97pub struct UserTranscriptEvent {
98 pub text: String,
99 pub timestamp: String,
100}
101
102#[derive(Debug, Clone)]
104pub struct ResponseEvent {
105 pub text: String,
106 pub is_final: bool,
107 pub timestamp: String,
108}
109
110#[derive(Debug, Clone)]
112pub struct AudioEvent {
113 pub data: Vec<u8>,
114 pub format: String,
115 pub sample_rate: u32,
116 pub timestamp: String,
117}
118
119#[derive(Debug, Clone)]
121pub struct TurnCompleteEvent {
122 pub timestamp: String,
123}
124
125#[derive(Debug, Clone)]
131pub struct ToolCallEvent {
132 pub id: String,
134 pub name: String,
136 pub args: serde_json::Value,
138 pub timestamp: String,
140}
141
142#[derive(Debug, Clone)]
147pub struct UserIdUpdatedEvent {
148 pub user_id: String,
150 pub migrated_messages: usize,
152 pub timestamp: String,
154}
155
156#[derive(Debug, Clone)]
162pub struct InterruptedEvent {
163 pub timestamp: String,
165}
166
167#[derive(Debug, Clone)]
169pub struct ErrorEvent {
170 pub code: ErrorCode,
171 pub message: String,
172 pub details: Option<String>,
173 pub timestamp: String,
174}
175
176#[derive(Debug, Clone)]
178pub struct ReconnectingEvent {
179 pub attempt: u32,
180 pub max_attempts: u32,
181 pub delay_ms: u64,
182 pub timestamp: String,
183}
184
185#[derive(Debug, Clone)]
187pub enum LiveSpeechEvent {
188 Connected(ConnectedEvent),
189 Disconnected(DisconnectedEvent),
190 Reconnecting(ReconnectingEvent),
191 SessionStarted(SessionStartedEvent),
192 SessionEnded(SessionEndedEvent),
193 Ready(ReadyEvent),
194 UserTranscript(UserTranscriptEvent),
195 Response(ResponseEvent),
196 Audio(AudioEvent),
197 TurnComplete(TurnCompleteEvent),
198 ToolCall(ToolCallEvent),
199 UserIdUpdated(UserIdUpdatedEvent),
200 Interrupted(InterruptedEvent),
202 Error(ErrorEvent),
203}
204
205pub type ResponseHandler = Box<dyn Fn(&str, bool) + Send + Sync>;
207
208pub type AudioHandler = Box<dyn Fn(&[u8]) + Send + Sync>;
210
211pub type ErrorHandler = Box<dyn Fn(&ErrorEvent) + Send + Sync>;