joshua 0.1.0

An mmap-based LLM inference engine for the Rust ecosystem — a Rust clone of Cactus
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
//! OpenAI-compatible HTTP API server for Joshua.
//!
//! Implements the following endpoints:
//! - `GET  /health`                   — liveness check
//! - `GET  /v1/models`                — list loaded model
//! - `POST /v1/chat/completions`      — chat completion (stream or non-stream)
//! - `POST /v1/completions`           — legacy text completion
//! - `POST /v1/embeddings`            — dense text embeddings
//! - `POST /v1/audio/transcriptions`  — Whisper speech-to-text (when a
//!   whisper model is configured)

use std::convert::Infallible;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};

use axum::{
    extract::{Request, State},
    http::{header, StatusCode},
    middleware::{self, Next},
    response::{
        sse::{Event, Sse},
        IntoResponse, Response,
    },
    routing::{get, post},
    Json, Router,
};
use futures_util::{stream, StreamExt};
use serde_json::json;
use tokio::net::TcpListener;
use tower_http::cors::CorsLayer;
use uuid::Uuid;

use crate::engine::Engine;
use crate::error::JoshuaError;
use crate::whisper::WhisperEngine;
use crate::tools::parse_tool_calls;
use crate::types::{
    AssistantMessage, ChatChoice, ChatCompletionChunk, ChatCompletionRequest,
    ChatCompletionResponse, ChatMessage, DeltaContent, EmbeddingData, EmbeddingRequest,
    EmbeddingResponse, ErrorResponse, FunctionCallResult, GenerationOptions, ModelInfo,
    ModelListResponse, ToolCall, UsageInfo,
};

/// Shared application state.
pub struct ServerState {
    /// The chat/embedding engine.
    pub engine: Arc<Engine>,
    /// Optional Whisper model for `/v1/audio/transcriptions`.
    pub whisper: Option<Arc<WhisperEngine>>,
    /// When set, every `/v1` request must carry this key as
    /// `Authorization: Bearer <key>`.  `/health` stays open for probes.
    pub api_key: Option<String>,
}

/// Shared application state handle.
pub type AppState = Arc<ServerState>;

// ─── Router ───────────────────────────────────────────────────────────────────

/// Build the Axum router with all API routes mounted.
pub fn create_router(state: AppState) -> Router {
    let api = Router::new()
        .route("/v1/models", get(list_models))
        .route("/v1/chat/completions", post(chat_completions))
        .route("/v1/completions", post(completions))
        .route("/v1/embeddings", post(embeddings))
        .route("/v1/audio/transcriptions", post(transcriptions))
        .layer(middleware::from_fn_with_state(
            Arc::clone(&state),
            require_api_key,
        ));
    Router::new()
        .route("/health", get(health))
        .merge(api)
        .layer(CorsLayer::permissive())
        .with_state(state)
}

/// Reject `/v1` requests that lack the configured bearer API key.
///
/// A no-op when no key is configured.  Comparison is constant-time so the
/// key can't be recovered byte-by-byte through response timing.
async fn require_api_key(State(state): State<AppState>, req: Request, next: Next) -> Response {
    if let Some(expected) = &state.api_key {
        let provided = req
            .headers()
            .get(header::AUTHORIZATION)
            .and_then(|v| v.to_str().ok())
            .and_then(|v| v.strip_prefix("Bearer "));
        if !provided.is_some_and(|key| api_keys_match(key.as_bytes(), expected.as_bytes())) {
            let body = ErrorResponse::new(
                "invalid or missing API key — pass the key as 'Authorization: Bearer <key>'",
                "invalid_request_error",
            );
            return (StatusCode::UNAUTHORIZED, Json(body)).into_response();
        }
    }
    next.run(req).await
}

/// Key equality that leaks nothing about the configured key through timing.
///
/// Both keys are hashed first, so the comparison always runs over two
/// fixed-size digests regardless of either key's length or contents; the
/// digests are then compared with a branch-free fold.  Timing can therefore
/// only reveal information about SHA-256 digests, which is useless without
/// inverting the hash.
fn api_keys_match(provided: &[u8], expected: &[u8]) -> bool {
    use sha2::{Digest, Sha256};

    let provided = Sha256::digest(provided);
    let expected = Sha256::digest(expected);
    provided
        .iter()
        .zip(expected.iter())
        .fold(0u8, |acc, (x, y)| acc | (x ^ y))
        == 0
}

