yapi-core 0.4.1

Coding agent core: sessions, compaction, settings, resources, built-in tools, system prompt, packages, extension runner and MCP. An internal library of yapi, with no stable API.
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
//! pi's image pipeline: what an image becomes before it enters the
//! conversation. Formats models do not accept become PNG, and with
//! `images.autoResize` images are fitted to the model's limits.

use std::borrow::Cow;
use std::io::Cursor;

use base64::Engine as _;
use base64::engine::general_purpose::STANDARD;
use image::imageops::{self, FilterType};
use image::{DynamicImage, ImageOutputFormat};
use yapi_types::message::{ContentBlock, ImageContent};
use yapi_types::models::ImageResize;

/// pi's note for an image that cannot be decoded for conversion.
const CONVERT_FAILED: &str =
    "[Image omitted: could not be converted to a supported inline image format.]";
/// pi's note for an image no encoding fits within the size limit.
const RESIZE_FAILED: &str =
    "[Image omitted: could not be resized below the inline image size limit.]";

/// pi's default limit: 4.5 MB of base64, below Anthropic's 5 MB.
const DEFAULT_MAX_BYTES: u64 = 4_718_592;

/// An image ready for the model, with pi's notes on how it changed.
#[derive(Debug, PartialEq)]
pub struct Processed {
    /// The image to send.
    pub image: ImageContent,
    /// pi's conversion and dimension notes, in order.
    pub hints: Vec<String>,
}

/// pi's `processImage`: an image that is not PNG, JPEG, GIF or WebP becomes
/// PNG when the `image` crate, with Photon's formats, recognizes it from its
/// bytes (BMP, TIFF, ICO, PNM, HDR, DDS or farbfeld), and with `auto_resize`
/// the image is fitted to `limits`, pi's defaults where unset: 2000×2000 and
/// 4.5 MB of base64. The error is pi's omission note.
pub fn process(
    bytes: &[u8],
    mime_type: &str,
    auto_resize: bool,
    limits: Option<&ImageResize>,
) -> Result<Processed, &'static str> {
    let base = mime_type
        .split(';')
        .next()
        .unwrap_or_default()
        .trim()
        .to_lowercase();
    let (bytes, mime_type, converted_from) = match base.as_str() {
        "image/png" => (Cow::Borrowed(bytes), "image/png", None),
        "image/jpeg" | "image/jpg" => (Cow::Borrowed(bytes), "image/jpeg", None),
        "image/gif" => (Cow::Borrowed(bytes), "image/gif", None),
        "image/webp" => (Cow::Borrowed(bytes), "image/webp", None),
        _ => {
            let png = decode(bytes)
                .and_then(|image| encode(&image, ImageOutputFormat::Png))
                .ok_or(CONVERT_FAILED)?;
            (Cow::Owned(png), "image/png", Some(base))
        }
    };
    let (image, note) = if auto_resize {
        resize(&bytes, mime_type, limits).ok_or(RESIZE_FAILED)?
    } else {
        (inline(&bytes, mime_type), None)
    };
    let mut hints = Vec::new();
    if let Some(from) = converted_from.filter(|from| !from.is_empty() && *from != image.mime_type) {
        hints.push(format!(
            "[Image converted from {from} to {}.]",
            image.mime_type
        ));
    }
    hints.extend(note);
    Ok(Processed { image, hints })
}

/// pi's `_normalizePromptImages`: a prompt's images processed for the
/// model, with pi's notes, which include the omission note of each image
/// that fails.
pub fn normalize_prompt(
    images: Vec<ImageContent>,
    auto_resize: bool,
    limits: Option<&ImageResize>,
) -> (Vec<ImageContent>, Vec<String>) {
    let mut normalized = Vec::with_capacity(images.len());
    let mut hints = Vec::new();
    for image in images {
        // Undecodable base64 fails as the empty image it decodes to in pi.
        let bytes = STANDARD.decode(&image.data).unwrap_or_default();
        match process(&bytes, &image.mime_type, auto_resize, limits) {
            Ok(processed) => {
                normalized.push(processed.image);
                hints.extend(processed.hints);
            }
            Err(message) => hints.push(message.to_owned()),
        }
    }
    (normalized, hints)
}

