api_openai/components/realtime_shared/
events_server.rs

1//! Server event structures for Realtime API communication.
2//!
3//! This module contains all server-to-client event types emitted during
4//! a Realtime API WebSocket session.
5
6/// Define a private namespace for server event items.
7mod private
8{
9  use crate::components::common::{ Error, LogProbProperties };
10  use crate::components::realtime_shared::session::RealtimeSession;
11  use crate::components::realtime_shared::transcription::RealtimeTranscriptionSessionCreateResponse;
12  use crate::components::realtime_shared::conversation::
13  {
14    RealtimeConversationItem,
15    RealtimeConversationInfo,
16  };
17  use crate::components::realtime_shared::response::RealtimeResponse;
18
19  use serde::{ Serialize, Deserialize };
20  use serde_json::Value;
21
22  /// Server event indicating a conversation was created.
23  ///
24  /// # Used By
25  /// - `RealtimeServerEvent`
26  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
27  pub struct RealtimeServerEventConversationCreated
28  {
29    /// The unique ID of the server event.
30    pub event_id : String,
31    /// The conversation resource info.
32    pub conversation : RealtimeConversationInfo,
33  }
34
35  /// Server event indicating a conversation item was created.
36  ///
37  /// # Used By
38  /// - `RealtimeServerEvent`
39  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
40  pub struct RealtimeServerEventConversationItemCreated
41  {
42    /// The unique ID of the server event.
43    pub event_id : String,
44    /// The ID of the preceding item in the Conversation context.
45    pub previous_item_id : Option< String >,
46    /// The created conversation item.
47    pub item : RealtimeConversationItem,
48  }
49
50  /// Server event indicating a conversation item was deleted.
51  ///
52  /// # Used By
53  /// - `RealtimeServerEvent`
54  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
55  pub struct RealtimeServerEventConversationItemDeleted
56  {
57    /// The unique ID of the server event.
58    pub event_id : String,
59    /// The ID of the item that was deleted.
60    pub item_id : String,
61  }
62
63  /// Server event indicating input audio transcription completed successfully.
64  ///
65  /// # Used By
66  /// - `RealtimeServerEvent`
67  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
68  pub struct RealtimeServerEventConversationItemInputAudioTranscriptionCompleted
69  {
70    /// The unique ID of the server event.
71    pub event_id : String,
72    /// The ID of the user message item containing the audio.
73    pub item_id : String,
74    /// The index of the content part containing the audio.
75    pub content_index : i32,
76    /// The transcribed text.
77    pub transcript : String,
78    /// The log probabilities of the transcription, if requested.
79    pub logprobs : Option< Vec< LogProbProperties > >,
80  }
81
82  /// Server event indicating a delta in the input audio transcription.
83  ///
84  /// # Used By
85  /// - `RealtimeServerEvent`
86  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
87  pub struct RealtimeServerEventConversationItemInputAudioTranscriptionDelta
88  {
89    /// The unique ID of the server event.
90    pub event_id : String,
91    /// The ID of the item being transcribed.
92    pub item_id : String,
93    /// The index of the content part being transcribed.
94    pub content_index : Option< i32 >, // Made optional as per example
95    /// The text delta.
96    pub delta : String,
97    /// The log probabilities of the transcription delta, if requested.
98    pub logprobs : Option< Vec< LogProbProperties > >,
99  }
100
101  /// Server event indicating input audio transcription failed.
102  ///
103  /// # Used By
104  /// - `RealtimeServerEvent`
105  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
106  pub struct RealtimeServerEventConversationItemInputAudioTranscriptionFailed
107  {
108    /// The unique ID of the server event.
109    pub event_id : String,
110    /// The ID of the user message item.
111    pub item_id : String,
112    /// The index of the content part containing the audio.
113    pub content_index : i32,
114    /// Details of the transcription error.
115    pub error : Error,
116  }
117
118  /// Server event containing the retrieved conversation item.
119  ///
120  /// # Used By
121  /// - `RealtimeServerEvent`
122  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
123  pub struct RealtimeServerEventConversationItemRetrieved
124  {
125    /// The unique ID of the server event.
126    pub event_id : String,
127    /// The retrieved conversation item.
128    pub item : RealtimeConversationItem,
129  }
130
131  /// Server event confirming an assistant audio message was truncated.
132  ///
133  /// # Used By
134  /// - `RealtimeServerEvent`
135  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
136  pub struct RealtimeServerEventConversationItemTruncated
137  {
138    /// The unique ID of the server event.
139    pub event_id : String,
140    /// The ID of the assistant message item that was truncated.
141    pub item_id : String,
142    /// The index of the content part that was truncated.
143    pub content_index : i32,
144    /// The duration up to which the audio was truncated (ms).
145    pub audio_end_ms : i32,
146  }
147
148  /// Server event indicating an error occurred.
149  ///
150  /// # Used By
151  /// - `RealtimeServerEvent`
152  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
153  pub struct RealtimeServerEventError
154  {
155    /// The unique ID of the server event.
156    pub event_id : String,
157    /// Details of the error.
158    pub error : RealtimeErrorDetails,
159  }
160
161  /// Details of an error reported by the Realtime server.
162  ///
163  /// # Used By
164  /// - `RealtimeServerEventError`
165  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
166  pub struct RealtimeErrorDetails
167  {
168    /// The type of error (e.g., "`invalid_request_error`").
169    pub r#type : String,
170    /// Error code, if any.
171    pub code : Option< String >,
172    /// A human-readable error message.
173    pub message : String,
174    /// Parameter related to the error, if any.
175    pub param : Option< String >,
176    /// The `event_id` of the client event that caused the error, if applicable.
177    pub event_id : Option< String >,
178  }
179
180  /// Server event confirming the input audio buffer was cleared.
181  ///
182  /// # Used By
183  /// - `RealtimeServerEvent`
184  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
185  pub struct RealtimeServerEventInputAudioBufferCleared
186  {
187    /// The unique ID of the server event.
188    pub event_id : String,
189  }
190
191  /// Server event confirming the input audio buffer was committed.
192  ///
193  /// # Used By
194  /// - `RealtimeServerEvent`
195  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
196  pub struct RealtimeServerEventInputAudioBufferCommitted
197  {
198    /// The unique ID of the server event.
199    pub event_id : String,
200    /// The ID of the preceding item after which the new item will be inserted.
201    pub previous_item_id : Option< String >,
202    /// The ID of the user message item that will be created.
203    pub item_id : String,
204  }
205
206  /// Server event indicating speech start detected by VAD.
207  ///
208  /// # Used By
209  /// - `RealtimeServerEvent`
210  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
211  pub struct RealtimeServerEventInputAudioBufferSpeechStarted
212  {
213    /// The unique ID of the server event.
214    pub event_id : String,
215    /// Milliseconds from session start when speech was first detected.
216    pub audio_start_ms : i32,
217    /// The ID of the user message item that will be created when speech stops.
218    pub item_id : String,
219  }
220
221  /// Server event indicating speech stop detected by VAD.
222  ///
223  /// # Used By
224  /// - `RealtimeServerEvent`
225  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
226  pub struct RealtimeServerEventInputAudioBufferSpeechStopped
227  {
228    /// The unique ID of the server event.
229    pub event_id : String,
230    /// Milliseconds since session start when speech stopped.
231    pub audio_end_ms : i32,
232    /// The ID of the user message item that will be created.
233    pub item_id : String,
234  }
235
236  /// Server event providing updated rate limit information.
237  ///
238  /// # Used By
239  /// - `RealtimeServerEvent`
240  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
241  pub struct RealtimeServerEventRateLimitsUpdated
242  {
243    /// The unique ID of the server event.
244    pub event_id : String,
245    /// List of rate limit information.
246    pub rate_limits : Vec< RateLimitInfo >,
247  }
248
249  /// Contains information about a specific rate limit.
250  ///
251  /// # Used By
252  /// - `RealtimeServerEventRateLimitsUpdated`
253  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
254  pub struct RateLimitInfo
255  {
256    /// The name of the rate limit (`requests` or `tokens`).
257    pub name : String,
258    /// The maximum allowed value for the rate limit.
259    pub limit : i32,
260    /// The remaining value before the limit is reached.
261    pub remaining : i32,
262    /// Seconds until the rate limit resets.
263    pub reset_seconds : f64,
264  }
265
266  /// Server event containing a delta of model-generated audio.
267  ///
268  /// # Used By
269  /// - `RealtimeServerEvent`
270  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
271  pub struct RealtimeServerEventResponseAudioDelta
272  {
273    /// The unique ID of the server event.
274    pub event_id : String,
275    /// The ID of the response.
276    pub response_id : String,
277    /// The ID of the item containing the audio.
278    pub item_id : String,
279    /// The index of the output item in the response.
280    pub output_index : i32,
281    /// The index of the content part in the item's content array.
282    pub content_index : i32,
283    /// Base64-encoded audio data delta.
284    pub delta : String,
285  }
286
287  /// Server event indicating the model-generated audio stream is done.
288  ///
289  /// # Used By
290  /// - `RealtimeServerEvent`
291  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
292  pub struct RealtimeServerEventResponseAudioDone
293  {
294    /// The unique ID of the server event.
295    pub event_id : String,
296    /// The ID of the response.
297    pub response_id : String,
298    /// The ID of the item containing the audio.
299    pub item_id : String,
300    /// The index of the output item in the response.
301    pub output_index : i32,
302    /// The index of the content part in the item's content array.
303    pub content_index : i32,
304  }
305
306  /// Server event containing a delta of the model-generated audio transcript.
307  ///
308  /// # Used By
309  /// - `RealtimeServerEvent`
310  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
311  pub struct RealtimeServerEventResponseAudioTranscriptDelta
312  {
313    /// The unique ID of the server event.
314    pub event_id : String,
315    /// The ID of the response.
316    pub response_id : String,
317    /// The ID of the item containing the audio.
318    pub item_id : String,
319    /// The index of the output item in the response.
320    pub output_index : i32,
321    /// The index of the content part in the item's content array.
322    pub content_index : i32,
323    /// The transcript delta.
324    pub delta : String,
325  }
326
327  /// Server event indicating the model-generated audio transcript is done.
328  ///
329  /// # Used By
330  /// - `RealtimeServerEvent`
331  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
332  pub struct RealtimeServerEventResponseAudioTranscriptDone
333  {
334    /// The unique ID of the server event.
335    pub event_id : String,
336    /// The ID of the response.
337    pub response_id : String,
338    /// The ID of the item containing the audio.
339    pub item_id : String,
340    /// The index of the output item in the response.
341    pub output_index : i32,
342    /// The index of the content part in the item's content array.
343    pub content_index : i32,
344    /// The final transcript of the audio.
345    pub transcript : String,
346  }
347
348  /// Server event indicating a new content part was added to an output item.
349  ///
350  /// # Used By
351  /// - `RealtimeServerEvent`
352  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
353  pub struct RealtimeServerEventResponseContentPartAdded
354  {
355    /// The unique ID of the server event.
356    pub event_id : String,
357    /// The ID of the response.
358    pub response_id : String,
359    /// The ID of the item to which the content part was added.
360    pub item_id : String,
361    /// The index of the output item in the response.
362    pub output_index : i32,
363    /// The index of the content part in the item's content array.
364    pub content_index : i32,
365    /// The content part that was added (structure depends on content type).
366    pub part : Value,
367  }
368
369  /// Server event indicating a content part is done streaming.
370  ///
371  /// # Used By
372  /// - `RealtimeServerEvent`
373  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
374  pub struct RealtimeServerEventResponseContentPartDone
375  {
376    /// The unique ID of the server event.
377    pub event_id : String,
378    /// The ID of the response.
379    pub response_id : String,
380    /// The ID of the item.
381    pub item_id : String,
382    /// The index of the output item in the response.
383    pub output_index : i32,
384    /// The index of the content part in the item's content array.
385    pub content_index : i32,
386    /// The content part that is done (structure depends on content type).
387    pub part : Value,
388  }
389
390  /// Server event indicating a new response was created.
391  ///
392  /// # Used By
393  /// - `RealtimeServerEvent`
394  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
395  pub struct RealtimeServerEventResponseCreated
396  {
397    /// The unique ID of the server event.
398    pub event_id : String,
399    /// The created response object (initial state).
400    pub response : RealtimeResponse,
401  }
402
403  /// Server event indicating a response is done streaming (completed, failed, etc.).
404  ///
405  /// # Used By
406  /// - `RealtimeServerEvent`
407  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
408  pub struct RealtimeServerEventResponseDone
409  {
410    /// The unique ID of the server event.
411    pub event_id : String,
412    /// The final response object (omits raw audio data).
413    pub response : RealtimeResponse,
414  }
415
416  /// Server event containing a delta for function call arguments.
417  ///
418  /// # Used By
419  /// - `RealtimeServerEvent`
420  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
421  pub struct RealtimeServerEventResponseFunctionCallArgumentsDelta
422  {
423    /// The unique ID of the server event.
424    pub event_id : String,
425    /// The ID of the response.
426    pub response_id : String,
427    /// The ID of the function call item.
428    pub item_id : String,
429    /// The index of the output item in the response.
430    pub output_index : i32,
431    /// The ID of the function call.
432    pub call_id : String,
433    /// The arguments delta as a JSON string.
434    pub delta : String,
435  }
436
437  /// Server event indicating function call arguments are finalized.
438  ///
439  /// # Used By
440  /// - `RealtimeServerEvent`
441  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
442  pub struct RealtimeServerEventResponseFunctionCallArgumentsDone
443  {
444    /// The unique ID of the server event.
445    pub event_id : String,
446    /// The ID of the response.
447    pub response_id : String,
448    /// The ID of the function call item.
449    pub item_id : String,
450    /// The index of the output item in the response.
451    pub output_index : i32,
452    /// The ID of the function call.
453    pub call_id : String,
454    /// The final arguments as a JSON string.
455    pub arguments : String,
456  }
457
458  /// Server event indicating a new output item was added to the response.
459  ///
460  /// # Used By
461  /// - `RealtimeServerEvent`
462  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
463  pub struct RealtimeServerEventResponseOutputItemAdded
464  {
465    /// The unique ID of the server event.
466    pub event_id : String,
467    /// The ID of the Response to which the item belongs.
468    pub response_id : String,
469    /// The index of the output item in the Response.
470    pub output_index : i32,
471    /// The output item that was added.
472    pub item : RealtimeConversationItem,
473  }
474
475  /// Server event indicating an output item is done streaming.
476  ///
477  /// # Used By
478  /// - `RealtimeServerEvent`
479  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
480  pub struct RealtimeServerEventResponseOutputItemDone
481  {
482    /// The unique ID of the server event.
483    pub event_id : String,
484    /// The ID of the Response to which the item belongs.
485    pub response_id : String,
486    /// The index of the output item in the Response.
487    pub output_index : i32,
488    /// The output item that was marked done.
489    pub item : RealtimeConversationItem,
490  }
491
492  /// Server event containing a delta for text content.
493  ///
494  /// # Used By
495  /// - `RealtimeServerEvent`
496  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
497  pub struct RealtimeServerEventResponseTextDelta
498  {
499    /// The unique ID of the server event.
500    pub event_id : String,
501    /// The ID of the response.
502    pub response_id : String,
503    /// The ID of the item containing the text.
504    pub item_id : String,
505    /// The index of the output item in the response.
506    pub output_index : i32,
507    /// The index of the content part in the item's content array.
508    pub content_index : i32,
509    /// The text delta.
510    pub delta : String,
511  }
512
513  /// Server event indicating text content is finalized.
514  ///
515  /// # Used By
516  /// - `RealtimeServerEvent`
517  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
518  pub struct RealtimeServerEventResponseTextDone
519  {
520    /// The unique ID of the server event.
521    pub event_id : String,
522    /// The ID of the response.
523    pub response_id : String,
524    /// The ID of the item containing the text.
525    pub item_id : String,
526    /// The index of the output item in the response.
527    pub output_index : i32,
528    /// The index of the content part in the item's content array.
529    pub content_index : i32,
530    /// The final text content.
531    pub text : String,
532  }
533
534  /// Server event indicating the session was created (initial event).
535  ///
536  /// # Used By
537  /// - `RealtimeServerEvent`
538  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
539  pub struct RealtimeServerEventSessionCreated
540  {
541    /// The unique ID of the server event.
542    pub event_id : String,
543    /// The initial session configuration.
544    pub session : RealtimeSession,
545  }
546
547  /// Server event confirming a session update.
548  ///
549  /// # Used By
550  /// - `RealtimeServerEvent`
551  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
552  pub struct RealtimeServerEventSessionUpdated
553  {
554    /// The unique ID of the server event.
555    pub event_id : String,
556    /// The updated session configuration.
557    pub session : RealtimeSession,
558  }
559
560  /// Response object returned when creating a Realtime transcription session via REST API.
561  ///
562  /// # Used By
563  /// - `/realtime/transcription_sessions` (POST)
564  /// - `RealtimeServerEventTranscriptionSessionUpdated`
565  #[ derive( Debug, Serialize, Deserialize, Clone, PartialEq ) ]
566  pub struct RealtimeServerEventTranscriptionSessionCreated
567  {
568    /// Unique identifier for the session.
569    #[ serde( skip_serializing_if = "Option::is_none" ) ]
570    pub id : Option< String >,
571    /// The object type, always "`realtime.transcription_session`".
572    #[ serde( skip_serializing_if = "Option::is_none" ) ]
573    pub object : Option< String >,
574    /// The set of modalities supported (always [`"audio"`, `"text"`] for transcription).
575    #[ serde( skip_serializing_if = "Option::is_none" ) ]
576    pub modalities : Option< Vec< String > >,
577    /// The format of input audio.
578    #[ serde( skip_serializing_if = "Option::is_none" ) ]
579    pub input_audio_format : Option< String >,
580    /// Configuration for input audio transcription.
581    #[ serde( skip_serializing_if = "Option::is_none" ) ]
582    pub input_audio_transcription : Option< crate::components::realtime_shared::session::RealtimeSessionInputAudioTranscription >,
583    /// Configuration for turn detection.
584    #[ serde( skip_serializing_if = "Option::is_none" ) ]
585    pub turn_detection : Option< crate::components::realtime_shared::session::RealtimeSessionTurnDetection >,
586    /// Configuration for input audio noise reduction.
587    #[ serde( skip_serializing_if = "Option::is_none" ) ]
588    pub input_audio_noise_reduction : Option< crate::components::realtime_shared::session::RealtimeSessionInputAudioNoiseReduction >,
589    /// Items to include in the transcription response (e.g., logprobs).
590    #[ serde( skip_serializing_if = "Option::is_none" ) ]
591    pub include : Option< Vec< String > >,
592  }
593
594  /// Server event confirming a transcription session update.
595  ///
596  /// # Used By
597  /// - `RealtimeServerEvent`
598  #[ derive( Debug, Deserialize, Clone, PartialEq ) ]
599  pub struct RealtimeServerEventTranscriptionSessionUpdated
600  {
601    /// The unique ID of the server event.
602    pub event_id : String,
603    /// The updated transcription session configuration.
604    pub session : RealtimeTranscriptionSessionCreateResponse,
605  }
606
607} // end mod private
608
609crate ::mod_interface!
610{
611  exposed use
612  {
613    RealtimeServerEventConversationCreated,
614    RealtimeServerEventConversationItemCreated,
615    RealtimeServerEventConversationItemDeleted,
616    RealtimeServerEventConversationItemInputAudioTranscriptionCompleted,
617    RealtimeServerEventConversationItemInputAudioTranscriptionDelta,
618    RealtimeServerEventConversationItemInputAudioTranscriptionFailed,
619    RealtimeServerEventConversationItemRetrieved,
620    RealtimeServerEventConversationItemTruncated,
621    RealtimeServerEventError,
622    RealtimeErrorDetails,
623    RealtimeServerEventInputAudioBufferCleared,
624    RealtimeServerEventInputAudioBufferCommitted,
625    RealtimeServerEventInputAudioBufferSpeechStarted,
626    RealtimeServerEventInputAudioBufferSpeechStopped,
627    RealtimeServerEventRateLimitsUpdated,
628    RateLimitInfo,
629    RealtimeServerEventResponseAudioDelta,
630    RealtimeServerEventResponseAudioDone,
631    RealtimeServerEventResponseAudioTranscriptDelta,
632    RealtimeServerEventResponseAudioTranscriptDone,
633    RealtimeServerEventResponseContentPartAdded,
634    RealtimeServerEventResponseContentPartDone,
635    RealtimeServerEventResponseCreated,
636    RealtimeServerEventResponseDone,
637    RealtimeServerEventResponseFunctionCallArgumentsDelta,
638    RealtimeServerEventResponseFunctionCallArgumentsDone,
639    RealtimeServerEventResponseOutputItemAdded,
640    RealtimeServerEventResponseOutputItemDone,
641    RealtimeServerEventResponseTextDelta,
642    RealtimeServerEventResponseTextDone,
643    RealtimeServerEventSessionCreated,
644    RealtimeServerEventSessionUpdated,
645    RealtimeServerEventTranscriptionSessionCreated,
646    RealtimeServerEventTranscriptionSessionUpdated,
647  };
648}