Skip to main content

outfox_openai/spec/webhooks/
webhooks_.rs

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