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>;
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()),
}
}