met 1.0.0-beta-preview

Metrale Engine: pure Rust LLM inference server (HTTP API, scheduler, TUI, CLI).
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
// SPDX-License-Identifier: MIT OR Apache-2.0

//! 2026-09-26: Encoder from `ir::StreamDelta`s to OpenAI
//! `chat.completion.chunk` SSE events.
//!
//! Owner: server (OpenAI adapter).
//! Invariants:
//! - Every `chat.completion.chunk` of one response carries the same id.
//! - The role chunk comes first and `[DONE]` last.

use axum::response::sse::Event;

use crate::ir::StreamDelta;
use crate::reasoning_parser::ReasoningWire;

use super::{ChatCompletionChunk, Usage};

/// 2026-09-26: Encode one delta into its OpenAI SSE event(s).
/// `include_usage` is the request's `stream_options.include_usage`: it
/// chooses between a usage-only chunk followed by a finish chunk without
/// usage, and one finish chunk carrying usage.
pub(crate) fn delta_to_chunk_events(
    d: &StreamDelta,
    model: &str,
    id: &str,
    include_usage: bool,
    reasoning_wire: ReasoningWire,
) -> Vec<Event> {
    delta_to_payloads(d, model, id, include_usage, reasoning_wire)
        .into_iter()
        .map(|payload| Event::default().data(payload))
        .collect()
}

/// 2026-09-26: The `data:` payloads for one delta, in emission order. A
/// separate function so tests can read the chunk JSON; axum's `Event`
/// cannot be read back.
fn delta_to_payloads(
    d: &StreamDelta,
    model: &str,
    id: &str,
    include_usage: bool,
    reasoning_wire: ReasoningWire,
) -> Vec<String> {
    match d {
        StreamDelta::Content { text, token_ids } => vec![chunk_json(
            ChatCompletionChunk::content_chunk(model, id, text.clone())
                .with_token_ids(token_ids.clone()),
        )],
        StreamDelta::Reasoning { text, token_ids } => vec![chunk_json(
            ChatCompletionChunk::reasoning_chunk(model, id, reasoning_wire, text.clone())
                .with_token_ids(token_ids.clone()),
        )],
        StreamDelta::ToolCallStart {
            index,
            id: call_id,
            name,
        } => {
            // 2026-09-26: `tool_call_start_chunk` takes a whole `ToolCall`;
            // the delta carries only id and name, and the start chunk's
            // arguments are empty.
            let tc = crate::tool_parser::ToolCall {
                id: call_id.clone(),
                call_type: "function".to_string(),
                function: crate::tool_parser::FunctionCall {
                    name: name.clone(),
                    arguments: String::new(),
                },
            };
            vec![chunk_json(ChatCompletionChunk::tool_call_start_chunk(
                model, id, &tc, *index,
            ))]
        }
        StreamDelta::ToolCallArgs {
            index,
            fragment,
            token_ids,
        } => vec![chunk_json(
            ChatCompletionChunk::tool_call_args_fragment(model, id, *index, fragment)
                .with_token_ids(token_ids.clone()),
        )],
        StreamDelta::Refusal { text } => vec![chunk_json(ChatCompletionChunk::refusal_chunk(
            model,
            id,
            text.clone(),
        ))],
        StreamDelta::Finish {
            reason,
            usage,
            token_ids,
        } => {
            let wire_usage = wire_usage(usage);
            if include_usage {
                // 2026-09-26: The usage-only chunk (`choices: []`) comes
                // first, then the finish chunk without usage. The residual
                // token ids go on the finish chunk: the usage-only chunk
                // has no choice to carry them.
                vec![
                    chunk_json(ChatCompletionChunk::usage_only_chunk(model, id, wire_usage)),
                    chunk_json(
                        ChatCompletionChunk::final_chunk_no_usage(model, id, reason.as_wire())
                            .with_token_ids(token_ids.clone()),
                    ),
                ]
            } else {
                vec![chunk_json(
                    ChatCompletionChunk::done_chunk(model, id, reason.as_wire(), wire_usage)
                        .with_token_ids(token_ids.clone()),
                )]
            }
        }
        // 2026-09-26: The error payload is already wire JSON; it is sent
        // unchanged.
        StreamDelta::Error { message } => vec![message.clone()],
    }
}

