mahbot 0.4.0

An autonomous agentic engineering system that manages software development through role separation, subagents, and deterministic diagnostics.
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
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
//! Generic OpenAI-compatible provider.
//! Most LLM APIs follow the same `/v1/chat/completions` format.
//! This module provides a single implementation that works for all of them.

use crate::providers::reasoning_roundtrip;
use crate::providers::{ScopedCallError, ensure_chat_completions_url, provider_routing_json};
use crate::retry::{FailureClass, RetryFailureRecord};
use crate::util::error::{HttpError, retry_after_header};
use crate::util::json::try_repair_json;
use crate::{
    ChatMessage, ChatRequest as ProviderChatRequest, ChatResponse as ProviderChatResponse,
    ChatRole, Provider, ProviderUsage, Reasoning, ToolCall as ProviderToolCall, ToolSpec,
};
use async_trait::async_trait;
use futures_util::StreamExt;
use reqwest::{
    Client, RequestBuilder,
    header::{HeaderMap, HeaderValue},
};
use serde::{Deserialize, Serialize};
use std::sync::OnceLock;
use std::time::{Duration, Instant};

/// A provider that speaks the OpenAI-compatible chat completions API.
pub(crate) struct OpenAiCompatibleProvider {
    pub name: String,
    pub base_url: String,
    pub credential: Option<String>,

    /// HTTP request timeout in seconds for LLM API calls. Default: 120.
    timeout_secs: u64,
    /// Extra HTTP headers to include in all API requests.
    extra_headers: std::collections::HashMap<String, String>,
    /// Cached HTTP client with connection reuse across all API calls.
    /// Initialized lazily on first `http_client()` call.
    http_client: OnceLock<Client>,
    /// Cached HTTP client for scoped calls: NO total request
    /// timeout — per-attempt total is enforced by the scoped caller against
    /// the remaining operation budget, and idle timeouts reset while data
    /// flows. Initialized lazily on first `http_client_scoped()` call.
    http_client_scoped: OnceLock<Client>,
}

impl OpenAiCompatibleProvider {
    #[must_use]
    pub fn new(name: &str, base_url: &str, credential: Option<&str>) -> Self {
        Self {
            name: name.to_string(),
            base_url: base_url.trim_end_matches('/').to_string(),
            credential: credential.map(ToString::to_string),
            timeout_secs: 120,
            extra_headers: std::collections::HashMap::new(),
            http_client: OnceLock::new(),
            http_client_scoped: OnceLock::new(),
        }
    }

    /// Set extra HTTP headers to include in all API requests.
    #[must_use]
    pub fn with_extra_headers(
        mut self,
        headers: std::collections::HashMap<String, String>,
    ) -> Self {
        self.extra_headers = headers;
        self
    }

    /// Build the shared HTTP client with the given total request timeout.
    ///
    /// `Some(timeout)` yields the default client (120 s total request
    /// timeout); `None` yields the scoped client (no total timeout — the
    /// scoped call paths enforce their own per-attempt deadline and idle
    /// timeout). Connection pool, connect timeout, and extra headers are
    /// identical in both.
    fn build_client(&self, timeout: Option<Duration>) -> Client {
        crate::util::http::install_ring_provider();
        let mut builder = Client::builder().connect_timeout(Duration::from_secs(10));
        if let Some(timeout) = timeout {
            builder = builder.timeout(timeout);
        }

        if !self.extra_headers.is_empty() {
            let mut headers = HeaderMap::new();
            for (key, value) in &self.extra_headers {
                match (
                    reqwest::header::HeaderName::from_bytes(key.as_bytes()),
                    HeaderValue::from_str(value),
                ) {
                    (Ok(name), Ok(val)) => {
                        headers.insert(name, val);
                    }
                    _ => {
                        tracing::warn!(header = key, "Skipping invalid extra header name or value");
                    }
                }
            }
            builder = builder.default_headers(headers);
        }

        builder
            .build()
            .expect("Failed to build HTTP client — check TLS/network configuration")
    }

    pub(crate) fn http_client(&self) -> &Client {
        self.http_client
            .get_or_init(|| self.build_client(Some(Duration::from_secs(self.timeout_secs))))
    }

    /// Scoped HTTP client: same connection pool and headers as
    /// [`Self::http_client`] but WITHOUT the total request timeout. The scoped
    /// call paths enforce their own per-attempt deadline and idle timeout.
    pub(crate) fn http_client_scoped(&self) -> &Client {
        self.http_client_scoped
            .get_or_init(|| self.build_client(None))
    }
}

#[derive(Debug, Serialize)]
struct ChatCompletionRequest {
    model: String,
    messages: Vec<NativeMessage>,
    #[serde(skip_serializing_if = "Option::is_none")]
    max_tokens: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tools: Option<Vec<serde_json::Value>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tool_choice: Option<String>,
    /// Provider-specific fields merged at the top level of the JSON body.
    #[serde(flatten)]
    extra: serde_json::Map<String, serde_json::Value>,
}
#[derive(Debug, Deserialize)]
struct ApiChatResponse {
    choices: Vec<Choice>,
    #[serde(default)]
    usage: Option<UsageInfo>,
    /// Telemetry fields below are permissive (`opt_field`): a present-but-
    /// wrong-typed value yields NULL instead of failing the envelope parse —
    /// telemetry must never break the request path.
    #[serde(default, deserialize_with = "opt_field")]
    system_fingerprint: Option<String>,
    /// Serving upstream provider — OpenRouter's top-level response field
    /// (undocumented in the API reference but consumed by OpenRouter's own
    /// SDK; empirically present incl. cache hits, where `openrouter_metadata`
    /// is stripped). NULL when the provider omits it.
    #[serde(default, deserialize_with = "opt_field")]
    provider: Option<String>,
}

#[derive(Debug, Deserialize)]
struct UsageInfo {
    #[serde(default)]
    prompt_tokens: Option<u64>,
    #[serde(default)]
    completion_tokens: Option<u64>,
    /// OpenRouter-normalized shape: `usage.prompt_tokens_details.cached_tokens`.
    #[serde(default)]
    prompt_tokens_details: Option<PromptTokensDetails>,
    /// DeepSeek-native shape: `usage.prompt_cache_hit_tokens` /
    /// `usage.prompt_cache_miss_tokens` (sum equals `prompt_tokens`).
    #[serde(default)]
    prompt_cache_hit_tokens: Option<u64>,
    #[serde(default)]
    prompt_cache_miss_tokens: Option<u64>,
    /// Billed cost from `usage.cost` — the invoice amount (OpenRouter-only).
    #[serde(default, deserialize_with = "opt_field")]
    cost: Option<f64>,
    /// Raw cost breakdown (OpenRouter only); any JSON value is accepted.
    /// `opt_field`-protected: a pathological value (e.g. `1e999`, which
    /// serde_json rejects as out of range) must not fail the envelope parse.
    #[serde(default, deserialize_with = "opt_field")]
    cost_details: Option<serde_json::Value>,
}

/// Permissive deserializer for optional telemetry fields: a present value of
/// the wrong type (provider shape drift) yields `None` instead of failing the
/// whole envelope parse. Missing and `null` also yield `None`.
#[allow(
    clippy::unnecessary_wraps,
    reason = "Result is the deserialize_with contract"
)]
fn opt_field<'de, D, T>(de: D) -> Result<Option<T>, D::Error>
where
    D: serde::Deserializer<'de>,
    T: serde::de::DeserializeOwned,
{
    Ok(serde_json::Value::deserialize(de)
        .ok()
        .and_then(|v| serde_json::from_value(v).ok()))
}

#[derive(Debug, Deserialize)]
struct PromptTokensDetails {
    #[serde(default)]
    cached_tokens: Option<u64>,
}

#[derive(Debug, Deserialize)]
struct Choice {
    message: ResponseMessage,
    #[serde(default)]
    finish_reason: Option<String>,
}

