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