furnace-server 0.13.0

Fast, unified, Rust-native, Axum-compatible inference endpoints and nervous system for tensor models and LLMs: KServe / Triton and OpenAI APIs from one pure-Rust server
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
//! The OpenAI-compatible routes: `POST /v1/chat/completions`, `POST /v1/completions` and
//! `GET /v1/models`, with SSE streaming, usage, and errors in OpenAI's shape.
//!
//! Cancellation is drop-based: when a streaming client disconnects, hyper drops the response
//! body, which drops the event receiver, and the engine stops generating for it. A
//! non-streaming request is cancelled the same way when its handler is dropped.

use std::collections::VecDeque;
use std::convert::Infallible;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

use axum::Router;
use axum::body::Bytes;
use axum::extract::State;
use axum::extract::rejection::BytesRejection;
use axum::http::StatusCode;
use axum::response::sse::{Event as SseEvent, KeepAlive, Sse};
use axum::response::{IntoResponse, Json, Response};
use axum::routing::{get, post};
use serde::{Deserialize, Serialize};
use tokio::sync::{OwnedSemaphorePermit, Semaphore};

use super::metrics::{self, EdgeMetrics, Tracked};
use super::{
    EngineError, Event, FinishReason, GenerateRequest, LlmEngine, LlmMetrics, Message, Prompt,
    SamplingParams,
};

/// OpenAI's limit on stop strings
const MAX_STOP_STRINGS: usize = 4;

/// The LLMs of a server
pub(crate) struct LlmState {
    engines: Vec<Served>,
    /// The nervous system the requests go through: each LLM is a brain neuron there
    serving: Arc<crate::nerve::serve::Serving>,
    /// The server's `request_timeout`: the bound on a whole answer, and on the wait for each
    /// event of a stream
    timeout: Option<Duration>,
    /// The server's other models, listed by `/v1/models` too
    other_models: Vec<String>,
    /// The server's concurrency limit (`max_concurrent_requests`), shared with every route
    slots: Arc<Semaphore>,
    /// When the server started, as `created` in `/v1/models`
    created: u64,
}

struct Served {
    engine: Arc<dyn LlmEngine>,
    metrics: Arc<EdgeMetrics>,
}

impl LlmState {
    pub(crate) fn new(
        engines: Vec<Arc<dyn LlmEngine>>,
        other_models: Vec<String>,
        slots: Arc<Semaphore>,
        serving: Arc<crate::nerve::serve::Serving>,
        timeout: Option<Duration>,
    ) -> Self {
        for (i, engine) in engines.iter().enumerate() {
            let name = engine.name();
            assert!(
                !other_models.iter().any(|m| m == name)
                    && !engines[..i].iter().any(|e| e.name() == name),
                "two models are named '{name}'"
            );
        }
        Self {
            engines: engines
                .into_iter()
                .map(|engine| Served {
                    engine,
                    metrics: Arc::default(),
                })
                .collect(),
            serving,
            timeout,
            other_models,
            slots,
            created: unix_seconds(),
        }
    }

    pub(crate) fn names(&self) -> Vec<&str> {
        self.engines.iter().map(|s| s.engine.name()).collect()
    }

    pub(crate) fn render_prometheus(&self, out: &mut String) {
        let engines: Vec<(&str, &EdgeMetrics, &dyn LlmEngine)> = self
            .engines
            .iter()
            .map(|s| (s.engine.name(), &*s.metrics, &*s.engine))
            .collect();
        metrics::render(out, &engines);
    }

    pub(crate) fn json_metrics(&self) -> Vec<LlmMetrics> {
        self.engines
            .iter()
            .map(|s| metrics::json(s.engine.name(), &s.metrics, &*s.engine))
            .collect()
    }

