agentai-image 0.5.0

Image generation and editing tools for AgentAI agents.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
//! # OpenAI image API
//!
//! [`OpenAiImageProvider`] generates images with `POST /images/generations` and edits them
//! with `POST /images/edits`, and lists models with `GET /models`. It works with OpenAI
//! (`gpt-image-*`, `dall-e-*`) and with other APIs that follow the same format.

use super::{ImageEditRequest, ImageError, ImageModel, ImageProvider, ImageRequest, ImageResponse};
use agentai_core::message::{Media, MediaSource};
use agentai_core::{HttpClient, HttpRequest, HttpResponse, Multipart, ReqwestClient};
use async_trait::async_trait;
use base64::Engine;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

/// The OpenAI API endpoint.
pub const DEFAULT_BASE_URL: &str = "https://api.openai.com/v1";
/// Environment variable with the API key.
pub const API_KEY_ENV: &str = "OPENAI_API_KEY";
/// Environment variable with the base URL, for OpenAI-compatible APIs.
pub const BASE_URL_ENV: &str = "OPENAI_BASE_URL";

/// Provider for the OpenAI image API and APIs compatible with it.
///
/// ```rust,no_run
/// # use agentai_image::provider::openai::OpenAiImageProvider;
/// # fn main() -> Result<(), agentai_image::ImageError> {
/// // From `OPENAI_API_KEY` and `OPENAI_BASE_URL`
/// let provider = OpenAiImageProvider::from_env("gpt-image-1")?;
/// // Or everything in code
/// let provider = OpenAiImageProvider::new("gpt-image-1", "sk-...", "https://api.openai.com/v1");
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct OpenAiImageProvider {
    model: String,
    api_key: String,
    base_url: String,
    client: Arc<dyn HttpClient>,
}

impl std::fmt::Debug for OpenAiImageProvider {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("OpenAiImageProvider")
            .field("model", &self.model)
            .field("base_url", &self.base_url)
            .finish_non_exhaustive()
    }
}

impl OpenAiImageProvider {
    /// Creates a provider for `model` with the API key and base URL given in code, for example
    /// [`DEFAULT_BASE_URL`]. An empty key sends no `Authorization` header.
    pub fn new(
        model: impl Into<String>,
        api_key: impl Into<String>,
        base_url: impl Into<String>,
    ) -> Self {
        Self {
            model: model.into(),
            api_key: api_key.into(),
            base_url: base_url.into().trim_end_matches('/').to_string(),
            client: Arc::new(ReqwestClient::new()),
        }
    }

    /// Creates a provider for `model` with the API key from `OPENAI_API_KEY` and the base URL
    /// from `OPENAI_BASE_URL`, or [`DEFAULT_BASE_URL`] when it is not set. The key may be left
    /// out when `OPENAI_BASE_URL` is set, for local servers without authentication.
    pub fn from_env(model: impl Into<String>) -> Result<Self, ImageError> {
        Self::from_lookup(model, |name| std::env::var(name).ok())
    }

    fn from_lookup(
        model: impl Into<String>,
        lookup: impl Fn(&str) -> Option<String>,
    ) -> Result<Self, ImageError> {
        let base_url = lookup(BASE_URL_ENV);
        let api_key = match (lookup(API_KEY_ENV), &base_url) {
            (Some(key), _) => key,
            (None, Some(_)) => String::new(),
            (None, None) => return Err(ImageError::MissingApiKey(API_KEY_ENV)),
        };
        let base_url = base_url.unwrap_or_else(|| DEFAULT_BASE_URL.to_string());
        Ok(Self::new(model, api_key, base_url))
    }

    /// Model name sent to the API.
    pub fn model(&self) -> &str {
        &self.model
    }

    #[cfg(test)]
    fn with_client(mut self, client: Arc<dyn HttpClient>) -> Self {
        self.client = client;
        self
    }

    async fn send(&self, request: HttpRequest) -> Result<ImageResponse, ImageError> {
        parse_response(&send_request(&*self.client, &self.api_key, request).await?)
    }

