dynamo-llm 1.7.0

Dynamo LLM Library
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

use futures::Stream;

use crate::protocols::openai::stream_aggregator::{StreamAggregable, aggregate_stream};
use crate::types::Annotated;
use dynamo_runtime::error::DynamoError;

use super::NvImagesResponse;

fn merge_if_consistent<T: PartialEq>(current: &mut Option<T>, next: Option<T>) {
    if current.as_ref() != next.as_ref() {
        *current = None;
    }
}

impl StreamAggregable for NvImagesResponse {
    fn empty() -> Self {
        Self::empty()
    }

    /// Fold `next` into `self`.
    ///
    /// Token `usage` keeps the first chunk's value: no image backend reports
    /// it today, and summing would double count a backend that repeats
    /// running totals. Image accounting folds per
    /// [`ImagesResponseNvExt::merge`](super::ImagesResponseNvExt::merge),
    /// except that a chunk carrying images without accounting also clears
    /// `num_inference_steps`, before or after the chunks that report it.
    /// `data` extends. The request-level fields come from the first chunk
    /// that carries images: `created` stays, while `size` and `output_format`
    /// are cleared when a later chunk disagrees, as are `background` and
    /// `quality`, so the response never claims a value that does not hold
    /// for every image.
    fn merge(&mut self, next: Self) {
        if self.usage.is_none() {
            self.usage = next.usage;
        }

        let mut next_nvext = next.nvext;
        let had_images = !self.data.is_empty();
        let current = self.nvext.as_mut().and_then(|nvext| nvext.usage.as_mut());
        let reported = next_nvext.as_mut().and_then(|nvext| nvext.usage.as_mut());
        match (current, reported) {
            // These images have no step count.
            (Some(current), None) if !next.data.is_empty() => current.num_inference_steps = None,
            // Earlier images had no step count, even if this report carries no images.
            (None, Some(reported)) if had_images => reported.num_inference_steps = None,
            _ => {}
        }
        if let Some(next) = next_nvext {
            self.nvext.get_or_insert_with(Default::default).merge(next);
        }

        if next.data.is_empty() {
            return;
        }

        if self.data.is_empty() {
            self.created = next.created;
            self.background = next.background;
            self.output_format = next.output_format;
            self.size = next.size;
            self.quality = next.quality;
        } else {
            merge_if_consistent(&mut self.background, next.background);
            merge_if_consistent(&mut self.output_format, next.output_format);
            merge_if_consistent(&mut self.size, next.size);
            merge_if_consistent(&mut self.quality, next.quality);
        }

        self.data.extend(next.data);
    }
}