    /// The engine a request names, or the first one when it names none
    fn engine(&self, model: Option<&str>) -> Result<&Served, Box<Response>> {
        let Some(model) = model else {
            return Ok(&self.engines[0]);
        };
        if let Some(served) = self.engines.iter().find(|s| s.engine.name() == model) {
            return Ok(served);
        }
        let response = if self.other_models.iter().any(|m| m == model) {
            error_response(
                StatusCode::BAD_REQUEST,
                &format!(
                    "The model '{model}' is not a language model; send it tensors on /v2/models/{model}/infer"
                ),
                "invalid_request_error",
                Some("model"),
                Some("model_not_supported"),
            )
        } else {
            error_response(
                StatusCode::NOT_FOUND,
                &format!(
                    "The model '{model}' does not exist; this server serves {}",
                    self.names().join(", ")
                ),
                "invalid_request_error",
                Some("model"),
                Some("model_not_found"),
            )
        };
        Err(Box::new(response))
    }
}

/// The routes, for the server's router (any state: they carry their own)
pub(crate) fn routes<S: Clone + Send + Sync + 'static>(state: Arc<LlmState>) -> Router<S> {
    let [chat, text, models] = crate::api::LLM_PATHS;
    Router::new()
        .route(chat, post(chat_completions))
        .route(text, post(completions))
        .route(models, get(list_models))
        .with_state(state)
}

/// `stop`: one string or several
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum OneOrMany {
    One(String),
    Many(Vec<String>),
}

#[derive(Debug, Default, Deserialize)]
struct StreamOptions {
    #[serde(default)]
    include_usage: bool,
}

/// What chat and completion requests share. Fields OpenAI defines and furnace does not serve
/// are rejected; unknown fields are ignored, as other servers do, so SDKs that send extras work.
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
struct Options {
    max_tokens: Option<u64>,
    max_completion_tokens: Option<u64>,
    temperature: Option<f32>,
    top_p: Option<f32>,
    /// Not OpenAI, but common (vLLM, llama.cpp); 0 or -1 turn it off
    top_k: Option<i64>,
    seed: Option<i64>,
    presence_penalty: Option<f32>,
    frequency_penalty: Option<f32>,
    stop: Option<OneOrMany>,
    stream: Option<bool>,
    stream_options: Option<StreamOptions>,
    n: Option<u64>,
    /// `true` for chat, a number for completions
    logprobs: Option<serde_json::Value>,
    tools: Option<Vec<serde_json::Value>>,
    response_format: Option<serde_json::Value>,
    echo: Option<bool>,
    best_of: Option<u64>,
    suffix: Option<String>,
}

#[derive(Debug, Deserialize)]
struct ChatRequest {
    model: Option<String>,
    messages: Vec<ChatMessage>,
    #[serde(flatten)]
    options: Options,
}

#[derive(Debug, Deserialize)]
struct ChatMessage {
    role: String,
    /// `null` for an assistant message with tool calls
    #[serde(default)]
    content: Option<Content>,
}

/// A message's content: text, or parts of which only text is served
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum Content {
    Text(String),
    Parts(Vec<ContentPart>),
}

#[derive(Debug, Deserialize)]
struct ContentPart {
    #[serde(rename = "type")]
    kind: String,
    text: Option<String>,
}

#[derive(Debug, Deserialize)]
struct CompletionRequest {
    model: Option<String>,
    prompt: WirePrompt,
    #[serde(flatten)]
    options: Options,
}

/// `prompt` of `/v1/completions`: a string, or a list of one
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum WirePrompt {
    Text(String),
    Many(Vec<serde_json::Value>),
}

/// How the response goes out
struct Delivery {
    stream: bool,
    include_usage: bool,
}

fn invalid(message: &str, param: &str) -> Box<Response> {
    Box::new(error_response(
        StatusCode::BAD_REQUEST,
        message,
        "invalid_request_error",
        Some(param),
        None,
    ))
}