    /// Bytes of an image to upload: decoded base64, or downloaded from its URL.
    async fn image_bytes(&self, media: &Media) -> Result<Vec<u8>, ImageError> {
        match &media.source {
            MediaSource::Base64(data) => base64::engine::general_purpose::STANDARD
                .decode(data)
                .map_err(|e| ImageError::InvalidResponse(format!("invalid base64 image: {e}"))),
            MediaSource::Url(url) => {
                let response = self.client.send(HttpRequest::get(url)).await?;
                if !response.is_success() {
                    return Err(api_error(&response));
                }
                Ok(response.body)
            }
        }
    }
}

#[derive(Serialize)]
struct GenerationBody<'a> {
    model: &'a str,
    prompt: &'a str,
    #[serde(skip_serializing_if = "Option::is_none")]
    size: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    quality: Option<&'a str>,
}

#[async_trait]
impl ImageProvider for OpenAiImageProvider {
    fn model(&self) -> Option<&str> {
        Some(&self.model)
    }

    async fn generate(&self, request: ImageRequest) -> Result<ImageResponse, ImageError> {
        // No `response_format`: gpt-image models reject it and always answer with base64
        let body = GenerationBody {
            model: &self.model,
            prompt: &request.prompt,
            size: request.size.as_deref(),
            quality: request.quality.as_deref(),
        };
        let http =
            HttpRequest::post(format!("{}/images/generations", self.base_url)).with_json(&body)?;
        self.send(http).await
    }

    fn supports_edit(&self) -> bool {
        true
    }

    async fn edit(&self, request: ImageEditRequest) -> Result<ImageResponse, ImageError> {
        let mut form = Multipart::new()
            .with_text("model", self.model.as_str())
            .with_text("prompt", request.prompt);
        if let Some(size) = request.size {
            form = form.with_text("size", size);
        }
        if let Some(quality) = request.quality {
            form = form.with_text("quality", quality);
        }
        for (index, image) in request.images.iter().enumerate() {
            let bytes = self.image_bytes(image).await?;
            let filename = image.name.clone().unwrap_or_else(|| {
                let extension = image.content_type.rsplit('/').next().unwrap_or("png");
                format!("image-{}.{extension}", index + 1)
            });
            form = form.with_file("image[]", filename, image.content_type.as_str(), bytes);
        }
        let http =
            HttpRequest::post(format!("{}/images/edits", self.base_url)).with_multipart(form);
        self.send(http).await
    }

    /// Lists the models of `GET /models`. On the OpenAI API only image models (`gpt-image-*`,
    /// `dall-e-*`) are kept; compatible servers keep every model they list.
    async fn list_models(&self) -> Result<Vec<ImageModel>, ImageError> {
        let request = HttpRequest::get(format!("{}/models", self.base_url));
        let response = send_request(&*self.client, &self.api_key, request).await?;
        let list: ModelList = response
            .json()
            .map_err(|e| ImageError::InvalidResponse(e.to_string()))?;
        let official = self.base_url == DEFAULT_BASE_URL;
        Ok(list
            .data
            .into_iter()
            .filter(|entry| {
                !official || entry.id.starts_with("gpt-image") || entry.id.starts_with("dall-e")
            })
            .map(|entry| ImageModel::new(format!("openai::{}", entry.id), entry.id))
            .collect())
    }
}

#[derive(Deserialize)]
struct ResponseBody {
    #[serde(default)]
    data: Vec<ImageData>,
    output_format: Option<String>,
}

#[derive(Deserialize)]
struct ImageData {
    b64_json: Option<String>,
    url: Option<String>,
    revised_prompt: Option<String>,
    /// Sent by OpenRouter.
    media_type: Option<String>,
}

#[derive(Deserialize)]
struct ModelList {
    #[serde(default)]
    data: Vec<ModelEntry>,
}

#[derive(Deserialize)]
struct ModelEntry {
    id: String,
}

