llmproxy 0.2.2

A simple HTTP proxy server for llm api requests
Documentation
use crate::models::{ModelExtractPayload, ResponseStatus, ServerResponse};
use axum::{
    body::{Body, Bytes},
    http::StatusCode,
    response::IntoResponse,
    response::Response,
    Json,
};
use tracing;

/// Extract model name from request body
/// Returns (model_name, body_bytes) on success, or an error Response on failure
pub async fn extract_model_name(body: Body) -> Result<(String, Bytes), Box<Response>> {
    // Read request body
    let body_bytes = match axum::body::to_bytes(body, usize::MAX).await {
        Ok(bytes) => bytes,
        Err(e) => {
            tracing::error!("Failed to read request body: {}", e);
            return Err(Box::new(
                (
                    StatusCode::BAD_REQUEST,
                    Json(ServerResponse {
                        status: ResponseStatus::Error,
                        message: "Failed to read request body".to_string(),
                    }),
                )
                    .into_response(),
            ));
        }
    };

    // Parse JSON to extract model field
    let model_payload: ModelExtractPayload = match serde_json::from_slice(&body_bytes) {
        Ok(payload) => payload,
        Err(e) => {
            tracing::warn!("Failed to parse JSON body for model extraction: {}", e);
            return Err(Box::new(
                (
                    StatusCode::BAD_REQUEST,
                    Json(ServerResponse {
                        status: ResponseStatus::Error,
                        message: format!("Invalid JSON body: {}", e),
                    }),
                )
                    .into_response(),
            ));
        }
    };

    // Validate model name exists and is not empty
    let model_name = match model_payload.model {
        Some(name) if !name.trim().is_empty() => name.trim().to_string(),
        _ => {
            tracing::warn!("Model name missing or empty in request body.");
            return Err(Box::new(
                (
                    StatusCode::BAD_REQUEST,
                    Json(ServerResponse {
                        status: ResponseStatus::Error,
                        message: "Model name is required in the request body".to_string(),
                    }),
                )
                    .into_response(),
            ));
        }
    };

    tracing::debug!("Extracted model name: {model_name}");
    Ok((model_name, body_bytes))
}