impl Options {
    /// The sampling parameters and stop strings, or a 400 for what furnace does not serve
    fn check(self) -> Result<(SamplingParams, Vec<String>, Delivery), Box<Response>> {
        if self.n.is_some_and(|n| n != 1) {
            return Err(invalid("Only n = 1 is supported", "n"));
        }
        if self.best_of.is_some_and(|n| n != 1) {
            return Err(invalid("Only best_of = 1 is supported", "best_of"));
        }
        let logprobs = match &self.logprobs {
            None | Some(serde_json::Value::Null) => false,
            Some(serde_json::Value::Bool(on)) => *on,
            Some(serde_json::Value::Number(n)) => n.as_u64() != Some(0),
            Some(_) => true,
        };
        if logprobs {
            return Err(invalid("logprobs are not supported", "logprobs"));
        }
        if self.tools.as_ref().is_some_and(|tools| !tools.is_empty()) {
            return Err(invalid("Tool calling is not supported", "tools"));
        }
        if let Some(format) = &self.response_format {
            let kind = format.get("type").and_then(|t| t.as_str());
            if kind.is_some_and(|k| k != "text") {
                return Err(invalid(
                    "Only the text response format is supported",
                    "response_format",
                ));
            }
        }
        if self.echo == Some(true) {
            return Err(invalid("echo is not supported", "echo"));
        }
        if self.suffix.as_ref().is_some_and(|s| !s.is_empty()) {
            return Err(invalid("suffix is not supported", "suffix"));
        }
        if let Some(t) = self.temperature
            && !(0.0..=2.0).contains(&t)
        {
            return Err(invalid(
                "temperature must be between 0 and 2",
                "temperature",
            ));
        }
        if let Some(p) = self.top_p
            && !(p > 0.0 && p <= 1.0)
        {
            return Err(invalid("top_p must be in (0, 1]", "top_p"));
        }
        for (penalty, name) in [
            (self.presence_penalty, "presence_penalty"),
            (self.frequency_penalty, "frequency_penalty"),
        ] {
            if penalty.is_some_and(|p| !(-2.0..=2.0).contains(&p)) {
                return Err(invalid(&format!("{name} must be between -2 and 2"), name));
            }
        }
        let max_tokens = self.max_completion_tokens.or(self.max_tokens);
        if max_tokens == Some(0) {
            return Err(invalid("max_tokens must be at least 1", "max_tokens"));
        }
        let stop = match self.stop {
            None => Vec::new(),
            Some(OneOrMany::One(stop)) => vec![stop],
            Some(OneOrMany::Many(stops)) => stops,
        };
        if stop.len() > MAX_STOP_STRINGS {
            return Err(invalid(
                &format!("At most {MAX_STOP_STRINGS} stop strings are supported"),
                "stop",
            ));
        }
        let params = SamplingParams {
            max_tokens: max_tokens.map(|m| usize::try_from(m).unwrap_or(usize::MAX)),
            temperature: self.temperature,
            top_p: self.top_p,
            top_k: self
                .top_k
                .filter(|&k| k > 0)
                .map(|k| usize::try_from(k).unwrap_or(usize::MAX)),
            seed: self.seed.map(|s| s as u64),
            presence_penalty: self.presence_penalty.unwrap_or(0.0),
            frequency_penalty: self.frequency_penalty.unwrap_or(0.0),
        };
        let stream = self.stream.unwrap_or(false);
        let delivery = Delivery {
            stream,
            include_usage: stream && self.stream_options.is_some_and(|o| o.include_usage),
        };
        Ok((params, stop, delivery))
    }
}

/// Parse a JSON body, with OpenAI's error for what doesn't parse
fn parse<T: serde::de::DeserializeOwned>(
    body: Result<Bytes, BytesRejection>,
) -> Result<T, Box<Response>> {
    let body = body.map_err(|rejection| {
        Box::new(error_response(
            rejection.status(),
            &rejection.body_text(),
            "invalid_request_error",
            None,
            None,
        ))
    })?;
    serde_json::from_slice(&body)
        .map_err(|e| invalid(&format!("Invalid request body: {e}"), "body"))
}

/// The two kinds of completion, which differ only in their JSON
#[derive(Clone, Copy, PartialEq)]
enum Kind {
    Chat,
    Text,
}

