async_openai/types/
webhooks.rs

1use serde::{Deserialize, Serialize};
2
3use crate::traits::{EventId, EventType};
4
5/// Sent when a batch API request has been cancelled.
6#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
7pub struct WebhookBatchCancelled {
8    /// The Unix timestamp (in seconds) of when the batch API request was cancelled.
9    pub created_at: i64,
10
11    /// The unique ID of the event.
12    pub id: String,
13
14    /// Event data payload.
15    pub data: WebhookBatchData,
16
17    /// The object of the event. Always `event`.
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub object: Option<String>,
20}
21
22/// Sent when a batch API request has been completed.
23#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
24pub struct WebhookBatchCompleted {
25    /// The Unix timestamp (in seconds) of when the batch API request was completed.
26    pub created_at: i64,
27
28    /// The unique ID of the event.
29    pub id: String,
30
31    /// Event data payload.
32    pub data: WebhookBatchData,
33
34    /// The object of the event. Always `event`.
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub object: Option<String>,
37}
38
39/// Sent when a batch API request has expired.
40#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
41pub struct WebhookBatchExpired {
42    /// The Unix timestamp (in seconds) of when the batch API request expired.
43    pub created_at: i64,
44
45    /// The unique ID of the event.
46    pub id: String,
47
48    /// Event data payload.
49    pub data: WebhookBatchData,
50
51    /// The object of the event. Always `event`.
52    #[serde(skip_serializing_if = "Option::is_none")]
53    pub object: Option<String>,
54}
55
56/// Sent when a batch API request has failed.
57#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
58pub struct WebhookBatchFailed {
59    /// The Unix timestamp (in seconds) of when the batch API request failed.
60    pub created_at: i64,
61
62    /// The unique ID of the event.
63    pub id: String,
64
65    /// Event data payload.
66    pub data: WebhookBatchData,
67
68    /// The object of the event. Always `event`.
69    #[serde(skip_serializing_if = "Option::is_none")]
70    pub object: Option<String>,
71}
72
73/// Data payload for batch webhook events.
74#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
75pub struct WebhookBatchData {
76    /// The unique ID of the batch API request.
77    pub id: String,
78}
79
80// EventType and EventId implementations for batch events
81
82impl EventType for WebhookBatchCancelled {
83    fn event_type(&self) -> &'static str {
84        "batch.cancelled"
85    }
86}
87
88impl EventId for WebhookBatchCancelled {
89    fn event_id(&self) -> &str {
90        &self.id
91    }
92}
93
94impl EventType for WebhookBatchCompleted {
95    fn event_type(&self) -> &'static str {
96        "batch.completed"
97    }
98}
99
100impl EventId for WebhookBatchCompleted {
101    fn event_id(&self) -> &str {
102        &self.id
103    }
104}
105
106impl EventType for WebhookBatchExpired {
107    fn event_type(&self) -> &'static str {
108        "batch.expired"
109    }
110}
111
112impl EventId for WebhookBatchExpired {
113    fn event_id(&self) -> &str {
114        &self.id
115    }
116}
117
118impl EventType for WebhookBatchFailed {
119    fn event_type(&self) -> &'static str {
120        "batch.failed"
121    }
122}
123
124impl EventId for WebhookBatchFailed {
125    fn event_id(&self) -> &str {
126        &self.id
127    }
128}
129
130/// Sent when an eval run has been canceled.
131#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
132pub struct WebhookEvalRunCanceled {
133    /// The Unix timestamp (in seconds) of when the eval run was canceled.
134    pub created_at: i64,
135
136    /// The unique ID of the event.
137    pub id: String,
138
139    /// Event data payload.
140    pub data: WebhookEvalRunData,
141
142    /// The object of the event. Always `event`.
143    #[serde(skip_serializing_if = "Option::is_none")]
144    pub object: Option<String>,
145}
146
147/// Sent when an eval run has failed.
148#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
149pub struct WebhookEvalRunFailed {
150    /// The Unix timestamp (in seconds) of when the eval run failed.
151    pub created_at: i64,
152
153    /// The unique ID of the event.
154    pub id: String,
155
156    /// Event data payload.
157    pub data: WebhookEvalRunData,
158
159    /// The object of the event. Always `event`.
160    #[serde(skip_serializing_if = "Option::is_none")]
161    pub object: Option<String>,
162}
163
164/// Sent when an eval run has succeeded.
165#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
166pub struct WebhookEvalRunSucceeded {
167    /// The Unix timestamp (in seconds) of when the eval run succeeded.
168    pub created_at: i64,
169
170    /// The unique ID of the event.
171    pub id: String,
172
173    /// Event data payload.
174    pub data: WebhookEvalRunData,
175
176    /// The object of the event. Always `event`.
177    #[serde(skip_serializing_if = "Option::is_none")]
178    pub object: Option<String>,
179}
180
181/// Data payload for eval run webhook events.
182#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
183pub struct WebhookEvalRunData {
184    /// The unique ID of the eval run.
185    pub id: String,
186}
187
188// EventType and EventId implementations for eval run events
189
190impl EventType for WebhookEvalRunCanceled {
191    fn event_type(&self) -> &'static str {
192        "eval.run.canceled"
193    }
194}
195
196impl EventId for WebhookEvalRunCanceled {
197    fn event_id(&self) -> &str {
198        &self.id
199    }
200}
201
202impl EventType for WebhookEvalRunFailed {
203    fn event_type(&self) -> &'static str {
204        "eval.run.failed"
205    }
206}
207
208impl EventId for WebhookEvalRunFailed {
209    fn event_id(&self) -> &str {
210        &self.id
211    }
212}
213
214impl EventType for WebhookEvalRunSucceeded {
215    fn event_type(&self) -> &'static str {
216        "eval.run.succeeded"
217    }
218}
219
220impl EventId for WebhookEvalRunSucceeded {
221    fn event_id(&self) -> &str {
222        &self.id
223    }
224}
225
226/// Sent when a fine-tuning job has been cancelled.
227#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
228pub struct WebhookFineTuningJobCancelled {
229    /// The Unix timestamp (in seconds) of when the fine-tuning job was cancelled.
230    pub created_at: i64,
231
232    /// The unique ID of the event.
233    pub id: String,
234
235    /// Event data payload.
236    pub data: WebhookFineTuningJobData,
237
238    /// The object of the event. Always `event`.
239    #[serde(skip_serializing_if = "Option::is_none")]
240    pub object: Option<String>,
241}
242
243/// Sent when a fine-tuning job has failed.
244#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
245pub struct WebhookFineTuningJobFailed {
246    /// The Unix timestamp (in seconds) of when the fine-tuning job failed.
247    pub created_at: i64,
248
249    /// The unique ID of the event.
250    pub id: String,
251
252    /// Event data payload.
253    pub data: WebhookFineTuningJobData,
254
255    /// The object of the event. Always `event`.
256    #[serde(skip_serializing_if = "Option::is_none")]
257    pub object: Option<String>,
258}
259
260/// Sent when a fine-tuning job has succeeded.
261#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
262pub struct WebhookFineTuningJobSucceeded {
263    /// The Unix timestamp (in seconds) of when the fine-tuning job succeeded.
264    pub created_at: i64,
265
266    /// The unique ID of the event.
267    pub id: String,
268
269    /// Event data payload.
270    pub data: WebhookFineTuningJobData,
271
272    /// The object of the event. Always `event`.
273    #[serde(skip_serializing_if = "Option::is_none")]
274    pub object: Option<String>,
275}
276
277/// Data payload for fine-tuning job webhook events.
278#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
279pub struct WebhookFineTuningJobData {
280    /// The unique ID of the fine-tuning job.
281    pub id: String,
282}
283
284// EventType and EventId implementations for fine-tuning job events
285
286impl EventType for WebhookFineTuningJobCancelled {
287    fn event_type(&self) -> &'static str {
288        "fine_tuning.job.cancelled"
289    }
290}
291
292impl EventId for WebhookFineTuningJobCancelled {
293    fn event_id(&self) -> &str {
294        &self.id
295    }
296}
297
298impl EventType for WebhookFineTuningJobFailed {
299    fn event_type(&self) -> &'static str {
300        "fine_tuning.job.failed"
301    }
302}
303
304impl EventId for WebhookFineTuningJobFailed {
305    fn event_id(&self) -> &str {
306        &self.id
307    }
308}
309
310impl EventType for WebhookFineTuningJobSucceeded {
311    fn event_type(&self) -> &'static str {
312        "fine_tuning.job.succeeded"
313    }
314}
315
316impl EventId for WebhookFineTuningJobSucceeded {
317    fn event_id(&self) -> &str {
318        &self.id
319    }
320}
321
322/// Sent when Realtime API receives an incoming SIP call.
323#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
324pub struct WebhookRealtimeCallIncoming {
325    /// The Unix timestamp (in seconds) of when the model response was completed.
326    pub created_at: i64,
327
328    /// The unique ID of the event.
329    pub id: String,
330
331    /// Event data payload.
332    pub data: WebhookRealtimeCallData,
333
334    /// The object of the event. Always `event`.
335    #[serde(skip_serializing_if = "Option::is_none")]
336    pub object: Option<String>,
337}
338
339/// Data payload for realtime call webhook events.
340#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
341pub struct WebhookRealtimeCallData {
342    /// The unique ID of this call.
343    pub call_id: String,
344
345    /// Headers from the SIP Invite.
346    pub sip_headers: Vec<SipHeader>,
347}
348
349/// A header from the SIP Invite.
350#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
351pub struct SipHeader {
352    /// Name of the SIP Header.
353    pub name: String,
354
355    /// Value of the SIP Header.
356    pub value: String,
357}
358
359// EventType and EventId implementations for realtime call events
360
361impl EventType for WebhookRealtimeCallIncoming {
362    fn event_type(&self) -> &'static str {
363        "realtime.call.incoming"
364    }
365}
366
367impl EventId for WebhookRealtimeCallIncoming {
368    fn event_id(&self) -> &str {
369        &self.id
370    }
371}
372
373/// Sent when a background response has been cancelled.
374#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
375pub struct WebhookResponseCancelled {
376    /// The Unix timestamp (in seconds) of when the model response was cancelled.
377    pub created_at: i64,
378
379    /// The unique ID of the event.
380    pub id: String,
381
382    /// Event data payload.
383    pub data: WebhookResponseData,
384
385    /// The object of the event. Always `event`.
386    #[serde(skip_serializing_if = "Option::is_none")]
387    pub object: Option<String>,
388}
389
390/// Sent when a background response has been completed.
391#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
392pub struct WebhookResponseCompleted {
393    /// The Unix timestamp (in seconds) of when the model response was completed.
394    pub created_at: i64,
395
396    /// The unique ID of the event.
397    pub id: String,
398
399    /// Event data payload.
400    pub data: WebhookResponseData,
401
402    /// The object of the event. Always `event`.
403    #[serde(skip_serializing_if = "Option::is_none")]
404    pub object: Option<String>,
405}
406
407/// Sent when a background response has failed.
408#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
409pub struct WebhookResponseFailed {
410    /// The Unix timestamp (in seconds) of when the model response failed.
411    pub created_at: i64,
412
413    /// The unique ID of the event.
414    pub id: String,
415
416    /// Event data payload.
417    pub data: WebhookResponseData,
418
419    /// The object of the event. Always `event`.
420    #[serde(skip_serializing_if = "Option::is_none")]
421    pub object: Option<String>,
422}
423
424/// Sent when a background response has been interrupted.
425#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
426pub struct WebhookResponseIncomplete {
427    /// The Unix timestamp (in seconds) of when the model response was interrupted.
428    pub created_at: i64,
429
430    /// The unique ID of the event.
431    pub id: String,
432
433    /// Event data payload.
434    pub data: WebhookResponseData,
435
436    /// The object of the event. Always `event`.
437    #[serde(skip_serializing_if = "Option::is_none")]
438    pub object: Option<String>,
439}
440
441/// Data payload for response webhook events.
442#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
443pub struct WebhookResponseData {
444    /// The unique ID of the model response.
445    pub id: String,
446}
447
448// EventType and EventId implementations for response events
449
450impl EventType for WebhookResponseCancelled {
451    fn event_type(&self) -> &'static str {
452        "response.cancelled"
453    }
454}
455
456impl EventId for WebhookResponseCancelled {
457    fn event_id(&self) -> &str {
458        &self.id
459    }
460}
461
462impl EventType for WebhookResponseCompleted {
463    fn event_type(&self) -> &'static str {
464        "response.completed"
465    }
466}
467
468impl EventId for WebhookResponseCompleted {
469    fn event_id(&self) -> &str {
470        &self.id
471    }
472}
473
474impl EventType for WebhookResponseFailed {
475    fn event_type(&self) -> &'static str {
476        "response.failed"
477    }
478}
479
480impl EventId for WebhookResponseFailed {
481    fn event_id(&self) -> &str {
482        &self.id
483    }
484}
485
486impl EventType for WebhookResponseIncomplete {
487    fn event_type(&self) -> &'static str {
488        "response.incomplete"
489    }
490}
491
492impl EventId for WebhookResponseIncomplete {
493    fn event_id(&self) -> &str {
494        &self.id
495    }
496}
497
498#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
499#[serde(tag = "type")]
500pub enum WebhookEvent {
501    #[serde(rename = "batch.cancelled")]
502    BatchCancelled(WebhookBatchCancelled),
503
504    #[serde(rename = "batch.completed")]
505    BatchCompleted(WebhookBatchCompleted),
506
507    #[serde(rename = "batch.expired")]
508    BatchExpired(WebhookBatchExpired),
509
510    #[serde(rename = "batch.failed")]
511    BatchFailed(WebhookBatchFailed),
512
513    #[serde(rename = "eval.run.canceled")]
514    EvalRunCanceled(WebhookEvalRunCanceled),
515
516    #[serde(rename = "eval.run.failed")]
517    EvalRunFailed(WebhookEvalRunFailed),
518
519    #[serde(rename = "eval.run.succeeded")]
520    EvalRunSucceeded(WebhookEvalRunSucceeded),
521
522    #[serde(rename = "fine_tuning.job.cancelled")]
523    FineTuningJobCancelled(WebhookFineTuningJobCancelled),
524
525    #[serde(rename = "fine_tuning.job.failed")]
526    FineTuningJobFailed(WebhookFineTuningJobFailed),
527
528    #[serde(rename = "fine_tuning.job.succeeded")]
529    FineTuningJobSucceeded(WebhookFineTuningJobSucceeded),
530
531    #[serde(rename = "realtime.call.incoming")]
532    RealtimeCallIncoming(WebhookRealtimeCallIncoming),
533
534    #[serde(rename = "response.cancelled")]
535    ResponseCancelled(WebhookResponseCancelled),
536
537    #[serde(rename = "response.completed")]
538    ResponseCompleted(WebhookResponseCompleted),
539
540    #[serde(rename = "response.failed")]
541    ResponseFailed(WebhookResponseFailed),
542
543    #[serde(rename = "response.incomplete")]
544    ResponseIncomplete(WebhookResponseIncomplete),
545}
546
547// Trait implementations for WebhookEvent enum
548
549impl EventType for WebhookEvent {
550    fn event_type(&self) -> &'static str {
551        match self {
552            WebhookEvent::BatchCancelled(e) => e.event_type(),
553            WebhookEvent::BatchCompleted(e) => e.event_type(),
554            WebhookEvent::BatchExpired(e) => e.event_type(),
555            WebhookEvent::BatchFailed(e) => e.event_type(),
556            WebhookEvent::EvalRunCanceled(e) => e.event_type(),
557            WebhookEvent::EvalRunFailed(e) => e.event_type(),
558            WebhookEvent::EvalRunSucceeded(e) => e.event_type(),
559            WebhookEvent::FineTuningJobCancelled(e) => e.event_type(),
560            WebhookEvent::FineTuningJobFailed(e) => e.event_type(),
561            WebhookEvent::FineTuningJobSucceeded(e) => e.event_type(),
562            WebhookEvent::RealtimeCallIncoming(e) => e.event_type(),
563            WebhookEvent::ResponseCancelled(e) => e.event_type(),
564            WebhookEvent::ResponseCompleted(e) => e.event_type(),
565            WebhookEvent::ResponseFailed(e) => e.event_type(),
566            WebhookEvent::ResponseIncomplete(e) => e.event_type(),
567        }
568    }
569}
570
571impl EventId for WebhookEvent {
572    fn event_id(&self) -> &str {
573        match self {
574            WebhookEvent::BatchCancelled(e) => e.event_id(),
575            WebhookEvent::BatchCompleted(e) => e.event_id(),
576            WebhookEvent::BatchExpired(e) => e.event_id(),
577            WebhookEvent::BatchFailed(e) => e.event_id(),
578            WebhookEvent::EvalRunCanceled(e) => e.event_id(),
579            WebhookEvent::EvalRunFailed(e) => e.event_id(),
580            WebhookEvent::EvalRunSucceeded(e) => e.event_id(),
581            WebhookEvent::FineTuningJobCancelled(e) => e.event_id(),
582            WebhookEvent::FineTuningJobFailed(e) => e.event_id(),
583            WebhookEvent::FineTuningJobSucceeded(e) => e.event_id(),
584            WebhookEvent::RealtimeCallIncoming(e) => e.event_id(),
585            WebhookEvent::ResponseCancelled(e) => e.event_id(),
586            WebhookEvent::ResponseCompleted(e) => e.event_id(),
587            WebhookEvent::ResponseFailed(e) => e.event_id(),
588            WebhookEvent::ResponseIncomplete(e) => e.event_id(),
589        }
590    }
591}
592
593impl WebhookEvent {
594    /// Get the timestamp when the event was created
595    pub fn created_at(&self) -> i64 {
596        match self {
597            WebhookEvent::BatchCancelled(w) => w.created_at,
598            WebhookEvent::BatchCompleted(w) => w.created_at,
599            WebhookEvent::BatchExpired(w) => w.created_at,
600            WebhookEvent::BatchFailed(w) => w.created_at,
601            WebhookEvent::EvalRunCanceled(w) => w.created_at,
602            WebhookEvent::EvalRunFailed(w) => w.created_at,
603            WebhookEvent::EvalRunSucceeded(w) => w.created_at,
604            WebhookEvent::FineTuningJobCancelled(w) => w.created_at,
605            WebhookEvent::FineTuningJobFailed(w) => w.created_at,
606            WebhookEvent::FineTuningJobSucceeded(w) => w.created_at,
607            WebhookEvent::RealtimeCallIncoming(w) => w.created_at,
608            WebhookEvent::ResponseCancelled(w) => w.created_at,
609            WebhookEvent::ResponseCompleted(w) => w.created_at,
610            WebhookEvent::ResponseFailed(w) => w.created_at,
611            WebhookEvent::ResponseIncomplete(w) => w.created_at,
612        }
613    }
614}