/// Sends a request with `Authorization: Bearer <key>` (none for an empty key) and turns an
/// error status into [`ImageError::Api`].
pub(super) async fn send_request(
    client: &dyn HttpClient,
    api_key: &str,
    request: HttpRequest,
) -> Result<HttpResponse, ImageError> {
    let request = if api_key.is_empty() {
        request
    } else {
        request.with_header("Authorization", format!("Bearer {api_key}"))
    };
    let response = client.send(request).await?;
    if !response.is_success() {
        return Err(api_error(&response));
    }
    Ok(response)
}

pub(super) fn parse_response(response: &HttpResponse) -> Result<ImageResponse, ImageError> {
    let body: ResponseBody = response
        .json()
        .map_err(|e| ImageError::InvalidResponse(e.to_string()))?;
    let data = body
        .data
        .into_iter()
        .next()
        .ok_or_else(|| ImageError::InvalidResponse("no image in the response".to_string()))?;
    let image = match (data.b64_json, data.url) {
        (Some(b64), _) => {
            // Compatible servers may leave out `output_format`; the bytes tell the type then
            let content_type = match (data.media_type, body.output_format.as_deref()) {
                (Some(media_type), _) => media_type,
                (None, Some("jpg")) => "image/jpeg".to_string(),
                (None, Some(format)) => format!("image/{format}"),
                (None, None) => sniff_image_type(&b64).to_string(),
            };
            Media::from_base64(content_type, b64)
        }
        (None, Some(url)) => Media::image_url(url),
        (None, None) => {
            return Err(ImageError::InvalidResponse(
                "the image has neither b64_json nor url".to_string(),
            ));
        }
    };
    let mut response = ImageResponse::new(image);
    response.revised_prompt = data.revised_prompt;
    Ok(response)
}

/// Image type from the first bytes of base64 data, `image/png` when unknown.
fn sniff_image_type(b64: &str) -> &'static str {
    let prefix: String = b64.chars().take(16).collect();
    let bytes = base64::engine::general_purpose::STANDARD
        .decode(prefix)
        .unwrap_or_default();
    match bytes.as_slice() {
        [0xFF, 0xD8, 0xFF, ..] => "image/jpeg",
        [b'G', b'I', b'F', b'8', ..] => "image/gif",
        [
            b'R',
            b'I',
            b'F',
            b'F',
            _,
            _,
            _,
            _,
            b'W',
            b'E',
            b'B',
            b'P',
            ..,
        ] => "image/webp",
        _ => "image/png",
    }
}