/// Start the server on `addr` (e.g. `"0.0.0.0:8080"`) with just a chat
/// engine.  Use [`serve_with_state`] to also mount a Whisper model.
pub async fn serve(engine: Arc<Engine>, addr: &str) -> std::io::Result<()> {
    serve_with_state(
        Arc::new(ServerState {
            engine,
            whisper: None,
            api_key: None,
        }),
        addr,
    )
    .await
}

/// Start the server with a fully configured [`ServerState`].
pub async fn serve_with_state(state: AppState, addr: &str) -> std::io::Result<()> {
    let app = create_router(state);
    let listener = TcpListener::bind(addr).await?;
    tracing::info!("Joshua server listening on http://{}", addr);
    axum::serve(listener, app).await
}

/// Start the server over HTTPS (the `tls` cargo feature).
///
/// `cert` and `key` are paths to a PEM-encoded certificate chain and
/// PKCS#8/RSA/SEC1 private key.  TLS is terminated in-process by rustls —
/// no reverse proxy needed.
#[cfg(feature = "tls")]
pub async fn serve_with_state_tls(
    state: AppState,
    addr: &str,
    cert: &std::path::Path,
    key: &std::path::Path,
) -> std::io::Result<()> {
    use std::io::{Error, ErrorKind};

    // axum-server is built without a default crypto provider; install ring
    // process-wide.  Err means a provider is already installed — fine.
    let _ = rustls::crypto::ring::default_provider().install_default();

    let addr: std::net::SocketAddr = addr
        .parse()
        .map_err(|e| Error::new(ErrorKind::InvalidInput, format!("invalid address: {e}")))?;
    let config = axum_server::tls_rustls::RustlsConfig::from_pem_file(cert, key).await?;
    let app = create_router(state);
    tracing::info!("Joshua server listening on https://{}", addr);
    axum_server::bind_rustls(addr, config)
        .serve(app.into_make_service())
        .await
}

// ─── Handlers ────────────────────────────────────────────────────────────────

/// `GET /health` — returns `{"status":"ok"}`.
async fn health() -> Json<serde_json::Value> {
    Json(json!({"status": "ok"}))
}

/// `GET /v1/models` — returns the single loaded model.
async fn list_models(State(state): State<AppState>) -> Json<ModelListResponse> {
    let created = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();
    let mut data = vec![ModelInfo {
        id: state.engine.model_name().to_string(),
        object: "model".to_string(),
        created,
        owned_by: "joshua".to_string(),
    }];
    if let Some(whisper) = &state.whisper {
        data.push(ModelInfo {
            id: whisper.model_name().to_string(),
            object: "model".to_string(),
            created,
            owned_by: "joshua".to_string(),
        });
    }
    Json(ModelListResponse {
        object: "list".to_string(),
        data,
    })
}