/// Remove `<think>...</think>` blocks from model output.
/// Some reasoning models (e.g. `MiniMax`) embed their chain-of-thought inline
/// in the `content` field rather than a separate `reasoning_content` field.
/// The resulting `<think>` tags must be stripped before returning to the user.

#[derive(Debug, Deserialize, Serialize)]
struct ResponseMessage {
    #[serde(default)]
    content: Option<String>,
    /// Reasoning/thinking models (e.g. Qwen3, GLM-4) may return their output
    /// in `reasoning_content` instead of `content`. Preserved on the response
    /// for replay/display — never promoted into visible text (see
    /// [`effective_content_optional`](Self::effective_content_optional)).
    #[serde(default)]
    reasoning_content: Option<String>,
    #[serde(default)]
    reasoning: Option<String>,
    #[serde(default)]
    reasoning_details: Option<serde_json::Value>,
    #[serde(default)]
    tool_calls: Option<Vec<ApiToolCall>>,
}

impl ResponseMessage {
    /// Extract the model's visible text content, stripping `<think>...</think>`
    /// blocks that some models (e.g. `MiniMax`) embed inline in `content`.
    ///
    /// There is NO reasoning fallback here: a reasoning-only response (empty
    /// content, no tool calls) stays empty so the agent loop can classify the
    /// class early and recover via bounded continuation (see
    /// [`crate::agent::Agent::recover_reasoning_only_stop`]) instead of
    /// surfacing chain-of-thought as the visible answer. Reasoning fields are
    /// preserved separately on the response for replay and display.
    fn effective_content_optional(&self) -> Option<String> {
        self.content
            .as_ref()
            .filter(|c| !c.is_empty())
            .and_then(|c| crate::providers::reasoning::strip_think_tags(c))
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub(crate) struct ApiToolCall {
    #[serde(skip_serializing_if = "Option::is_none")]
    id: Option<String>,
    #[serde(rename = "type")]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    kind: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    function: Option<ApiToolCallFunction>,

    // Compatibility: Some providers (e.g., older GLM) may use 'name' directly
    #[serde(default, skip_serializing_if = "Option::is_none")]
    name: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    arguments: Option<String>,

    // Compatibility: DeepSeek sometimes wraps arguments differently
    #[serde(
        rename = "parameters",
        default,
        skip_serializing_if = "Option::is_none"
    )]
    parameters: Option<serde_json::Value>,
}

/// Resolve tool-call name from `function.name` or top-level `name`.
/// Returns the first non-empty name, or `None` if both are absent/empty.
#[must_use]
pub(crate) fn resolve_tool_call_name(
    function_name: Option<&str>,
    direct_name: Option<&str>,
) -> Option<String> {
    function_name
        .filter(|n| !n.is_empty())
        .or_else(|| direct_name.filter(|n| !n.is_empty()))
        .map(String::from)
}

/// Resolve tool-call arguments from `function.arguments`, top-level `arguments`,
/// or the `parameters` field (DeepSeek compatibility where arguments arrive as an object).
/// Returns the first non-empty arguments string, or `None` if all are absent/empty.
#[must_use]
pub(crate) fn resolve_tool_call_arguments(
    function_arguments: Option<&str>,
    direct_arguments: Option<&str>,
    parameters: Option<&serde_json::Value>,
) -> Option<String> {
    if let Some(args) = function_arguments.filter(|a| !a.is_empty()) {
        return Some(args.to_string());
    }
    if let Some(args) = direct_arguments.filter(|a| !a.is_empty()) {
        return Some(args.to_string());
    }
    // Compatibility: Some providers return parameters as object instead of string
    parameters.and_then(|params| serde_json::to_string(params).ok())
}

impl ApiToolCall {
    /// Extract function name with fallback logic for various provider formats
    fn function_name(&self) -> Option<String> {
        resolve_tool_call_name(
            self.function.as_ref().and_then(|f| f.name.as_deref()),
            self.name.as_deref(),
        )
    }

    /// Extract arguments with fallback logic and type conversion
    fn function_arguments(&self) -> Option<String> {
        resolve_tool_call_arguments(
            self.function.as_ref().and_then(|f| f.arguments.as_deref()),
            self.arguments.as_deref(),
            self.parameters.as_ref(),
        )
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub(crate) struct ApiToolCallFunction {
    #[serde(default)]
    pub(crate) name: Option<String>,
    #[serde(default)]
    pub(crate) arguments: Option<String>,
}

#[derive(Debug, Serialize)]
struct NativeMessage {
    role: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    content: Option<MessageContent>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tool_call_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tool_calls: Option<Vec<ApiToolCall>>,
    /// Raw reasoning content from thinking models; pass-through for providers
    /// that require it in assistant tool-call history messages.
    #[serde(skip_serializing_if = "Option::is_none")]
    reasoning_content: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    reasoning: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    reasoning_details: Option<serde_json::Value>,
}

impl NativeMessage {
    #[cfg(test)]
    fn user(content: &str) -> Self {
        NativeMessage {
            role: "user".into(),
            content: Some(MessageContent::Text(content.into())),
            tool_call_id: None,
            tool_calls: None,
            reasoning_content: None,
            reasoning: None,
            reasoning_details: None,
        }
    }
}

// ── Message content types for API serialization ──

/// Parse `[IMAGE:path]` markers from content, returning cleaned text and extracted paths.
///
/// Uses the shared [`MEDIA_MARKER_RE`](crate::util::MEDIA_MARKER_RE) to find markers.
/// Non‑IMAGE markers (e.g. `[AUDIO:…]`) are left untouched in the cleaned text.
/// Empty `[IMAGE:]` markers are preserved verbatim.
#[must_use]
pub(crate) fn parse_image_markers(content: &str) -> (String, Vec<String>) {
    let mut refs: Vec<String> = Vec::new();

    let cleaned = crate::util::MEDIA_MARKER_RE
        .replace_all(content, |caps: &regex::Captures| {
            let (kind, path) = crate::util::parse_media_marker(caps);
            let path = path.trim();

            if kind == "IMAGE" {
                refs.push(path.to_string());
                // IMAGE markers are stripped — don't emit anything.
                String::new()
            } else {
                // AUDIO/VIDEO markers are preserved verbatim.
                caps.get_match().as_str().to_string()
            }
        })
        .to_string();

    (cleaned.trim().to_string(), refs)
}

#[derive(Debug, Serialize)]
#[serde(untagged)]
pub(crate) enum MessageContent {
    Text(String),
    Parts(Vec<MessagePart>),
    Null,
}

#[derive(Debug, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub(crate) enum MessagePart {
    Text { text: String },
    ImageUrl { image_url: ImageUrlPart },
}

#[derive(Debug, Serialize)]
pub(crate) struct ImageUrlPart {
    pub url: String,
}

/// Convert a role+content pair into the appropriate [`MessageContent`] variant.
///
/// When `allow_user_image_parts` is true and the role is [`ChatRole::User`], image markers
/// (e.g. `[IMAGE:data:image/png;base64,...]`) are parsed into [`MessagePart::ImageUrl`]
/// entries alongside the cleaned text. Otherwise the raw content is returned as
/// [`MessageContent::Text`].
///
/// The old estimator-side mirror of this marker handling
/// (`crate::session::estimate_tokens`) was removed with the
/// token-estimation heuristic — this conversion is now the only place
/// marker parsing lives.
pub(crate) fn to_message_content(
    role: ChatRole,
    content: &str,
    allow_user_image_parts: bool,
) -> MessageContent {
    if role != ChatRole::User || !allow_user_image_parts {
        return MessageContent::Text(content.to_string());
    }

    // Fast path: avoid regex work when there are no IMAGE markers at all.
    // All valid markers begin with "[IMAGE:" so a simple substring check is safe.
    if !content.contains("[IMAGE:") {
        return MessageContent::Text(content.to_string());
    }

    let (cleaned_text, image_refs) = parse_image_markers(content);
    if image_refs.is_empty() {
        return MessageContent::Text(content.to_string());
    }

    let mut parts = Vec::with_capacity(image_refs.len() + 1);
    let trimmed_text = cleaned_text.trim();
    if !trimmed_text.is_empty() {
        parts.push(MessagePart::Text {
            text: trimmed_text.to_string(),
        });
    }

    for image_ref in image_refs {
        parts.push(MessagePart::ImageUrl {
            image_url: ImageUrlPart { url: image_ref },
        });
    }

    MessageContent::Parts(parts)
}

impl OpenAiCompatibleProvider {
    fn convert_tool_specs(tools: Option<&[ToolSpec]>) -> Option<Vec<serde_json::Value>> {
        let items = tools?;
        let converted: Vec<_> = items
            .iter()
            .map(|tool| {
                let params = tool.parameters.clone();
                serde_json::json!({
                    "type": "function",
                    "function": {
                        "name": tool.name,
                        "description": tool.description,
                        "parameters": params,
                    }
                })
            })
            .collect();
        if converted.is_empty() {
            None
        } else {
            Some(converted)
        }
    }