/// pi's `normalizeToolResultImages`: each image of a tool result processed
/// as for the model, followed by a text block of pi's notes when there are
/// any. An image that fails keeps its block. `None` when nothing changed.
pub fn normalize_tool_result(
    content: &[ContentBlock],
    auto_resize: bool,
    limits: Option<&ImageResize>,
) -> Option<Vec<ContentBlock>> {
    let mut changed = false;
    let mut normalized = Vec::with_capacity(content.len());
    for block in content {
        let ContentBlock::Image(image) = block else {
            normalized.push(block.clone());
            continue;
        };
        let processed = STANDARD
            .decode(&image.data)
            .ok()
            .and_then(|bytes| process(&bytes, &image.mime_type, auto_resize, limits).ok());
        match processed {
            Some(processed) if processed.image != *image || !processed.hints.is_empty() => {
                normalized.push(ContentBlock::Image(processed.image));
                if !processed.hints.is_empty() {
                    normalized.push(ContentBlock::text(processed.hints.join("\n")));
                }
                changed = true;
            }
            _ => normalized.push(block.clone()),
        }
    }
    changed.then_some(normalized)
}

/// pi's `resizeImage` with its `formatDimensionNote`.
fn resize(
    bytes: &[u8],
    mime_type: &str,
    limits: Option<&ImageResize>,
) -> Option<(ImageContent, Option<String>)> {
    let resized = resize_image(bytes, mime_type, limits)?;
    let note = resized.was_resized.then(|| {
        format!(
            "[Image: original {}x{}, displayed at {}x{}. Multiply coordinates by {} to map to original image.]",
            resized.original_width,
            resized.original_height,
            resized.width,
            resized.height,
            yapi_types::js::to_fixed(
                f64::from(resized.original_width) / f64::from(resized.width),
                2
            )
        )
    });
    Some((resized.image, note))
}

/// pi's `ResizedImage`: an image fitted to the limits, with its dimensions
/// before and after.
#[derive(Debug, PartialEq, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Resized {
    /// The image to send.
    #[serde(flatten)]
    pub image: ImageContent,
    /// Width as decoded, turned upright.
    pub original_width: u32,
    /// Height as decoded, turned upright.
    pub original_height: u32,
    /// Width as sent.
    pub width: u32,
    /// Height as sent.
    pub height: u32,
    /// Whether the image was scaled and encoded again.
    pub was_resized: bool,
}

