cortiq-server 0.7.0

OpenAI-compatible HTTP serving for CMF models, built on the cortiq inference engine.
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
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
//! OpenAI-compatible API endpoints, backed by the real inference
//! pipeline. Generation runs in `spawn_blocking` behind a Mutex — a
//! panic inside the pipeline becomes a 500, never a dead process.

use crate::AppState;
use crate::streaming::{self, ChatStream};
use axum::{
    Router,
    extract::State,
    http::StatusCode,
    response::{IntoResponse, Json, Response},
    routing::{get, post},
};
use cortiq_core::TaskMask;
use cortiq_engine::SamplerConfig;
use cortiq_engine::dsv41_encoding::{self, EncodeOptions, ReasoningEffort, ThinkingMode};
use cortiq_engine::dsv41_vision::{self, VisionConfig};
use cortiq_engine::pipeline::GenerateResult;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

/// Register OpenAI-compatible routes.
pub fn routes() -> Router<Arc<AppState>> {
    Router::new()
        .route("/v1/models", get(list_models))
        .route("/v1/chat/completions", post(chat_completions))
        .route("/v1/completions", post(completions))
}

// ─── Models ──────────────────────────────────────────────

#[derive(Serialize)]
struct ModelsResponse {
    object: String,
    data: Vec<ModelEntry>,
}

#[derive(Serialize)]
struct ModelEntry {
    id: String,
    object: String,
    created: u64,
    owned_by: String,
}

async fn list_models(State(state): State<Arc<AppState>>) -> Json<ModelsResponse> {
    let arch = state.runtime.model().arch();
    Json(ModelsResponse {
        object: "list".to_string(),
        data: vec![ModelEntry {
            id: format!("{}-cortiq", arch.arch_name),
            object: "model".to_string(),
            created: chrono::Utc::now().timestamp() as u64,
            owned_by: "cortiq".to_string(),
        }],
    })
}

// ─── Shared types ────────────────────────────────────────

#[derive(Deserialize, Serialize, Clone)]
struct ChatMessage {
    role: String,
    /// Nullable: an assistant turn that made tool calls has
    /// `content: null` in the OpenAI shape, and a required field here
    /// 422'd the whole conversation on the SECOND request of every
    /// agent loop — the one that carries the history.
    #[serde(default)]
    content: Option<MessageContent>,
    /// Assistant history: the calls it made, echoed back by the client.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    tool_calls: Option<serde_json::Value>,
    /// `role: "tool"` results reference the call they answer.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    tool_call_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    name: Option<String>,
    /// Harmony/V4.1 assistant history carries reasoning separately from its
    /// user-visible summary. Preserve it across the next request.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    reasoning_content: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    response_format: Option<serde_json::Value>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    task: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    wo_eos: Option<bool>,
}

/// `content` in the shape clients actually send it.
///
/// The OpenAI schema allows a bare string OR an array of typed blocks, and
/// coding assistants (Cline, Roo/Zoo Code) switch to the array form as soon
/// as they attach file context or a system preamble. A `String`-only field
/// rejected those with `422 Failed to deserialize the JSON body ... expected
/// a string` before the model was ever reached — which is why the admin
/// playground worked (flat prompt) and the IDE did not.
///
/// Untagged: serde tries the string first, then the block list.
#[derive(Deserialize, Serialize, Clone)]
#[serde(untagged)]
enum MessageContent {
    Text(String),
    Blocks(Vec<ContentBlock>),
}

/// One block of a structured `content` array. Deliberately permissive —
/// every field optional, unknown fields ignored — so a new block type from
/// some future client degrades to "no text" instead of a 422.
#[derive(Deserialize, Serialize, Clone)]
struct ContentBlock {
    #[serde(default, rename = "type", skip_serializing_if = "Option::is_none")]
    kind: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    text: Option<String>,
    /// Image URL/source and future content block fields survive the initial
    /// deserialization; the V4.1 encoder consumes these records verbatim.
    #[serde(flatten)]
    extra: serde_json::Map<String, serde_json::Value>,
}

impl MessageContent {
    /// Flatten to the plain prompt text the pipeline consumes. Text blocks
    /// join with newlines, in order; non-text blocks (images and friends)
    /// contribute nothing rather than failing the turn.
    fn text(&self) -> String {
        match self {
            Self::Text(s) => s.clone(),
            Self::Blocks(bs) => bs
                .iter()
                .filter_map(|b| b.text.as_deref())
                .collect::<Vec<_>>()
                .join("\n"),
        }
    }

    fn to_value(&self) -> serde_json::Value {
        serde_json::to_value(self).unwrap_or_else(|_| serde_json::Value::String(self.text()))
    }
}

impl From<String> for MessageContent {
    fn from(s: String) -> Self {
        Self::Text(s)
    }
}

#[derive(Deserialize)]
struct CortiqExtension {
    task: Option<String>,
    /// Cortiq classifier mode: score these single-token labels at the next
    /// position and normalize only across them. This is the exact confidence
    /// used by binary/multiclass skills; no sampled generation is involved.
    #[serde(default)]
    class_tokens: Option<Vec<String>>,
}

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

#[derive(Serialize)]
struct CortiqResponseMeta {
    task_used: String,
    sparsity: f32,
    active_layers: usize,
    execution_mode: String,
    tokens_per_second: f64,
    #[serde(skip_serializing_if = "Option::is_none")]
    classification: Option<ClassTokenClassification>,
}

#[derive(Serialize)]
struct ClassTokenScore {
    token: String,
    token_id: u32,
    logit: f32,
    probability: f32,
}

#[derive(Serialize)]
struct ClassTokenClassification {
    label: String,
    confidence: f32,
    scores: Vec<ClassTokenScore>,
}

#[derive(Serialize)]
struct ApiError {
    error: ApiErrorBody,
}

#[derive(Serialize)]
struct ApiErrorBody {
    message: String,
    r#type: String,
}

fn error_response(status: StatusCode, message: impl Into<String>) -> Response {
    (
        status,
        Json(ApiError {
            error: ApiErrorBody {
                message: message.into(),
                r#type: "invalid_request_error".to_string(),
            },
        }),
    )
        .into_response()
}