    fn convert_messages_for_native(
        messages: &[ChatMessage],
        allow_user_image_parts: bool,
    ) -> Vec<NativeMessage> {
        messages
            .iter()
            .map(|message| {
                // Shared fields extracted from a `DecodedNativeHistoryMessage` used
                // to build provider-native message types.
                // Tool calls are returned as `Vec<ToolCall>` so each provider can convert them
                // to its own tool-call type.
                let decoded = crate::session::decode_native_history_message(message);
                let Some((role, content, tool_call_id, tool_calls, reasoning)) =
                    decoded.map(|msg| match msg {
                        crate::session::DecodedNativeHistoryMessage::Assistant {
                            content,
                            tool_calls,
                            reasoning,
                        } => (
                            ChatRole::Assistant.to_string(),
                            content,
                            None, // tool_call_id
                            tool_calls,
                            reasoning,
                        ),
                        crate::session::DecodedNativeHistoryMessage::ToolResult {
                            tool_call_id,
                            content,
                        } => (
                            ChatRole::Tool.to_string(),
                            Some(content),
                            Some(tool_call_id),
                            None, // tool_calls
                            None, // reasoning
                        ),
                    })
                else {
                    return NativeMessage {
                        role: message.role.to_string(),
                        content: Some(to_message_content(
                            message.role,
                            &message.content,
                            allow_user_image_parts,
                        )),
                        tool_call_id: None,
                        tool_calls: None,
                        reasoning: None,
                        reasoning_content: None,
                        reasoning_details: None,
                    };
                };
                let has_tool_calls = tool_calls.as_ref().is_some_and(|c| !c.is_empty());
                let (r_reasoning, r_content, r_details) =
                    reasoning_roundtrip::native_reasoning_triple_for_replay(
                        reasoning.as_ref(),
                        has_tool_calls,
                    );
                let tool_calls = tool_calls.map(|tc| {
                    tc.into_iter()
                        .map(|tc| ApiToolCall {
                            id: Some(tc.id),
                            kind: Some("function".to_string()),
                            function: Some(ApiToolCallFunction {
                                name: Some(tc.name),
                                arguments: Some(
                                    serde_json::to_string(&tc.arguments)
                                        .unwrap_or_else(|_| "{}".into()),
                                ),
                            }),
                            name: None,
                            arguments: None,
                            parameters: None,
                        })
                        .collect()
                });
                let has_reasoning =
                    r_content.is_some() || r_reasoning.is_some() || r_details.is_some();
                let content = match (&content, has_reasoning, has_tool_calls) {
                    (Some(s), _, _) => Some(MessageContent::Text(s.clone())),
                    (None, true, true) => Some(MessageContent::Null),
                    (None, true, false) => Some(MessageContent::Text(String::new())),
                    (None, false, _) => None,
                };
                NativeMessage {
                    role,
                    content,
                    tool_call_id,
                    tool_calls,
                    reasoning: r_reasoning,
                    reasoning_content: r_content,
                    reasoning_details: r_details,
                }
            })
            .collect()
    }
}

/// Parse tool-call arguments JSON with repair fallback and fallback to empty object on parse failure.
#[must_use]
fn parse_tool_call_arguments(name: &str, arguments: &str) -> serde_json::Value {
    serde_json::from_str(arguments).unwrap_or_else(|parse_err| {
        if let Some(value) = try_repair_json::<serde_json::Value>(arguments) {
            tracing::debug!(
                function = %name,
                original_error = %parse_err,
                "Repaired malformed JSON in tool-call arguments"
            );
            return value;
        }
        tracing::debug!(
            function = %name,
            arguments = %arguments,
            error = %parse_err,
            "Invalid JSON in tool-call arguments, using empty object"
        );
        serde_json::json!({})
    })
}

/// Shared helper to build a [`ProviderToolCall`] from parsed API tool-call data.
///
/// Delegates argument parsing to [`parse_tool_call_arguments`], which handles
/// JSON parsing with repair fallback, and generates a fallback ID when none is
/// provided.
#[must_use]
fn make_provider_tool_call(id: Option<String>, name: String, arguments: &str) -> ProviderToolCall {
    let arguments = parse_tool_call_arguments(&name, arguments);
    ProviderToolCall {
        id: id.unwrap_or_else(crate::generate_id),
        name,
        arguments,
    }
}

/// Normalize provider-reported cache tokens into `(cached, miss)`.
///
/// OpenRouter reports only the hit side (`prompt_tokens_details.cached_tokens`;
/// the miss side is computed as `prompt_tokens − cached`), while DeepSeek
/// reports both sides natively (`prompt_cache_hit_tokens` /
/// `prompt_cache_miss_tokens`). Native miss wins; otherwise the computed
/// miss is used when both operands are known.
#[must_use]
fn normalize_cache_tokens(
    cached_tokens: Option<u64>,
    hit_tokens: Option<u64>,
    miss_tokens: Option<u64>,
    prompt_tokens: Option<u64>,
) -> (Option<u64>, Option<u64>) {
    let cached = cached_tokens.or(hit_tokens);
    let miss = miss_tokens.or_else(|| match (cached, prompt_tokens) {
        (Some(c), Some(p)) => p.checked_sub(c),
        _ => None,
    });
    (cached, miss)
}

impl OpenAiCompatibleProvider {
    /// `finish_reason`, `upstream_provider`, `system_fingerprint` are three
    /// consecutive `Option<String>`s — keep call sites in envelope order.
    fn parse_native_response(
        message: ResponseMessage,
        usage: Option<ProviderUsage>,
        finish_reason: Option<String>,
        upstream_provider: Option<String>,
        system_fingerprint: Option<String>,
    ) -> ProviderChatResponse {
        let text = message.effective_content_optional();
        let reasoning = Reasoning::from_optional_parts(
            message.reasoning,
            message.reasoning_content,
            message.reasoning_details,
        );
        let tool_calls = message
            .tool_calls
            .unwrap_or_default()
            .into_iter()
            .filter_map(|tc| {
                let name = tc.function_name()?;
                let arguments = tc.function_arguments().unwrap_or_default();
                Some(make_provider_tool_call(tc.id, name, &arguments))
            })
            .collect::<Vec<_>>();

        ProviderChatResponse {
            text,
            tool_calls,
            usage,
            reasoning,
            finish_reason,
            upstream_provider,
            system_fingerprint,
        }
    }