/// pi's `resizeImage`: the image unchanged when it is within `limits`, pi's
/// defaults where unset (2000×2000 and 4.5 MB of base64). Otherwise the image
/// scaled to fit, as the first of PNG and JPEG at falling qualities that fits
/// the byte limit, shrinking by a quarter until one does. `None` when the
/// image does not decode or no encoding fits.
pub fn resize_image(
    bytes: &[u8],
    mime_type: &str,
    limits: Option<&ImageResize>,
) -> Option<Resized> {
    let limits = limits.cloned().unwrap_or_default();
    let max_width = limits.max_width.unwrap_or(2000);
    let max_height = limits.max_height.unwrap_or(2000);
    let max_bytes = limits.max_bytes.unwrap_or(DEFAULT_MAX_BYTES);
    let image = decode(bytes)?;
    let (original_width, original_height) = (image.width(), image.height());
    let result = |image, width, height, was_resized| Resized {
        image,
        original_width,
        original_height,
        width,
        height,
        was_resized,
    };
    if original_width <= max_width
        && original_height <= max_height
        && base64_len(bytes.len()) < max_bytes
    {
        let image = inline(bytes, mime_type);
        return Some(result(image, original_width, original_height, false));
    }
    let scale = |value: u32, max: u32, other: u32| {
        yapi_types::js::round(f64::from(value) * f64::from(max) / f64::from(other)) as u32
    };
    let (mut width, mut height) = (original_width, original_height);
    if width > max_width {
        height = scale(height, max_width, width);
        width = max_width;
    }
    if height > max_height {
        width = scale(width, max_height, height);
        height = max_height;
    }
    let quality = limits.jpeg_quality.unwrap_or(80);
    let fallbacks = [85, 70, 55, 40]
        .into_iter()
        .filter(|other| *other != quality);
    let formats: Vec<_> = [ImageOutputFormat::Png, ImageOutputFormat::Jpeg(quality)]
        .into_iter()
        .chain(fallbacks.map(ImageOutputFormat::Jpeg))
        .collect();
    loop {
        // pi's Photon fails to encode an empty image.
        if width == 0 || height == 0 {
            return None;
        }
        let resized = DynamicImage::ImageRgba8(imageops::resize(
            &image,
            width,
            height,
            FilterType::Lanczos3,
        ));
        for format in &formats {
            let encoded = encode(&resized, format.clone())?;
            if base64_len(encoded.len()) < max_bytes {
                let mime_type = match format {
                    ImageOutputFormat::Png => "image/png",
                    _ => "image/jpeg",
                };
                return Some(result(inline(&encoded, mime_type), width, height, true));
            }
        }
        let shrink = |value: u32| ((f64::from(value) * 0.75).floor() as u32).max(1);
        let next = (shrink(width), shrink(height));
        if next == (width, height) {
            return None;
        }
        (width, height) = next;
    }
}

/// `bytes` as an image block.
fn inline(bytes: &[u8], mime_type: &str) -> ImageContent {
    ImageContent {
        data: STANDARD.encode(bytes),
        mime_type: mime_type.to_owned(),
    }
}

/// The length of `len` bytes in base64.
fn base64_len(len: usize) -> u64 {
    len.div_ceil(3) as u64 * 4
}

/// The image as RGBA, as Photon holds it, turned upright by its EXIF
/// orientation as pi does.
fn decode(bytes: &[u8]) -> Option<DynamicImage> {
    let image = image::load_from_memory(bytes).ok()?.to_rgba8();
    Some(DynamicImage::ImageRgba8(match exif_orientation(bytes) {
        2 => imageops::flip_horizontal(&image),
        3 => imageops::rotate180(&image),
        4 => imageops::flip_vertical(&image),
        5 => imageops::flip_horizontal(&imageops::rotate90(&image)),
        6 => imageops::rotate90(&image),
        7 => imageops::flip_horizontal(&imageops::rotate270(&image)),
        8 => imageops::rotate270(&image),
        _ => image,
    }))
}

/// pi's `convertImageBytesToPng`: `bytes` decoded, turned upright by
/// their EXIF orientation and encoded as PNG. `None` when they do not decode.
pub fn convert_to_png(bytes: &[u8]) -> Option<Vec<u8>> {
    encode(&decode(bytes)?, ImageOutputFormat::Png)
}

/// The clipboard's `convertToPng`: `bytes` decoded to RGBA and encoded as
/// PNG, with no EXIF orientation and no resizing. `None` when they do not
/// decode.
pub fn to_png(bytes: &[u8]) -> Option<Vec<u8>> {
    let image = DynamicImage::ImageRgba8(image::load_from_memory(bytes).ok()?.to_rgba8());
    encode(&image, ImageOutputFormat::Png)
}

/// `image` in `format`. JPEG drops the alpha channel, as with Photon.
fn encode(image: &DynamicImage, format: ImageOutputFormat) -> Option<Vec<u8>> {
    let mut bytes = Cursor::new(Vec::new());
    image.write_to(&mut bytes, format).ok()?;
    Some(bytes.into_inner())
}