impl NvImagesResponse {
    /// Aggregates an annotated stream of image responses into a final response.
    pub async fn from_annotated_stream(
        stream: impl Stream<Item = Annotated<NvImagesResponse>>,
    ) -> Result<NvImagesResponse, DynamoError> {
        aggregate_stream(stream).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::protocols::openai::images::{ImageAccounting, ImageUsage, ImagesResponseNvExt};
    use futures::stream;

    fn response(
        created: i64,
        output_format: Option<&str>,
        size: Option<&str>,
        image: &str,
    ) -> NvImagesResponse {
        serde_json::from_value(serde_json::json!({
            "created": created,
            "data": [{"b64_json": image}],
            "output_format": output_format,
            "size": size,
        }))
        .unwrap()
    }

    fn accounting(output_images: u32, steps: Option<u8>) -> ImagesResponseNvExt {
        ImagesResponseNvExt {
            usage: Some(ImageAccounting {
                output_images,
                input_images: 1,
                num_inference_steps: steps,
            }),
        }
    }

    #[tokio::test]
    async fn aggregates_chunk_metadata_and_image_data() {
        let result = NvImagesResponse::from_annotated_stream(stream::iter(vec![
            Annotated::from_data(response(100, Some("jpeg"), Some("37x19"), "first")),
            Annotated::from_data(response(101, Some("jpeg"), Some("37x19"), "second")),
        ]))
        .await
        .unwrap();
        let value = serde_json::to_value(result).unwrap();

        assert_eq!(value["created"], 100);
        assert_eq!(value["output_format"], "jpeg");
        assert_eq!(value["size"], "37x19");
        assert_eq!(value["data"].as_array().unwrap().len(), 2);
        // No chunk reported accounting.
        assert_eq!(value["usage"], serde_json::Value::Null);
        assert!(value.get("nvext").is_none());
    }

    #[tokio::test]
    async fn keeps_metadata_from_first_nonempty_chunk_after_empty_chunk() {
        let result = NvImagesResponse::from_annotated_stream(stream::iter(vec![
            Annotated::from_data(NvImagesResponse::empty()),
            Annotated::from_data(response(100, Some("webp"), Some("41x23"), "image")),
        ]))
        .await
        .unwrap();
        let value = serde_json::to_value(result).unwrap();

        assert_eq!(value["created"], 100);
        assert_eq!(value["output_format"], "webp");
        assert_eq!(value["size"], "41x23");
        assert_eq!(value["data"].as_array().unwrap().len(), 1);
    }

    #[tokio::test]
    async fn clears_size_when_chunk_dimensions_differ_or_are_missing() {
        for second_size in [Some("41x23"), None] {
            let result = NvImagesResponse::from_annotated_stream(stream::iter(vec![
                Annotated::from_data(response(100, Some("png"), Some("37x19"), "first")),
                Annotated::from_data(response(101, Some("png"), second_size, "second")),
            ]))
            .await
            .unwrap();
            let value = serde_json::to_value(result).unwrap();

            assert_eq!(value["size"], serde_json::Value::Null);
            assert_eq!(value["output_format"], "png");
            assert_eq!(value["data"].as_array().unwrap().len(), 2);
        }
    }

    #[tokio::test]
    async fn clears_output_format_when_chunks_disagree() {
        let result = NvImagesResponse::from_annotated_stream(stream::iter(vec![
            Annotated::from_data(response(100, Some("jpeg"), Some("37x19"), "first")),
            Annotated::from_data(response(101, Some("webp"), Some("37x19"), "second")),
        ]))
        .await
        .unwrap();
        let value = serde_json::to_value(result).unwrap();

        assert_eq!(value["output_format"], serde_json::Value::Null);
        assert_eq!(value["size"], "37x19");
        assert_eq!(value["data"].as_array().unwrap().len(), 2);
    }

    #[test]
    fn merge_folds_accounting_across_chunks() {
        let token_usage: ImageUsage = serde_json::from_value(serde_json::json!({
            "input_tokens": 3, "output_tokens": 4, "total_tokens": 7,
            "input_tokens_details": {"text_tokens": 3, "image_tokens": 0}
        }))
        .unwrap();

        // Chunks that agree: output counts add up, the request-side fields hold.
        let mut first = response(100, Some("png"), Some("37x19"), "first");
        first.nvext = Some(accounting(1, Some(4)));
        first.usage = Some(token_usage.clone());
        let mut second = response(101, Some("png"), Some("37x19"), "second");
        second.nvext = Some(accounting(2, Some(4)));
        second.usage = Some(token_usage.clone());
        first.merge(second);
        assert_eq!(first.nvext, Some(accounting(3, Some(4))));
        assert_eq!(first.data.len(), 2);
        // Token usage is the first reported value, not a sum.
        assert_eq!(first.usage, Some(token_usage));

        // A chunk without accounting leaves it unchanged.
        first.merge(NvImagesResponse::empty());
        assert_eq!(first.nvext, Some(accounting(3, Some(4))));

        // A chunk that disagrees on the step count, or does not report it,
        // clears the step count; output counts still add up.
        let mut third = response(102, Some("png"), Some("37x19"), "third");
        third.nvext = Some(accounting(1, None));
        first.merge(third);
        assert_eq!(first.nvext, Some(accounting(4, None)));

        // An empty aggregate adopts the first accounting it sees, even from a
        // chunk that carries no images.
        let mut empty = NvImagesResponse::empty();
        let mut terminal = NvImagesResponse::empty();
        terminal.nvext = Some(accounting(2, Some(4)));
        empty.merge(terminal);
        assert_eq!(empty.nvext, Some(accounting(2, Some(4))));
        assert!(empty.data.is_empty());
        assert!(empty.usage.is_none());
    }

    fn image_chunk(image: &str, nvext: Option<ImagesResponseNvExt>) -> NvImagesResponse {
        let mut chunk = response(100, Some("png"), Some("37x19"), image);
        chunk.nvext = nvext;
        chunk
    }

    /// Start with no images or accounting so the first chunk also exercises `merge`.
    fn merge_all(chunks: Vec<NvImagesResponse>) -> NvImagesResponse {
        let mut merged = NvImagesResponse::empty();
        for chunk in chunks {
            merged.merge(chunk);
        }
        merged
    }

    fn images(response: &NvImagesResponse) -> Vec<&str> {
        response
            .data
            .iter()
            .map(|image| image.b64_json.as_deref().unwrap())
            .collect()
    }

    /// Missing extensions and missing usage must keep steps absent through later reports.
    #[test]
    fn image_chunk_without_accounting_clears_step_count() {
        // A chunk can omit accounting with no `nvext` or with `nvext: {}`.
        let missing_shapes = [None, Some(ImagesResponseNvExt { usage: None })];
        for missing in missing_shapes {
            let reported = || Some(accounting(1, Some(4)));
            let mut terminal = NvImagesResponse::empty();
            terminal.nvext = reported();
            let cases = [
                (
                    "missing then image-free reporting",
                    vec![image_chunk("a", missing.clone()), terminal],
                    1,
                ),
                (
                    "reporting then missing",
                    vec![
                        image_chunk("a", reported()),
                        image_chunk("b", missing.clone()),
                    ],
                    1,
                ),
                (
                    "missing then reporting",
                    vec![
                        image_chunk("a", missing.clone()),
                        image_chunk("b", reported()),
                    ],
                    1,
                ),
                (
                    "a later matching report does not restore it",
                    vec![
                        image_chunk("a", reported()),
                        image_chunk("b", missing.clone()),
                        image_chunk("c", reported()),
                    ],
                    2,
                ),
            ];
            for (case, chunks, output_images) in cases {
                let expected_images: Vec<String> = chunks
                    .iter()
                    .flat_map(|chunk| images(chunk).into_iter().map(str::to_owned))
                    .collect();
                let merged = merge_all(chunks);
                assert_eq!(
                    merged.nvext,
                    Some(accounting(output_images, None)),
                    "{case}, missing shape {missing:?}"
                );
                assert_eq!(images(&merged), expected_images, "{case}");
            }
        }
    }
}