o402 0.1.5

OpenAI-compatible gateway, paid with x402.
//! Unbuffered upstream SSE body.

use axum::body::Body;
use axum::http::{HeaderMap, StatusCode, header};
use axum::response::Response;
use futures_util::TryStreamExt as _;

/// True when the upstream advertised `text/event-stream`.
#[must_use]
pub(super) fn is_event_stream(headers: &HeaderMap) -> bool {
    headers
        .get(header::CONTENT_TYPE)
        .and_then(|value| value.to_str().ok())
        .and_then(|content_type| content_type.split(';').next())
        .is_some_and(|mime| mime.trim().eq_ignore_ascii_case("text/event-stream"))
}

/// Forward upstream bytes as they arrive. Does not collect the body.
#[must_use]
pub(super) fn stream_response(
    upstream: reqwest::Response,
    status: StatusCode,
    headers: HeaderMap,
) -> Response {
    let stream = upstream.bytes_stream().map_err(std::io::Error::other);
    let mut response = Response::new(Body::from_stream(stream));
    *response.status_mut() = status;
    *response.headers_mut() = headers;
    response
}