/// pi's `getExifOrientation`: the EXIF orientation of a JPEG or WebP image,
/// or 1 when it has none.
fn exif_orientation(bytes: &[u8]) -> u16 {
    let tiff = if bytes.starts_with(&[0xff, 0xd8]) {
        jpeg_tiff_offset(bytes)
    } else if bytes.len() >= 12 && bytes.starts_with(b"RIFF") && &bytes[8..12] == b"WEBP" {
        webp_tiff_offset(bytes)
    } else {
        None
    };
    tiff.and_then(|start| tiff_orientation(bytes, start))
        .unwrap_or(1)
}

fn has_exif_header(bytes: &[u8], offset: usize) -> bool {
    bytes.get(offset..offset + 6) == Some(b"Exif\0\0")
}

fn jpeg_tiff_offset(bytes: &[u8]) -> Option<usize> {
    let mut offset = 2;
    while offset + 1 < bytes.len() {
        if bytes[offset] != 0xff {
            return None;
        }
        let marker = bytes[offset + 1];
        if marker == 0xff {
            offset += 1;
            continue;
        }
        if marker == 0xe1 {
            if offset + 10 > bytes.len() {
                return None;
            }
            if has_exif_header(bytes, offset + 4) {
                return Some(offset + 10);
            }
        }
        let length = usize::from(u16::from_be_bytes(array(bytes, offset + 2)?));
        offset += 2 + length;
    }
    None
}

fn webp_tiff_offset(bytes: &[u8]) -> Option<usize> {
    let mut offset = 12;
    while offset + 8 <= bytes.len() {
        let size = u32::from_le_bytes(array(bytes, offset + 4)?) as usize;
        let data = offset + 8;
        if &bytes[offset..offset + 4] == b"EXIF" {
            if data.saturating_add(size) > bytes.len() {
                return None;
            }
            // Some files put "Exif\0\0" before the TIFF header.
            return Some(if size >= 6 && has_exif_header(bytes, data) {
                data + 6
            } else {
                data
            });
        }
        // Chunks are padded to an even size.
        offset = data.checked_add(size)?.checked_add(size % 2)?;
    }
    None
}

/// The orientation tag of the TIFF header at `start`, if it has a valid one.
fn tiff_orientation(bytes: &[u8], start: usize) -> Option<u16> {
    let little_endian = array::<8>(bytes, start)?[..2] == *b"II";
    let read16 = |at: usize| {
        let pair = array(bytes, at)?;
        Some(if little_endian {
            u16::from_le_bytes(pair)
        } else {
            u16::from_be_bytes(pair)
        })
    };
    let read32 = |at: usize| {
        let quad = array(bytes, at)?;
        Some(if little_endian {
            u32::from_le_bytes(quad)
        } else {
            u32::from_be_bytes(quad)
        })
    };
    let ifd = start.checked_add(read32(start + 4)? as usize)?;
    for entry in 0..usize::from(read16(ifd)?) {
        let at = ifd + 2 + entry * 12;
        // pi reads an entry only when all 12 bytes are there.
        array::<12>(bytes, at)?;
        if read16(at)? == 0x0112 {
            return read16(at + 8).filter(|value| (1..=8).contains(value));
        }
    }
    None
}

/// The `N` bytes at `at`, if there are that many.
pub(crate) fn array<const N: usize>(bytes: &[u8], at: usize) -> Option<[u8; N]> {
    bytes.get(at..at.checked_add(N)?)?.try_into().ok()
}