/// Run one generation on the shared pipeline (blocking thread).
/// Returns the result plus wall-clock milliseconds.
async fn run_generation(
    state: Arc<AppState>,
    prompt_ids: Vec<u32>,
    vl_inputs: Option<dsv41_vision::PreparedVlInputs>,
    max_tokens: usize,
    mask: Option<TaskMask>,
    sampler_config: SamplerConfig,
    on_token: Option<cortiq_engine::TokenCallback>,
) -> Result<(GenerateResult, f64), Response> {
    let started = std::time::Instant::now();
    // The organism's day side: idle marker + OOD buffer (CMF_OOD_DIR).
    crate::ood::touch_last_request();
    let ood_on = crate::ood::ood_dir().is_some();
    let ood_state = state.clone();

    // Check a pipeline slot out for this generation: up to
    // `slots` requests decode concurrently, the rest queue here.
    let mut slot = state.slots.acquire().await;
    let remote = state.remote.clone();
    let outcome = tokio::task::spawn_blocking(move || {
        if ood_on {
            let text = ood_state.tokenizer.decode(&prompt_ids);
            let p = &mut *slot.pipe;
            crate::ood::record_if_ood(ood_state.runtime.model(), p, &prompt_ids, &text);
        }
        // Replica mode: the compute happens HERE, on a blocking-pool
        // thread that never saw the pin `acquire` set on the async
        // thread. Pin it again or every replica quietly shares card 0
        // (measured: 400 tokens across two "replicas" ran 103 tok/s,
        // exactly one card's worth, with the second card at 17 MB).
        cortiq_engine::gpu::set_current_device(slot.device);
        let p = &mut *slot.pipe;
        // A pooled pipeline must not inherit sampling state or RNG position
        // from the request that previously occupied this slot.
        p.set_sampler_config(sampler_config);
        match remote {
            Some(rm) => {
                if vl_inputs.is_some() {
                    return Err(
                        "V4.1 multimodal generation is not supported with a network-split pipeline"
                            .to_string(),
                    );
                }
                // Task masks would apply to this side's layers only —
                // refuse rather than run half a mask (same rule as
                // `run --peer`).
                if mask.is_some() {
                    return Err(
                        "this server runs a network split (--peer): task masks are not \
                         supported yet — use task 'general'"
                            .to_string(),
                    );
                }
                let mut rm = rm.lock().expect("remote segment mutex");
                cortiq_net::generate_split(p, &mut rm, &prompt_ids, max_tokens, None, on_token)
                    .map(
                    |(r, st)| {
                        if st.remote_steps > 0 {
                            tracing::info!(
                                "net: prefill {:.0} ms ({} of {} pos) · {} trips · {:.2} ms avg · {:.0}% of decode",
                                st.prefill_s * 1e3,
                                st.prefilled,
                                r.prompt_tokens,
                                st.remote_steps,
                                st.net_s * 1e3 / st.remote_steps as f64,
                                100.0 * st.net_s / st.decode_s.max(1e-9),
                            );
                        }
                        r
                    },
                )
            }
            None => match vl_inputs.as_ref() {
                Some(inputs) => p.generate_from_vl(inputs, max_tokens, mask.as_ref(), on_token),
                None => p.generate_from_ids(&prompt_ids, max_tokens, mask.as_ref(), on_token),
            },
        }
    })
    .await;

    let elapsed_ms = started.elapsed().as_secs_f64() * 1000.0;
    match outcome {
        Ok(Ok(result)) => Ok((result, elapsed_ms)),
        Ok(Err(e)) => Err(error_response(StatusCode::BAD_REQUEST, e)),
        Err(join_err) => {
            tracing::error!("generation task panicked: {join_err}");
            Err(error_response(
                StatusCode::INTERNAL_SERVER_ERROR,
                "generation failed",
            ))
        }
    }
}

/// One prefill, then an exact softmax restricted to caller-declared labels.
/// A market skill can therefore return both UP/DOWN and a reproducible
/// confidence coefficient without sampling or parsing free-form text.
async fn run_classification(
    state: Arc<AppState>,
    prompt_ids: Vec<u32>,
    mask: Option<TaskMask>,
    labels: Vec<(String, u32)>,
) -> Result<(ClassTokenClassification, f64), Response> {
    if state.remote.is_some() {
        return Err(error_response(
            StatusCode::BAD_REQUEST,
            "class-token scoring is not available with --peer",
        ));
    }
    let started = std::time::Instant::now();
    let mut slot = state.slots.acquire().await;
    let outcome = tokio::task::spawn_blocking(move || {
        cortiq_engine::gpu::set_current_device(slot.device);
        let logits = slot.pipe.prefill_next_logits(&prompt_ids, mask.as_ref());
        let selected: Vec<f32> = labels.iter().map(|(_, id)| logits[*id as usize]).collect();
        let max = selected.iter().copied().fold(f32::NEG_INFINITY, f32::max);
        let denom: f32 = selected.iter().map(|value| (*value - max).exp()).sum();
        let mut scores: Vec<ClassTokenScore> = labels
            .into_iter()
            .zip(selected)
            .map(|((token, token_id), logit)| ClassTokenScore {
                token,
                token_id,
                logit,
                probability: (logit - max).exp() / denom,
            })
            .collect();
        scores.sort_by(|left, right| right.probability.total_cmp(&left.probability));
        ClassTokenClassification {
            label: scores[0].token.clone(),
            confidence: scores[0].probability,
            scores,
        }
    })
    .await;
    let elapsed_ms = started.elapsed().as_secs_f64() * 1000.0;
    match outcome {
        Ok(classification) => Ok((classification, elapsed_ms)),
        Err(join_err) => {
            tracing::error!("classification task panicked: {join_err}");
            Err(error_response(
                StatusCode::INTERNAL_SERVER_ERROR,
                "classification failed",
            ))
        }
    }
}

fn request_sampler(
    temperature: Option<f32>,
    top_p: Option<f32>,
    seed: Option<u64>,
) -> Result<SamplerConfig, Response> {
    if temperature.is_some_and(|v| !v.is_finite() || v < 0.0) {
        return Err(error_response(
            StatusCode::BAD_REQUEST,
            "temperature must be finite and >= 0",
        ));
    }
    if top_p.is_some_and(|v| !v.is_finite() || !(0.0..=1.0).contains(&v)) {
        return Err(error_response(
            StatusCode::BAD_REQUEST,
            "top_p must be finite and between 0 and 1",
        ));
    }
    let mut config = SamplerConfig::default();
    if let Some(v) = temperature {
        config.temperature = v;
    }
    if let Some(v) = top_p {
        config.top_p = v;
    }
    config.seed = seed;
    Ok(config)
}

// ─── Chat Completions ────────────────────────────────────