/// `POST /v1/chat/completions`
async fn chat_completions(
    State(state): State<Arc<LlmState>>,
    body: Result<Bytes, BytesRejection>,
) -> Response {
    let received = Instant::now();
    let request: ChatRequest = match parse(body) {
        Ok(request) => request,
        Err(response) => return *response,
    };
    let mut messages = Vec::with_capacity(request.messages.len());
    for message in request.messages {
        let content = match message.content {
            None => String::new(),
            Some(Content::Text(text)) => text,
            Some(Content::Parts(parts)) => {
                let mut text = String::new();
                for part in parts {
                    match (part.kind.as_str(), part.text) {
                        ("text", Some(part)) => text.push_str(&part),
                        (kind, _) => {
                            return *invalid(
                                &format!("Content of type '{kind}' is not supported, only text"),
                                "messages",
                            );
                        }
                    }
                }
                text
            }
        };
        messages.push(Message {
            role: message.role,
            content,
        });
    }
    if messages.is_empty() {
        return *invalid("messages must not be empty", "messages");
    }
    generate(
        &state,
        Kind::Chat,
        request.model.as_deref(),
        Prompt::Chat(messages),
        request.options,
        received,
    )
    .await
}

/// `POST /v1/completions`
async fn completions(
    State(state): State<Arc<LlmState>>,
    body: Result<Bytes, BytesRejection>,
) -> Response {
    let received = Instant::now();
    let request: CompletionRequest = match parse(body) {
        Ok(request) => request,
        Err(response) => return *response,
    };
    let prompt = match request.prompt {
        WirePrompt::Text(text) => text,
        WirePrompt::Many(mut prompts) => match (prompts.len(), prompts.pop()) {
            (1, Some(serde_json::Value::String(text))) => text,
            _ => {
                return *invalid(
                    "Only one prompt, as a string, is supported per request",
                    "prompt",
                );
            }
        },
    };
    generate(
        &state,
        Kind::Text,
        request.model.as_deref(),
        Prompt::Text(prompt),
        request.options,
        received,
    )
    .await
}

async fn generate(
    state: &LlmState,
    kind: Kind,
    model: Option<&str>,
    input: Prompt,
    options: Options,
    received: Instant,
) -> Response {
    let served = match state.engine(model) {
        Ok(served) => served,
        Err(response) => return *response,
    };
    let (params, stop, delivery) = match options.check() {
        Ok(checked) => checked,
        Err(response) => return *response,
    };
    // Backpressure, as for every other route: refuse at once when all slots are taken
    let Ok(permit) = state.slots.clone().try_acquire_owned() else {
        served.metrics.reject();
        return busy_response("Too many concurrent requests, retry later");
    };
    let request = GenerateRequest {
        input,
        params,
        stop,
    };
    let events = match state
        .serving
        .clone()
        .generate_off_runtime(served.engine.name().to_string(), request)
        .await
    {
        Ok(events) => events,
        Err(e) => {
            if matches!(e, EngineError::Busy(_)) {
                served.metrics.reject();
            }
            return engine_error_response(&e, kind);
        }
    };
    let mut tracked = Tracked::new(events, served.metrics.clone(), received);
    let reply = Reply {
        kind,
        id: completion_id(kind),
        created: unix_seconds(),
        model: served.engine.name().to_string(),
    };
    if delivery.stream {
        // The status goes out with the first chunk, so it waits for the engine's first event:
        // an engine that refuses only once it has looked at the request (a prompt too long for
        // the context, say) gets its 4xx, not an error payload after a 200
        let first = match state.timeout {
            Some(timeout) => match tokio::time::timeout(timeout, tracked.next()).await {
                Ok(first) => first,
                Err(_) => return timeout_response(timeout),
            },
            None => tracked.next().await,
        };
        let first = match first {
            Some(Event::Error(e)) => {
                if matches!(e, EngineError::Busy(_)) {
                    served.metrics.reject();
                }
                return engine_error_response(&e, kind);
            }
            None => return engine_error_response(&ended_early(), kind),
            Some(first) => first,
        };
        stream(
            reply,
            tracked,
            first,
            delivery.include_usage,
            state.timeout,
            permit,
        )
    } else {
        match state.timeout {
            // Dropping the events on timeout cancels the generation
            Some(timeout) => tokio::time::timeout(timeout, collect(reply, tracked, permit))
                .await
                .unwrap_or_else(|_| timeout_response(timeout)),
            None => collect(reply, tracked, permit).await,
        }
    }
}

