Skip to main content

async_openai/types/images/
stream.rs

1use serde::{Deserialize, Serialize};
2
3use crate::types::images::{
4    ImageBackground, ImageGenUsage, ImageOutputFormat, ImageQuality, ImageSize,
5};
6
7/// Emitted when a partial image is available during image generation streaming.
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct ImageGenPartialImageEvent {
10    /// Base64-encoded partial image data, suitable for rendering as an image.
11    pub b64_json: String,
12    /// The Unix timestamp when the event was created.
13    pub created_at: u64,
14    /// The size of the requested image.
15    pub size: ImageSize,
16    /// The quality setting for the requested image.
17    pub quality: ImageQuality,
18    /// The background setting for the requested image.
19    pub background: ImageBackground,
20    /// The output format for the requested image.
21    pub output_format: ImageOutputFormat,
22    /// 0-based index for the partial image (streaming).
23    pub partial_image_index: u8,
24}
25
26/// Emitted when image generation has completed and the final image is available.
27#[derive(Debug, Clone, Serialize, Deserialize)]
28pub struct ImageGenCompletedEvent {
29    /// Base64-encoded image data, suitable for rendering as an image.
30    pub b64_json: String,
31    /// The Unix timestamp when the event was created.
32    pub created_at: u64,
33    /// The size of the generated image.
34    pub size: ImageSize,
35    /// The quality setting for the generated image.
36    pub quality: ImageQuality,
37    /// The background setting for the generated image.
38    pub background: ImageBackground,
39    /// The output format for the generated image.
40    pub output_format: ImageOutputFormat,
41    /// Token usage information for the image generation.
42    pub usage: ImageGenUsage,
43}
44
45#[derive(Debug, Clone, Serialize, Deserialize)]
46#[serde(tag = "type")]
47pub enum ImageGenStreamEvent {
48    /// Emitted when a partial image is available during image generation streaming.
49    #[serde(rename = "image_generation.partial_image")]
50    PartialImage(ImageGenPartialImageEvent),
51    /// Emitted when image generation has completed and the final image is available.
52    #[serde(rename = "image_generation.completed")]
53    Completed(ImageGenCompletedEvent),
54}
55
56/// Emitted when a partial image is available during image editing streaming.
57#[derive(Debug, Clone, Serialize, Deserialize)]
58pub struct ImageEditPartialImageEvent {
59    /// Base64-encoded partial image data, suitable for rendering as an image.
60    pub b64_json: String,
61    /// The Unix timestamp when the event was created.
62    pub created_at: u64,
63    /// The size of the requested edited image.
64    pub size: ImageSize,
65    /// The quality setting for the requested edited image.
66    pub quality: ImageQuality,
67    /// The background setting for the requested edited image.
68    pub background: ImageBackground,
69    /// The output format for the requested edited image.
70    pub output_format: ImageOutputFormat,
71    /// 0-based index for the partial image (streaming).
72    pub partial_image_index: u8,
73}
74
75/// Emitted when image editing has completed and the final image is available.
76#[derive(Debug, Clone, Serialize, Deserialize)]
77pub struct ImageEditCompletedEvent {
78    /// Base64-encoded final image data, suitable for rendering as an image.
79    pub b64_json: String,
80    /// The Unix timestamp when the event was created.
81    pub created_at: u64,
82    /// The size of the edited image.
83    pub size: ImageSize,
84    /// The quality setting for the edited image.
85    pub quality: ImageQuality,
86    /// The background setting for the edited image.
87    pub background: ImageBackground,
88    /// The output format for the edited image.
89    pub output_format: ImageOutputFormat,
90    /// Token usage information for the image edit.
91    pub usage: ImageGenUsage,
92}
93
94#[derive(Debug, Clone, Serialize, Deserialize)]
95#[serde(tag = "type")]
96pub enum ImageEditStreamEvent {
97    /// Emitted when a partial image is available during image editing streaming.
98    #[serde(rename = "image_edit.partial_image")]
99    PartialImage(ImageEditPartialImageEvent),
100    /// Emitted when image editing has completed and the final image is available.
101    #[serde(rename = "image_edit.completed")]
102    Completed(ImageEditCompletedEvent),
103}
104
105#[cfg(feature = "_api")]
106pub type ImageEditStream = crate::types::stream::StreamResponse<ImageEditStreamEvent>;
107
108#[cfg(feature = "_api")]
109pub type ImageGenStream = crate::types::stream::StreamResponse<ImageGenStreamEvent>;
110
111#[cfg(feature = "_api")]
112macro_rules! impl_event_type {
113    ($($ty:ty => $event_type:expr),* $(,)?) => {
114        $(
115            impl crate::traits::EventType for $ty {
116                fn event_type(&self) -> &'static str {
117                    $event_type
118                }
119            }
120        )*
121    };
122}
123
124#[cfg(feature = "_api")]
125impl_event_type! {
126    ImageGenPartialImageEvent => "image_generation.partial_image",
127    ImageGenCompletedEvent => "image_generation.completed",
128    ImageEditPartialImageEvent => "image_edit.partial_image",
129    ImageEditCompletedEvent => "image_edit.completed",
130}
131
132#[cfg(feature = "_api")]
133impl crate::traits::EventType for ImageGenStreamEvent {
134    fn event_type(&self) -> &'static str {
135        match self {
136            ImageGenStreamEvent::PartialImage(event) => event.event_type(),
137            ImageGenStreamEvent::Completed(event) => event.event_type(),
138        }
139    }
140}
141
142#[cfg(feature = "_api")]
143impl crate::traits::EventType for ImageEditStreamEvent {
144    fn event_type(&self) -> &'static str {
145        match self {
146            ImageEditStreamEvent::PartialImage(event) => event.event_type(),
147            ImageEditStreamEvent::Completed(event) => event.event_type(),
148        }
149    }
150}