#[derive(Deserialize)]
struct ChatCompletionsRequest {
    model: String,
    messages: Vec<ChatMessage>,
    temperature: Option<f32>,
    top_p: Option<f32>,
    seed: Option<u64>,
    #[serde(default = "default_max_tokens")]
    max_tokens: u32,
    #[serde(default)]
    stream: bool,
    /// Reasoning-model switch: `false` renders the chat template with
    /// `enable_thinking=false` (e.g. Qwen3/3.5 prefill an empty <think> block,
    /// so the model answers directly). Absent = the template's default.
    #[serde(default)]
    enable_thinking: Option<bool>,
    /// DeepSeek-V4.1 accepts an integer budget 1..=100 or low/high/max.
    /// Keep JSON at the protocol edge so numeric values remain integers.
    #[serde(default)]
    reasoning_effort: Option<serde_json::Value>,
    /// vLLM-style alternative: {"enable_thinking": false} — the explicit
    /// top-level field above wins when both are present.
    #[serde(default)]
    chat_template_kwargs: Option<serde_json::Value>,
    /// Cortiq extension: task routing
    #[serde(default)]
    cortiq: Option<CortiqExtension>,
    /// OpenAI function calling. Passed through to the FILE's chat
    /// template, whose `{%- if tools %}` branch has been waiting for
    /// them since the first Qwen-family convert.
    #[serde(default)]
    tools: Option<Vec<serde_json::Value>>,
    /// "none" suppresses the tool prompt; "auto"/absent lets the model
    /// decide. A forced {"function": {...}} is honoured as "auto" —
    /// grammar-constrained forcing is not implemented, and pretending
    /// otherwise would be worse than saying so.
    #[serde(default)]
    tool_choice: Option<serde_json::Value>,
}

impl ChatCompletionsRequest {
    /// Tools the template should see: none when absent, empty, or
    /// explicitly refused via tool_choice: "none".
    fn effective_tools(&self) -> Option<&[serde_json::Value]> {
        if matches!(
            self.tool_choice.as_ref().and_then(|v| v.as_str()),
            Some("none")
        ) {
            return None;
        }
        match self.tools.as_deref() {
            Some([]) | None => None,
            Some(ts) => Some(ts),
        }
    }

    /// Effective enable_thinking: top-level field, else chat_template_kwargs.
    fn thinking(&self) -> Option<bool> {
        self.enable_thinking.or_else(|| {
            self.chat_template_kwargs
                .as_ref()
                .and_then(|k| k.get("enable_thinking"))
                .and_then(|v| v.as_bool())
        })
    }
}

/// Prompt payload produced by the protocol layer. Text-only models use just
/// `token_ids`; V4.1 image requests additionally carry per-position types and
/// decoded image patches for the engine's vision-aware prefill.
#[derive(Debug)]
pub struct PromptIngress {
    pub token_ids: Vec<u32>,
    pub token_types: Vec<i8>,
    pub images: Vec<dsv41_vision::ImageInput>,
    pub dsv41: bool,
}

fn message_to_json(message: &ChatMessage) -> serde_json::Value {
    let content = message
        .content
        .as_ref()
        .map(MessageContent::to_value)
        .unwrap_or_else(|| serde_json::Value::String(String::new()));
    let mut object = serde_json::json!({
        "role": message.role,
        "content": content,
    });
    if let Some(tool_calls) = &message.tool_calls {
        let mut tool_calls = tool_calls.clone();
        // Some chat templates iterate arguments as an object while the
        // OpenAI wire format sends them as a JSON string. Preserve the
        // historical normalization for those templates; V4.1 accepts both
        // forms when rendering DSML.
        if let Some(calls) = tool_calls.as_array_mut() {
            for call in calls {
                if let Some(arguments) = call
                    .get_mut("function")
                    .and_then(|function| function.get_mut("arguments"))
                {
                    if let Some(string) = arguments.as_str() {
                        if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(string) {
                            if parsed.is_object() {
                                *arguments = parsed;
                            }
                        }
                    }
                }
            }
        }
        object["tool_calls"] = tool_calls;
    }
    if let Some(tool_call_id) = &message.tool_call_id {
        object["tool_call_id"] = serde_json::Value::String(tool_call_id.clone());
    }
    if let Some(name) = &message.name {
        object["name"] = serde_json::Value::String(name.clone());
    }
    if let Some(reasoning) = &message.reasoning_content {
        object["reasoning_content"] = serde_json::Value::String(reasoning.clone());
    }
    if let Some(response_format) = &message.response_format {
        object["response_format"] = response_format.clone();
    }
    if let Some(task) = &message.task {
        object["task"] = serde_json::Value::String(task.clone());
    }
    if let Some(wo_eos) = message.wo_eos {
        object["wo_eos"] = serde_json::Value::Bool(wo_eos);
    }
    object
}

fn request_messages_json(
    messages: &[ChatMessage],
    parse_dsv41_images: bool,
) -> Result<Vec<serde_json::Value>, String> {
    messages
        .iter()
        .map(|message| {
            let mut value = message_to_json(message);
            if !parse_dsv41_images {
                // Preserve the public template path: structured OpenAI
                // blocks are flattened to text for non-V4.1 models.
                value["content"] = serde_json::Value::String(
                    message
                        .content
                        .as_ref()
                        .map(|content| content.text())
                        .unwrap_or_default(),
                );
            }
            // Compact `<image>...</image>` notation is converted before the
            // image walker so it shares the OpenAI content-block path.
            if parse_dsv41_images {
                if let Some(text) = value.get("content").and_then(|v| v.as_str()) {
                    let blocks = dsv41_encoding::parse_tagged_text(text)
                        .map_err(|error| error.to_string())?;
                    if blocks.is_array() {
                        value["content"] = blocks;
                    }
                }
            }
            Ok(value)
        })
        .collect()
}

/// Encode OpenAI messages with the pinned V4.1 harmony formatter and image
/// processor. This is public so the CLI/server protocol layers share the same
/// image-span normalization without duplicating it in the runtime.
pub fn encode_dsv41_messages(
    messages: &[serde_json::Value],
    tools: Option<&[serde_json::Value]>,
    enable_thinking: Option<bool>,
    reasoning_effort: Option<&serde_json::Value>,
    tokenizer: &cortiq_engine::tokenizer::Tokenizer,
    config: &VisionConfig,
) -> Result<PromptIngress, String> {
    let mut messages = messages.to_vec();
    if let Some(tools) = tools.filter(|items| !items.is_empty()) {
        let tools_value = serde_json::Value::Array(tools.to_vec());
        if messages
            .first()
            .and_then(|message| message.get("role"))
            .and_then(|role| role.as_str())
            == Some("system")
        {
            messages[0]["tools"] = tools_value;
        } else {
            messages.insert(
                0,
                serde_json::json!({"role":"system", "content":"", "tools":tools_value}),
            );
        }
    }
    let thinking_mode = if enable_thinking == Some(true)
        || (enable_thinking.is_none() && reasoning_effort.is_some())
    {
        ThinkingMode::Thinking
    } else {
        ThinkingMode::Chat
    };
    let effort = reasoning_effort
        .map(ReasoningEffort::from_json)
        .transpose()
        .map_err(|error| error.to_string())?;
    let encoded = dsv41_encoding::encode_messages(
        &messages,
        &EncodeOptions {
            thinking_mode,
            reasoning_effort: effort,
            ..EncodeOptions::default()
        },
    )
    .map_err(|error| error.to_string())?;
    let prepared =
        dsv41_vision::prepare_vl_inputs(&encoded.prompt, &encoded.images, tokenizer, config)
            .map_err(|error| error.to_string())?;
    Ok(PromptIngress {
        token_ids: prepared.token_ids,
        token_types: prepared.token_types,
        images: prepared.images,
        dsv41: true,
    })
}