/// What every chunk of a response repeats
struct Reply {
    kind: Kind,
    id: String,
    created: u64,
    model: String,
}

#[derive(Serialize)]
struct Usage {
    prompt_tokens: usize,
    completion_tokens: usize,
    total_tokens: usize,
}

impl Usage {
    fn new(prompt_tokens: usize, completion_tokens: usize) -> Self {
        Self {
            prompt_tokens,
            completion_tokens,
            total_tokens: prompt_tokens + completion_tokens,
        }
    }
}

#[derive(Serialize)]
struct Chunk<'a, C> {
    id: &'a str,
    object: &'static str,
    created: u64,
    model: &'a str,
    choices: &'a [C],
    /// Absent without `include_usage`; with it, `null` but in the last chunk
    #[serde(skip_serializing_if = "Option::is_none")]
    usage: Option<Option<Usage>>,
}

#[derive(Serialize)]
struct ChatChoice<'a> {
    index: u32,
    delta: Delta<'a>,
    logprobs: Option<()>,
    finish_reason: Option<&'static str>,
}

#[derive(Serialize)]
struct Delta<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    role: Option<&'static str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    content: Option<&'a str>,
}

#[derive(Serialize)]
struct TextChoice<'a> {
    index: u32,
    text: &'a str,
    logprobs: Option<()>,
    finish_reason: Option<&'static str>,
}

impl Reply {
    fn object(&self, stream: bool) -> &'static str {
        match (self.kind, stream) {
            (Kind::Chat, true) => "chat.completion.chunk",
            (Kind::Chat, false) => "chat.completion",
            (Kind::Text, _) => "text_completion",
        }
    }

    /// One `data:` payload: `role` and `content` for chat, `content` alone as text for
    /// completions; no choice at all with `usage`
    fn chunk(
        &self,
        role: Option<&'static str>,
        content: Option<&str>,
        finish_reason: Option<FinishReason>,
        usage: Option<Option<Usage>>,
        last: bool,
    ) -> String {
        let finish_reason = finish_reason.map(FinishReason::as_str);
        let serialized = match self.kind {
            Kind::Chat => {
                let choice = [ChatChoice {
                    index: 0,
                    delta: Delta { role, content },
                    logprobs: None,
                    finish_reason,
                }];
                serde_json::to_string(&Chunk {
                    id: &self.id,
                    object: self.object(true),
                    created: self.created,
                    model: &self.model,
                    choices: if last { &[] } else { &choice },
                    usage,
                })
            }
            Kind::Text => {
                let choice = [TextChoice {
                    index: 0,
                    text: content.unwrap_or(""),
                    logprobs: None,
                    finish_reason,
                }];
                serde_json::to_string(&Chunk {
                    id: &self.id,
                    object: self.object(true),
                    created: self.created,
                    model: &self.model,
                    choices: if last { &[] } else { &choice },
                    usage,
                })
            }
        };
        serialized.expect("chunks serialize")
    }
}

/// A streaming response in progress
struct Streamed {
    reply: Reply,
    tracked: Tracked,
    include_usage: bool,
    /// Payloads ready to go out
    pending: VecDeque<String>,
    done: bool,
    completion_tokens: usize,
    /// How long to wait for the engine's next event before giving up on the stream
    idle: Option<Duration>,
    _permit: OwnedSemaphorePermit,
}

impl Streamed {
    fn usage_field(&self) -> Option<Option<Usage>> {
        self.include_usage.then_some(None)
    }

