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 = std::pin::Pin<
107    Box<dyn futures::Stream<Item = Result<ImageEditStreamEvent, crate::error::OpenAIError>> + Send>,
108>;
109
110#[cfg(feature = "_api")]
111pub type ImageGenStream = std::pin::Pin<
112    Box<dyn futures::Stream<Item = Result<ImageGenStreamEvent, crate::error::OpenAIError>> + Send>,
113>;
114
115#[cfg(feature = "_api")]
116macro_rules! impl_event_type {
117    ($($ty:ty => $event_type:expr),* $(,)?) => {
118        $(
119            impl crate::traits::EventType for $ty {
120                fn event_type(&self) -> &'static str {
121                    $event_type
122                }
123            }
124        )*
125    };
126}
127
128#[cfg(feature = "_api")]
129impl_event_type! {
130    ImageGenPartialImageEvent => "image_generation.partial_image",
131    ImageGenCompletedEvent => "image_generation.completed",
132    ImageEditPartialImageEvent => "image_edit.partial_image",
133    ImageEditCompletedEvent => "image_edit.completed",
134}
135
136#[cfg(feature = "_api")]
137impl crate::traits::EventType for ImageGenStreamEvent {
138    fn event_type(&self) -> &'static str {
139        match self {
140            ImageGenStreamEvent::PartialImage(event) => event.event_type(),
141            ImageGenStreamEvent::Completed(event) => event.event_type(),
142        }
143    }
144}
145
146#[cfg(feature = "_api")]
147impl crate::traits::EventType for ImageEditStreamEvent {
148    fn event_type(&self) -> &'static str {
149        match self {
150            ImageEditStreamEvent::PartialImage(event) => event.event_type(),
151            ImageEditStreamEvent::Completed(event) => event.event_type(),
152        }
153    }
154}