#[derive(Serialize)]
struct ChatCompletionsResponse {
    id: String,
    object: String,
    created: u64,
    model: String,
    choices: Vec<ChatChoice>,
    usage: Usage,
    #[serde(skip_serializing_if = "Option::is_none")]
    cortiq: Option<CortiqResponseMeta>,
}

#[derive(Serialize)]
struct ChatChoice {
    index: u32,
    message: ChatMessage,
    finish_reason: String,
}

async fn chat_completions(
    State(state): State<Arc<AppState>>,
    Json(req): Json<ChatCompletionsRequest>,
) -> Response {
    if req.messages.is_empty() {
        return error_response(StatusCode::BAD_REQUEST, "messages must not be empty");
    }

    // Resolve task selection into request-local state. Mutating the runtime's
    // global active task here made concurrent requests use each other's mask.
    let (task_used, request_mask) =
        if let Some(task) = req.cortiq.as_ref().and_then(|c| c.task.as_deref()) {
            let Some(mask) = state.runtime.masks().get(task).cloned() else {
                return error_response(
                    StatusCode::NOT_FOUND,
                    format!("Task mask '{task}' not found"),
                );
            };
            (task.to_string(), Some(mask))
        } else {
            state.runtime.active_selection().await
        };
    let mut sampler_config = match request_sampler(req.temperature, req.top_p, req.seed) {
        Ok(config) => config,
        Err(response) => return response,
    };
    if req.thinking() == Some(false) {
        let think_tokens = state.tokenizer.encode("<think>");
        sampler_config.suppress_tokens.extend(think_tokens);
    }

    // Chat template → prompt ids (uses real special tokens).
    let (prompt_ids, prompt_ingress) = if let Some(source) =
        state.runtime.model().arch().deepseek_v41.as_ref()
    {
        let config = match VisionConfig::from_source(source) {
            Ok(config) => config,
            Err(error) => return error_response(StatusCode::BAD_REQUEST, error),
        };
        let messages = match request_messages_json(&req.messages, true) {
            Ok(messages) => messages,
            Err(error) => return error_response(StatusCode::BAD_REQUEST, error),
        };
        let ingress = match encode_dsv41_messages(
            &messages,
            req.effective_tools(),
            req.thinking(),
            req.reasoning_effort.as_ref(),
            &state.tokenizer,
            &config,
        ) {
            Ok(ingress) => ingress,
            Err(error) => return error_response(StatusCode::BAD_REQUEST, error),
        };
        (ingress.token_ids.clone(), Some(ingress))
    } else {
        let prompt_ids = {
            let mut msgs: Vec<serde_json::Value> = req
                .messages
                .iter()
                .map(|m| {
                    let mut o = serde_json::json!({
                        "role": m.role,
                        "content": m.content.as_ref().map(|c| c.text()).unwrap_or_default(),
                    });
                    if let Some(tc) = &m.tool_calls {
                        // OpenAI sends function.arguments as a STRING of
                        // JSON; some templates (Nanbeige's XML history
                        // branch) iterate it as an object. Normalise:
                        // parseable strings become objects, everything else
                        // passes through untouched. Qwen-style templates
                        // tojson the object back to the identical text.
                        let mut tc = tc.clone();
                        if let Some(arr) = tc.as_array_mut() {
                            for call in arr {
                                if let Some(args) = call
                                    .get_mut("function")
                                    .and_then(|f| f.get_mut("arguments"))
                                {
                                    if let Some(s) = args.as_str() {
                                        if let Ok(v) = serde_json::from_str::<serde_json::Value>(s)
                                        {
                                            if v.is_object() {
                                                *args = v;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        o["tool_calls"] = tc;
                    }
                    if let Some(id) = &m.tool_call_id {
                        o["tool_call_id"] = serde_json::json!(id);
                    }
                    if let Some(n) = &m.name {
                        o["name"] = serde_json::json!(n);
                    }
                    o
                })
                .collect();
            // Hard thinking suppression: when enable_thinking=false, inject a
            // system-level directive so even models that ignore the empty
            //  block still produce direct answers.
            eprintln!("[serve] thinking={:?}", req.thinking());
            if req.thinking() == Some(false) {
                let has_system = msgs.iter().any(|m| m["role"] == "system");
                let directive = "Answer directly and concisely. Do NOT reason, think step-by-step, or explain your process. Output ONLY the final answer.";
                if has_system {
                    // Prepend to existing system message
                    if let Some(m) = msgs.iter_mut().find(|m| m["role"] == "system") {
                        let cur = m["content"].as_str().unwrap_or_default();
                        m["content"] = serde_json::json!(format!("{directive}\n\n{cur}"));
                    }
                } else {
                    msgs.insert(
                        0,
                        serde_json::json!({"role": "system", "content": directive}),
                    );
                }
            }
            eprintln!("[serve] msgs[0]={:?}", msgs.first());
            state
                .tokenizer
                .apply_chat_template_json(&msgs, req.effective_tools(), req.thinking())
        };
        (prompt_ids, None)
    };

    // Runtime integration consumes this payload for V4.1 image-aware prefill.
    let vl_inputs = prompt_ingress.map(|ingress| dsv41_vision::PreparedVlInputs {
        token_ids: ingress.token_ids,
        token_types: ingress.token_types,
        images: ingress.images,
    });
    let dsv41 = vl_inputs.is_some();

    let request_id = format!("cmf-{}", uuid::Uuid::new_v4());
    let created = chrono::Utc::now().timestamp() as u64;
    let max_tokens = req.max_tokens as usize;
    let dsv41_thinking = dsv41
        && req.thinking() != Some(false)
        && (req.thinking() == Some(true) || req.reasoning_effort.is_some());

    if let Some(class_tokens) = req
        .cortiq
        .as_ref()
        .and_then(|extension| extension.class_tokens.clone())
    {
        if vl_inputs
            .as_ref()
            .is_some_and(|inputs| !inputs.images.is_empty())
        {
            return error_response(
                StatusCode::BAD_REQUEST,
                "cortiq.class_tokens does not support image prompts",
            );
        }
        if req.stream {
            return error_response(
                StatusCode::BAD_REQUEST,
                "cortiq.class_tokens does not support stream=true",
            );
        }
        if !(2..=32).contains(&class_tokens.len()) {
            return error_response(
                StatusCode::BAD_REQUEST,
                "cortiq.class_tokens must contain 2..32 labels",
            );
        }
        let mut labels = Vec::with_capacity(class_tokens.len());
        for token in class_tokens {
            let ids = state.tokenizer.encode(&token);
            if ids.len() != 1 {
                return error_response(
                    StatusCode::BAD_REQUEST,
                    format!("class label {token:?} must encode to exactly one token, got {ids:?}"),
                );
            }
            labels.push((token, ids[0]));
        }
        let prompt_tokens = prompt_ids.len() as u32;
        let (classification, elapsed_ms) =
            match run_classification(state.clone(), prompt_ids, request_mask, labels).await {
                Ok(result) => result,
                Err(response) => return response,
            };
        let status = state.runtime.status().await;
        let task_mask = state.runtime.masks().get(&task_used);
        return Json(ChatCompletionsResponse {
            id: request_id,
            object: "chat.completion".to_string(),
            created,
            model: req.model,
            choices: vec![ChatChoice {
                index: 0,
                message: ChatMessage {
                    role: "assistant".to_string(),
                    content: Some(classification.label.clone().into()),
                    tool_calls: None,
                    tool_call_id: None,
                    name: None,
                    reasoning_content: None,
                    response_format: None,
                    task: None,
                    wo_eos: None,
                },
                finish_reason: "stop".to_string(),
            }],
            usage: Usage {
                prompt_tokens,
                completion_tokens: 0,
                total_tokens: prompt_tokens,
            },
            cortiq: Some(CortiqResponseMeta {
                task_used,
                sparsity: task_mask.map(|mask| mask.sparsity).unwrap_or(0.0),
                active_layers: task_mask
                    .map(|mask| mask.active_layer_count())
                    .unwrap_or(state.runtime.model().arch().num_layers),
                execution_mode: format!("{:?}", status.execution_mode),
                tokens_per_second: prompt_tokens as f64 / (elapsed_ms / 1000.0).max(1e-9),
                classification: Some(classification),
            }),
        })
        .into_response();
    }

    if req.stream {
        let tool_names: Vec<String> = req
            .effective_tools()
            .map(|ts| {
                ts.iter()
                    .filter_map(|t| t["function"]["name"].as_str().map(String::from))
                    .collect()
            })
            .unwrap_or_default();
        let (tx, stream) = ChatStream::new(64);
        let model = req.model.clone();
        let id = request_id.clone();
        let state2 = state.clone();

        tokio::spawn(async move {
            // Role prelude chunk.
            let _ = tx
                .send(streaming::StreamChunk {
                    id: id.clone(),
                    object: "chat.completion.chunk".to_string(),
                    created,
                    model: model.clone(),
                    choices: vec![streaming::StreamChoice {
                        index: 0,
                        delta: streaming::StreamDelta {
                            role: Some("assistant".to_string()),
                            content: None,
                            tool_calls: None,
                        },
                        finish_reason: None,
                    }],
                    usage: None,
                })
                .await;

            // Real tokens flow from the generation thread through the
            // channel; a closed channel (client gone) cancels generation.
            let tx_tokens = tx.clone();
            let id2 = id.clone();
            let model2 = model.clone();
            // Shared with the post-generation flush: a short reply that
            // never opens a <think> block (the template prefilled an
            // empty one) used to be swallowed whole — the filter waited
            // for a </think> that never comes.
            let filter_shared = std::sync::Arc::new(std::sync::Mutex::new(String::new()));
            let filter_cb = filter_shared.clone();
            let mut filter_passthrough = req.thinking() != Some(false);
            // Tool-call holdback: once the model opens a <tool_call>
            // block, nothing more goes out as content — the calls are
            // parsed whole at the end and shipped as a tool_calls delta.
            // Until the marker is certain, the last few characters stay
            // buffered so a marker split across tokens cannot leak.
            let tools_active = req.effective_tools().is_some();
            let mut tool_tail = String::new();
            let mut tool_holding = false;
            const MARK: &str = "<tool_call>";

            let callback: cortiq_engine::TokenCallback = Box::new(move |token: &str| {
                if filter_passthrough {
                    if tools_active {
                        if tool_holding {
                            return !tx_tokens.is_closed();
                        }
                        tool_tail.push_str(token);
                        if let Some(pos) = tool_tail.find(MARK) {
                            tool_holding = true;
                            let before = tool_tail[..pos].to_string();
                            if !before.is_empty() {
                                let chunk = streaming::token_chunk(&id2, &model2, &before, created);
                                return tx_tokens.blocking_send(chunk).is_ok();
                            }
                            return !tx_tokens.is_closed();
                        }
                        // Flush all but a marker's worth of tail.
                        if tool_tail.len() > MARK.len() {
                            let cut = tool_tail.len() - (MARK.len() - 1);
                            let safe_cut = (0..=cut)
                                .rev()
                                .find(|&c| tool_tail.is_char_boundary(c))
                                .unwrap_or(0);
                            if safe_cut > 0 {
                                let out: String = tool_tail.drain(..safe_cut).collect();
                                let chunk = streaming::token_chunk(&id2, &model2, &out, created);
                                return tx_tokens.blocking_send(chunk).is_ok();
                            }
                        }
                        return !tx_tokens.is_closed();
                    }
                    let chunk = streaming::token_chunk(&id2, &model2, token, created);
                    return tx_tokens.blocking_send(chunk).is_ok();
                }
                let mut filter_buf = filter_cb.lock().expect("think filter buf");
                filter_buf.push_str(token);
                if let Some(pos) = filter_buf.find("</think>") {
                    let tail = filter_buf[pos + "</think>".len()..].to_string();
                    filter_buf.clear();
                    filter_passthrough = true;
                    let tail_trimmed = tail.trim_start_matches('\n');
                    if !tail_trimmed.is_empty() {
                        let chunk = streaming::token_chunk(&id2, &model2, tail_trimmed, created);
                        return tx_tokens.blocking_send(chunk).is_ok();
                    }
                    return true;
                }
                if filter_buf.len() > 100 && !filter_buf.contains("<think>") {
                    let b = std::mem::take(&mut *filter_buf);
                    filter_passthrough = true;
                    let chunk = streaming::token_chunk(&id2, &model2, &b, created);
                    return tx_tokens.blocking_send(chunk).is_ok();
                }
                true
            });

            let outcome = run_generation(
                state2.clone(),
                prompt_ids,
                vl_inputs,
                max_tokens,
                request_mask,
                sampler_config,
                Some(callback),
            )
            .await;

            match outcome {
                Ok((result, elapsed_ms)) => {
                    // End-of-generation flush of the think filter, by the
                    // filter's own rules: a buffer that never opened a
                    // <think> block IS the answer; a closed block ships
                    // its tail; an unterminated block stays private.
                    let leftover = std::mem::take(&mut *filter_shared.lock().expect("filter buf"));
                    if !leftover.is_empty() {
                        let out = if !leftover.contains("<think>") {
                            leftover
                        } else if let Some(pos) = leftover.find("</think>") {
                            leftover[pos + "</think>".len()..]
                                .trim_start_matches('\n')
                                .to_string()
                        } else {
                            String::new()
                        };
                        if !out.is_empty() {
                            let _ = tx
                                .send(streaming::token_chunk(&id, &model, &out, created))
                                .await;
                        }
                    }
                    state2
                        .runtime
                        .record_generation(result.tokens_generated, elapsed_ms, elapsed_ms)
                        .await;
                    let (plain2, mut calls, _) = if dsv41 {
                        extract_dsv41_result(&result, dsv41_thinking, &state2.tokenizer)
                    } else {
                        let (plain, calls) = extract_tool_calls(&result.text);
                        (plain, calls, None)
                    };
                    if calls.is_empty() {
                        if let Some(c) = bare_call_fallback(&plain2, &tool_names) {
                            calls = vec![c];
                        }
                    }
                    let finish = if calls.is_empty() {
                        result.finish_reason.clone()
                    } else {
                        let _ = tx
                            .send(streaming::tool_calls_chunk(
                                &id,
                                &model,
                                serde_json::Value::Array(
                                    calls
                                        .into_iter()
                                        .enumerate()
                                        .map(|(i, mut c)| {
                                            c["index"] = serde_json::json!(i);
                                            c
                                        })
                                        .collect(),
                                ),
                                created,
                            ))
                            .await;
                        "tool_calls".to_string()
                    };
                    // exact counts ahead of the finish chunk (OpenAI include_usage shape)
                    let _ = tx
                        .send(streaming::usage_chunk(
                            &id,
                            &model,
                            created,
                            result.prompt_tokens as u32,
                            result.tokens_generated as u32,
                        ))
                        .await;
                    let _ = tx
                        .send(streaming::finish_chunk(&id, &model, &finish, created))
                        .await;
                }
                Err(_) => {
                    let _ = tx
                        .send(streaming::finish_chunk(&id, &model, "error", created))
                        .await;
                }
            }
        });

        stream.into_sse().into_response()
    } else {
        let (result, elapsed_ms) = match run_generation(
            state.clone(),
            prompt_ids,
            vl_inputs,
            max_tokens,
            request_mask,
            sampler_config,
            None,
        )
        .await
        {
            Ok(r) => r,
            Err(resp) => return resp,
        };

        state
            .runtime
            .record_generation(result.tokens_generated, elapsed_ms, elapsed_ms)
            .await;
        let status = state.runtime.status().await;
        let task_mask = state.runtime.masks().get(&task_used);

        let cortiq_meta = req.cortiq.as_ref().map(|_| CortiqResponseMeta {
            task_used,
            sparsity: task_mask.map(|m| m.sparsity).unwrap_or(0.0),
            active_layers: task_mask
                .map(|m| m.active_layer_count())
                .unwrap_or(state.runtime.model().arch().num_layers),
            execution_mode: format!("{:?}", status.execution_mode),
            tokens_per_second: result.tokens_generated as f64 / (elapsed_ms / 1000.0).max(1e-9),
            classification: None,
        });

        let (mut plain, mut calls, reasoning_content) = if dsv41 {
            extract_dsv41_result(&result, dsv41_thinking, &state.tokenizer)
        } else if req.thinking() == Some(false) {
            let content = strip_think_block(&result.text);
            let (plain, calls) = extract_tool_calls(&content);
            (plain, calls, None)
        } else {
            let (plain, calls) = extract_tool_calls(&result.text);
            (plain, calls, None)
        };
        if calls.is_empty() {
            if let Some(names) = req.effective_tools().map(|ts| {
                ts.iter()
                    .filter_map(|t| t["function"]["name"].as_str().map(String::from))
                    .collect::<Vec<_>>()
            }) {
                if let Some(c) = bare_call_fallback(&plain, &names) {
                    calls = vec![c];
                    plain = String::new();
                }
            }
        }
        let made_calls = !calls.is_empty();
        Json(ChatCompletionsResponse {
            id: request_id,
            object: "chat.completion".to_string(),
            created,
            model: req.model,
            choices: vec![ChatChoice {
                index: 0,
                message: ChatMessage {
                    role: "assistant".to_string(),
                    // OpenAI shape: a pure tool-call turn has null content.
                    content: if made_calls && plain.is_empty() {
                        None
                    } else {
                        Some(plain.into())
                    },
                    tool_calls: made_calls.then_some(serde_json::Value::Array(calls)),
                    tool_call_id: None,
                    name: None,
                    reasoning_content,
                    response_format: None,
                    task: None,
                    wo_eos: None,
                },
                finish_reason: if made_calls {
                    "tool_calls".to_string()
                } else {
                    result.finish_reason.clone()
                },
            }],
            usage: Usage {
                prompt_tokens: result.prompt_tokens as u32,
                completion_tokens: result.tokens_generated as u32,
                total_tokens: (result.prompt_tokens + result.tokens_generated) as u32,
            },
            cortiq: cortiq_meta,
        })
        .into_response()
    }
}

/// Small-model fallback: the whole reply is ONE bare JSON object that
/// names a REQUESTED tool. Qwen-family minis often emit the call
/// without its <tool_call> wrapper; vLLM and llama.cpp both accept
/// this shape, and refusing it here would fail every agent loop on a
/// small model while a human can see the call sitting in the text.
/// Conditions are strict on purpose: tools were requested, the text
/// parses as a single object, `name` is a string matching a declared
/// tool, and `arguments` (when present) is an object.
fn bare_call_fallback(text: &str, allowed: &[String]) -> Option<serde_json::Value> {
    let t = text.trim();
    if !t.starts_with('{') || !t.ends_with('}') {
        return None;
    }
    let v: serde_json::Value = serde_json::from_str(t).ok()?;
    let name = v.get("name")?.as_str()?;
    if !allowed.iter().any(|a| a == name) {
        return None;
    }
    let args = v.get("arguments").cloned().unwrap_or(serde_json::json!({}));
    if !args.is_object() {
        return None;
    }
    Some(serde_json::json!({
        "id": format!("call_{}", uuid::Uuid::new_v4().simple()),
        "type": "function",
        "function": {
            "name": name,
            "arguments": serde_json::to_string(&args).unwrap_or_else(|_| "{}".into()),
        }
    }))
}

/// Nanbeige's XML tool grammar, normalised to the JSON shape:
/// `<function=NAME>\n<parameter=K>\nV\n</parameter>...</function>`.
/// Parameter values keep inner newlines (the format allows multi-line
/// values); the surrounding single newline the grammar inserts is
/// trimmed.
fn parse_xml_function(body: &str) -> Option<serde_json::Value> {
    let t = body.trim();
    let name_start = t.find("<function=")? + "<function=".len();
    let name_end = t[name_start..].find(['>', '\n'])? + name_start;
    let name = t[name_start..name_end].trim().to_string();
    if name.is_empty() {
        return None;
    }
    let mut args = serde_json::Map::new();
    let mut rest = &t[name_end..];
    while let Some(ps) = rest.find("<parameter=") {
        let key_start = ps + "<parameter=".len();
        let key_end = rest[key_start..].find('>')? + key_start;
        let key = rest[key_start..key_end].trim().to_string();
        let val_start = key_end + 1;
        let val_end = rest[val_start..].find("</parameter>")? + val_start;
        let val = rest[val_start..val_end]
            .strip_prefix('\n')
            .unwrap_or(&rest[val_start..val_end])
            .strip_suffix('\n')
            .unwrap_or(&rest[val_start..val_end])
            .to_string();
        args.insert(key, serde_json::Value::String(val));
        rest = &rest[val_end + "</parameter>".len()..];
    }
    Some(serde_json::json!({"name": name, "arguments": args}))
}

/// Extract `<tool_call>{...}</tool_call>` blocks from generated text —
/// the format every Qwen-family template (Nanbeige included) trains the
/// model to emit. Returns the text OUTSIDE the blocks and the calls in
/// OpenAI shape. `arguments` stays a STRING of JSON per the OpenAI
/// contract; a block whose body does not parse as JSON is left in the
/// text rather than shipped as a broken call — a client can read prose,
/// but it cannot execute garbage.
fn extract_tool_calls(text: &str) -> (String, Vec<serde_json::Value>) {
    const OPEN: &str = "<tool_call>";
    const CLOSE: &str = "</tool_call>";
    let mut rest = text;
    let mut plain = String::new();
    let mut calls = Vec::new();
    while let Some(i) = rest.find(OPEN) {
        let Some(j) = rest[i + OPEN.len()..].find(CLOSE) else {
            break; // unterminated block: keep as text (truncated output)
        };
        let body = rest[i + OPEN.len()..i + OPEN.len() + j].trim();
        let after = &rest[i + OPEN.len() + j + CLOSE.len()..];
        // Two trained grammars share the <tool_call> wrapper: the JSON
        // object, and Nanbeige's XML `<function=name><parameter=k>v...`.
        // Parse whichever arrived.
        let parsed = serde_json::from_str::<serde_json::Value>(body)
            .ok()
            .or_else(|| parse_xml_function(body));
        match parsed {
            Some(v) if v.get("name").map(|n| n.is_string()) == Some(true) => {
                plain.push_str(&rest[..i]);
                let args = v.get("arguments").cloned().unwrap_or(serde_json::json!({}));
                calls.push(serde_json::json!({
                    "id": format!("call_{}", uuid::Uuid::new_v4().simple()),
                    "type": "function",
                    "function": {
                        "name": v["name"],
                        "arguments": serde_json::to_string(&args).unwrap_or_else(|_| "{}".into()),
                    }
                }));
            }
            _ => {
                // Not a call: keep the whole block verbatim as text.
                plain.push_str(&rest[..i + OPEN.len() + j + CLOSE.len()]);
            }
        }
        rest = after;
    }
    plain.push_str(rest);
    (plain.trim().to_string(), calls)
}

/// Parse a DeepSeek-V4.1 harmony completion. The engine normally decodes the
/// EOS marker, but a tokenizer configured to skip special tokens may omit it;
/// append one only for this strict parser and preserve raw text when malformed
/// or truncated.
fn extract_dsv41_completion(
    text: &str,
    thinking: bool,
) -> (String, Vec<serde_json::Value>, Option<String>) {
    let mode = if thinking {
        ThinkingMode::Thinking
    } else {
        ThinkingMode::Chat
    };
    let mut wire = text.to_string();
    if !wire.contains(dsv41_encoding::EOS_TOKEN) {
        wire.push_str(dsv41_encoding::EOS_TOKEN);
    }
    let Ok(message) = dsv41_encoding::parse_message_from_completion_text(&wire, mode) else {
        return (text.to_string(), Vec::new(), None);
    };
    let content = message
        .get("content")
        .and_then(serde_json::Value::as_str)
        .unwrap_or_default()
        .to_string();
    let reasoning = message
        .get("reasoning_content")
        .and_then(serde_json::Value::as_str)
        .filter(|value| !value.is_empty())
        .map(str::to_string);
    let calls = message
        .get("tool_calls")
        .and_then(serde_json::Value::as_array)
        .map(|calls| {
            calls
                .iter()
                .enumerate()
                .map(|(index, call)| {
                    let mut call = call.clone();
                    if call.get("id").is_none() {
                        call["id"] = serde_json::json!(format!("call_dsv41_{index}"));
                    }
                    call
                })
                .collect()
        })
        .unwrap_or_default();
    (content, calls, reasoning)
}

fn extract_dsv41_result(
    result: &GenerateResult,
    thinking: bool,
    tokenizer: &cortiq_engine::tokenizer::Tokenizer,
) -> (String, Vec<serde_json::Value>, Option<String>) {
    // `GenerateResult::text` uses the normal user-facing decoder, which may
    // drop special tokens. Reconstruct the harmony wire text from ids so EOS,
    // thinking markers, and DSML tags reach the strict parser.
    if !result.token_ids.is_empty() {
        let wire = tokenizer.decode_for_protocol(&result.token_ids);
        return extract_dsv41_completion(&wire, thinking);
    }
    extract_dsv41_completion(&result.text, thinking)
}

fn strip_think_block(s: &str) -> String {
    let mut rest = s;
    if let Some(pos) = rest.find("</think>") {
        rest = &rest[pos + "</think>".len()..];
    } else if rest.starts_with("<think>") {
        return String::new();
    }
    rest.trim_start_matches('\n').to_string()
}

// ─── Completions (legacy) ────────────────────────────────

#[derive(Deserialize)]
struct CompletionsRequest {
    model: String,
    prompt: String,
    temperature: Option<f32>,
    #[serde(default = "default_max_tokens")]
    max_tokens: u32,
}

#[derive(Serialize)]
struct CompletionsResponse {
    id: String,
    object: String,
    created: u64,
    model: String,
    choices: Vec<CompletionChoice>,
    usage: Usage,
}

#[derive(Serialize)]
struct CompletionChoice {
    text: String,
    index: u32,
    finish_reason: String,
}

async fn completions(
    State(state): State<Arc<AppState>>,
    Json(req): Json<CompletionsRequest>,
) -> Response {
    let prompt_ids = state.tokenizer.encode(&req.prompt);

    let sampler_config = match request_sampler(req.temperature, None, None) {
        Ok(config) => config,
        Err(response) => return response,
    };
    let (_, request_mask) = state.runtime.active_selection().await;

    let (result, elapsed_ms) = match run_generation(
        state.clone(),
        prompt_ids,
        None,
        req.max_tokens as usize,
        request_mask,
        sampler_config,
        None,
    )
    .await
    {
        Ok(r) => r,
        Err(resp) => return resp,
    };

    state
        .runtime
        .record_generation(result.tokens_generated, elapsed_ms, elapsed_ms)
        .await;

    Json(CompletionsResponse {
        id: format!("cmf-{}", uuid::Uuid::new_v4()),
        object: "text_completion".to_string(),
        created: chrono::Utc::now().timestamp() as u64,
        model: req.model,
        choices: vec![CompletionChoice {
            text: result.text,
            index: 0,
            finish_reason: result.finish_reason,
        }],
        usage: Usage {
            prompt_tokens: result.prompt_tokens as u32,
            completion_tokens: result.tokens_generated as u32,
            total_tokens: (result.prompt_tokens + result.tokens_generated) as u32,
        },
    })
    .into_response()
}

fn default_max_tokens() -> u32 {
    256
}

#[cfg(test)]
mod tests {

    #[test]
    fn tool_calls_extract_single() {
        let (text, calls) = extract_tool_calls(
            "<tool_call>\n{\"name\": \"get_weather\", \"arguments\": {\"city\": \"Paris\"}}\n</tool_call>",
        );
        assert_eq!(text, "");
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0]["function"]["name"], "get_weather");
        // arguments is a STRING of JSON per the OpenAI contract
        let args: serde_json::Value =
            serde_json::from_str(calls[0]["function"]["arguments"].as_str().unwrap()).unwrap();
        assert_eq!(args["city"], "Paris");
        assert!(calls[0]["id"].as_str().unwrap().starts_with("call_"));
    }

    #[test]
    fn tool_calls_extract_text_and_multiple() {
        let (text, calls) = extract_tool_calls(
            "Let me check both.\n<tool_call>\n{\"name\": \"a\", \"arguments\": {}}\n</tool_call>\n<tool_call>\n{\"name\": \"b\", \"arguments\": {\"x\": 1}}\n</tool_call>",
        );
        assert_eq!(text, "Let me check both.");
        assert_eq!(calls.len(), 2);
        assert_eq!(calls[1]["function"]["name"], "b");
    }

    #[test]
    fn tool_calls_malformed_body_stays_text() {
        let (text, calls) = extract_tool_calls("<tool_call>\nnot json at all\n</tool_call> done");
        assert!(calls.is_empty());
        assert!(
            text.contains("not json at all"),
            "broken call must stay readable text"
        );
    }

    #[test]
    fn tool_calls_unterminated_stays_text() {
        let (text, calls) = extract_tool_calls("<tool_call>\n{\"name\": \"a\"");
        assert!(calls.is_empty());
        assert!(
            text.contains("<tool_call>"),
            "truncated output must not vanish"
        );
    }

    use super::*;

    #[test]
    fn sampler_options_start_from_defaults_and_validate_ranges() {
        let changed = request_sampler(Some(0.2), Some(0.5), Some(7)).unwrap();
        assert_eq!(changed.temperature, 0.2);
        assert_eq!(changed.top_p, 0.5);
        assert_eq!(changed.seed, Some(7));

        let fresh = request_sampler(None, None, None).unwrap();
        let defaults = SamplerConfig::default();
        assert_eq!(fresh.temperature, defaults.temperature);
        assert_eq!(fresh.top_p, defaults.top_p);
        assert_eq!(fresh.seed, None);

        assert!(request_sampler(Some(-1.0), None, None).is_err());
        assert!(request_sampler(None, Some(1.1), None).is_err());
    }

    /// Cline / Roo-style clients send `content` as a block array once they
    /// attach file context. Both shapes must deserialize, and the array must
    /// flatten to the same prompt text a flat string would give.
    #[test]
    fn content_accepts_both_a_string_and_a_block_array() {
        let flat: ChatMessage =
            serde_json::from_str(r#"{"role":"user","content":"hello"}"#).unwrap();
        assert_eq!(flat.content.as_ref().unwrap().text(), "hello");

        let blocks: ChatMessage = serde_json::from_str(
            r#"{"role":"user","content":[
                 {"type":"text","text":"file context"},
                 {"type":"text","text":"the question"}]}"#,
        )
        .unwrap();
        assert_eq!(
            blocks.content.as_ref().unwrap().text(),
            "file context\nthe question"
        );

        // A non-text block must not fail the turn — it contributes nothing.
        let mixed: ChatMessage = serde_json::from_str(
            r#"{"role":"user","content":[
                 {"type":"image_url","image_url":{"url":"data:x"}},
                 {"type":"text","text":"describe"}]}"#,
        )
        .unwrap();
        assert_eq!(mixed.content.as_ref().unwrap().text(), "describe");

        // And a whole request round-trips, which is what 422'd before.
        let req: ChatCompletionsRequest = serde_json::from_str(
            r#"{"model":"m","messages":[
                 {"role":"system","content":[{"type":"text","text":"sys"}]},
                 {"role":"user","content":"hi"}]}"#,
        )
        .unwrap();
        assert_eq!(req.messages[0].content.as_ref().unwrap().text(), "sys");
        assert_eq!(req.messages[1].content.as_ref().unwrap().text(), "hi");
    }
}