fn chunk_json(chunk: ChatCompletionChunk) -> String {
    serde_json::to_string(&chunk).unwrap_or_default()
}

/// 2026-09-26: The same `From<&ir::Usage>` mapping the blocking encoder
/// uses.
fn wire_usage(u: &crate::ir::Usage) -> Usage {
    Usage::from(u)
}

/// 2026-09-26: The streamed `/v1/chat/completions` response: the role
/// chunk, the encoded deltas, then `[DONE]`, with SSE keep-alive. It mints
/// the chunk id that every chunk of the response shares.
pub(crate) fn encode_sse_response(
    deltas: crate::ir::DeltaStream,
    model: String,
    include_usage: bool,
    reasoning_wire: ReasoningWire,
) -> axum::response::Response {
    use axum::response::IntoResponse;
    use axum::response::sse::{Event, KeepAlive, Sse};
    use futures::StreamExt;

    let chunk_id = super::new_chunk_id();
    let role_chunk = super::ChatCompletionChunk::role_chunk(&model, &chunk_id);
    let role_json = serde_json::to_string(&role_chunk).unwrap_or_default();
    let role_event = futures::stream::once(async move {
        Ok::<_, std::convert::Infallible>(Event::default().data(role_json))
    });
    let done_event = futures::stream::once(async {
        Ok::<_, std::convert::Infallible>(Event::default().data("[DONE]"))
    });
    // 2026-09-26: Logged once, when the first delta reaches this encoder;
    // compare with `handle_token`'s first-delta log line.
    let first_event_logged = std::cell::Cell::new(false);
    let token_stream = deltas.flat_map(move |d| {
        if !first_event_logged.replace(true) {
            tracing::debug!("stream: first delta reaches the SSE encoder");
        }
        let events: Vec<Result<Event, std::convert::Infallible>> =
            delta_to_chunk_events(&d, &model, &chunk_id, include_usage, reasoning_wire)
                .into_iter()
                .map(Ok)
                .collect();
        futures::stream::iter(events)
    });
    let full_stream = role_event.chain(token_stream).chain(done_event);
    Sse::new(full_stream)
        .keep_alive(KeepAlive::default())
        .into_response()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ir::{FinishReason, StreamDelta};

    /// 2026-09-26: Parse a chunk payload and zero `created`, so two chunks
    /// built across a second boundary still compare equal.
    fn norm(payload: &str) -> serde_json::Value {
        let mut v: serde_json::Value = serde_json::from_str(payload).expect("valid chunk JSON");
        v["created"] = serde_json::json!(0);
        v
    }

    #[test]
    fn role_chunk_carries_an_empty_content_string_as_vllm_does() {
        let v = norm(
            &serde_json::to_string(&ChatCompletionChunk::role_chunk("m", "chatcmpl-x")).unwrap(),
        );
        assert_eq!(
            v["choices"][0]["delta"],
            serde_json::json!({"role": "assistant", "content": ""})
        );
    }

    #[test]
    fn content_delta_encodes_via_content_chunk() {
        let d = StreamDelta::Content {
            text: "hi".into(),
            token_ids: Vec::new(),
        };
        let p = delta_to_payloads(
            &d,
            "m",
            "chatcmpl-x",
            false,
            ReasoningWire::ReasoningContent,
        );
        assert_eq!(p.len(), 1);
        assert!(p[0].contains("\"content\":\"hi\""), "payload: {}", p[0]);
        assert!(
            !p[0].contains("token_ids"),
            "opt-out payload must omit token_ids: {}",
            p[0]
        );
        let expected = serde_json::to_string(&ChatCompletionChunk::content_chunk(
            "m",
            "chatcmpl-x",
            "hi".to_string(),
        ))
        .unwrap();
        assert_eq!(norm(&p[0]), norm(&expected));

        let d = StreamDelta::Content {
            text: "hi".into(),
            token_ids: vec![1, 2],
        };
        let p = delta_to_payloads(
            &d,
            "m",
            "chatcmpl-x",
            false,
            ReasoningWire::ReasoningContent,
        );
        assert!(p[0].contains("\"token_ids\":[1,2]"), "payload: {}", p[0]);
    }

    #[test]
    fn reasoning_delta_takes_the_wire_it_is_given() {
        // 2026-10-14: The SSE encoder writes the key of the wire `encode_sse_response`
        // passes (`AppState::reasoning_wire`); for GLM that is vLLM's `reasoning`.
        let d = StreamDelta::Reasoning {
            text: "mull".into(),
            token_ids: vec![9],
        };
        let p = delta_to_payloads(&d, "m", "id-1", false, ReasoningWire::Vllm);
        assert!(p[0].contains("\"reasoning\":\"mull\""), "payload: {}", p[0]);
        assert!(!p[0].contains("reasoning_content"), "payload: {}", p[0]);
    }

    #[test]
    fn reasoning_delta_encodes_via_reasoning_chunk() {
        let d = StreamDelta::Reasoning {
            text: "mull".into(),
            token_ids: vec![9],
        };
        let p = delta_to_payloads(&d, "m", "id-1", false, ReasoningWire::ReasoningContent);
        assert_eq!(p.len(), 1);
        assert!(
            p[0].contains("\"reasoning_content\":\"mull\""),
            "payload: {}",
            p[0]
        );
        let expected = serde_json::to_string(
            &ChatCompletionChunk::reasoning_chunk(
                "m",
                "id-1",
                ReasoningWire::ReasoningContent,
                "mull".to_string(),
            )
            .with_token_ids(vec![9]),
        )
        .unwrap();
        assert_eq!(norm(&p[0]), norm(&expected));
    }

    #[test]
    fn tool_call_start_and_args_deltas_encode_openai_tool_chunks() {
        let start = StreamDelta::ToolCallStart {
            index: 3,
            id: "call_abc".into(),
            name: "get_weather".into(),
        };
        let p = delta_to_payloads(&start, "m", "id-1", false, ReasoningWire::ReasoningContent);
        assert_eq!(p.len(), 1);
        assert!(p[0].contains("\"role\":\"assistant\""), "payload: {}", p[0]);
        assert!(
            p[0].contains(
                "\"tool_calls\":[{\"index\":3,\"id\":\"call_abc\",\"type\":\"function\",\"function\":{\"name\":\"get_weather\",\"arguments\":\"\"}}]"
            ),
            "payload: {}",
            p[0]
        );

        let args = StreamDelta::ToolCallArgs {
            index: 3,
            fragment: "{\"city\":".into(),
            token_ids: Vec::new(),
        };
        let p = delta_to_payloads(&args, "m", "id-1", false, ReasoningWire::ReasoningContent);
        assert_eq!(p.len(), 1);
        let expected = serde_json::to_string(&ChatCompletionChunk::tool_call_args_fragment(
            "m",
            "id-1",
            3,
            "{\"city\":",
        ))
        .unwrap();
        assert_eq!(norm(&p[0]), norm(&expected));
    }

    /// 2026-09-26: The usage-only chunk carries the timing fields that
    /// `Usage::from(&ir::Usage)` produces.
    #[test]
    fn finish_usage_chunk_carries_the_raw_timing_components() {
        let usage = crate::ir::Usage {
            prompt_tokens: 10,
            completion_tokens: 5,
            cached_prompt_tokens: 0,
            reasoning_tokens: 0,
            accepted_prediction_tokens: 0,
            time_to_first_token_ms: 12.5,
            decode_time_ms: 100.0,
            response_tokens_per_second: 40.0,
        };
        let d = StreamDelta::Finish {
            reason: FinishReason::Stop,
            usage,
            token_ids: Vec::new(),
        };
        let p = delta_to_payloads(&d, "m", "id-1", true, ReasoningWire::ReasoningContent);
        let v: serde_json::Value = serde_json::from_str(&p[0]).unwrap();
        assert_eq!(v["usage"]["decode_time_ms"], 100.0, "{}", p[0]);
        assert_eq!(v["usage"]["total_time_ms"], 112.5, "{}", p[0]);
        assert_eq!(v["usage"]["time_to_first_token_ms"], 12.5, "{}", p[0]);
        assert_eq!(v["usage"]["response_token/s"], 40.0, "{}", p[0]);
    }

    #[test]
    fn finish_framing_matches_include_usage_modes() {
        let usage = crate::ir::Usage {
            prompt_tokens: 10,
            completion_tokens: 5,
            cached_prompt_tokens: 2,
            reasoning_tokens: 3,
            accepted_prediction_tokens: 4,
            time_to_first_token_ms: 12.5,
            decode_time_ms: 100.0,
            response_tokens_per_second: 40.0,
        };
        let d = StreamDelta::Finish {
            reason: FinishReason::Stop,
            usage,
            token_ids: vec![7],
        };

        let p = delta_to_payloads(&d, "m", "id-1", true, ReasoningWire::ReasoningContent);
        assert_eq!(p.len(), 2);
        assert!(p[0].contains("\"choices\":[]"), "payload: {}", p[0]);
        assert!(p[0].contains("\"prompt_tokens\":10"), "payload: {}", p[0]);
        assert!(
            p[0].contains("\"completion_tokens\":5"),
            "payload: {}",
            p[0]
        );
        assert!(p[0].contains("\"total_tokens\":15"), "payload: {}", p[0]);
        assert!(p[0].contains("\"cached_tokens\":2"), "payload: {}", p[0]);
        assert!(p[0].contains("\"reasoning_tokens\":3"), "payload: {}", p[0]);
        assert!(
            p[0].contains("\"accepted_prediction_tokens\":4"),
            "payload: {}",
            p[0]
        );
        assert!(
            p[0].contains("\"rejected_prediction_tokens\":0"),
            "payload: {}",
            p[0]
        );
        assert!(
            p[0].contains("\"time_to_first_token_ms\":12.5"),
            "payload: {}",
            p[0]
        );
        assert!(
            p[0].contains("\"response_token/s\":40.0"),
            "payload: {}",
            p[0]
        );
        assert!(
            p[1].contains("\"finish_reason\":\"stop\""),
            "payload: {}",
            p[1]
        );
        assert!(
            !p[1].contains("\"usage\""),
            "final chunk must omit usage when include_usage=true: {}",
            p[1]
        );
        assert!(p[1].contains("\"token_ids\":[7]"), "payload: {}", p[1]);

        let p = delta_to_payloads(&d, "m", "id-1", false, ReasoningWire::ReasoningContent);
        assert_eq!(p.len(), 1);
        assert!(
            p[0].contains("\"finish_reason\":\"stop\""),
            "payload: {}",
            p[0]
        );
        assert!(p[0].contains("\"usage\":{"), "payload: {}", p[0]);
        assert!(p[0].contains("\"total_tokens\":15"), "payload: {}", p[0]);
        assert!(p[0].contains("\"token_ids\":[7]"), "payload: {}", p[0]);
    }

    #[test]
    fn error_delta_payload_is_verbatim() {
        let msg = r#"{"error":{"message":"boom","type":"server_error","code":500}}"#;
        let d = StreamDelta::Error {
            message: msg.to_string(),
        };
        let p = delta_to_payloads(&d, "m", "id-1", false, ReasoningWire::ReasoningContent);
        assert_eq!(p, vec![msg.to_string()]);
    }
}