/// `POST /v1/chat/completions` — OpenAI chat completions.
async fn chat_completions(
    State(state): State<AppState>,
    Json(req): Json<ChatCompletionRequest>,
) -> Result<Response, ApiError> {
    let engine = Arc::clone(&state.engine);
    let stream = req.stream.unwrap_or(false);
    let options = req.to_generation_options();
    let messages = req.messages.clone();
    let tools = req.tools.clone();
    let model = engine.model_name().to_string();

    if stream {
        // ── Streaming path ────────────────────────────────────────────────────
        let id = format!("chatcmpl-{}", Uuid::new_v4().simple());

        // Run inference in a blocking thread to avoid stalling the async runtime.
        let (text, usage, _, _) = tokio::task::spawn_blocking({
            let engine = Arc::clone(&engine);
            let tools = tools.clone();
            move || engine.complete_chat(&messages, tools.as_deref(), &options)
        })
        .await
        .map_err(|e| ApiError::internal(e.to_string()))?
        .map_err(ApiError::from)?;

        // When tools were requested and the model emitted calls, send them as
        // a single delta chunk (OpenAI wire format) instead of char-streaming
        // the raw markup.
        if tools.is_some() {
            let (prose, calls) = parse_tool_calls(&text);
            if !calls.is_empty() {
                let delta = DeltaContent {
                    role: Some("assistant".to_string()),
                    content: if prose.is_empty() { None } else { Some(prose) },
                    tool_calls: Some(tool_call_deltas(&calls)),
                };
                let first = ChatCompletionChunk::new(id.clone(), model.clone(), delta, None);
                let stop = ChatCompletionChunk::new(
                    id.clone(),
                    model.clone(),
                    DeltaContent::default(),
                    Some("tool_calls".to_string()),
                );
                let events = stream::iter([first, stop].into_iter().map(|chunk| {
                    let data = serde_json::to_string(&chunk).unwrap_or_default();
                    Ok::<Event, Infallible>(Event::default().data(data))
                }))
                .chain(stream::once(async {
                    Ok::<Event, Infallible>(Event::default().data("[DONE]"))
                }));
                return Ok(Sse::new(events).into_response());
            }
        }

        // Stream the response character-by-character (word-level chunks are
        // possible too, but char chunks give the smoothest streaming experience).
        let chunks: Vec<String> = text
            .char_indices()
            .map(|(_, c)| c.to_string())
            .collect();

        let id2 = id.clone();
        let model2 = model.clone();
        let n_chunks = chunks.len();

        // Content chunks — include the role header on the very first chunk.
        let content_events =
            stream::iter(chunks.into_iter().enumerate().map(move |(i, chunk)| {
                let delta = if i == 0 {
                    DeltaContent {
                        role: Some("assistant".to_string()),
                        content: Some(chunk),
                        tool_calls: None,
                    }
                } else {
                    DeltaContent {
                        role: None,
                        content: Some(chunk),
                        tool_calls: None,
                    }
                };
                let payload =
                    ChatCompletionChunk::new(id2.clone(), model2.clone(), delta, None);
                let data = serde_json::to_string(&payload).unwrap_or_default();
                Ok::<Event, Infallible>(Event::default().data(data))
            }));

        // Final "stop" chunk — includes usage statistics as per the OpenAI spec
        // (`stream_options.include_usage`). We always include them so that clients
        // that inspect this chunk get accurate token counts.
        let stop_payload = {
            let chunk =
                ChatCompletionChunk::new(id.clone(), model.clone(), DeltaContent::default(), Some("stop".to_string()));
            // Attach usage as an extra field via serde_json (ChatCompletionChunk
            // doesn't have a `usage` field to keep streaming chunks lean, so we
            // serialise it manually here and embed it).
            let mut value = serde_json::to_value(&chunk).unwrap_or_default();
            value["usage"] = serde_json::json!({
                "prompt_tokens":     usage.prompt_tokens,
                "completion_tokens": usage.completion_tokens,
                "total_tokens":      usage.total_tokens,
            });
            serde_json::to_string(&value).unwrap_or_default()
        };

        let sse_stream = content_events
            .chain(stream::once(async move {
                Ok::<Event, Infallible>(Event::default().data(stop_payload))
            }))
            .chain(stream::once(async {
                Ok::<Event, Infallible>(Event::default().data("[DONE]"))
            }));

        let _ = n_chunks; // consumed above

        return Ok(Sse::new(sse_stream).into_response());
    }

    // ── Non-streaming path ────────────────────────────────────────────────────
    let (text, usage, _, _) = tokio::task::spawn_blocking({
        let engine = Arc::clone(&engine);
        let tools = tools.clone();
        move || engine.complete_chat(&messages, tools.as_deref(), &options)
    })
    .await
    .map_err(|e| ApiError::internal(e.to_string()))?
    .map_err(ApiError::from)?;

    // Extract tool calls from the output when the request offered tools.
    let (content, tool_calls, finish_reason) = if tools.is_some() {
        let (prose, calls) = parse_tool_calls(&text);
        if calls.is_empty() {
            (Some(text), None, "stop")
        } else {
            let calls: Vec<ToolCall> = calls
                .into_iter()
                .map(|c| ToolCall {
                    id: format!("call_{}", Uuid::new_v4().simple()),
                    call_type: "function".to_string(),
                    function: FunctionCallResult {
                        name: c.name,
                        arguments: c.arguments,
                    },
                })
                .collect();
            (
                if prose.is_empty() { None } else { Some(prose) },
                Some(calls),
                "tool_calls",
            )
        }
    } else {
        (Some(text), None, "stop")
    };

    let id = format!("chatcmpl-{}", Uuid::new_v4().simple());
    let response = ChatCompletionResponse::new(
        id,
        model,
        vec![ChatChoice {
            index: 0,
            message: AssistantMessage {
                role: "assistant".to_string(),
                content,
                tool_calls,
            },
            finish_reason: finish_reason.to_string(),
        }],
        usage,
    );
    Ok(Json(response).into_response())
}