    /// Turn an event into payloads
    fn on(&mut self, event: Option<Event>) {
        match event {
            Some(Event::Started { .. }) => {}
            Some(Event::Text(text)) => {
                let chunk = self
                    .reply
                    .chunk(None, Some(&text), None, self.usage_field(), false);
                self.pending.push_back(chunk);
            }
            Some(Event::Finished {
                reason,
                completion_tokens,
            }) => {
                self.completion_tokens = completion_tokens;
                let content = (self.reply.kind == Kind::Text).then_some("");
                let chunk =
                    self.reply
                        .chunk(None, content, Some(reason), self.usage_field(), false);
                self.pending.push_back(chunk);
                if self.include_usage {
                    let usage = Usage::new(self.tracked.prompt_tokens, completion_tokens);
                    let chunk = self.reply.chunk(None, None, None, Some(Some(usage)), true);
                    self.pending.push_back(chunk);
                }
                self.end();
            }
            Some(Event::Error(e)) => {
                self.pending.push_back(error_body(&e).to_string());
                self.end();
            }
            None => {
                self.pending
                    .push_back(error_body(&ended_early()).to_string());
                self.end();
            }
        }
    }

    /// The engine sent nothing for `idle`: an error payload, and the stream ends
    fn stalled(&mut self, idle: Duration) {
        let message = timeout_message(idle);
        let error = error_value(&message, "server_error", None, Some("timeout"));
        self.pending.push_back(error.to_string());
        self.end();
    }

    fn end(&mut self) {
        self.pending.push_back("[DONE]".to_string());
        self.done = true;
    }
}

/// Server-sent events: chunks as the engine produces them, from its `first` event on, then
/// `[DONE]`. Even an error after the stream started ends with `[DONE]`, after an error payload,
/// as vLLM does.
fn stream(
    reply: Reply,
    tracked: Tracked,
    first: Event,
    include_usage: bool,
    idle: Option<Duration>,
    permit: OwnedSemaphorePermit,
) -> Response {
    let mut streamed = Streamed {
        reply,
        tracked,
        include_usage,
        pending: VecDeque::new(),
        done: false,
        completion_tokens: 0,
        idle,
        _permit: permit,
    };
    if streamed.reply.kind == Kind::Chat {
        let chunk = streamed.reply.chunk(
            Some("assistant"),
            Some(""),
            None,
            streamed.usage_field(),
            false,
        );
        streamed.pending.push_back(chunk);
    }
    streamed.on(Some(first));
    let events = futures_util::stream::unfold(streamed, |mut streamed| async move {
        loop {
            if let Some(data) = streamed.pending.pop_front() {
                return Some((
                    Ok::<_, Infallible>(SseEvent::default().data(data)),
                    streamed,
                ));
            }
            if streamed.done {
                return None;
            }
            match streamed.idle {
                Some(idle) => match tokio::time::timeout(idle, streamed.tracked.next()).await {
                    Ok(event) => streamed.on(event),
                    Err(_) => streamed.stalled(idle),
                },
                None => {
                    let event = streamed.tracked.next().await;
                    streamed.on(event);
                }
            }
        }
    });
    Sse::new(events)
        .keep_alive(KeepAlive::default())
        .into_response()
}

/// A whole response at once
async fn collect(reply: Reply, mut tracked: Tracked, _permit: OwnedSemaphorePermit) -> Response {
    let mut text = String::new();
    let (reason, completion_tokens) = loop {
        match tracked.next().await {
            Some(Event::Started { .. }) => {}
            Some(Event::Text(piece)) => text.push_str(&piece),
            Some(Event::Finished {
                reason,
                completion_tokens,
            }) => break (reason, completion_tokens),
            Some(Event::Error(e)) => return engine_error_response(&e, reply.kind),
            None => return engine_error_response(&ended_early(), reply.kind),
        }
    };
    let choice = match reply.kind {
        Kind::Chat => serde_json::json!({
            "index": 0,
            "message": {"role": "assistant", "content": text},
            "logprobs": null,
            "finish_reason": reason.as_str(),
        }),
        Kind::Text => serde_json::json!({
            "index": 0,
            "text": text,
            "logprobs": null,
            "finish_reason": reason.as_str(),
        }),
    };
    Json(serde_json::json!({
        "id": reply.id,
        "object": reply.object(false),
        "created": reply.created,
        "model": reply.model,
        "choices": [choice],
        "usage": Usage::new(tracked.prompt_tokens, completion_tokens),
    }))
    .into_response()
}

