use std::collections::BTreeMap;
use std::sync::{Arc, OnceLock};
use axum::extract::rejection::{BytesRejection, JsonRejection, MissingJsonContentType};
use axum::http::{HeaderMap, StatusCode};
use axum::response::{IntoResponse, Response};
use dynamo_runtime::error::DynamoError;
use serde_json::{Value, json};
use crate::protocols::common::llm_backend::{LLMEngineOutput, PreprocessedRequest};
mod error;
pub use error::{ErrorPhase, WorkerError};
pub const FORMAT_ENV: &str = "DYN_GENERATE_FORMAT";
pub struct ClientRequest<'a> {
pub body: &'a [u8],
pub headers: &'a HeaderMap,
pub default_request_id: &'a str,
}
impl ClientRequest<'_> {
pub fn json<T: serde::de::DeserializeOwned>(&self) -> Result<T, JsonRejection> {
json_content_type(self.headers)?;
axum::Json::from_bytes(self.body).map(|axum::Json(value)| value)
}
}
fn json_content_type(headers: &HeaderMap) -> Result<(), JsonRejection> {
let content_type = headers
.get(axum::http::header::CONTENT_TYPE)
.and_then(|value| value.to_str().ok())
.and_then(|value| value.parse::<mime::Mime>().ok());
if !content_type.is_some_and(|mime| {
mime.type_() == mime::APPLICATION
&& (mime.subtype() == mime::JSON || mime.suffix() == Some(mime::JSON))
}) {
return Err(MissingJsonContentType::default().into());
}
Ok(())
}
pub fn json_body_error(headers: &HeaderMap, error: BytesRejection) -> JsonRejection {
json_content_type(headers)
.err()
.unwrap_or_else(|| error.into())
}
#[derive(Debug)]
pub struct ClientError {
pub status: StatusCode,
pub message: String,
pub response: Option<Box<Response>>,
}
impl ClientError {
pub fn new(message: impl Into<String>) -> Self {
Self {
status: StatusCode::BAD_REQUEST,
message: message.into(),
response: None,
}
}
}
impl From<JsonRejection> for ClientError {
fn from(error: JsonRejection) -> Self {
if error.status() == StatusCode::UNPROCESSABLE_ENTITY {
crate::http::service::metrics::record_failure(
&DynamoError::builder()
.class(dynamo_runtime::error::ErrorClass::InvalidRequest)
.build(),
);
let message = error.body_text();
return ClientError {
status: StatusCode::BAD_REQUEST,
response: Some(Box::new(
(
StatusCode::BAD_REQUEST,
axum::Json(json!({
"message": message, "type": "Bad Request", "code": 400,
})),
)
.into_response(),
)),
message,
};
}
ClientError {
status: error.status(),
message: error.body_text(),
response: Some(Box::new(error.into_response())),
}
}
}
pub struct Worker<'a> {
pub model: &'a str,
pub kv_cache_block_size: u32,
pub lora_name: Option<&'a str>,
pub tower_connector_lora_enabled: bool,
}
pub struct Opened {
pub request_id: String,
pub stream: bool,
pub model: Option<String>,
pub input_tokens: usize,
pub validation: Result<(), ClientError>,
pub bind: Bind,
}
pub type Bind = Box<dyn FnOnce(&Worker<'_>) -> Result<Bound, ClientError> + Send>;
pub struct Bound {
pub request: PreprocessedRequest,
pub exchange: Box<dyn GenerateExchange>,
}
pub trait GenerateExchange: Send {
fn chunk(
&mut self,
output: LLMEngineOutput,
prefill: bool,
emit: &mut dyn FnMut(&Value),
) -> Result<(), DynamoError>;
fn finish(&mut self) -> Result<Option<Value>, DynamoError>;
}
pub trait GenerateFormat: Send + Sync + 'static {
fn name(&self) -> &str;
fn capability(&self) -> &'static str;
fn open(&self, request: ClientRequest<'_>) -> Result<Opened, ClientError>;
fn supports_put(&self) -> bool {
true
}
fn ambiguous_model_message(&self) -> &'static str {
"multiple models are registered; specify `model` in the request"
}
fn frontend_error(&self, mut error: ClientError) -> ClientError {
error.response = None;
error
}
fn body_error(&self, error: BytesRejection, _headers: &HeaderMap) -> ClientError {
ClientError {
status: error.status(),
message: error.body_text(),
response: Some(Box::new(error.into_response())),
}
}
fn worker_error(
&self,
error: &(dyn std::error::Error + 'static),
_phase: ErrorPhase,
) -> WorkerError {
WorkerError::dispatch(error, None)
}
fn error_body(&self, _status: StatusCode, message: &str) -> Value {
json!({"error": {"message": message}})
}
}
pub type SharedFormat = Arc<dyn GenerateFormat>;
#[derive(Clone, Default)]
pub struct Registry(BTreeMap<String, SharedFormat>);
impl Registry {
pub fn register(&mut self, format: SharedFormat) -> anyhow::Result<()> {
let name = format.name().to_string();
anyhow::ensure!(
!self.0.contains_key(&name),
"generate format {name:?} is already registered"
);
self.0.insert(name, format);
Ok(())
}
pub fn get(&self, name: &str) -> anyhow::Result<SharedFormat> {
self.0.get(name).cloned().ok_or_else(|| {
let linked: Vec<&str> = self.0.keys().map(String::as_str).collect();
anyhow::anyhow!(
"unknown generate format {name:?}; linked: {}",
linked.join(", ")
)
})
}
}
static INSTALLED: OnceLock<Registry> = OnceLock::new();
pub fn install(registry: Registry) -> bool {
INSTALLED.set(registry).is_ok()
}
pub fn selected() -> anyhow::Result<SharedFormat> {
let name = std::env::var(FORMAT_ENV)
.ok()
.filter(|name| !name.is_empty())
.unwrap_or_else(|| "sglang".to_string());
named(&name)
}
pub fn named(name: &str) -> anyhow::Result<SharedFormat> {
INSTALLED
.get()
.ok_or_else(|| anyhow::anyhow!("generate format catalog is not installed"))?
.get(name)
}
#[cfg(test)]
pub(crate) mod test_formats {
use super::*;
use crate::local_model::runtime_config::{
SGLANG_GENERATE_CAPABILITY, VLLM_INFERENCE_V1_GENERATE_CAPABILITY,
};
use crate::protocols::common::preprocessor::RoutingHints;
use crate::protocols::common::{OutputOptions, SamplingOptions, StopConditions};
struct Passthrough;
struct PassthroughExchange;
impl GenerateExchange for PassthroughExchange {
fn chunk(
&mut self,
output: LLMEngineOutput,
prefill: bool,
emit: &mut dyn FnMut(&Value),
) -> Result<(), DynamoError> {
match output
.engine_data
.and_then(|mut data| data.as_object_mut()?.remove("sglang_response"))
{
Some(response) if !prefill => emit(&response),
Some(_) => {}
None if prefill => {}
None => {
return Err(DynamoError::builder()
.class(dynamo_runtime::error::ErrorClass::BackendProtocol)
.message("missing opaque SGLang response")
.build());
}
}
Ok(())
}
fn finish(&mut self) -> Result<Option<Value>, DynamoError> {
Ok(None)
}
}
impl GenerateFormat for Passthrough {
fn name(&self) -> &str {
"sglang"
}
fn capability(&self) -> &'static str {
SGLANG_GENERATE_CAPABILITY
}
fn open(&self, request: ClientRequest<'_>) -> Result<Opened, ClientError> {
let mut body: serde_json::Map<String, Value> = request.json()?;
let input_ids: Vec<u32> =
serde_json::from_value(body.remove("input_ids").unwrap_or_default())
.map_err(|error| ClientError::new(error.to_string()))?;
let request_id = body
.get("rid")
.and_then(Value::as_str)
.unwrap_or(request.default_request_id)
.to_string();
body.insert("rid".to_string(), Value::String(request_id.clone()));
let max_tokens = body["sampling_params"]["max_new_tokens"]
.as_u64()
.map(|tokens| tokens as u32);
let cache_namespace = body
.get("cache_salt")
.and_then(Value::as_str)
.filter(|salt| !salt.is_empty())
.map(str::to_owned);
Ok(Opened {
request_id,
stream: body.get("stream").and_then(Value::as_bool).unwrap_or(false),
model: None,
input_tokens: input_ids.len(),
validation: Ok(()),
bind: Box::new(move |worker| {
let request = PreprocessedRequest::builder()
.model(worker.model.to_string())
.token_ids(input_ids)
.stop_conditions(StopConditions {
max_tokens,
..Default::default()
})
.sampling_options(SamplingOptions::default())
.output_options(OutputOptions::default())
.routing(Some(RoutingHints {
cache_namespace,
..Default::default()
}))
.extra_args(Some(json!({ "sglang_tito": body })))
.annotations(vec![])
.build()
.map_err(|error| ClientError::new(error.to_string()))?;
Ok(Bound {
request,
exchange: Box::new(PassthroughExchange),
})
}),
})
}
}
struct Readiness;
impl GenerateFormat for Readiness {
fn name(&self) -> &str {
"vllm"
}
fn capability(&self) -> &'static str {
VLLM_INFERENCE_V1_GENERATE_CAPABILITY
}
fn open(&self, _: ClientRequest<'_>) -> Result<Opened, ClientError> {
unreachable!("route readiness must not parse inference requests")
}
}
pub(crate) fn install() {
static INSTALL: std::sync::Once = std::sync::Once::new();
INSTALL.call_once(|| {
let mut registry = Registry::default();
registry.register(Arc::new(Passthrough)).unwrap();
registry.register(Arc::new(Readiness)).unwrap();
assert!(super::install(registry));
});
}
}