/// Build the OpenAI streaming `delta.tool_calls` payload (index per entry).
fn tool_call_deltas(calls: &[crate::tools::ParsedToolCall]) -> serde_json::Value {
    serde_json::Value::Array(
        calls
            .iter()
            .enumerate()
            .map(|(i, c)| {
                json!({
                    "index": i,
                    "id": format!("call_{}", Uuid::new_v4().simple()),
                    "type": "function",
                    "function": {"name": c.name, "arguments": c.arguments},
                })
            })
            .collect(),
    )
}

/// `POST /v1/completions` — legacy (non-chat) text completion.
async fn completions(
    State(state): State<AppState>,
    Json(body): Json<serde_json::Value>,
) -> Result<Json<serde_json::Value>, ApiError> {
    let engine = Arc::clone(&state.engine);
    let prompt = body
        .get("prompt")
        .and_then(|v| v.as_str())
        .ok_or_else(|| ApiError::bad_request("Missing required field 'prompt'"))?
        .to_string();

    let options = GenerationOptions {
        max_tokens: body
            .get("max_tokens")
            .and_then(|v| v.as_u64())
            .map(|v| v as u32)
            .unwrap_or(256),
        temperature: body
            .get("temperature")
            .and_then(|v| v.as_f64())
            .map(|v| v as f32)
            .unwrap_or(0.7),
        ..GenerationOptions::default()
    };

    let messages = vec![ChatMessage::text("user".to_string(), prompt)];

    let (text, usage, _, _) = tokio::task::spawn_blocking({
        let engine = Arc::clone(&engine);
        move || engine.complete(&messages, &options)
    })
    .await
    .map_err(|e| ApiError::internal(e.to_string()))?
    .map_err(ApiError::from)?;

    let created = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();

    Ok(Json(json!({
        "id": format!("cmpl-{}", Uuid::new_v4().simple()),
        "object": "text_completion",
        "created": created,
        "model": engine.model_name(),
        "choices": [{
            "text": text,
            "index": 0,
            "finish_reason": "stop"
        }],
        "usage": {
            "prompt_tokens": usage.prompt_tokens,
            "completion_tokens": usage.completion_tokens,
            "total_tokens": usage.total_tokens
        }
    })))
}

/// `POST /v1/embeddings` — dense text embeddings.
async fn embeddings(
    State(state): State<AppState>,
    Json(req): Json<EmbeddingRequest>,
) -> Result<Json<EmbeddingResponse>, ApiError> {
    let engine = Arc::clone(&state.engine);
    let texts: Vec<String> = req.input.into_vec();
    let model = engine.model_name().to_string();

    let (vectors, prompt_tokens) = tokio::task::spawn_blocking({
        let engine = Arc::clone(&engine);
        move || engine.embed_with_usage(&texts)
    })
    .await
    .map_err(|e| ApiError::internal(e.to_string()))?
    .map_err(ApiError::from)?;

    let data = vectors
        .into_iter()
        .enumerate()
        .map(|(i, embedding)| EmbeddingData {
            object: "embedding".to_string(),
            embedding,
            index: i as u32,
        })
        .collect();

    Ok(Json(EmbeddingResponse {
        object: "list".to_string(),
        data,
        model,
        usage: UsageInfo {
            prompt_tokens,
            completion_tokens: 0,
            total_tokens: prompt_tokens,
        },
    }))
}

