async_openai_wasm/types/realtime/
server_event.rs

1use serde::{Deserialize, Serialize};
2
3use super::{
4    content_part::ContentPart, conversation::Conversation, error::RealtimeAPIError, item::Item,
5    rate_limit::RateLimit, response_resource::ResponseResource, session_resource::SessionResource,
6};
7
8#[derive(Debug, Serialize, Deserialize, Clone)]
9pub struct ErrorEvent {
10    /// The unique ID of the server event.
11    pub event_id: String,
12    /// Details of the error.
13    pub error: RealtimeAPIError,
14}
15
16#[derive(Debug, Serialize, Deserialize, Clone)]
17pub struct SessionCreatedEvent {
18    /// The unique ID of the server event.
19    pub event_id: String,
20    /// The session resource.
21    pub session: SessionResource,
22}
23
24#[derive(Debug, Serialize, Deserialize, Clone)]
25pub struct SessionUpdatedEvent {
26    /// The unique ID of the server event.
27    pub event_id: String,
28    /// The updated session resource.
29    pub session: SessionResource,
30}
31
32#[derive(Debug, Serialize, Deserialize, Clone)]
33pub struct ConversationCreatedEvent {
34    /// The unique ID of the server event.
35    pub event_id: String,
36    /// The conversation resource.
37    pub conversation: Conversation,
38}
39
40#[derive(Debug, Serialize, Deserialize, Clone)]
41pub struct InputAudioBufferCommitedEvent {
42    /// The unique ID of the server event.
43    pub event_id: String,
44    /// The ID of the preceding item after which the new item will be inserted.
45    pub previous_item_id: String,
46    /// The ID of the user message item that will be created.
47    pub item_id: String,
48}
49
50#[derive(Debug, Serialize, Deserialize, Clone)]
51pub struct InputAudioBufferClearedEvent {
52    /// The unique ID of the server event.
53    pub event_id: String,
54}
55
56#[derive(Debug, Serialize, Deserialize, Clone)]
57pub struct InputAudioBufferSpeechStartedEvent {
58    /// The unique ID of the server event.
59    pub event_id: String,
60    /// Milliseconds since the session started when speech was detected.
61    pub audio_start_ms: u32,
62    /// The ID of the user message item that will be created when speech stops.
63    pub item_id: String,
64}
65
66#[derive(Debug, Serialize, Deserialize, Clone)]
67pub struct InputAudioBufferSpeechStoppedEvent {
68    /// The unique ID of the server event.
69    pub event_id: String,
70    /// Milliseconds since the session started when speech stopped.
71    pub audio_end_ms: u32,
72    /// The ID of the user message item that will be created.
73    pub item_id: String,
74}
75
76#[derive(Debug, Serialize, Deserialize, Clone)]
77pub struct ConversationItemCreatedEvent {
78    /// The unique ID of the server event.
79    pub event_id: String,
80    /// The ID of the preceding item.
81    pub previous_item_id: Option<String>,
82    /// The item that was created.
83    pub item: Item,
84}
85
86#[derive(Debug, Serialize, Deserialize, Clone)]
87pub struct ConversationItemInputAudioTranscriptionCompletedEvent {
88    /// The unique ID of the server event.
89    pub event_id: String,
90    /// The ID of the user message item.
91    pub item_id: String,
92    /// The index of the content part containing the audio.
93    pub content_index: u32,
94    /// The transcribed text.
95    pub transcript: String,
96}
97
98#[derive(Debug, Serialize, Deserialize, Clone)]
99pub struct ConversationItemInputAudioTranscriptionFailedEvent {
100    /// The unique ID of the server event.
101    pub event_id: String,
102    /// The ID of the user message item.
103    pub item_id: String,
104    /// The index of the content part containing the audio.
105    pub content_index: u32,
106    /// Details of the transcription error.
107    pub error: RealtimeAPIError,
108}
109
110#[derive(Debug, Serialize, Deserialize, Clone)]
111pub struct ConversationItemTruncatedEvent {
112    /// The unique ID of the server event.
113    pub event_id: String,
114    /// The ID of the assistant message item that was truncated.
115    pub item_id: String,
116    /// The index of the content part that was truncated.
117    pub content_index: u32,
118    /// The duration up to which the audio was truncated, in milliseconds.
119    pub audio_end_ms: u32,
120}
121
122#[derive(Debug, Serialize, Deserialize, Clone)]
123pub struct ConversationItemDeletedEvent {
124    /// The unique ID of the server event.
125    pub event_id: String,
126    /// The ID of the item that was deleted.
127    pub item_id: String,
128}
129
130#[derive(Debug, Serialize, Deserialize, Clone)]
131pub struct ResponseCreatedEvent {
132    /// The unique ID of the server event.
133    pub event_id: String,
134    /// The response resource.
135    pub response: ResponseResource,
136}
137
138#[derive(Debug, Serialize, Deserialize, Clone)]
139pub struct ResponseDoneEvent {
140    /// The unique ID of the server event.
141    pub event_id: String,
142    /// The response resource.
143    pub response: ResponseResource,
144}
145
146#[derive(Debug, Serialize, Deserialize, Clone)]
147pub struct ResponseOutputItemAddedEvent {
148    /// The unique ID of the server event.
149    pub event_id: String,
150    /// The ID of the response to which the item belongs.
151    pub response_id: String,
152    /// The index of the output item in the response.
153    pub output_index: u32,
154    /// The item that was added.
155    pub item: Item,
156}
157
158#[derive(Debug, Serialize, Deserialize, Clone)]
159pub struct ResponseOutputItemDoneEvent {
160    /// The unique ID of the server event.
161    pub event_id: String,
162    /// The ID of the response to which the item belongs.
163    pub response_id: String,
164    /// The index of the output item in the response.
165    pub output_index: u32,
166    /// The completed item.
167    pub item: Item,
168}
169
170#[derive(Debug, Serialize, Deserialize, Clone)]
171pub struct ResponseContentPartAddedEvent {
172    /// The unique ID of the server event.
173    pub event_id: String,
174    /// The ID of the response.
175    pub response_id: String,
176    /// The ID of the item to which the content part was added.
177    pub item_id: String,
178    /// The index of the output item in the response.
179    pub output_index: u32,
180    /// The index of the content part in the item's content array.
181    pub content_index: u32,
182    /// The content part that was added.
183    pub part: ContentPart,
184}
185
186#[derive(Debug, Serialize, Deserialize, Clone)]
187pub struct ResponseContentPartDoneEvent {
188    /// The unique ID of the server event.
189    pub event_id: String,
190    /// The ID of the response.
191    pub response_id: String,
192    /// The ID of the item to which the content part was added.
193    pub item_id: String,
194    /// The index of the output item in the response.
195    pub output_index: u32,
196    /// The index of the content part in the item's content array.
197    pub content_index: u32,
198    /// The content part that is done.
199    pub part: ContentPart,
200}
201
202#[derive(Debug, Serialize, Deserialize, Clone)]
203pub struct ResponseTextDeltaEvent {
204    /// The unique ID of the server event.
205    pub event_id: String,
206    /// The ID of the response.
207    pub response_id: String,
208    /// The ID of the item.
209    pub item_id: String,
210    /// The index of the output item in the response.
211    pub output_index: u32,
212    /// The index of the content part in the item's content array.
213    pub content_index: u32,
214    /// The text delta.
215    pub delta: String,
216}
217
218#[derive(Debug, Serialize, Deserialize, Clone)]
219pub struct ResponseTextDoneEvent {
220    /// The unique ID of the server event.
221    pub event_id: String,
222    /// The ID of the response.
223    pub response_id: String,
224    /// The ID of the item.
225    pub item_id: String,
226    /// The index of the output item in the response.
227    pub output_index: u32,
228    /// The index of the content part in the item's content array.
229    pub content_index: u32,
230    /// The final text content.
231    pub text: String,
232}
233
234#[derive(Debug, Serialize, Deserialize, Clone)]
235pub struct ResponseAudioTranscriptDeltaEvent {
236    /// The unique ID of the server event.
237    pub event_id: String,
238    /// The ID of the response.
239    pub response_id: String,
240    /// The ID of the item.
241    pub item_id: String,
242    /// The index of the output item in the response.
243    pub output_index: u32,
244    /// The index of the content part in the item's content array.
245    pub content_index: u32,
246    /// The text delta.
247    pub delta: String,
248}
249
250#[derive(Debug, Serialize, Deserialize, Clone)]
251pub struct ResponseAudioTranscriptDoneEvent {
252    /// The unique ID of the server event.
253    pub event_id: String,
254    /// The ID of the response.
255    pub response_id: String,
256    /// The ID of the item.
257    pub item_id: String,
258    /// The index of the output item in the response.
259    pub output_index: u32,
260    /// The index of the content part in the item's content array.
261    pub content_index: u32,
262    ///The final transcript of the audio.
263    pub transcript: String,
264}
265
266#[derive(Debug, Serialize, Deserialize, Clone)]
267pub struct ResponseAudioDeltaEvent {
268    /// The unique ID of the server event.
269    pub event_id: String,
270    /// The ID of the response.
271    pub response_id: String,
272    /// The ID of the item.
273    pub item_id: String,
274    /// The index of the output item in the response.
275    pub output_index: u32,
276    /// The index of the content part in the item's content array.
277    pub content_index: u32,
278    /// Base64-encoded audio data delta.
279    pub delta: String,
280}
281
282#[derive(Debug, Serialize, Deserialize, Clone)]
283pub struct ResponseAudioDoneEvent {
284    /// The unique ID of the server event.
285    pub event_id: String,
286    /// The ID of the response.
287    pub response_id: String,
288    /// The ID of the item.
289    pub item_id: String,
290    /// The index of the output item in the response.
291    pub output_index: u32,
292    /// The index of the content part in the item's content array.
293    pub content_index: u32,
294}
295
296#[derive(Debug, Serialize, Deserialize, Clone)]
297pub struct ResponseFunctionCallArgumentsDeltaEvent {
298    /// The unique ID of the server event.
299    pub event_id: String,
300    /// The ID of the response.
301    pub response_id: String,
302    /// The ID of the function call item.
303    pub item_id: String,
304    /// The index of the output item in the response.
305    pub output_index: u32,
306    /// The ID of the function call.
307    pub call_id: String,
308    /// The arguments delta as a JSON string.
309    pub delta: String,
310}
311
312#[derive(Debug, Serialize, Deserialize, Clone)]
313pub struct ResponseFunctionCallArgumentsDoneEvent {
314    /// The unique ID of the server event.
315    pub event_id: String,
316    /// The ID of the response.
317    pub response_id: String,
318    /// The ID of the function call item.
319    pub item_id: String,
320    /// The index of the output item in the response.
321    pub output_index: u32,
322    /// The ID of the function call.
323    pub call_id: String,
324    /// The final arguments as a JSON string.
325    pub arguments: String,
326}
327
328#[derive(Debug, Serialize, Deserialize, Clone)]
329pub struct RateLimitsUpdatedEvent {
330    /// The unique ID of the server event.
331    pub event_id: String,
332    pub rate_limits: Vec<RateLimit>,
333}
334
335/// These are events emitted from the OpenAI Realtime WebSocket server to the client.
336#[derive(Debug, Serialize, Deserialize, Clone)]
337#[serde(tag = "type")]
338pub enum ServerEvent {
339    /// Returned when an error occurs.
340    #[serde(rename = "error")]
341    Error(ErrorEvent),
342
343    /// Returned when a session is created. Emitted automatically when a new connection is established.
344    #[serde(rename = "session.created")]
345    SessionCreated(SessionCreatedEvent),
346
347    /// Returned when a session is updated.
348    #[serde(rename = "session.updated")]
349    SessionUpdated(SessionUpdatedEvent),
350
351    /// Returned when a conversation is created. Emitted right after session creation.
352    #[serde(rename = "conversation.created")]
353    ConversationCreated(ConversationCreatedEvent),
354
355    /// Returned when an input audio buffer is committed, either by the client or automatically in server VAD mode.
356    #[serde(rename = "input_audio_buffer.committed")]
357    InputAudioBufferCommited(InputAudioBufferCommitedEvent),
358
359    /// Returned when the input audio buffer is cleared by the client.
360    #[serde(rename = "input_audio_buffer.cleared")]
361    InputAudioBufferCleared(InputAudioBufferClearedEvent),
362
363    /// Returned in server turn detection mode when speech is detected.
364    #[serde(rename = "input_audio_buffer.speech_started")]
365    InputAudioBufferSpeechStarted(InputAudioBufferSpeechStartedEvent),
366
367    /// Returned in server turn detection mode when speech stops.
368    #[serde(rename = "input_audio_buffer.speech_stopped")]
369    InputAudioBufferSpeechStopped(InputAudioBufferSpeechStoppedEvent),
370
371    /// Returned when a conversation item is created.
372    #[serde(rename = "conversation.item.created")]
373    ConversationItemCreated(ConversationItemCreatedEvent),
374
375    /// Returned when input audio transcription is enabled and a transcription succeeds.
376    #[serde(rename = "conversation.item.input_audio_transcription.completed")]
377    ConversationItemInputAudioTranscriptionCompleted(
378        ConversationItemInputAudioTranscriptionCompletedEvent,
379    ),
380
381    /// Returned when input audio transcription is configured, and a transcription request for a user message failed.
382    #[serde(rename = "conversation.item.input_audio_transcription.failed")]
383    ConversationItemInputAudioTranscriptionFailed(
384        ConversationItemInputAudioTranscriptionFailedEvent,
385    ),
386
387    /// Returned when an earlier assistant audio message item is truncated by the client.
388    #[serde(rename = "conversation.item.truncated")]
389    ConversationItemTruncated(ConversationItemTruncatedEvent),
390
391    /// Returned when an item in the conversation is deleted.
392    #[serde(rename = "conversation.item.deleted")]
393    ConversationItemDeleted(ConversationItemDeletedEvent),
394
395    /// Returned when a new Response is created. The first event of response creation, where the response is in an initial state of "in_progress".
396    #[serde(rename = "response.created")]
397    ResponseCreated(ResponseCreatedEvent),
398
399    /// Returned when a Response is done streaming. Always emitted, no matter the final state.
400    #[serde(rename = "response.done")]
401    ResponseDone(ResponseDoneEvent),
402
403    /// Returned when a new Item is created during response generation.
404    #[serde(rename = "response.output_item.added")]
405    ResponseOutputItemAdded(ResponseOutputItemAddedEvent),
406
407    /// Returned when an Item is done streaming. Also emitted when a Response is interrupted, incomplete, or cancelled.
408    #[serde(rename = "response.output_item.done")]
409    ResponseOutputItemDone(ResponseOutputItemDoneEvent),
410
411    /// Returned when a new content part is added to an assistant message item during response generation.
412    #[serde(rename = "response.content_part.added")]
413    ResponseContentPartAdded(ResponseContentPartAddedEvent),
414
415    /// Returned when a content part is done streaming in an assistant message item.
416    /// Also emitted when a Response is interrupted, incomplete, or cancelled.
417    #[serde(rename = "response.content_part.done")]
418    ResponseContentPartDone(ResponseContentPartDoneEvent),
419
420    /// Returned when the text value of a "text" content part is updated.
421    #[serde(rename = "response.text.delta")]
422    ResponseTextDelta(ResponseTextDeltaEvent),
423
424    /// Returned when the text value of a "text" content part is done streaming.
425    /// Also emitted when a Response is interrupted, incomplete, or cancelled.
426    #[serde(rename = "response.text.done")]
427    ResponseTextDone(ResponseTextDoneEvent),
428
429    /// Returned when the model-generated transcription of audio output is updated.
430    #[serde(rename = "response.audio_transcript.delta")]
431    ResponseAudioTranscriptDelta(ResponseAudioTranscriptDeltaEvent),
432
433    /// Returned when the model-generated transcription of audio output is done streaming.
434    /// Also emitted when a Response is interrupted, incomplete, or cancelled.
435    #[serde(rename = "response.audio_transcript.done")]
436    ResponseAudioTranscriptDone(ResponseAudioTranscriptDoneEvent),
437
438    /// Returned when the model-generated audio is updated.
439    #[serde(rename = "response.audio.delta")]
440    ResponseAudioDelta(ResponseAudioDeltaEvent),
441
442    /// Returned when the model-generated audio is done.
443    /// Also emitted when a Response is interrupted, incomplete, or cancelled.
444    #[serde(rename = "response.audio.done")]
445    ResponseAudioDone(ResponseAudioDoneEvent),
446
447    /// Returned when the model-generated function call arguments are updated.
448    #[serde(rename = "response.function_call_arguments.delta")]
449    ResponseFunctionCallArgumentsDelta(ResponseFunctionCallArgumentsDeltaEvent),
450
451    /// Returned when the model-generated function call arguments are done streaming.
452    /// Also emitted when a Response is interrupted, incomplete, or cancelled.
453    #[serde(rename = "response.function_call_arguments.done")]
454    ResponseFunctionCallArgumentsDone(ResponseFunctionCallArgumentsDoneEvent),
455
456    /// Emitted after every "response.done" event to indicate the updated rate limits.
457    #[serde(rename = "rate_limits.updated")]
458    RateLimitsUpdated(RateLimitsUpdatedEvent),
459}