Skip to main content

aimux_core/
image_model.rs

1//! The `ImageModel` trait — the provider-facing interface for image generation.
2//!
3//! Aligned with Vercel AI SDK `ImageModelV4`
4//! (`reference/ai/packages/provider/src/image-model/v4/`).
5
6use async_trait::async_trait;
7use serde::{Deserialize, Serialize};
8use ts_rs::TS;
9
10use crate::error::AiMuxError;
11use crate::shared::{
12    AbortSignal, AspectRatio, SharedHeaders, SharedProviderMetadata, SharedProviderOptions, Size,
13    Warning,
14};
15
16/// An image file used for image editing or variation generation.
17///
18/// Aligned with V4 `ImageModelV4File`.
19#[derive(Debug, Clone, Serialize, Deserialize, TS)]
20#[ts(export)]
21pub enum ImageFile {
22    /// Inline file data (base64 or binary) with an explicit media type.
23    File {
24        /// IANA media type, e.g. `"image/png"`.
25        media_type: String,
26        /// File data as base64 string or binary bytes.
27        data: ImageFileData,
28    },
29    /// A URL pointing to the image.
30    Url {
31        /// The URL of the image file.
32        url: String,
33    },
34}
35
36/// File payload for an [`ImageFile::File`]: base64 string or raw bytes.
37#[derive(Debug, Clone, Serialize, Deserialize, TS)]
38#[ts(export)]
39pub enum ImageFileData {
40    /// Base64-encoded string.
41    Base64(String),
42    /// Raw binary bytes.
43    Binary(Vec<u8>),
44}
45
46/// The generated images, returned as a homogeneous collection.
47///
48/// The TS result type is `Array<string> | Array<Uint8Array>` — i.e. either all
49/// base64 strings or all binary blobs, never mixed. This enum models that
50/// union. Providers should return data without unnecessary conversion: if the
51/// upstream API returns base64, return [`ImageOutputs::Base64`]; if binary,
52/// return [`ImageOutputs::Binary`].
53#[derive(Debug, Clone, Serialize, Deserialize, TS)]
54#[ts(export)]
55pub enum ImageOutputs {
56    /// Images as base64-encoded strings.
57    Base64(Vec<String>),
58    /// Images as raw binary bytes.
59    Binary(Vec<Vec<u8>>),
60}
61
62/// Token usage for an image generation call (if the provider reports it).
63///
64/// Aligned with V4 `ImageModelV4Usage`.
65#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
66#[ts(export)]
67pub struct ImageUsage {
68    /// Number of input (prompt) tokens used.
69    pub input_tokens: Option<u32>,
70    /// Number of output tokens used, if reported.
71    pub output_tokens: Option<u32>,
72    /// Total tokens as reported by the provider.
73    pub total_tokens: Option<u32>,
74}
75
76/// Options passed to [`ImageModel::do_generate`].
77///
78/// Aligned with V4 `ImageModelV4CallOptions`.
79#[derive(Debug, Clone, Serialize, Deserialize, TS)]
80#[ts(export)]
81pub struct ImageCallOptions {
82    /// Prompt for the image generation. `None` for operations (e.g. upscaling)
83    /// that do not require a prompt.
84    pub prompt: Option<String>,
85
86    /// Number of images to generate.
87    pub n: u32,
88
89    /// Size of the images, in `{width}x{height}` format.
90    /// `None` uses the provider's default size.
91    pub size: Option<Size>,
92
93    /// Aspect ratio of the images, in `{width}:{height}` format.
94    /// `None` uses the provider's default aspect ratio.
95    pub aspect_ratio: Option<AspectRatio>,
96
97    /// Seed for deterministic generation. `None` uses the provider's default.
98    pub seed: Option<u64>,
99
100    /// Images for image editing or variation generation.
101    pub files: Option<Vec<ImageFile>>,
102
103    /// Mask image for inpainting operations.
104    pub mask: Option<ImageFile>,
105
106    /// Additional provider-specific options, keyed by provider name.
107    pub provider_options: SharedProviderOptions,
108
109    /// Abort signal for cancelling the operation.
110    #[serde(skip)]
111    #[ts(skip)]
112    pub abort_signal: Option<AbortSignal>,
113
114    /// Additional HTTP headers to send with the request.
115    pub headers: Option<SharedHeaders>,
116}
117
118impl ImageCallOptions {
119    /// Create options with a prompt and `n = 1`, all other fields defaulted.
120    pub fn new(prompt: impl Into<String>) -> Self {
121        Self {
122            prompt: Some(prompt.into()),
123            n: 1,
124            size: None,
125            aspect_ratio: None,
126            seed: None,
127            files: None,
128            mask: None,
129            provider_options: SharedProviderOptions::new(),
130            abort_signal: None,
131            headers: None,
132        }
133    }
134}
135
136/// The result of [`ImageModel::do_generate`].
137///
138/// Aligned with V4 `ImageModelV4Result`.
139#[derive(Debug, Clone, Serialize, Deserialize, TS)]
140#[ts(export)]
141pub struct ImageResult {
142    /// Generated images (base64 strings or binary data).
143    pub images: ImageOutputs,
144
145    /// Warnings for the call, e.g. unsupported features.
146    pub warnings: Vec<Warning>,
147
148    /// Additional provider-specific metadata, keyed by provider name.
149    pub provider_metadata: Option<SharedProviderMetadata>,
150
151    /// Response information for telemetry and debugging.
152    pub response: ImageResponse,
153
154    /// Optional token usage for the call (if the provider reports it).
155    pub usage: Option<ImageUsage>,
156}
157
158/// Response information for an image generation call.
159#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
160#[ts(export)]
161pub struct ImageResponse {
162    /// Timestamp for the start of the generated response (ISO 8601 string).
163    pub timestamp: Option<String>,
164    /// The ID of the model that was used to generate the response.
165    pub model_id: Option<String>,
166    /// Response headers.
167    pub headers: Option<SharedHeaders>,
168}
169
170/// The unified image generation model trait (provider-facing).
171///
172/// Aligned with V4 `ImageModelV4`.
173#[async_trait]
174pub trait ImageModel: Send + Sync {
175    /// Specification version (always `"v4"`).
176    fn specification_version(&self) -> &'static str {
177        "v4"
178    }
179
180    /// Provider name, e.g. `"openai"`.
181    fn provider(&self) -> &str;
182
183    /// Provider-specific model ID, e.g. `"dall-e-3"`.
184    fn model_id(&self) -> &str;
185
186    /// Limit of how many images can be generated in a single API call.
187    ///
188    /// `None` means there is no fixed limit. The TS spec allows this to be a
189    /// function of `modelId`; in Rust the provider resolves it to a plain
190    /// `Option<u32>`.
191    fn max_images_per_call(&self) -> Option<u32>;
192
193    /// Generate an array of images.
194    ///
195    /// Naming: the `do_` prefix prevents accidental direct usage by users.
196    async fn do_generate(&self, options: &ImageCallOptions) -> Result<ImageResult, AiMuxError>;
197}