/// `POST /v1/audio/transcriptions` — OpenAI-compatible Whisper STT.
///
/// Multipart form fields: `file` (required, WAV), `language` (optional
/// two-letter code), `response_format` (`json` default, or `text`), and
/// `model` (accepted and ignored — the loaded whisper model is used).
async fn transcriptions(
    State(state): State<AppState>,
    mut multipart: axum::extract::Multipart,
) -> Result<Response, ApiError> {
    let Some(whisper) = state.whisper.clone() else {
        return Err(ApiError::bad_request(
            "no whisper model is loaded — start the server with --whisper-model",
        ));
    };

    let mut file: Option<Vec<u8>> = None;
    let mut language: Option<String> = None;
    let mut response_format = "json".to_string();
    while let Some(field) = multipart
        .next_field()
        .await
        .map_err(|e| ApiError::bad_request(format!("invalid multipart body: {e}")))?
    {
        match field.name().unwrap_or_default() {
            "file" => {
                let bytes = field
                    .bytes()
                    .await
                    .map_err(|e| ApiError::bad_request(format!("file upload failed: {e}")))?;
                file = Some(bytes.to_vec());
            }
            "language" => {
                language = Some(field.text().await.unwrap_or_default());
            }
            "response_format" => {
                response_format = field.text().await.unwrap_or_default();
            }
            // `model`, `prompt`, `temperature`, … accepted and ignored.
            _ => {
                let _ = field.bytes().await;
            }
        }
    }
    let file = file.ok_or_else(|| ApiError::bad_request("missing required field 'file'"))?;

    let transcription = tokio::task::spawn_blocking({
        let language = language.clone();
        move || whisper.transcribe_wav(&file, language.as_deref(), false)
    })
    .await
    .map_err(|e| ApiError::internal(e.to_string()))?
    .map_err(ApiError::from)?;

    if response_format == "text" {
        return Ok(transcription.text.into_response());
    }
    Ok(Json(json!({
        "text": transcription.text,
        "duration": transcription.duration,
        "language": transcription.language,
    }))
    .into_response())
}

// ─── API error type ───────────────────────────────────────────────────────────

/// Internal helper that maps [`JoshuaError`] to HTTP responses.
pub struct ApiError {
    status: StatusCode,
    body: ErrorResponse,
}

impl ApiError {
    fn bad_request(msg: impl Into<String>) -> Self {
        Self {
            status: StatusCode::BAD_REQUEST,
            body: ErrorResponse::invalid_request(msg),
        }
    }

    /// Internal failure: the detail is logged server-side, and the client
    /// receives only a generic message (no internal strings / panic text).
    fn internal(msg: impl Into<String>) -> Self {
        tracing::error!("internal error serving request: {}", msg.into());
        Self {
            status: StatusCode::INTERNAL_SERVER_ERROR,
            body: ErrorResponse::server_error("internal error"),
        }
    }
}

impl From<JoshuaError> for ApiError {
    fn from(err: JoshuaError) -> Self {
        match &err {
            // Client errors carry a caller-actionable message and are safe
            // to echo verbatim.
            JoshuaError::InvalidRequest(_) | JoshuaError::PromptTooLong(_, _) => Self {
                status: StatusCode::BAD_REQUEST,
                body: ErrorResponse::invalid_request(err.to_string()),
            },
            JoshuaError::Overloaded(_) => Self {
                status: StatusCode::SERVICE_UNAVAILABLE,
                body: ErrorResponse::new(err.to_string(), "overloaded"),
            },
            // Everything else may embed internal detail (tokenizer/candle
            // messages, io errors). Log it server-side; return a generic body.
            _ => {
                tracing::error!("internal error serving request: {err}");
                Self {
                    status: StatusCode::INTERNAL_SERVER_ERROR,
                    body: ErrorResponse::server_error("internal error"),
                }
            }
        }
    }
}

impl IntoResponse for ApiError {
    fn into_response(self) -> Response {
        (self.status, Json(self.body)).into_response()
    }
}

#[cfg(test)]
mod tests {
    use super::api_keys_match;

    #[test]
    fn api_keys_match_accepts_only_the_exact_key() {
        assert!(api_keys_match(b"sekret", b"sekret"));
        assert!(!api_keys_match(b"Sekret", b"sekret"));
        assert!(!api_keys_match(b"sek", b"sekret"));
        assert!(!api_keys_match(b"sekretsekret", b"sekret"));
        assert!(!api_keys_match(b"", b"sekret"));
    }

    #[test]
    fn api_keys_match_handles_an_empty_expected_key() {
        assert!(api_keys_match(b"", b""));
        assert!(!api_keys_match(b"a", b""));
    }
}