/// `GET /v1/models`: the LLMs, then the server's other models
async fn list_models(State(state): State<Arc<LlmState>>) -> Json<serde_json::Value> {
    let llms = state.engines.iter().map(|s| {
        serde_json::json!({
            "id": s.engine.name(),
            "object": "model",
            "created": state.created,
            "owned_by": "furnace",
            "max_model_len": s.engine.max_context(),
        })
    });
    let others = state.other_models.iter().map(|name| {
        serde_json::json!({
            "id": name,
            "object": "model",
            "created": state.created,
            "owned_by": "furnace",
        })
    });
    Json(serde_json::json!({
        "object": "list",
        "data": llms.chain(others).collect::<Vec<_>>(),
    }))
}

/// OpenAI's error body: `{"error": {"message", "type", "param", "code"}}`
fn error_value(
    message: &str,
    kind: &str,
    param: Option<&str>,
    code: Option<&str>,
) -> serde_json::Value {
    serde_json::json!({
        "error": {"message": message, "type": kind, "param": param, "code": code}
    })
}

fn error_response(
    status: StatusCode,
    message: &str,
    kind: &str,
    param: Option<&str>,
    code: Option<&str>,
) -> Response {
    (status, Json(error_value(message, kind, param, code))).into_response()
}

fn busy_response(message: &str) -> Response {
    error_response(
        StatusCode::SERVICE_UNAVAILABLE,
        message,
        "server_error",
        None,
        Some("server_busy"),
    )
}

/// The status, type and code of an engine's error
fn classify(e: &EngineError) -> (StatusCode, &'static str, Option<&'static str>) {
    match e {
        EngineError::TooLong { .. } => (
            StatusCode::BAD_REQUEST,
            "invalid_request_error",
            Some("context_length_exceeded"),
        ),
        EngineError::Busy(_) => (
            StatusCode::SERVICE_UNAVAILABLE,
            "server_error",
            Some("server_busy"),
        ),
        EngineError::Invalid(_) => (StatusCode::BAD_REQUEST, "invalid_request_error", None),
        EngineError::Failed(_) => (StatusCode::INTERNAL_SERVER_ERROR, "server_error", None),
    }
}

/// An engine that dropped a request without `Finished` or `Error`
fn ended_early() -> EngineError {
    EngineError::failed("the engine ended the request early")
}

fn error_body(e: &EngineError) -> serde_json::Value {
    let (_, kind, code) = classify(e);
    error_value(&e.to_string(), kind, None, code)
}

fn engine_error_response(e: &EngineError, kind: Kind) -> Response {
    let (status, error_kind, code) = classify(e);
    let param = matches!(e, EngineError::TooLong { .. }).then_some(match kind {
        Kind::Chat => "messages",
        Kind::Text => "prompt",
    });
    error_response(status, &e.to_string(), error_kind, param, code)
}

fn timeout_message(timeout: Duration) -> String {
    format!(
        "The generation exceeded the timeout of {}ms",
        timeout.as_millis()
    )
}

/// A whole answer that took longer than the server's `request_timeout`
fn timeout_response(timeout: Duration) -> Response {
    error_response(
        StatusCode::GATEWAY_TIMEOUT,
        &timeout_message(timeout),
        "server_error",
        None,
        Some("timeout"),
    )
}

fn unix_seconds() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0, |d| d.as_secs())
}

/// `chatcmpl-…` or `cmpl-…`: a random per-process value plus a counter, unique without asking
/// the OS for randomness per request
fn completion_id(kind: Kind) -> String {
    static SEED: std::sync::LazyLock<u64> =
        std::sync::LazyLock::new(|| crate::api::process_random(2));
    static NEXT: AtomicU64 = AtomicU64::new(0);
    let prefix = match kind {
        Kind::Chat => "chatcmpl",
        Kind::Text => "cmpl",
    };
    format!(
        "{prefix}-{:016x}{:08x}",
        *SEED,
        NEXT.fetch_add(1, Ordering::Relaxed)
    )
}