use crate::error::{MojenticError, Result};
use crate::llm::gateway::{
CompletionConfig, LlmGateway, ModelContextWindow, ReasoningEffort, ResponseFormat, StreamChunk,
};
use crate::llm::gateways::openai::{add_response_format, openai_response_evidence};
use crate::llm::gateways::openai_legacy_stream::{
legacy_body_stream, read_legacy_line, LegacyLine, OpenAiLegacyParser,
};
use crate::llm::gateways::openai_messages_adapter::{
adapt_messages_to_openai_with_reasoning, convert_tool_calls,
};
use crate::llm::gateways::openai_stream_events::OpenAiEventParser;
use crate::llm::models::{LlmGatewayResponse, LlmMessage, ResponseEvidence};
use crate::llm::stream_events::{
drive_event_stream, FrameParser, StreamEvent, StreamEventError, StreamEventStream,
};
use crate::llm::stream_timing::{system_clock, StreamClock, StreamTiming};
use crate::llm::tools::LlmTool;
use async_trait::async_trait;
use futures::stream::Stream;
use percent_encoding::{utf8_percent_encode, AsciiSet, NON_ALPHANUMERIC};
use reqwest::{Client, Method, RequestBuilder, Response};
use serde_json::Value;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;
use tracing::{debug, info, warn};
pub const RESPONSE_FORMAT_WARNING: &str = "response_format_warning";
const DEFAULT_HOST: &str = "http://localhost:8000";
const DEFAULT_TIMEOUT: Duration = Duration::from_millis(600_000);
const KEEPALIVE_MODEL: &str = "keepalive";
const PATH_SEGMENT: &AsciiSet =
&NON_ALPHANUMERIC.remove(b'-').remove(b'.').remove(b'_').remove(b'~');
#[derive(Clone, PartialEq, Eq)]
pub struct OmlxConfig {
pub host: String,
pub api_key: Option<String>,
pub timeout: Option<Duration>,
}
impl std::fmt::Debug for OmlxConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("OmlxConfig")
.field("host", &self.host)
.field("api_key", &self.api_key.as_ref().map(|_| "<redacted>"))
.field("timeout", &self.timeout)
.finish()
}
}
impl Default for OmlxConfig {
fn default() -> Self {
Self {
host: std::env::var("OMLX_HOST").unwrap_or_else(|_| DEFAULT_HOST.to_string()),
api_key: std::env::var("OMLX_API_KEY").ok().filter(|key| !key.is_empty()),
timeout: Some(timeout_from_env(std::env::var("OMLX_TIMEOUT").ok())),
}
}
}
fn timeout_from_env(value: Option<String>) -> Duration {
let Some(value) = value else {
return DEFAULT_TIMEOUT;
};
match value.trim().parse::<u64>() {
Ok(millis) => Duration::from_millis(millis),
Err(_) => {
warn!(value = %value, "OMLX_TIMEOUT is not a number of milliseconds, using the default");
DEFAULT_TIMEOUT
}
}
}
pub struct OmlxGateway {
clock: Arc<dyn StreamClock>,
client: Client,
config: OmlxConfig,
}
impl OmlxGateway {
pub fn with_stream_clock(mut self, clock: Arc<dyn StreamClock>) -> Self {
self.clock = clock;
self
}
pub fn with_recovery(
self,
policy: crate::llm::recovery::RecoveryPolicy,
) -> Result<crate::llm::recovery::RecoveryGateway<Self>> {
crate::llm::recovery::RecoveryGateway::new(self, policy)
}
pub fn new() -> Self {
Self::with_config(OmlxConfig::default())
}
pub fn with_config(config: OmlxConfig) -> Self {
Self {
client: Client::new(),
clock: system_clock(),
config,
}
}
pub fn with_host(host: impl Into<String>) -> Self {
Self::with_config(OmlxConfig {
host: host.into(),
..Default::default()
})
}
pub async fn load_model(&self, model: &str) -> Result<()> {
info!(model, "Loading oMLX model");
self.model_action(model, "load").await
}
pub async fn unload_model(&self, model: &str) -> Result<()> {
info!(model, "Unloading oMLX model");
self.model_action(model, "unload").await
}
async fn model_action(&self, model: &str, action: &str) -> Result<()> {
if model.trim().is_empty() {
return Err(MojenticError::InvalidArgument(
"oMLX model id must not be blank".to_string(),
));
}
let model = utf8_percent_encode(model, PATH_SEGMENT);
let response = self
.request(Method::POST, &format!("/models/{model}/{action}"), false)
.send()
.await?;
success(response).await?;
Ok(())
}
fn request(&self, method: Method, path: &str, streaming: bool) -> RequestBuilder {
let url = format!("{}/v1{}", self.config.host.trim_end_matches('/'), path);
let mut request = self.client.request(method, url);
if let Some(key) = self.config.api_key.as_ref().filter(|key| !key.trim().is_empty()) {
request = request.bearer_auth(key);
}
if !streaming {
if let Some(timeout) = self.config.timeout {
request = request.timeout(timeout);
}
}
request
}
fn chat_request(&self, body: &Value) -> RequestBuilder {
self.request(Method::POST, "/chat/completions", body["stream"] == true)
.json(body)
}
async fn chat(&self, body: &Value, structured: bool) -> Result<ChatReply> {
let response = success(self.chat_request(body).send().await?).await?;
let warning = structured.then(|| format_warning(&response)).flatten();
let body: Value = response.json().await?;
let mut evidence = openai_response_evidence(&body);
if let Some(warning) = warning {
warn!(warning = %warning, "oMLX did not enforce the requested response format");
evidence
.metadata
.insert(RESPONSE_FORMAT_WARNING.to_string(), Value::String(warning));
}
Ok(ChatReply { body, evidence })
}
}
impl Default for OmlxGateway {
fn default() -> Self {
Self::new()
}
}
struct ChatReply {
body: Value,
evidence: ResponseEvidence,
}
impl ChatReply {
fn message(&self) -> &Value {
&self.body["choices"][0]["message"]
}
fn text(&self, field: &str) -> Option<String> {
self.message()[field].as_str().map(String::from)
}
}
async fn success(response: Response) -> Result<Response> {
let status = response.status();
if status.is_success() {
return Ok(response);
}
let body = response.text().await.unwrap_or_default();
Err(MojenticError::GatewayError(format!("oMLX API error: {status} - {body}")))
}
fn format_warning(response: &Response) -> Option<String> {
format_warning_headers(response.headers())
}
pub(crate) fn format_warning_headers(headers: &reqwest::header::HeaderMap) -> Option<String> {
let values: Vec<_> = headers
.get_all(reqwest::header::WARNING)
.iter()
.map(|value| String::from_utf8_lossy(value.as_bytes()).into_owned())
.collect();
(!values.is_empty()).then(|| values.join(", "))
}
fn requests_structured_output(config: &CompletionConfig) -> bool {
matches!(config.response_format, Some(ResponseFormat::JsonObject { .. }))
}
fn chat_body(model: &str, messages: &[LlmMessage], config: &CompletionConfig) -> Result<Value> {
config.validate_reasoning()?;
let mut body = serde_json::json!({
"model": model,
"messages": adapt_messages_to_openai_with_reasoning(messages, true)?,
});
if let Some(temperature) = config.temperature {
body["temperature"] = serde_json::json!(temperature);
}
if let Some(max_tokens) = config.max_tokens {
body["max_tokens"] = serde_json::json!(max_tokens);
}
if config.preserve_reasoning {
body["chat_template_kwargs"] = serde_json::json!({"preserve_thinking": true});
}
if let Some(top_p) = config.top_p {
body["top_p"] = serde_json::json!(top_p);
}
if let Some(top_k) = config.top_k {
body["top_k"] = serde_json::json!(top_k);
}
match config.reasoning_effort {
Some(ReasoningEffort::Disabled) => body["enable_thinking"] = Value::Bool(false),
Some(ReasoningEffort::Enabled) => body["enable_thinking"] = Value::Bool(true),
Some(effort) => body["reasoning_effort"] = serde_json::to_value(effort)?,
None => {}
}
add_response_format(&mut body, config);
Ok(body)
}
fn tool_definitions(tools: &[Box<dyn LlmTool>]) -> Result<Value> {
let descriptors: Vec<_> = tools.iter().map(|tool| tool.descriptor()).collect();
Ok(serde_json::to_value(descriptors)?)
}
fn is_keepalive(frame: &Value) -> bool {
frame["model"] == KEEPALIVE_MODEL
}
fn is_keepalive_line(line: &str) -> bool {
if !line.contains(KEEPALIVE_MODEL) {
return false;
}
line.strip_prefix("data:")
.and_then(|data| serde_json::from_str::<Value>(data.trim_start()).ok())
.is_some_and(|frame| is_keepalive(&frame))
}
#[derive(Default)]
struct OmlxEventParser {
inner: OpenAiEventParser,
}
impl FrameParser for OmlxEventParser {
fn parse_line(&mut self, line: &str) -> Vec<StreamEvent> {
if is_keepalive_line(line) {
return Vec::new();
}
self.inner.parse_line(line)
}
fn partial_evidence(&self) -> Option<ResponseEvidence> {
self.inner.partial_evidence()
}
}
fn legacy_chunks(parser: &mut OpenAiLegacyParser, line: &str) -> Vec<StreamChunk> {
match read_legacy_line(line) {
Some(LegacyLine::Done) => parser.done(),
Some(LegacyLine::Frame(frame)) if is_keepalive(&frame) => Vec::new(),
Some(LegacyLine::Frame(frame)) => {
let reasoning = frame["choices"][0]["delta"]["reasoning_content"]
.as_str()
.filter(|text| !text.is_empty())
.map(|text| StreamChunk::Thinking(text.to_string()));
reasoning.into_iter().chain(parser.frame(&frame)).collect()
}
None => Vec::new(),
}
}
#[async_trait]
impl LlmGateway for OmlxGateway {
fn stream_clock(&self) -> Arc<dyn StreamClock> {
self.clock.clone()
}
async fn get_model_context_window(&self, model: &str) -> Result<Option<ModelContextWindow>> {
let response = success(self.request(Method::GET, "/models", false).send().await?).await?;
let body: Value = response.json().await?;
let models = body["data"]
.as_array()
.ok_or_else(|| MojenticError::GatewayError("Invalid response format".into()))?;
let Some(entry) = models.iter().find(|entry| entry["id"].as_str() == Some(model)) else {
return Ok(None);
};
entry
.get("max_model_len")
.map(|value| ModelContextWindow::advertised(value, "max_model_len".into()))
.transpose()
}
async fn complete(
&self,
model: &str,
messages: &[LlmMessage],
tools: Option<&[Box<dyn LlmTool>]>,
config: &CompletionConfig,
) -> Result<LlmGatewayResponse> {
info!("Delegating to oMLX for completion");
debug!("Model: {}, Message count: {}", model, messages.len());
let mut body = chat_body(model, messages, config)?;
if let Some(tools) = tools {
body["tools"] = tool_definitions(tools)?;
}
let reply = self.chat(&body, requests_structured_output(config)).await?;
let tool_calls =
reply.message()["tool_calls"].as_array().map(|calls| convert_tool_calls(calls));
Ok(LlmGatewayResponse {
content: reply.text("content"),
object: None,
tool_calls: tool_calls.unwrap_or_default(),
thinking: reply.text("reasoning_content"),
evidence: reply.evidence,
})
}
async fn complete_json(
&self,
model: &str,
messages: &[LlmMessage],
schema: Value,
config: &CompletionConfig,
) -> Result<Value> {
self.complete_json_response(model, messages, schema, config)
.await?
.object
.ok_or_else(|| MojenticError::GatewayError("No content in response".to_string()))
}
async fn complete_json_response(
&self,
model: &str,
messages: &[LlmMessage],
schema: Value,
config: &CompletionConfig,
) -> Result<LlmGatewayResponse<Value>> {
info!("Requesting structured output from oMLX");
let config = CompletionConfig {
response_format: Some(ResponseFormat::JsonObject {
schema: Some(schema),
}),
..config.clone()
};
let body = chat_body(model, messages, &config)?;
let reply = self.chat(&body, true).await?;
let content = reply
.text("content")
.ok_or_else(|| MojenticError::GatewayError("No content in response".to_string()))?;
let object: Value = serde_json::from_str(&content)?;
Ok(LlmGatewayResponse {
content: Some(content),
object: Some(object),
tool_calls: Vec::new(),
thinking: reply.text("reasoning_content"),
evidence: reply.evidence,
})
}
async fn get_available_models(&self) -> Result<Vec<String>> {
debug!("Fetching available oMLX models");
let response = success(self.request(Method::GET, "/models", false).send().await?).await?;
let body: Value = response.json().await?;
let mut models: Vec<String> = body["data"]
.as_array()
.ok_or_else(|| MojenticError::GatewayError("Invalid response format".to_string()))?
.iter()
.filter_map(|model| model["id"].as_str().map(String::from))
.collect();
models.sort();
Ok(models)
}
async fn calculate_embeddings(&self, text: &str, model: Option<&str>) -> Result<Vec<f32>> {
let model = model.filter(|model| !model.trim().is_empty()).ok_or_else(|| {
MojenticError::InvalidArgument(
"oMLX embeddings need a model: oMLX has no default embedding model".to_string(),
)
})?;
debug!("Calculating embeddings with model: {}", model);
let body = serde_json::json!({"model": model, "input": text});
let response =
success(self.request(Method::POST, "/embeddings", false).json(&body).send().await?)
.await?;
let body: Value = response.json().await?;
let embedding = body["data"][0]["embedding"]
.as_array()
.ok_or_else(|| MojenticError::GatewayError("Invalid embeddings response".to_string()))?
.iter()
.filter_map(|value| value.as_f64().map(|number| number as f32))
.collect();
Ok(embedding)
}
fn complete_stream<'a>(
&'a self,
model: &'a str,
messages: &'a [LlmMessage],
tools: Option<&'a [Box<dyn LlmTool>]>,
config: &'a CompletionConfig,
) -> Pin<Box<dyn Stream<Item = Result<StreamChunk>> + Send + 'a>> {
let timing = StreamTiming::new(self.stream_clock());
let measure = timing.clone();
let source: Pin<Box<dyn Stream<Item = Result<StreamChunk>> + Send + 'a>> =
Box::pin(async_stream::stream! {
info!("Starting oMLX streaming completion");
debug!("Model: {}, Message count: {}", model, messages.len());
let mut body = match chat_body(model, messages, config) {
Ok(body) => body,
Err(e) => {
yield Err(e);
return;
}
};
body["stream"] = serde_json::json!(true);
if let Some(tools) = tools {
match tool_definitions(tools) {
Ok(definitions) => body["tools"] = definitions,
Err(e) => {
yield Err(e);
return;
}
}
}
measure.dispatch();
let response = match self.chat_request(&body).send().await {
Ok(response) => response,
Err(e) => {
yield Err(e.into());
return;
}
};
let response = match success(response).await {
Ok(response) => response,
Err(e) => {
yield Err(e);
return;
}
};
let mut parser = OpenAiLegacyParser::default();
let chunks = legacy_body_stream(response.bytes_stream(), move |line: &str| {
measure.observe_line(line);
legacy_chunks(&mut parser, line)
});
for await chunk in chunks {
yield chunk;
}
});
timing.legacy(source, "omlx")
}
fn complete_stream_events<'a>(
&'a self,
model: &'a str,
messages: &'a [LlmMessage],
config: &'a CompletionConfig,
) -> std::result::Result<StreamEventStream<'a>, StreamEventError> {
info!("Starting oMLX stream events");
let mut body =
chat_body(model, messages, config).map_err(StreamEventError::RequestFailed)?;
body["stream"] = serde_json::json!(true);
body["stream_options"] = serde_json::json!({"include_usage": true});
Ok(drive_event_stream(
self.chat_request(&body),
OmlxEventParser::default(),
self.stream_clock(),
"omlx",
))
}
}
impl crate::llm::recovery::RecoveryProvider for OmlxGateway {
fn recovery_provider(&self) -> crate::llm::recovery::Provider {
crate::llm::recovery::Provider::Omlx
}
fn prepare_recovery(
&self,
model: &str,
messages: &[LlmMessage],
tools: Option<&[Box<dyn LlmTool>]>,
config: &CompletionConfig,
schema: Option<Value>,
stream: bool,
) -> Result<reqwest::Request> {
let mut config = config.clone();
if let Some(schema) = schema {
config.response_format = Some(ResponseFormat::JsonObject {
schema: Some(schema),
});
}
let mut body = chat_body(model, messages, &config)?;
body["stream"] = Value::Bool(stream);
if let Some(tools) = tools {
body["tools"] = tool_definitions(tools)?;
}
let mut request = self.chat_request(&body).build()?;
*request.timeout_mut() = None;
Ok(request)
}
}
#[cfg(test)]
mod tests {
use super::*;
use futures::stream::StreamExt;
#[tokio::test]
async fn blank_model_actions_fail_before_a_request() {
let gateway = OmlxGateway::with_host("http://127.0.0.1:1");
for model in ["", " ", "\t"] {
assert!(matches!(
gateway.load_model(model).await,
Err(MojenticError::InvalidArgument(_))
));
assert!(matches!(
gateway.unload_model(model).await,
Err(MojenticError::InvalidArgument(_))
));
}
}
#[tokio::test]
async fn event_stream_is_not_cut_off_by_the_whole_response_timeout() {
use crate::llm::stream_events::testing::endless_stream_server;
let (url, closed) =
endless_stream_server("data: {\"choices\":[{\"delta\":{\"content\":\"first\"}}]}\n\n")
.await;
let gateway = OmlxGateway::with_config(OmlxConfig {
host: url,
api_key: None,
timeout: Some(Duration::from_millis(50)),
});
let messages = [LlmMessage::user("Hi")];
let config = CompletionConfig::default();
let mut stream = gateway.complete_stream_events("m", &messages, &config).unwrap();
assert!(matches!(stream.next().await, Some(crate::llm::StreamEvent::Content(_))));
tokio::time::sleep(Duration::from_millis(100)).await;
assert!(tokio::time::timeout(Duration::from_millis(20), stream.next()).await.is_err());
drop(stream);
tokio::time::timeout(Duration::from_secs(2), closed).await.unwrap().unwrap();
}
#[tokio::test]
async fn legacy_stream_outlives_the_timeout_and_cancels_on_drop() {
use crate::llm::stream_events::testing::endless_stream_server;
let (url, closed) =
endless_stream_server("data: {\"choices\":[{\"delta\":{\"content\":\"first\"}}]}\n\n")
.await;
let gateway = OmlxGateway::with_config(OmlxConfig {
host: url,
api_key: None,
timeout: Some(Duration::from_millis(50)),
});
let messages = [LlmMessage::user("Hi")];
let config = CompletionConfig::default();
let mut stream = gateway.complete_stream("m", &messages, None, &config);
assert!(matches!(stream.next().await, Some(Ok(StreamChunk::Content(_)))));
tokio::time::sleep(Duration::from_millis(100)).await;
assert!(tokio::time::timeout(Duration::from_millis(20), stream.next()).await.is_err());
drop(stream);
tokio::time::timeout(Duration::from_secs(2), closed).await.unwrap().unwrap();
}
#[tokio::test]
async fn legacy_stream_keeps_a_character_split_across_network_chunks() {
use crate::llm::stream_events::testing::{
split_body_server, split_inside_first_multibyte_char,
};
let body = concat!(
"data: {\"model\":\"m\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\"\u{1f914} hmm\"}}]}\n\n",
"data: {\"model\":\"m\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"caf\u{e9}\"}}]}\n\n",
"data: [DONE]\n\n",
);
let url = split_body_server(split_inside_first_multibyte_char(body)).await;
let gateway = OmlxGateway::with_config(OmlxConfig {
host: url,
api_key: None,
timeout: None,
});
let messages = vec![LlmMessage::user("Hi")];
let config = CompletionConfig::default();
let items: Vec<_> = gateway.complete_stream("m", &messages, None, &config).collect().await;
assert!(
matches!(
items.as_slice(),
[Ok(StreamChunk::Thinking(thinking)), Ok(StreamChunk::Content(content)), Ok(StreamChunk::Metrics(_))]
if thinking == "\u{1f914} hmm" && content == "caf\u{e9}"
),
"{items:?}"
);
}
#[test]
fn debug_output_hides_the_api_key() {
let config = OmlxConfig {
host: "http://localhost:8000".into(),
api_key: Some("secret-key".into()),
timeout: None,
};
let shown = format!("{config:?}");
assert!(!shown.contains("secret-key"), "{shown}");
assert!(shown.contains("<redacted>"), "{shown}");
}
#[test]
fn timeout_env_is_milliseconds_with_a_ten_minute_default() {
assert_eq!(timeout_from_env(None), Duration::from_secs(600));
assert_eq!(timeout_from_env(Some("2500".into())), Duration::from_millis(2500));
assert_eq!(timeout_from_env(Some(" 1000 ".into())), Duration::from_secs(1));
assert_eq!(timeout_from_env(Some("ten".into())), Duration::from_secs(600));
assert_eq!(timeout_from_env(Some("-5".into())), Duration::from_secs(600));
}
#[test]
fn a_keepalive_line_has_exactly_the_keepalive_model() {
assert!(is_keepalive_line(r#"data: {"model":"keepalive","choices":[]}"#));
assert!(is_keepalive_line(r#"data:{"model":"keepalive","choices":[]}"#));
assert!(!is_keepalive_line(r#"data: {"model":"keepalive-7b","choices":[]}"#));
assert!(!is_keepalive_line(
r#"data: {"model":"m","choices":[{"delta":{"content":"keepalive"}}]}"#
));
assert!(!is_keepalive_line(": keepalive"));
assert!(!is_keepalive_line("data: [DONE]"));
}
#[test]
fn the_event_parser_ignores_keepalive_evidence() {
let mut parser = OmlxEventParser::default();
let events = parser.parse_line(
r#"data: {"id":"c","created":0,"model":"keepalive","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}"#,
);
assert!(events.is_empty());
assert_eq!(parser.partial_evidence(), None);
}
#[test]
fn reasoning_comes_before_content_in_one_delta() {
let mut parser = OpenAiLegacyParser::default();
let chunks = legacy_chunks(
&mut parser,
r#"data: {"model":"m","choices":[{"delta":{"reasoning_content":"why","content":"what"}}]}"#,
);
assert!(matches!(
chunks.as_slice(),
[StreamChunk::Thinking(why), StreamChunk::Content(what)] if why == "why" && what == "what"
));
}
#[test]
fn only_json_formats_request_structured_output() {
let with = |format| CompletionConfig {
response_format: format,
..Default::default()
};
assert!(!requests_structured_output(&with(None)));
assert!(!requests_structured_output(&with(Some(ResponseFormat::Text))));
assert!(requests_structured_output(&with(Some(ResponseFormat::JsonObject {
schema: None
}))));
}
}