switchyard-server 0.2.0

Rust HTTP server surface for libsy algorithms
Documentation
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! Response encoding glue for libsy server endpoints.

use std::error::Error;

use axum::Json;
use axum::response::{IntoResponse, Response as HttpResponse};
use switchyard_protocol::{LlmResponse, Response as AlgorithmResponse};
use switchyard_translation::{WireFormat, encode_aggregated_response, encode_stream};

use crate::sse::frame_stream;

type BoxError = Box<dyn Error + Send + Sync>;

/// Encodes a libsy response into the endpoint's wire format, reporting
/// `served_model` as the response model so the body names the model that
/// answered rather than the route the caller addressed.
pub(crate) fn into_http_response(
    response: AlgorithmResponse,
    target_format: WireFormat,
    served_model: Option<String>,
) -> Result<HttpResponse, BoxError> {
    match response.llm_response {
        LlmResponse::Agg(response) => Ok(Json(encode_aggregated_response(
            &response,
            target_format,
            served_model.as_deref(),
        )?)
        .into_response()),
        LlmResponse::Stream(stream) => Ok(frame_stream(
            encode_stream(stream, target_format, served_model)?,
            target_format,
        )
        .into_response()),
    }
}