#[cfg(test)]
mod tests {
    #![allow(
        clippy::unwrap_used,
        reason = "test helpers; a panic is a test failure"
    )]

    use super::*;
    use image::{Rgba, RgbaImage};

    /// A `width`×`height` image: a gradient, or noise that compresses badly.
    fn pixels(width: u32, height: u32, noisy: bool) -> DynamicImage {
        let mut seed = 7u32;
        DynamicImage::ImageRgba8(RgbaImage::from_fn(width, height, |x, y| {
            seed = seed.wrapping_mul(1_103_515_245).wrapping_add(12_345);
            let noise = if noisy { (seed >> 16) as u8 } else { 0 };
            Rgba([(x % 256) as u8 ^ noise, (y % 256) as u8, noise, 255])
        }))
    }

    fn file(image: &DynamicImage, format: ImageOutputFormat) -> Vec<u8> {
        encode(image, format).unwrap()
    }

    fn limits(width: u32, height: u32, bytes: Option<u64>) -> ImageResize {
        ImageResize {
            max_width: Some(width),
            max_height: Some(height),
            max_bytes: bytes,
            jpeg_quality: None,
        }
    }

    fn dimensions(image: &ImageContent) -> (u32, u32) {
        let decoded = image::load_from_memory(&STANDARD.decode(&image.data).unwrap()).unwrap();
        (decoded.width(), decoded.height())
    }

    #[test]
    fn small_images_unchanged() {
        let png = file(&pixels(30, 20, false), ImageOutputFormat::Png);
        let processed = process(&png, "image/png", true, None).unwrap();
        assert_eq!(processed.image.data, STANDARD.encode(&png));
        assert_eq!(processed.image.mime_type, "image/png");
        assert!(processed.hints.is_empty());
    }

    #[test]
    fn large_images_scaled() {
        let png = file(&pixels(2100, 10, false), ImageOutputFormat::Png);
        let processed = process(&png, "image/png", true, None).unwrap();
        assert_eq!(processed.image.mime_type, "image/png");
        assert_eq!(dimensions(&processed.image), (2000, 10));
        assert_eq!(
            processed.hints,
            [
                "[Image: original 2100x10, displayed at 2000x10. Multiply coordinates by 1.05 to map to original image.]"
            ]
        );

        let tall = file(&pixels(30, 90, false), ImageOutputFormat::Png);
        let limits = limits(40, 40, None);
        let processed = process(&tall, "image/png", true, Some(&limits)).unwrap();
        assert_eq!(dimensions(&processed.image), (13, 40));
        assert_eq!(
            processed.hints,
            [
                "[Image: original 30x90, displayed at 13x40. Multiply coordinates by 2.31 to map to original image.]"
            ]
        );
        // Without auto-resize the image passes as it is.
        let processed = process(&tall, "image/png", false, Some(&limits)).unwrap();
        assert_eq!(processed.image.data, STANDARD.encode(&tall));
        assert!(processed.hints.is_empty());
    }

    #[test]
    fn jpeg_replaces_large_png() {
        let image = pixels(64, 64, true);
        let png = file(&image, ImageOutputFormat::Png);
        let limits = limits(2000, 2000, Some(base64_len(png.len())));
        let processed = process(&png, "image/png", true, Some(&limits)).unwrap();
        assert_eq!(processed.image.mime_type, "image/jpeg");
        assert_eq!(
            STANDARD.decode(&processed.image.data).unwrap(),
            file(&image, ImageOutputFormat::Jpeg(80))
        );
        assert_eq!(
            processed.hints,
            [
                "[Image: original 64x64, displayed at 64x64. Multiply coordinates by 1.00 to map to original image.]"
            ]
        );
    }

    #[test]
    fn shrinks_then_omits() {
        let png = file(&pixels(40, 40, true), ImageOutputFormat::Png);
        let limits = limits(2000, 2000, Some(1200));
        let processed = process(&png, "image/png", true, Some(&limits)).unwrap();
        assert!(processed.image.data.len() < 1200);
        let (width, height) = dimensions(&processed.image);
        assert!(width < 40 && width == height);
        let none_fit = ImageResize {
            max_bytes: Some(1),
            ..ImageResize::default()
        };
        assert_eq!(
            process(&png, "image/png", true, Some(&none_fit)),
            Err(RESIZE_FAILED)
        );
    }

    #[test]
    fn exif_upright_before_resize() {
        let jpeg = file(&pixels(4, 2, false), ImageOutputFormat::Jpeg(90));
        // An APP1 segment with one IFD (image file directory) entry:
        // orientation 6, rotate clockwise.
        let mut exif = b"\xff\xe1\x00\x22Exif\0\0MM\0\x2a\0\0\0\x08\0\x01".to_vec();
        exif.extend_from_slice(b"\x01\x12\0\x03\0\0\0\x01\0\x06\0\0\0\0\0\0");
        let rotated = [&jpeg[..2], &exif, &jpeg[2..]].concat();
        assert_eq!(exif_orientation(&rotated), 6);
        let limits = limits(3, 3, None);
        let processed = process(&rotated, "image/jpeg", true, Some(&limits)).unwrap();
        assert_eq!(dimensions(&processed.image), (2, 3));
        assert_eq!(
            processed.hints,
            [
                "[Image: original 2x4, displayed at 2x3. Multiply coordinates by 1.00 to map to original image.]"
            ]
        );
    }

    #[test]
    fn to_png_ignores_exif() {
        let jpeg = file(&pixels(4, 2, false), ImageOutputFormat::Jpeg(90));
        let mut exif = b"\xff\xe1\x00\x22Exif\0\0MM\0\x2a\0\0\0\x08\0\x01".to_vec();
        exif.extend_from_slice(b"\x01\x12\0\x03\0\0\0\x01\0\x06\0\0\0\0\0\0");
        let rotated = [&jpeg[..2], &exif, &jpeg[2..]].concat();
        let png = to_png(&rotated).unwrap();
        assert!(png.starts_with(b"\x89PNG"));
        let decoded = image::load_from_memory(&png).unwrap();
        assert_eq!((decoded.width(), decoded.height()), (4, 2));
    }

    /// Runs `bytes` through `process` as `mime_type` with and without resizing
    /// and returns the PNG it becomes, after checking pi's conversion note.
    fn converted(bytes: &[u8], mime_type: &str) -> Vec<u8> {
        let mut png = None;
        for auto_resize in [true, false] {
            let processed = process(bytes, mime_type, auto_resize, None)
                .unwrap_or_else(|note| panic!("{mime_type}: {note}"));
            assert_eq!(processed.image.mime_type, "image/png");
            assert_eq!(
                processed.hints,
                [format!("[Image converted from {mime_type} to image/png.]")]
            );
            let data = STANDARD.decode(&processed.image.data).unwrap();
            assert!(png.replace(data.clone()).is_none_or(|first| first == data));
        }
        png.unwrap()
    }

    /// A 2x2 RGB TIFF holding red, green, blue and white.
    const TIFF: &str = "SUkqAAgAAAAJAAABAwABAAAAAgAAAAEBAwABAAAAAgAAAAIBAwADAAAAegAAAAMBAwABAAAAAQAAAAYBAwABAAAAAgAAABEBBAABAAAAgAAAABUBAwABAAAAAwAAABYBAwABAAAAAgAAABcBBAABAAAADAAAAAAAAAAIAAgACAD/AAAA/wAAAP////8=";

    /// A 4x4 DDS of one DXT1 block that is solid red.
    fn dds() -> Vec<u8> {
        let mut bytes = b"DDS ".to_vec();
        let mut header = [0u32; 31];
        header[0] = 124; // size
        header[1] = 0x1 | 0x2 | 0x4 | 0x1000; // caps, height, width, pixel format
        header[2] = 4; // height
        header[3] = 4; // width
        header[4] = 8; // linear size
        header[18] = 32; // pixel format size
        header[19] = 0x4; // fourcc
        header[20] = u32::from_le_bytes(*b"DXT1");
        header[26] = 0x1000; // texture
        bytes.extend(header.iter().flat_map(|word| word.to_le_bytes()));
        bytes.extend([0x00, 0xf8, 0x00, 0x00, 0, 0, 0, 0]);
        bytes
    }

    #[test]
    fn other_formats_become_png() {
        let image = pixels(3, 2, false);
        let png = file(&image, ImageOutputFormat::Png);
        let rgba16 = DynamicImage::ImageRgba16(image.to_rgba16());
        // PNM has no alpha channel, so it takes the opaque pixels as RGB.
        let rgb = DynamicImage::ImageRgb8(image.to_rgb8());
        let pnm = ImageOutputFormat::Pnm(image::codecs::pnm::PnmSubtype::Pixmap(
            image::codecs::pnm::SampleEncoding::Binary,
        ));
        for (mime_type, bytes) in [
            ("image/bmp", file(&image, ImageOutputFormat::Bmp)),
            ("image/x-icon", file(&image, ImageOutputFormat::Ico)),
            ("image/x-portable-pixmap", file(&rgb, pnm)),
            (
                "image/x-farbfeld",
                file(&rgba16, ImageOutputFormat::Farbfeld),
            ),
        ] {
            assert_eq!(converted(&bytes, mime_type), png, "{mime_type}");
        }

        let tiff = RgbaImage::from_fn(2, 2, |x, y| match (x, y) {
            (0, 0) => Rgba([255, 0, 0, 255]),
            (1, 0) => Rgba([0, 255, 0, 255]),
            (0, 1) => Rgba([0, 0, 255, 255]),
            _ => Rgba([255; 4]),
        });
        assert_eq!(
            converted(&STANDARD.decode(TIFF).unwrap(), "image/tiff"),
            file(&DynamicImage::ImageRgba8(tiff), ImageOutputFormat::Png)
        );

        let red = DynamicImage::ImageRgba8(RgbaImage::from_pixel(4, 4, Rgba([255, 0, 0, 255])));
        assert_eq!(
            converted(&dds(), "image/vnd.ms-dds"),
            file(&red, ImageOutputFormat::Png)
        );

        // Radiance HDR stores shared-exponent floats, so only the size is exact.
        let mut hdr = Vec::new();
        image::codecs::hdr::HdrEncoder::new(&mut hdr)
            .encode(&[image::Rgb([0.5f32, 0.25, 1.0]); 6], 3, 2)
            .unwrap();
        let decoded = image::load_from_memory(&converted(&hdr, "image/vnd.radiance")).unwrap();
        assert_eq!((decoded.width(), decoded.height()), (3, 2));

        assert_eq!(
            process(b"not an image", "image/tiff", true, None),
            Err(CONVERT_FAILED)
        );
        assert_eq!(
            process(b"not an image", "image/png", true, None),
            Err(RESIZE_FAILED)
        );
    }

    #[test]
    fn normalizes_prompts_and_results() {
        let png = ImageContent {
            data: STANDARD.encode(file(&pixels(3, 2, false), ImageOutputFormat::Png)),
            mime_type: "image/png".into(),
        };
        let bmp = ImageContent {
            data: STANDARD.encode(file(&pixels(3, 2, false), ImageOutputFormat::Bmp)),
            mime_type: "image/bmp".into(),
        };
        let broken = ImageContent {
            data: "AAAA".into(),
            mime_type: "image/png".into(),
        };

        let (images, hints) =
            normalize_prompt(vec![png.clone(), bmp.clone(), broken.clone()], true, None);
        assert_eq!(images[0], png);
        assert_eq!(images[1].mime_type, "image/png");
        assert_eq!(images.len(), 2);
        assert_eq!(
            hints,
            [
                "[Image converted from image/bmp to image/png.]",
                RESIZE_FAILED
            ]
        );

        let unchanged = [ContentBlock::text("a"), ContentBlock::Image(png.clone())];
        assert_eq!(normalize_tool_result(&unchanged, true, None), None);
        let content = [
            ContentBlock::Image(bmp),
            ContentBlock::Image(broken.clone()),
            ContentBlock::text("a"),
        ];
        let normalized = normalize_tool_result(&content, true, None).unwrap();
        assert_eq!(
            normalized,
            [
                ContentBlock::Image(images[1].clone()),
                ContentBlock::text("[Image converted from image/bmp to image/png.]"),
                ContentBlock::Image(broken),
                ContentBlock::text("a"),
            ]
        );
    }
}