/// Reads `error.message` of an OpenAI error, or falls back to the body.
fn api_error(response: &HttpResponse) -> ImageError {
    let message = response
        .json::<serde_json::Value>()
        .ok()
        .and_then(|json| json["error"]["message"].as_str().map(str::to_string))
        .unwrap_or_else(|| response.text());
    ImageError::Api {
        status: response.status,
        message,
    }
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;
    use agentai_core::HttpError;
    use serde_json::{Value, json};
    use std::collections::VecDeque;
    use std::sync::Mutex;

    /// Records the requests and answers with the prepared responses, in order.
    #[derive(Default)]
    pub(crate) struct MockHttp {
        pub(crate) requests: Mutex<Vec<HttpRequest>>,
        responses: Mutex<VecDeque<HttpResponse>>,
    }

    impl MockHttp {
        pub(crate) fn new(responses: Vec<HttpResponse>) -> Arc<Self> {
            Arc::new(Self {
                requests: Mutex::default(),
                responses: Mutex::new(responses.into()),
            })
        }

        pub(crate) fn request(&self, index: usize) -> HttpRequest {
            self.requests.lock().unwrap()[index].clone()
        }
    }

    #[async_trait]
    impl HttpClient for MockHttp {
        async fn send(&self, request: HttpRequest) -> Result<HttpResponse, HttpError> {
            self.requests.lock().unwrap().push(request);
            Ok(self
                .responses
                .lock()
                .unwrap()
                .pop_front()
                .expect("no response left"))
        }
    }

    pub(crate) fn json_response(status: u16, body: Value) -> HttpResponse {
        HttpResponse::new(status, body.to_string())
    }

    pub(crate) fn provider(http: &Arc<MockHttp>) -> OpenAiImageProvider {
        OpenAiImageProvider::new("gpt-image-1", "key", "https://api.test/v1/")
            .with_client(http.clone())
    }

    fn body_json(request: &HttpRequest) -> Value {
        serde_json::from_slice(request.body.as_ref().unwrap()).unwrap()
    }

    #[tokio::test]
    async fn generate_sends_only_the_set_parameters_and_reads_base64() {
        let http = MockHttp::new(vec![
            json_response(
                200,
                json!({"data": [{"b64_json": "aGk=", "revised_prompt": "a red cat"}],
                       "output_format": "webp"}),
            ),
            json_response(200, json!({"data": [{"b64_json": "aGk="}]})),
        ]);
        let provider = provider(&http);

        let response = provider
            .generate(ImageRequest::new("a cat").with_size("1024x1024"))
            .await
            .unwrap();
        provider.generate(ImageRequest::new("a dog")).await.unwrap();

        let request = http.request(0);
        assert_eq!(request.url, "https://api.test/v1/images/generations");
        assert_eq!(request.header("authorization"), Some("Bearer key"));
        assert_eq!(
            body_json(&request),
            json!({"model": "gpt-image-1", "prompt": "a cat", "size": "1024x1024"})
        );
        assert_eq!(
            body_json(&http.request(1)),
            json!({"model": "gpt-image-1", "prompt": "a dog"})
        );
        assert_eq!(
            response,
            ImageResponse::new(Media::from_base64("image/webp", "aGk="))
                .with_revised_prompt("a red cat")
        );
    }

    #[tokio::test]
    async fn without_output_format_the_type_comes_from_the_bytes_and_no_key_sends_no_header() {
        use base64::Engine;
        let jpeg = base64::engine::general_purpose::STANDARD.encode([0xFF, 0xD8, 0xFF, 0xE0, 0, 1]);
        // As a local OpenAI-compatible server answers: `url: null`, no `output_format`
        let http = MockHttp::new(vec![json_response(
            200,
            json!({"created": 1, "data": [{"b64_json": jpeg, "url": null}]}),
        )]);
        let provider =
            OpenAiImageProvider::new("m", "", "http://local/v1").with_client(http.clone());

        let response = provider.generate(ImageRequest::new("x")).await.unwrap();

        assert_eq!(response.image.content_type, "image/jpeg");
        assert_eq!(http.request(0).header("authorization"), None);
    }

    #[tokio::test]
    async fn generate_keeps_a_url_without_downloading_it() {
        let http = MockHttp::new(vec![json_response(
            200,
            json!({"data": [{"url": "https://cdn.test/a.png"}]}),
        )]);

        let response = provider(&http)
            .generate(ImageRequest::new("a cat"))
            .await
            .unwrap();

        assert_eq!(response.image, Media::image_url("https://cdn.test/a.png"));
        assert_eq!(http.requests.lock().unwrap().len(), 1);
    }

    #[tokio::test]
    async fn api_errors_carry_the_message() {
        let http = MockHttp::new(vec![
            json_response(
                400,
                json!({"error": {"message": "rejected by the safety system"}}),
            ),
            HttpResponse::new(502, "bad gateway"),
        ]);
        let provider = provider(&http);

        let err = provider.generate(ImageRequest::new("x")).await.unwrap_err();
        assert!(
            matches!(err, ImageError::Api { status: 400, ref message } if message == "rejected by the safety system")
        );
        let err = provider.generate(ImageRequest::new("x")).await.unwrap_err();
        assert!(
            matches!(err, ImageError::Api { status: 502, ref message } if message == "bad gateway")
        );
    }

    #[tokio::test]
    async fn edit_uploads_every_image_as_multipart() {
        let http = MockHttp::new(vec![
            // The image given by URL is downloaded first
            HttpResponse::new(200, "JPEGBYTES"),
            json_response(200, json!({"data": [{"b64_json": "aGk="}]})),
        ]);
        let images = vec![
            Media::from_bytes("image/png", b"PNGBYTES").with_name("cat.png"),
            Media::image_url("https://cdn.test/dog.jpg"),
        ];

        provider(&http)
            .edit(ImageEditRequest::new("add a hat", images).with_quality("low"))
            .await
            .unwrap();

        assert_eq!(http.request(0).url, "https://cdn.test/dog.jpg");
        assert_eq!(http.request(0).header("authorization"), None);
        let request = http.request(1);
        assert_eq!(request.url, "https://api.test/v1/images/edits");
        let content_type = request.header("content-type").unwrap();
        let boundary = content_type
            .strip_prefix("multipart/form-data; boundary=")
            .unwrap();
        let body = String::from_utf8(request.body.clone().unwrap()).unwrap();
        let field = |name: &str, value: &str| {
            format!("Content-Disposition: form-data; name=\"{name}\"\r\n\r\n{value}\r\n")
        };
        let file = |filename: &str, content_type: &str, data: &str| {
            format!(
                "Content-Disposition: form-data; name=\"image[]\"; filename=\"{filename}\"\r\n\
                 Content-Type: {content_type}\r\n\r\n{data}\r\n"
            )
        };
        let parts: Vec<&str> = body
            .split(&format!("--{boundary}"))
            .map(|part| part.trim_start_matches("\r\n"))
            .collect();
        assert_eq!(
            parts,
            [
                "",
                &field("model", "gpt-image-1"),
                &field("prompt", "add a hat"),
                &field("quality", "low"),
                &file("cat.png", "image/png", "PNGBYTES"),
                &file("image-2.jpeg", "image/jpeg", "JPEGBYTES"),
                "--\r\n",
            ]
        );
    }

    #[tokio::test]
    async fn listing_keeps_only_image_models_on_the_openai_api() {
        let models = || {
            json_response(
                200,
                json!({"data": [{"id": "gpt-4o"}, {"id": "gpt-image-1"}, {"id": "dall-e-3"}]}),
            )
        };
        let http = MockHttp::new(vec![models(), models()]);
        let official = OpenAiImageProvider::new("", "key", DEFAULT_BASE_URL)
            .with_client(http.clone())
            .list_models()
            .await
            .unwrap();
        let compatible = provider(&http).list_models().await.unwrap();

        let ids = |models: &[ImageModel]| -> Vec<String> {
            models.iter().map(|m| m.model.clone()).collect()
        };
        assert_eq!(ids(&official), ["openai::gpt-image-1", "openai::dall-e-3"]);
        assert_eq!(
            ids(&compatible),
            ["openai::gpt-4o", "openai::gpt-image-1", "openai::dall-e-3"]
        );
        assert_eq!(http.request(1).url, "https://api.test/v1/models");
        assert_eq!(http.request(1).header("Authorization"), Some("Bearer key"));
    }

    #[test]
    fn from_env_reads_the_key_and_the_base_url() {
        let vars = |key: &'static str, base: Option<&'static str>| {
            move |name: &str| match name {
                API_KEY_ENV => Some(key.to_string()),
                BASE_URL_ENV => base.map(str::to_string),
                _ => None,
            }
        };
        let provider = OpenAiImageProvider::from_lookup("m", vars("k", None)).unwrap();
        assert_eq!(provider.base_url, DEFAULT_BASE_URL);
        assert_eq!(provider.api_key, "k");
        let provider =
            OpenAiImageProvider::from_lookup("m", vars("k", Some("http://local:8000/v1"))).unwrap();
        assert_eq!(provider.base_url, "http://local:8000/v1");
        // Local servers often need no key
        let provider = OpenAiImageProvider::from_lookup("m", |name| {
            (name == BASE_URL_ENV).then(|| "http://local:8000/v1".to_string())
        })
        .unwrap();
        assert_eq!(provider.api_key, "");
        assert!(matches!(
            OpenAiImageProvider::from_lookup("m", |_| None),
            Err(ImageError::MissingApiKey(API_KEY_ENV))
        ));
    }
}