    /// Shared success tail of [`Provider::chat`] and [`Provider::chat_scoped`]:
    /// usage mapping (incl. provider-reported cache tokens), first-choice
    /// extraction, native-response parsing, and the tool-turn debug log.
    /// `no_response` builds the error when no choice exists.
    fn finalize_response<E>(
        &self,
        model: &str,
        native_response: ApiChatResponse,
        no_response: impl FnOnce() -> E,
    ) -> Result<ProviderChatResponse, E> {
        let usage = native_response.usage.map(|u| {
            let (cached, miss) = normalize_cache_tokens(
                u.prompt_tokens_details
                    .as_ref()
                    .and_then(|d| d.cached_tokens),
                u.prompt_cache_hit_tokens,
                u.prompt_cache_miss_tokens,
                u.prompt_tokens,
            );
            ProviderUsage {
                input_tokens: u.prompt_tokens,
                output_tokens: u.completion_tokens,
                cached_input_tokens: cached,
                cache_miss_tokens: miss,
                cost: u.cost,
                cost_details: u.cost_details,
            }
        });
        let upstream_provider = native_response.provider.clone();
        let choice = native_response
            .choices
            .into_iter()
            .next()
            .ok_or_else(no_response)?;
        let finish_reason = choice.finish_reason;
        let message = choice.message;

        let result = Self::parse_native_response(
            message,
            usage,
            finish_reason,
            upstream_provider,
            native_response.system_fingerprint,
        );

        if !result.tool_calls.is_empty() && result.reasoning.is_none() {
            tracing::debug!(
                provider = %self.name,
                model,
                "tool turn: parsed response has no reasoning fields",
            );
        }

        Ok(result)
    }

    /// Build the HTTP request for [`Provider::chat`] / [`Provider::chat_scoped`].
    /// This function itself is synchronous; the caller sends the request asynchronously.
    ///
    /// The request BYTES are identical regardless of which client is used —
    /// the scoped client only differs in timeout semantics, never in payload.
    fn build_http_request_with_client(
        &self,
        client: &Client,
        request: &ProviderChatRequest,
    ) -> RequestBuilder {
        let native =
            Self::convert_messages_for_native(&request.messages, request.allow_image_parts);
        let tool_specs = Self::convert_tool_specs(request.tools.as_deref());

        let mut extra = serde_json::Map::new();

        // Provider routing — per-request values only; no global fallback.
        // If provider_order is present and non-empty, build the routing block.
        if let Some(order) = &request.provider_order
            && let Some(routing) = provider_routing_json(order)
        {
            extra.insert("provider".to_string(), routing);
        }

        // Reasoning effort
        if let Some(effort) = request
            .reasoning_effort
            .as_deref()
            .filter(|e| !e.is_empty())
        {
            extra.insert("reasoning_effort".to_string(), serde_json::json!(effort));
        }

        let payload = ChatCompletionRequest {
            model: request.model.clone(),
            messages: native,
            max_tokens: request.max_tokens,
            tool_choice: tool_specs.as_ref().map(|_| "auto".to_string()),
            tools: tool_specs,
            extra,
        };

        let url = ensure_chat_completions_url(&self.base_url);
        let builder = client.post(url).json(&payload);
        self.attach_auth_header(builder)
    }

    /// Attach the `Authorization: Bearer` header if a credential is configured.
    /// Returns the builder (with or without the header added) for chaining.
    fn attach_auth_header(&self, mut builder: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
        if let Some(ref credential) = self.credential {
            builder = builder.header("Authorization", format!("Bearer {credential}"));
        }
        builder
    }
}

/// Outcome of an idle-timeout body read.
enum BodyReadOutcome {
    /// Body fully read.
    Complete(Vec<u8>),
    /// Read failed partway (transport / idle timeout / shutdown).
    /// Carries the partial bytes plus a message for the error trail.
    Failed {
        partial: Vec<u8>,
        message: String,
        class: FailureClass,
    },
}

/// Read a response body chunk-by-chunk with an idle timeout that resets while
/// data flows, also bounding the whole read by `deadline`.
///
/// `idle_timeout` guards against a stalled connection mid-body — the
/// truncation signature this hardening targets. The read is shutdown-abortable
/// via [`crate::shutdown::race_shutdown`].
///
/// Every chunk wait is bounded by `min(idle_timeout, remaining_budget)`, so a
/// stalled chunk can never overshoot the operation deadline by more than the
/// scheduling latency between the timeout firing and the next check — the
/// wall-clock cap holds precisely for the body-read phase, not just the send
/// phase. When the tighter bound was the wall budget, the failure classifies
/// as [`FailureClass::WallClockExceeded`] rather than a truncation idle
/// timeout.
async fn read_body_idle(
    response: reqwest::Response,
    idle_timeout: Duration,
    deadline: Instant,
) -> BodyReadOutcome {
    let mut body = Vec::new();
    let mut stream = response.bytes_stream();
    loop {
        let remaining = deadline.saturating_duration_since(Instant::now());
        if remaining.is_zero() {
            return BodyReadOutcome::Failed {
                partial: body,
                message: "response body read exceeded remaining operation budget".to_string(),
                class: FailureClass::WallClockExceeded,
            };
        }
        let wait_bound = idle_timeout.min(remaining);
        let next_chunk = crate::shutdown::race_shutdown(stream.next());
        let chunk = match tokio::time::timeout(wait_bound, next_chunk).await {
            Err(_) => {
                if Instant::now() >= deadline {
                    // The tighter bound was the wall budget — classify as such,
                    // not as an idle timeout.
                    return BodyReadOutcome::Failed {
                        partial: body,
                        message: "response body read exceeded remaining operation budget"
                            .to_string(),
                        class: FailureClass::WallClockExceeded,
                    };
                }
                return BodyReadOutcome::Failed {
                    partial: body,
                    message: format!(
                        "response body read idle timeout after {idle_timeout:?} \
                         with no data flowing"
                    ),
                    class: FailureClass::TruncatedEnvelope,
                };
            }
            Ok(Err(_)) => {
                return BodyReadOutcome::Failed {
                    partial: body,
                    message: "shutdown during response body read".to_string(),
                    class: FailureClass::Shutdown,
                };
            }
            Ok(Ok(Some(chunk))) => chunk,
            Ok(Ok(None)) => break,
        };
        match chunk {
            Ok(bytes) => body.extend_from_slice(&bytes),
            Err(e) => {
                return BodyReadOutcome::Failed {
                    partial: body,
                    message: format!("{e}"),
                    class: FailureClass::TruncatedEnvelope,
                };
            }
        }
    }
    BodyReadOutcome::Complete(body)
}

/// Best-effort extraction of `finish_reason` from a possibly-truncated JSON
/// body.
///
/// A full `ApiChatResponse` parse is attempted first; if the envelope is cut
/// mid-body we fall back to lenient JSON value parsing so telemetry survives
/// where possible.
fn envelope_telemetry(body: &str) -> Option<String> {
    if let Ok(native) = serde_json::from_str::<ApiChatResponse>(body) {
        return native.choices.first().and_then(|c| c.finish_reason.clone());
    }
    // Lenient fallback for truncated envelopes.
    if let Ok(value) = serde_json::from_str::<serde_json::Value>(body) {
        return value
            .pointer("/choices/0/finish_reason")
            .and_then(serde_json::Value::as_str)
            .map(str::to_string);
    }
    None
}

#[async_trait]
impl Provider for OpenAiCompatibleProvider {
    async fn warmup(&self) -> anyhow::Result<()> {
        // Hit the chat completions URL with a GET to establish the connection pool.
        // The server will likely return 405 Method Not Allowed, which is fine -
        // the goal is TLS handshake and HTTP/2 negotiation.
        let url = ensure_chat_completions_url(&self.base_url);
        let builder = self.http_client().get(&url);
        let _ = self.attach_auth_header(builder).send().await?;
        Ok(())
    }

