async_openai/types/images/
stream.rs

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