    /// Scoped single-attempt chat: one HTTP request, no
    /// provider-internal retries, idle-timeout body reads, per-attempt total
    /// bounded by the remaining operation deadline.
    async fn chat_scoped(
        &self,
        request: ProviderChatRequest,
        idle_timeout: Duration,
        deadline: Instant,
    ) -> Result<ProviderChatResponse, ScopedCallError> {
        let req_builder = self.build_http_request_with_client(self.http_client_scoped(), &request);
        let model = request.model;

        // ── Send — bounded by the idle timeout (TTFB) AND the remaining budget ──
        // A pre-header server stall must not consume the whole operation
        // budget on attempt 1 (burst-shaped recovery needs later attempts), so
        // the header wait is capped by `idle_timeout`; the remaining wall
        // budget remains the outer bound.
        let remaining = deadline.saturating_duration_since(Instant::now());
        let send_timeout = idle_timeout.min(remaining);
        let send_fut = crate::shutdown::race_shutdown(req_builder.send());
        let response = match tokio::time::timeout(send_timeout, send_fut).await {
            Err(_) => {
                let budget_expired = remaining <= idle_timeout;
                let err = if budget_expired {
                    anyhow::anyhow!("{} request exceeded remaining operation budget", self.name)
                } else {
                    anyhow::anyhow!(
                        "{} request timed out waiting for response headers \
                         (idle timeout {idle_timeout:?})",
                        self.name
                    )
                };
                let class = if budget_expired {
                    FailureClass::WallClockExceeded
                } else {
                    FailureClass::Transport
                };
                return Err(scoped_simple_error(err, class));
            }
            Ok(Err(_)) => {
                let err = anyhow::anyhow!("shutdown during request");
                return Err(scoped_simple_error(err, FailureClass::Shutdown));
            }
            Ok(Ok(Err(e))) => {
                let err = anyhow::Error::from(e).context(format!("{} transport error", self.name));
                return Err(scoped_simple_error(err, FailureClass::Transport));
            }
            Ok(Ok(Ok(resp))) => resp,
        };

        // ── Response metadata (telemetry) ──
        let content_length = response.content_length();

        if !response.status().is_success() {
            return Err(scoped_http_error(self, response, idle_timeout, deadline).await);
        }

        // ── Read body with idle timeout ──
        let (body_bytes, read_failure) =
            match read_body_idle(response, idle_timeout, deadline).await {
                BodyReadOutcome::Complete(bytes) => (bytes, None),
                BodyReadOutcome::Failed {
                    partial,
                    message,
                    class,
                } => (partial, Some((message, class))),
            };
        let body_str = String::from_utf8_lossy(&body_bytes).into_owned();
        let actual_len = body_bytes.len();

        if let Some((read_msg, class)) = read_failure {
            let err = anyhow::anyhow!("{} error reading response body: {read_msg}", self.name);
            return Err(scoped_metadata_error(err, class, &body_str, None));
        }

        let native_response: ApiChatResponse = match serde_json::from_str(&body_str) {
            Ok(native) => native,
            Err(e) => {
                // Content-length vs actual comparison: equal ⇒ the server
                // finalized a short body; shorter ⇒ framing anomaly (truncated
                // envelope — the same defect class as body-read errors).
                let truncated = content_length.is_some_and(|cl| cl != actual_len as u64);
                let class = if truncated {
                    FailureClass::TruncatedEnvelope
                } else {
                    FailureClass::Parse
                };
                let err = anyhow::anyhow!(
                    "{} chat completions parse error: {e}; body ({}): {:.500}",
                    self.name,
                    body_str.len(),
                    body_str
                );
                return Err(scoped_metadata_error(err, class, &body_str, None));
            }
        };

        self.finalize_response(&model, native_response, || {
            scoped_simple_error(
                anyhow::anyhow!("No response from {}", self.name),
                FailureClass::NoResponse,
            )
        })
    }
}

/// Build a scoped-call error with no envelope telemetry (send-phase failures).
fn scoped_simple_error(err: anyhow::Error, class: FailureClass) -> ScopedCallError {
    let record = RetryFailureRecord::new_simple(class, &err, None);
    ScopedCallError::new(err, record, class)
}

/// Build a scoped-call error with envelope telemetry (finish_reason).
fn scoped_metadata_error(
    err: anyhow::Error,
    class: FailureClass,
    body: &str,
    retry_after_ms: Option<u64>,
) -> ScopedCallError {
    let record =
        RetryFailureRecord::with_metadata(class, &err, envelope_telemetry(body), retry_after_ms);
    ScopedCallError::new(err, record, class)
}

/// Build a [`ScopedCallError`] from an HTTP error response (non-2xx).
///
/// Reads the error body with idle-timeout semantics so a stalled error-body
/// read cannot hang past the operation deadline, and classifies via the
/// provider error classifier.
async fn scoped_http_error(
    provider: &OpenAiCompatibleProvider,
    response: reqwest::Response,
    idle_timeout: Duration,
    deadline: Instant,
) -> ScopedCallError {
    let status = response.status().as_u16();
    let retry_after_ms = retry_after_header(response.headers());
    let (body_bytes, message) = match read_body_idle(response, idle_timeout, deadline).await {
        BodyReadOutcome::Complete(bytes) => (bytes, None),
        BodyReadOutcome::Failed {
            partial, message, ..
        } => (partial, Some(message)),
    };
    let body = String::from_utf8_lossy(&body_bytes).into_owned();

    let http_err = HttpError {
        status,
        body: body.clone(),
        context: provider.name.clone(),
    };
    let inner = anyhow::Error::from(http_err);
    let body_read_failed = message.is_some();
    let inner = match message {
        None => inner,
        Some(read_msg) => inner.context(format!(
            "{} error reading response body: {read_msg}",
            provider.name
        )),
    };

    let class = crate::providers::failure_class(
        crate::providers::reliable::classify_err(&inner),
        body_read_failed,
    );
    scoped_metadata_error(inner, class, &body, retry_after_ms)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::providers::test_request;

    #[tokio::test]
    async fn chat_without_key_attempts_request() {
        let p = OpenAiCompatibleProvider::new("Local", "http://127.0.0.1:1", None);
        let result = p
            .chat(test_request(vec![ChatMessage::user("hello")], None))
            .await;
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(
            !err_msg.contains("API key not set"),
            "should not get credential error, got: {err_msg}"
        );
    }

    #[test]
    #[expect(clippy::type_complexity)]
    fn resolve_tool_call_name_cases() {
        let cases: &[(&str, Option<&str>, Option<&str>, Option<&str>)] = &[
            (
                "function wins",
                Some("func_name"),
                Some("direct_name"),
                Some("func_name"),
            ),
            (
                "direct fallback",
                None,
                Some("direct_name"),
                Some("direct_name"),
            ),
            ("both none", None, None, None),
            (
                "empty function name",
                Some(""),
                Some("direct_name"),
                Some("direct_name"),
            ),
            (
                "empty direct name",
                Some("func_name"),
                Some(""),
                Some("func_name"),
            ),
            ("both empty", Some(""), Some(""), None),
        ];
        for (name, fn_name, direct_name, expected) in cases {
            assert_eq!(
                resolve_tool_call_name(*fn_name, *direct_name),
                expected.map(String::from),
                "{name}",
            );
        }
    }

    #[test]
    #[expect(clippy::type_complexity)]
    fn resolve_tool_call_arguments_cases() {
        let cases: &[(&str, Option<&str>, Option<&str>, Option<&str>, Option<&str>)] = &[
            (
                "function wins",
                Some(r#"{"key":"func_val"}"#),
                Some(r#"{"key":"direct_val"}"#),
                None,
                Some(r#"{"key":"func_val"}"#),
            ),
            (
                "direct fallback",
                None,
                Some(r#"{"key":"val"}"#),
                None,
                Some(r#"{"key":"val"}"#),
            ),
            ("both none", None, None, None, None),
            (
                "empty function args",
                Some(""),
                Some(r#"{"key":"val"}"#),
                None,
                Some(r#"{"key":"val"}"#),
            ),
            (
                "empty direct args",
                Some(r#"{"key":"val"}"#),
                Some(""),
                None,
                Some(r#"{"key":"val"}"#),
            ),
            ("both empty", Some(""), Some(""), None, None),
            (
                "parameters fallback",
                None,
                None,
                Some(r#"{"command":"pwd"}"#),
                Some(r#"{"command":"pwd"}"#),
            ),
            (
                "string fields empty with parameters",
                Some(""),
                Some(""),
                Some(r#"{"command":"ls"}"#),
                Some(r#"{"command":"ls"}"#),
            ),
            (
                "function wins over parameters",
                Some(r#"{"key":"val"}"#),
                None,
                Some(r#"{"query":"test"}"#),
                Some(r#"{"key":"val"}"#),
            ),
        ];
        for (name, fn_args, direct_args, params_json, expected) in cases {
            let params = params_json.map(|j| serde_json::from_str(j).expect(name));
            assert_eq!(
                resolve_tool_call_arguments(*fn_args, *direct_args, params.as_ref()),
                expected.map(String::from),
                "{name}",
            );
        }
    }

    // ----------------------------------------------------------
    // URL endpoint tests
    // ----------------------------------------------------------

    #[test]
    fn parse_native_response_preserves_tool_call_id() {
        let message = ResponseMessage {
            content: None,
            tool_calls: Some(vec![ApiToolCall {
                id: Some("call_123".to_string()),
                kind: Some("function".to_string()),
                function: Some(ApiToolCallFunction {
                    name: Some("shell".to_string()),
                    arguments: Some(r#"{"command":"pwd"}"#.to_string()),
                }),
                name: None,
                arguments: None,
                parameters: None,
            }]),
            reasoning_content: None,
            reasoning: None,
            reasoning_details: None,
        };

        let parsed =
            OpenAiCompatibleProvider::parse_native_response(message, None, None, None, None);
        assert_eq!(parsed.tool_calls.len(), 1);
        assert_eq!(parsed.tool_calls[0].id, "call_123");
        assert_eq!(parsed.tool_calls[0].name, "shell");
    }

    #[test]
    fn convert_messages_for_native_maps_tool_result_payload() {
        let input = vec![ChatMessage::tool_result("call_abc", "done")];

        let converted = OpenAiCompatibleProvider::convert_messages_for_native(&input, true);
        assert_eq!(converted[0].tool_call_id.as_deref(), Some("call_abc"));
        assert!(matches!(
            converted[0].content.as_ref(),
            Some(MessageContent::Text(value)) if value == "done"
        ));
    }

    #[test]
    fn convert_messages_for_native_keeps_user_image_markers_as_text_when_disabled() {
        let input = vec![ChatMessage::user(
            "System primer [IMAGE:data:image/png;base64,abcd] user turn",
        )];

        let converted = OpenAiCompatibleProvider::convert_messages_for_native(&input, false);
        assert_eq!(converted.len(), 1);
        assert_eq!(converted[0].role, "user");
        assert!(matches!(
            converted[0].content.as_ref(),
            Some(MessageContent::Text(value))
                if value == "System primer [IMAGE:data:image/png;base64,abcd] user turn"
        ));
    }

    #[test]
    fn effective_content_optional_never_promotes_reasoning() {
        // Empty content with reasoning → stays empty (the reasoning-only-stop
        // class is recovered by the agent loop, never promoted to visible text).
        let json = r#"{"choices":[{"message":{"content":"","reasoning_content":"Thinking output here"}}]}"#;
        let resp: ApiChatResponse = serde_json::from_str(json).unwrap();
        assert_eq!(
            resp.choices[0]
                .message
                .effective_content_optional()
                .unwrap_or_default(),
            ""
        );
        // Null content, reasoning present → stays empty
        let json =
            r#"{"choices":[{"message":{"content":null,"reasoning_content":"Fallback text"}}]}"#;
        let resp: ApiChatResponse = serde_json::from_str(json).unwrap();
        assert_eq!(
            resp.choices[0]
                .message
                .effective_content_optional()
                .unwrap_or_default(),
            ""
        );
        // Reasoning present but no content field at all → stays empty
        let json = r#"{"choices":[{"message":{"reasoning_content":"Only thinking"}}]}"#;
        let resp: ApiChatResponse = serde_json::from_str(json).unwrap();
        assert_eq!(
            resp.choices[0]
                .message
                .effective_content_optional()
                .unwrap_or_default(),
            ""
        );
        // Normal content, reasoning present → uses content (ignores reasoning)
        let json = r#"{"choices":[{"message":{"content":"Normal response","reasoning_content":"Should be ignored"}}]}"#;
        let resp: ApiChatResponse = serde_json::from_str(json).unwrap();
        assert_eq!(
            resp.choices[0]
                .message
                .effective_content_optional()
                .unwrap_or_default(),
            "Normal response"
        );
        // Content only think tags → empty (think tags are reasoning, not content)
        let json = r#"{"choices":[{"message":{"content":"<think>secret</think>","reasoning_content":"Fallback text"}}]}"#;
        let resp: ApiChatResponse = serde_json::from_str(json).unwrap();
        assert_eq!(
            resp.choices[0]
                .message
                .effective_content_optional()
                .unwrap_or_default(),
            ""
        );
        // Think tags plus visible text → visible text only
        let json =
            r#"{"choices":[{"message":{"content":"<think>secret</think>\nVisible answer"}}]}"#;
        let resp: ApiChatResponse = serde_json::from_str(json).unwrap();
        assert_eq!(
            resp.choices[0]
                .message
                .effective_content_optional()
                .unwrap_or_default(),
            "Visible answer"
        );
        // Both absent → empty
        let json = r#"{"choices":[{"message":{}}]}"#;
        let resp: ApiChatResponse = serde_json::from_str(json).unwrap();
        assert_eq!(
            resp.choices[0]
                .message
                .effective_content_optional()
                .unwrap_or_default(),
            ""
        );
        // Normal model without reasoning_content
        let json = r#"{"choices":[{"message":{"content":"Hello from Venice!"}}]}"#;
        let resp: ApiChatResponse = serde_json::from_str(json).unwrap();
        assert!(resp.choices[0].message.reasoning_content.is_none());
        assert_eq!(
            resp.choices[0]
                .message
                .effective_content_optional()
                .unwrap_or_default(),
            "Hello from Venice!"
        );
    }

    #[tokio::test]
    async fn warmup_without_key_attempts_connection() {
        let provider = OpenAiCompatibleProvider::new("test", "http://127.0.0.1:1", None);
        let result = provider.warmup().await;
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(
            !err_msg.contains("API key not set"),
            "should not get credential error, got: {err_msg}"
        );
    }

    // ────────────────────────────────────────────────────────────────────
    // Native tool calling tests
    // ────────────────────────────────────────────────────────────────────

    #[test]
    fn parse_image_markers_extracts_multiple_markers() {
        let input = "Check this [IMAGE:/tmp/a.png] and this [IMAGE:https://example.com/b.jpg]";
        let (cleaned, refs) = parse_image_markers(input);

        assert_eq!(cleaned, "Check this  and this");
        assert_eq!(refs.len(), 2);
        assert_eq!(refs[0], "/tmp/a.png");
        assert_eq!(refs[1], "https://example.com/b.jpg");
    }

    #[test]
    fn parse_image_markers_keeps_invalid_empty_marker() {
        let input = "hello [IMAGE:] world";
        let (cleaned, refs) = parse_image_markers(input);

        assert_eq!(cleaned, "hello [IMAGE:] world");
        assert!(refs.is_empty());
    }

    /// Stripping `[IMAGE:]` markers from history messages leaves only the text
    /// portion, which is the behaviour needed for non-vision providers.
    #[test]
    fn parse_image_markers_strips_markers_leaving_caption() {
        let input = "[IMAGE:/tmp/photo.jpg]\n\nDescribe this screenshot";
        let (cleaned, refs) = parse_image_markers(input);
        assert_eq!(cleaned, "Describe this screenshot");
        assert_eq!(refs.len(), 1);
        assert_eq!(refs[0], "/tmp/photo.jpg");
    }

    /// An image-only message (no caption) should produce an empty string after
    /// marker stripping, so callers can drop it from history.
    #[test]
    fn parse_image_markers_image_only_message_becomes_empty() {
        let input = "[IMAGE:/tmp/photo.jpg]";
        let (cleaned, refs) = parse_image_markers(input);
        assert!(
            cleaned.is_empty(),
            "expected empty string, got: {cleaned:?}"
        );
        assert_eq!(refs.len(), 1);
    }

    /// Non‑IMAGE markers (AUDIO, VIDEO) are preserved verbatim in the cleaned
    /// output while IMAGE markers are stripped. This test covers the mixed case
    /// to prevent regression of the preservation behaviour.
    #[test]
    fn parse_image_markers_preserves_audio_and_video_markers() {
        let input =
            "[AUDIO:/tmp/sound.mp3] Listen to this [VIDEO:/tmp/clip.mp4] and [IMAGE:/tmp/img.png]";
        let (cleaned, refs) = parse_image_markers(input);

        assert_eq!(
            cleaned,
            "[AUDIO:/tmp/sound.mp3] Listen to this [VIDEO:/tmp/clip.mp4] and"
        );
        assert_eq!(refs, vec!["/tmp/img.png"]);
    }

    #[test]
    fn to_message_content_converts_image_markers_to_openai_parts() {
        let content = "Describe this\n\n[IMAGE:data:image/png;base64,abcd]";
        let value =
            serde_json::to_value(to_message_content(ChatRole::User, content, true)).unwrap();
        let parts = value
            .as_array()
            .expect("multimodal content should be an array");
        assert_eq!(parts.len(), 2);
        assert_eq!(parts[0]["type"], "text");
        assert_eq!(parts[0]["text"], "Describe this");
        assert_eq!(parts[1]["type"], "image_url");
        assert_eq!(parts[1]["image_url"]["url"], "data:image/png;base64,abcd");
    }

    #[test]
    fn to_message_content_keeps_markers_as_text_when_user_image_parts_disabled() {
        let content = "Policy [IMAGE:data:image/png;base64,abcd]";
        let value =
            serde_json::to_value(to_message_content(ChatRole::User, content, false)).unwrap();
        assert_eq!(value, serde_json::json!(content));
    }

    #[test]
    fn to_message_content_keeps_plain_text_for_non_user_roles() {
        let value = serde_json::to_value(to_message_content(
            ChatRole::System,
            "You are a helpful assistant.",
            true,
        ))
        .unwrap();
        assert_eq!(value, serde_json::json!("You are a helpful assistant."));
    }

    #[test]
    fn request_serializes_with_tools() {
        let tools = vec![serde_json::json!({
            "type": "function",
            "function": {
                "name": "get_weather",
                "description": "Get weather for a location",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "location": {"type": "string"}
                    }
                }
            }
        })];

        let req = ChatCompletionRequest {
            model: "test-model".to_string(),
            messages: vec![NativeMessage::user("What is the weather?")],
            max_tokens: Some(32000),
            tools: Some(tools),
            tool_choice: Some("auto".to_string()),
            extra: serde_json::Map::new(),
        };
        let json = serde_json::to_string(&req).unwrap();
        assert!(json.contains("\"tools\""));
        assert!(json.contains("get_weather"));
        assert!(json.contains("\"tool_choice\":\"auto\""));
    }

    #[test]
    fn response_with_tool_calls_deserializes() {
        let json = r#"{
            "choices": [{
                "message": {
                    "content": null,
                    "tool_calls": [{
                        "type": "function",
                        "function": {
                            "name": "get_weather",
                            "arguments": "{\"location\":\"London\"}"
                        }
                    }]
                }
            }]
        }"#;

        let resp: ApiChatResponse = serde_json::from_str(json).unwrap();
        let msg = &resp.choices[0].message;
        assert!(msg.content.is_none());
        let tool_calls = msg.tool_calls.as_ref().unwrap();
        assert_eq!(tool_calls.len(), 1);
        assert_eq!(
            tool_calls[0].function.as_ref().unwrap().name.as_deref(),
            Some("get_weather")
        );
        assert_eq!(
            tool_calls[0]
                .function
                .as_ref()
                .unwrap()
                .arguments
                .as_deref(),
            Some("{\"location\":\"London\"}")
        );
    }

    #[test]
    fn response_with_multiple_tool_calls() {
        let json = r#"{
            "choices": [{
                "message": {
                    "content": "I'll check both.",
                    "tool_calls": [
                        {
                            "type": "function",
                            "function": {
                                "name": "get_weather",
                                "arguments": "{\"location\":\"London\"}"
                            }
                        },
                        {
                            "type": "function",
                            "function": {
                                "name": "get_time",
                                "arguments": "{\"timezone\":\"UTC\"}"
                            }
                        }
                    ]
                }
            }]
        }"#;

        let resp: ApiChatResponse = serde_json::from_str(json).unwrap();
        let msg = &resp.choices[0].message;
        assert_eq!(msg.content.as_deref(), Some("I'll check both."));
        let tool_calls = msg.tool_calls.as_ref().unwrap();
        assert_eq!(tool_calls.len(), 2);
        assert_eq!(
            tool_calls[0].function.as_ref().unwrap().name.as_deref(),
            Some("get_weather")
        );
        assert_eq!(
            tool_calls[1].function.as_ref().unwrap().name.as_deref(),
            Some("get_time")
        );
    }

    #[test]
    fn response_with_no_tool_calls_has_empty_vec() {
        let json = r#"{"choices":[{"message":{"content":"Just text, no tools."}}]}"#;
        let resp: ApiChatResponse = serde_json::from_str(json).unwrap();
        let msg = &resp.choices[0].message;
        assert_eq!(msg.content.as_deref(), Some("Just text, no tools."));
        assert!(msg.tool_calls.is_none());
    }
    #[test]
    fn api_response_parses_usage() {
        let json = r#"{
            "choices": [{"message": {"content": "Hello"}}],
            "usage": {"prompt_tokens": 150, "completion_tokens": 60}
        }"#;
        let resp: ApiChatResponse = serde_json::from_str(json).unwrap();
        let usage = resp.usage.unwrap();
        assert_eq!(usage.prompt_tokens, Some(150));
        assert_eq!(usage.completion_tokens, Some(60));
    }

    #[test]
    fn cache_tokens_normalized_for_both_provider_shapes() {
        // OpenRouter: only cached_tokens reported — miss is computed.
        let (cached, miss) = normalize_cache_tokens(Some(90), None, None, Some(150));
        assert_eq!(cached, Some(90));
        assert_eq!(miss, Some(60));
        // DeepSeek: both sides native — no computation needed.
        let (cached, miss) = normalize_cache_tokens(None, Some(90), Some(60), Some(150));
        assert_eq!(cached, Some(90));
        assert_eq!(miss, Some(60));
        // Unknown prompt total — miss stays unknown.
        let (cached, miss) = normalize_cache_tokens(Some(90), None, None, None);
        assert_eq!(cached, Some(90));
        assert_eq!(miss, None);
    }

    #[test]
    fn api_response_parses_cached_tokens() {
        // OpenRouter-shaped usage with prompt_tokens_details.cached_tokens.
        let json = r#"{
            "choices": [{"message": {"content": "Hello"}}],
            "usage": {
                "prompt_tokens": 150,
                "completion_tokens": 60,
                "prompt_tokens_details": {"cached_tokens": 90}
            }
        }"#;
        let resp: ApiChatResponse = serde_json::from_str(json).unwrap();
        let usage = resp.usage.unwrap();
        assert_eq!(usage.prompt_tokens_details.unwrap().cached_tokens, Some(90));
    }

    #[test]
    fn api_response_parses_without_usage() {
        let json = r#"{"choices": [{"message": {"content": "Hello"}}]}"#;
        let resp: ApiChatResponse = serde_json::from_str(json).unwrap();
        assert!(resp.usage.is_none());
    }

    #[test]
    fn api_response_parses_cost_fingerprint_and_upstream_provider() {
        // OpenRouter envelope with billed cost, system_fingerprint and the
        // top-level serving provider.
        let json = r#"{
            "choices": [{"message": {"content": "Hello"}}],
            "provider": "DeepSeek",
            "system_fingerprint": "fp_44709d6fcb",
            "usage": {
                "prompt_tokens": 150,
                "completion_tokens": 60,
                "cost": 0.0012,
                "cost_details": {
                    "upstream_inference_prompt_cost": 0.0008,
                    "upstream_inference_completions_cost": 0.0004
                }
            }
        }"#;
        let resp: ApiChatResponse = serde_json::from_str(json).unwrap();
        assert_eq!(resp.system_fingerprint.as_deref(), Some("fp_44709d6fcb"));
        assert_eq!(resp.provider.as_deref(), Some("DeepSeek"));
        let usage = resp.usage.unwrap();
        assert!((usage.cost.expect("cost") - 0.0012).abs() < 1e-12);
        // Parse→re-serialize normalizes key order (BTreeMap) — the stored
        // TEXT is reference-only; SQL slicing uses cost REAL.
        assert_eq!(
            usage.cost_details.as_ref().map(serde_json::Value::to_string),
            Some(
                r#"{"upstream_inference_completions_cost":0.0004,"upstream_inference_prompt_cost":0.0008}"#
                    .to_string()
            )
        );
        assert_eq!(
            usage
                .cost_details
                .expect("cost_details")
                .pointer("/upstream_inference_prompt_cost")
                .and_then(serde_json::Value::as_f64),
            Some(0.0008)
        );
    }

    #[test]
    fn upstream_provider_present_on_cache_hit() {
        // Cache hits strip openrouter_metadata, but the top-level `provider`
        // field survives — the eviction/backend-switch rows this telemetry
        // exists to attribute must not be NULL.
        let json = r#"{
            "choices": [{"message": {"content": "cached answer"}}],
            "provider": "Friendli",
            "system_fingerprint": "fp_44709d6fcb",
            "usage": {
                "prompt_tokens": 8,
                "completion_tokens": 4,
                "prompt_tokens_details": {"cached_tokens": 8},
                "cost": 0.0001
            }
        }"#;
        let resp: ApiChatResponse = serde_json::from_str(json).unwrap();
        assert_eq!(resp.provider.as_deref(), Some("Friendli"));
        // Missing/absent provider stays None (NULL) — non-OpenRouter endpoints.
        let resp: ApiChatResponse =
            serde_json::from_str(r#"{"choices":[{"message":{"content":"x"}}]}"#).unwrap();
        assert!(resp.provider.is_none());
    }

    #[test]
    fn wrong_typed_telemetry_fields_do_not_break_envelope_parse() {
        // Provider shape drift (system_fingerprint as number, cost as string,
        // provider as number) must yield NULL — not a parse failure
        // that would retry an otherwise-successful response.
        let json = r#"{
            "choices": [{"message": {"content": "Hello"}}],
            "provider": 42,
            "system_fingerprint": 42,
            "usage": {"cost": "not-a-number", "cost_details": [1, 2]}
        }"#;
        let resp: ApiChatResponse = serde_json::from_str(json).unwrap();
        assert!(resp.system_fingerprint.is_none());
        assert!(resp.provider.is_none());
        let usage = resp.usage.unwrap();
        assert!(usage.cost.is_none());
        assert!(usage.cost_details.is_some(), "Value accepts any JSON");
        // Missing fields stay None too.
        let resp: ApiChatResponse =
            serde_json::from_str(r#"{"choices":[{"message":{"content":"x"}}]}"#).unwrap();
        assert!(resp.system_fingerprint.is_none());
        assert!(resp.provider.is_none());
        assert!(resp.usage.is_none());
    }

    #[test]
    fn pathological_cost_details_number_does_not_break_envelope_parse() {
        // serde_json rejects `1e999` as an out-of-range number — without the
        // `opt_field` gate on cost_details this would fail the whole envelope
        // parse and retry on byte-identical bytes.
        let json = r#"{
            "choices": [{"message": {"content": "Hello"}}],
            "usage": {"cost_details": {"upstream_inference_prompt_cost": 1e999}}
        }"#;
        let resp: ApiChatResponse = serde_json::from_str(json).unwrap();
        let usage = resp.usage.unwrap();
        assert!(usage.cost_details.is_none());
        assert!(usage.cost.is_none());
    }

    // ─────────────────────────────────────────────────────────────────────
    // reasoning_content pass-through tests
    // ─────────────────────────────────────────────────────────────────────

    #[test]
    fn parse_native_response_captures_reasoning_content() {
        let message = ResponseMessage {
            content: Some("answer".to_string()),
            reasoning_content: Some("thinking step".to_string()),
            reasoning: None,
            reasoning_details: None,
            tool_calls: Some(vec![ApiToolCall {
                id: Some("call_1".to_string()),
                kind: Some("function".to_string()),
                function: Some(ApiToolCallFunction {
                    name: Some("shell".to_string()),
                    arguments: Some(r#"{"cmd":"ls"}"#.to_string()),
                }),
                name: None,
                arguments: None,
                parameters: None,
            }]),
        };

        let parsed =
            OpenAiCompatibleProvider::parse_native_response(message, None, None, None, None);
        let rc = parsed
            .reasoning
            .as_ref()
            .and_then(|r| r.reasoning_content.clone());
        assert_eq!(rc.as_deref(), Some("thinking step"));
        assert_eq!(parsed.text.as_deref(), Some("answer"));
        assert_eq!(parsed.tool_calls.len(), 1);
    }

    #[test]
    fn parse_native_response_none_reasoning_content_for_normal_model() {
        let message = ResponseMessage {
            content: Some("hello".to_string()),
            reasoning_content: None,
            reasoning: None,
            reasoning_details: None,
            tool_calls: None,
        };

        let parsed =
            OpenAiCompatibleProvider::parse_native_response(message, None, None, None, None);
        assert!(parsed.reasoning.is_none());
        assert_eq!(parsed.text.as_deref(), Some("hello"));
    }

    #[test]
    fn convert_messages_for_native_round_trips_reasoning_content() {
        // Simulate stored assistant history JSON that includes reasoning_content
        let history_json = serde_json::json!({
            "content": "I will check",
            "tool_calls": [{
                "id": "tc_1",
                "name": "shell",
                "arguments": "{\"cmd\":\"ls\"}"
            }],
            "reasoning_content": "Let me think about this..."
        });

        let messages = vec![ChatMessage::assistant(history_json.to_string())];
        let native = OpenAiCompatibleProvider::convert_messages_for_native(&messages, true);
        assert_eq!(native.len(), 1);
        assert_eq!(native[0].role, "assistant");
        assert_eq!(
            native[0].reasoning_content.as_deref(),
            Some("Let me think about this...")
        );
        assert!(native[0].tool_calls.is_some());
    }

    #[test]
    fn convert_messages_for_native_no_reasoning_content_when_absent() {
        // Normal model history without reasoning_content key
        let history_json = serde_json::json!({
            "content": "I will check",
            "tool_calls": [{
                "id": "tc_1",
                "name": "shell",
                "arguments": "{\"cmd\":\"ls\"}"
            }]
        });

        let messages = vec![ChatMessage::assistant(history_json.to_string())];
        let native = OpenAiCompatibleProvider::convert_messages_for_native(&messages, true);
        assert_eq!(native.len(), 1);
        assert!(native[0].reasoning_content.is_none());
    }

    #[test]
    fn convert_messages_for_native_synthesizes_reasoning_content_from_details_for_tool_calls() {
        let details = serde_json::json!([
            {"type": "reasoning.text", "text": "from details", "format": "x", "index": 0}
        ]);
        let history_json = serde_json::json!({
            "content": "I will check",
            "tool_calls": [{
                "id": "tc_1",
                "name": "shell",
                "arguments": "{\"cmd\":\"ls\"}"
            }],
            "reasoning_details": details.clone(),
        });

        let messages = vec![ChatMessage::assistant(history_json.to_string())];
        let native = OpenAiCompatibleProvider::convert_messages_for_native(&messages, true);
        assert_eq!(native.len(), 1);
        assert_eq!(native[0].reasoning_content.as_deref(), Some("from details"));
        assert_eq!(native[0].reasoning_details.as_ref(), Some(&details));
    }
}