edgequake-llm 0.6.14

Multi-provider LLM abstraction library with caching, rate limiting, and cost tracking
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
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
//! Mistral AI Provider
//!
//! @implements FEAT-007: Mistral AI provider (chat, embeddings, list-models)
//! Closes <https://github.com/raphaelmansuy/edgequake-llm/issues/7>
//!
//! # Overview
//!
//! This provider integrates with the Mistral AI API (`api.mistral.ai`) and exposes:
//! - **Chat completions** (sync + SSE streaming, tool/function calling, JSON mode)
//! - **Embeddings** (`mistral-embed`, 1024-dimensional)
//! - **Model listing** (`GET /v1/models`)
//!
//! The chat/tool path is delegated to [`OpenAICompatibleProvider`] because Mistral's
//! `/v1/chat/completions` is fully OpenAI-compatible.  Embeddings are implemented
//! natively because `OpenAICompatibleProvider::embed` is not yet wired up.
//!
//! ```text
//! ┌───────────────────────────────────────────────────────────────────────────┐
//! │                      MistralProvider architecture                         │
//! ├───────────────────────────────────────────────────────────────────────────┤
//! │                                                                           │
//! │   User Request                                                            │
//! │        │                                                                  │
//! │        ▼                                                                  │
//! │   ┌────────────────┐    chat/tools    ┌───────────────────────────────┐  │
//! │   │ MistralProvider│ ───────────────► │ OpenAICompatibleProvider      │  │
//! │   │ (wrapper)      │                  │ POST /v1/chat/completions      │  │
//! │   │                │ ◄─────────────── │ (SSE streaming + tool calls)  │  │
//! │   │                │                  └───────────────────────────────┘  │
//! │   │                │    embeddings    ┌───────────────────────────────┐  │
//! │   │                │ ───────────────► │ reqwest POST /v1/embeddings   │  │
//! │   │                │                  │ (native implementation)       │  │
//! │   │                │ ◄─────────────── └───────────────────────────────┘  │
//! │   │                │    list models   ┌───────────────────────────────┐  │
//! │   │                │ ───────────────► │ reqwest GET  /v1/models       │  │
//! │   └────────────────┘ ◄─────────────── └───────────────────────────────┘  │
//! │                                                                           │
//! └───────────────────────────────────────────────────────────────────────────┘
//! ```
//!
//! # Environment Variables
//!
//! | Variable | Required | Default | Description |
//! |----------|----------|---------|-------------|
//! | `MISTRAL_API_KEY` | ✅ Yes | - | API key from console.mistral.ai |
//! | `MISTRAL_MODEL` | ❌ No | `mistral-small-latest` | Default chat model |
//! | `MISTRAL_EMBEDDING_MODEL` | ❌ No | `mistral-embed` | Default embedding model |
//! | `MISTRAL_BASE_URL` | ❌ No | `https://api.mistral.ai/v1` | Endpoint override |
//!
//! # Available Chat Models (April 2026)
//!
//! | Model alias | Snapshot | Context | Vision | FC |
//! |-------------|----------|---------|--------|----|
//! | `mistral-large-latest` | 2512 (Large 3) | 256 K | ✓ | ✓ |
//! | `mistral-medium-latest` | 2508 (Med 3.1) | 128 K | ✓ | ✓ |
//! | `mistral-small-latest` | 2603 (Small 4) | 256 K | ✓ | ✓ |
//! | `magistral-medium-latest` | Med 1.2 (reasoning) | 128 K | | ✓ |
//! | `magistral-small-latest` | Sm 1.2 (reasoning) | 128 K | | ✓ |
//! | `codestral-latest` | 2508 | 256 K | | |
//! | `devstral-latest` | 2512 | 128 K | | ✓ |
//! | `devstral-small-latest` | | 128 K | | ✓ |
//! | `ministral-3b-latest` | 3B | 128 K | ✓ | ✓ |
//! | `ministral-8b-latest` | 8B | 128 K | ✓ | ✓ |
//! | `ministral-14b-latest` | 14B | 128 K | ✓ | ✓ |
//! | `open-mistral-nemo` | Nemo 12B | 128 K | | ✓ |
//! | `mistral-embed` | — | 8 K | | |
//!
//! # Quick start
//!
//! ```bash
//! export MISTRAL_API_KEY=your-key
//! cargo run --example mistral_chat
//! ```
//!
//! ```rust,no_run
//! use edgequake_llm::{MistralProvider, LLMProvider};
//! use edgequake_llm::traits::ChatMessage;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let provider = MistralProvider::from_env()?;
//! let messages = vec![ChatMessage::user("Hello from Mistral!")];
//! let resp = provider.chat(&messages, None).await?;
//! println!("{}", resp.content);
//! # Ok(())
//! # }
//! ```

use async_trait::async_trait;
use futures::stream::BoxStream;
use reqwest::{multipart, Client};
use serde::{Deserialize, Serialize};
use std::time::Duration;
use tracing::debug;

use crate::error::{LlmError, Result};
use crate::model_config::{
    ModelCapabilities, ModelCard, ModelType, ProviderConfig, ProviderType as ConfigProviderType,
};
use crate::providers::openai_compatible::OpenAICompatibleProvider;
use crate::traits::StreamChunk;
use crate::traits::{
    ChatMessage, CompletionOptions, EmbeddingProvider, LLMProvider, LLMResponse, ToolChoice,
    ToolDefinition,
};

// ============================================================================
// Constants
// ============================================================================

/// Default Mistral API base URL (includes /v1 prefix for OpenAI compatibility)
const MISTRAL_BASE_URL: &str = "https://api.mistral.ai/v1";

/// Default chat model
const MISTRAL_DEFAULT_MODEL: &str = "mistral-small-latest";

/// Default embedding model
const MISTRAL_DEFAULT_EMBEDDING_MODEL: &str = "mistral-embed";

/// Maximum output tokens for standard Mistral chat models.
///
/// Mistral Large 3 and most frontier models support up to 16 384 output tokens.
/// Models that do not have a specific limit advertised default to 4 096.
const MISTRAL_DEFAULT_MAX_OUTPUT_TOKENS: usize = 4096;

/// Maximum output tokens for larger frontier models (Large / Medium series).
const MISTRAL_FRONTIER_MAX_OUTPUT_TOKENS: usize = 16384;
const MISTRAL_EMBED_DIMENSION: usize = 1024;

/// Maximum tokens for `mistral-embed`
const MISTRAL_EMBED_MAX_TOKENS: usize = 8192;

/// Provider display name
const MISTRAL_PROVIDER_NAME: &str = "mistral";

/// Mistral chat model catalog: (id, display_name, context_length, supports_vision, supports_function_calling)
///
/// Context lengths are sourced from official Mistral documentation (docs.mistral.ai, April 2026).
/// 256 K  = 262_144 tokens, 128 K = 131_072 tokens, 32 K = 32_768 tokens.
///
/// "-latest" aliases always point to the newest stable snapshot.
const MISTRAL_CHAT_MODELS: &[(&str, &str, usize, bool, bool)] = &[
    // ----------------------------------------------------------------
    // Premier frontier — general purpose
    // ----------------------------------------------------------------
    (
        "mistral-large-latest", // → mistral-large-2512 (Mistral Large 3)
        "Mistral Large 3 (latest)",
        262_144, // 256 K
        true,    // vision
        true,    // function calling
    ),
    (
        "mistral-large-2512",
        "Mistral Large 3 (2512)",
        262_144,
        true,
        true,
    ),
    (
        "mistral-medium-latest", // → mistral-medium-2508 (Mistral Medium 3.1)
        "Mistral Medium 3.1 (latest)",
        131_072, // 128 K
        true,
        true,
    ),
    (
        "mistral-medium-2508",
        "Mistral Medium 3.1 (2508)",
        131_072,
        true,
        true,
    ),
    (
        "mistral-small-latest", // → mistral-small-2603 (Mistral Small 4)
        "Mistral Small 4 (latest)",
        262_144, // 256 K
        true,
        true,
    ),
    (
        "mistral-small-2603",
        "Mistral Small 4 (2603)",
        262_144,
        true,
        true,
    ),
    // ----------------------------------------------------------------
    // Reasoning models (Magistral series)
    // ----------------------------------------------------------------
    (
        "magistral-medium-latest", // → Magistral Medium 1.2
        "Magistral Medium 1.2 (latest)",
        131_072,
        false,
        true,
    ),
    (
        "magistral-small-latest", // → Magistral Small 1.2
        "Magistral Small 1.2 (latest)",
        131_072,
        false,
        true,
    ),
    // ----------------------------------------------------------------
    // Code models
    // ----------------------------------------------------------------
    (
        "codestral-latest", // → codestral-2508
        "Codestral (latest)",
        262_144, // 256 K
        false,
        false, // FIM model; tool calling not supported on chat path
    ),
    ("codestral-2508", "Codestral 2508", 262_144, false, false),
    // ----------------------------------------------------------------
    // Code agent models (Devstral series)
    // ----------------------------------------------------------------
    (
        "devstral-latest", // → devstral-2512 (Devstral 2)
        "Devstral 2 (latest)",
        131_072,
        false,
        true,
    ),
    (
        "devstral-small-latest",
        "Devstral Small (latest)",
        131_072,
        false,
        true,
    ),
    // ----------------------------------------------------------------
    // Ministral edge / on-device models
    // ----------------------------------------------------------------
    (
        "ministral-3b-latest",
        "Ministral 3 3B (latest)",
        131_072,
        true,
        true,
    ),
    (
        "ministral-8b-latest",
        "Ministral 3 8B (latest)",
        131_072,
        true,
        true,
    ),
    (
        "ministral-14b-latest",
        "Ministral 3 14B (latest)",
        131_072,
        true,
        true,
    ),
    // ----------------------------------------------------------------
    // Open-weights (now available under new aliases)
    // ----------------------------------------------------------------
    (
        "open-mistral-nemo", // Mistral Nemo 12B — still served
        "Mistral Nemo 12B (open weights)",
        131_072,
        false,
        true,
    ),
    // ----------------------------------------------------------------
    // Legacy / deprecated — kept for backward compatibility only.
    // New code should prefer "-latest" aliases above.
    // ----------------------------------------------------------------
    (
        "mistral-small-2506", // Mistral Small 3.2 snapshot
        "Mistral Small 3.2 (2506, legacy)",
        131_072,
        false,
        true,
    ),
    (
        "mistral-large-2411", // Mistral Large 2.1 snapshot (deprecated)
        "Mistral Large 2411 (deprecated)",
        131_072,
        false,
        true,
    ),
    (
        "pixtral-large-2411", // Pixtral Large snapshot (deprecated)
        "Pixtral Large 2411 (deprecated)",
        131_072,
        true,
        true,
    ),
    (
        "pixtral-12b-2409", // Pixtral 12B snapshot (deprecated)
        "Pixtral 12B 2409 (deprecated)",
        131_072,
        true,
        true,
    ),
    (
        "open-mistral-7b",
        "Mistral 7B (open weights, deprecated)",
        32_768,
        false,
        false,
    ),
    (
        "open-mixtral-8x7b",
        "Mixtral 8x7B (open weights, deprecated)",
        32_768,
        false,
        true,
    ),
    (
        "open-mixtral-8x22b",
        "Mixtral 8x22B (open weights, deprecated)",
        65_536,
        false,
        true,
    ),
    (
        "codestral-2501",
        "Codestral 2501 (deprecated)",
        262_144,
        false,
        false,
    ),
    (
        "mistral-small-2501", // Mistral Small 3.0 snapshot (deprecated)
        "Mistral Small 2501 (deprecated)",
        32_768,
        false,
        true,
    ),
];

// ============================================================================
// Embedding request/response structs (native implementation)
// ============================================================================

/// Request body for `POST /v1/embeddings`
#[derive(Debug, Serialize)]
struct EmbeddingRequest<'a> {
    model: &'a str,
    input: &'a [String],
    /// Output format for embedding vectors.
    /// - `"float"` (default): standard 32-bit float arrays.
    /// - `"base64"`: base64-encoded arrays (more compact for large batches).
    #[serde(skip_serializing_if = "Option::is_none")]
    encoding_format: Option<&'a str>,
}

/// Response from `/v1/embeddings`
#[derive(Debug, Deserialize)]
struct EmbeddingResponse {
    data: Vec<EmbeddingData>,
}

#[derive(Debug, Deserialize)]
struct EmbeddingData {
    embedding: Vec<f32>,
    index: usize,
}

// ============================================================================
// Audio / OCR request-response structs (native implementation)
// ============================================================================

/// Request body for `POST /v1/audio/speech`.
#[derive(Debug, Serialize)]
pub struct MistralSpeechRequest<'a> {
    /// TTS model ID (e.g. `voxtral-mini-tts-latest`).
    pub model: &'a str,
    /// Text prompt to synthesize into speech.
    pub input: &'a str,
    /// Optional voice ID from `/v1/audio/voices`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub voice_id: Option<&'a str>,
    /// Optional base64 reference audio for voice cloning.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ref_audio: Option<&'a str>,
    /// Output codec (`pcm`, `wav`, `mp3`, `flac`, `opus`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_format: Option<&'a str>,
    /// SSE streaming toggle (false by default).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream: Option<bool>,
}

#[derive(Debug, Deserialize)]
pub struct MistralSpeechResponse {
    /// Base64-encoded audio payload.
    pub audio_data: String,
}

/// Request body for `POST /v1/audio/transcriptions`.
#[derive(Debug, Serialize)]
pub struct MistralTranscriptionRequest<'a> {
    /// Transcription model ID (e.g. `voxtral-mini-transcribe-26-02`).
    pub model: &'a str,
    /// Public URL to the audio file.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file_url: Option<&'a str>,
    /// File id previously uploaded to `/v1/files`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file_id: Option<&'a str>,
    /// Optional language hint (e.g. `en`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub language: Option<&'a str>,
    /// SSE streaming toggle.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream: Option<bool>,
}

#[derive(Debug, Deserialize)]
pub struct MistralTranscriptionResponse {
    pub text: String,
    pub language: Option<String>,
    pub model: Option<String>,
}

/// Request body for `POST /v1/ocr` with document URL.
#[derive(Debug, Serialize)]
pub struct MistralOcrRequest<'a> {
    /// OCR model ID (e.g. `mistral-ocr-latest`).
    pub model: &'a str,
    pub document: MistralOcrDocument<'a>,
}

#[derive(Debug, Serialize)]
pub struct MistralOcrDocument<'a> {
    #[serde(rename = "type")]
    pub doc_type: &'a str,
    #[serde(rename = "document_url")]
    pub document_url: &'a str,
}

#[derive(Debug, Deserialize)]
pub struct MistralOcrResponse {
    pub model: Option<String>,
    #[serde(default)]
    pub pages: Vec<MistralOcrPage>,
}

#[derive(Debug, Deserialize)]
pub struct MistralOcrPage {
    pub index: Option<usize>,
    pub markdown: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct MistralVoicesListResponse {
    pub items: Vec<MistralVoice>,
    pub page: Option<usize>,
    pub page_size: Option<usize>,
    pub total: Option<usize>,
    pub total_pages: Option<usize>,
}

#[derive(Debug, Deserialize)]
pub struct MistralVoice {
    pub id: String,
    pub name: String,
}

#[derive(Debug, Serialize)]
pub struct MistralCreateVoiceRequest<'a> {
    pub name: &'a str,
    /// Base64-encoded audio sample for cloning.
    pub sample_audio: &'a str,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sample_filename: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub languages: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub gender: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub age: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub color: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub slug: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub retention_notice: Option<u32>,
}

#[derive(Debug, Serialize)]
pub struct MistralUpdateVoiceRequest<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub gender: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub age: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub languages: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<Vec<String>>,
}

// ============================================================================
// Model listing structs
// ============================================================================

/// Response from `GET /v1/models`
#[derive(Debug, Deserialize)]
pub struct MistralModelsResponse {
    pub data: Vec<MistralModelInfo>,
}

/// A single model entry from `GET /v1/models`
#[derive(Debug, Deserialize)]
pub struct MistralModelInfo {
    pub id: String,
    #[serde(default)]
    pub created: Option<u64>,
    #[serde(default)]
    pub owned_by: Option<String>,
    #[serde(default)]
    pub description: Option<String>,
    #[serde(default)]
    pub max_context_length: Option<usize>,
    #[serde(default)]
    pub capabilities: Option<MistralModelCapabilities>,
}

/// Capabilities returned by the Mistral models endpoint
#[derive(Debug, Deserialize)]
pub struct MistralModelCapabilities {
    #[serde(default)]
    pub completion_chat: bool,
    #[serde(default)]
    pub completion_fim: bool,
    #[serde(default)]
    pub function_calling: bool,
    #[serde(default)]
    pub fine_tuning: bool,
    #[serde(default)]
    pub vision: bool,
}

// ============================================================================
// MistralProvider
// ============================================================================

/// Mistral AI provider for chat completions and embeddings.
///
/// - Chat / streaming / tool-calls → delegates to [`OpenAICompatibleProvider`]
/// - Embeddings → native `reqwest` call to `/v1/embeddings`
/// - Model listing → native `reqwest` call to `GET /v1/models`
#[derive(Debug)]
pub struct MistralProvider {
    /// Inner provider for chat operations (OpenAI-compatible)
    inner: OpenAICompatibleProvider,
    /// Current chat model name
    model: String,
    /// Embedding model name
    embedding_model: String,
    /// Base URL (with `/v1` suffix)
    base_url: String,
    /// API key
    api_key: String,
    /// Shared HTTP client for native requests (embeddings, model listing)
    client: Client,
}

impl MistralProvider {
    // -----------------------------------------------------------------------
    // Constructors
    // -----------------------------------------------------------------------

    /// Create a provider from environment variables.
    ///
    /// Reads:
    /// - `MISTRAL_API_KEY` (required)
    /// - `MISTRAL_MODEL` (optional, default: `mistral-small-latest`)
    /// - `MISTRAL_EMBEDDING_MODEL` (optional, default: `mistral-embed`)
    /// - `MISTRAL_BASE_URL` (optional)
    pub fn from_env() -> Result<Self> {
        let api_key = std::env::var("MISTRAL_API_KEY").map_err(|_| {
            LlmError::ConfigError(
                "MISTRAL_API_KEY environment variable not set. \
                 Get your API key from https://console.mistral.ai"
                    .to_string(),
            )
        })?;

        if api_key.is_empty() {
            return Err(LlmError::ConfigError(
                "MISTRAL_API_KEY is empty. Please set a valid API key.".to_string(),
            ));
        }

        let model =
            std::env::var("MISTRAL_MODEL").unwrap_or_else(|_| MISTRAL_DEFAULT_MODEL.to_string());
        let embedding_model = std::env::var("MISTRAL_EMBEDDING_MODEL")
            .unwrap_or_else(|_| MISTRAL_DEFAULT_EMBEDDING_MODEL.to_string());
        let base_url =
            std::env::var("MISTRAL_BASE_URL").unwrap_or_else(|_| MISTRAL_BASE_URL.to_string());

        Self::new(api_key, model, embedding_model, Some(base_url))
    }

    /// Create a provider from a [`ProviderConfig`].
    pub fn from_config(config: &ProviderConfig) -> Result<Self> {
        let api_key = if let Some(env_var) = &config.api_key_env {
            std::env::var(env_var).map_err(|_| {
                LlmError::ConfigError(format!(
                    "API key environment variable '{}' not set for Mistral provider.",
                    env_var
                ))
            })?
        } else {
            return Err(LlmError::ConfigError(
                "Mistral provider requires api_key_env to be set.".to_string(),
            ));
        };

        let model = config
            .default_llm_model
            .clone()
            .unwrap_or_else(|| MISTRAL_DEFAULT_MODEL.to_string());
        let embedding_model = config
            .default_embedding_model
            .clone()
            .unwrap_or_else(|| MISTRAL_DEFAULT_EMBEDDING_MODEL.to_string());
        let base_url = config
            .base_url
            .clone()
            .unwrap_or_else(|| MISTRAL_BASE_URL.to_string());

        Self::new(api_key, model, embedding_model, Some(base_url))
    }

    /// Create a provider with explicit configuration.
    ///
    /// # Arguments
    ///
    /// * `api_key` - Mistral API key
    /// * `model` - Chat model (e.g., `"mistral-small-latest"`)
    /// * `embedding_model` - Embedding model (e.g., `"mistral-embed"`)
    /// * `base_url` - Optional custom base URL
    pub fn new(
        api_key: String,
        model: String,
        embedding_model: String,
        base_url: Option<String>,
    ) -> Result<Self> {
        let base_url = base_url.unwrap_or_else(|| MISTRAL_BASE_URL.to_string());

        // Build ProviderConfig for the inner OpenAICompatibleProvider.
        // We inject the literal API key directly into the config so that
        // OpenAICompatibleProvider never needs to call std::env::var (or
        // force us to call std::env::set_var, which is thread-unsafe).
        let config = Self::build_provider_config(&api_key, &model, &embedding_model, &base_url);

        let inner = OpenAICompatibleProvider::from_config(config)?;

        // Build a plain reqwest client for native requests
        let client = Client::builder()
            .timeout(Duration::from_secs(120))
            .build()
            .map_err(|e| LlmError::ConfigError(format!("Failed to build HTTP client: {}", e)))?;

        debug!(
            provider = MISTRAL_PROVIDER_NAME,
            model = %model,
            base_url = %base_url,
            "Created Mistral provider"
        );

        Ok(Self {
            inner,
            model,
            embedding_model,
            base_url,
            api_key,
            client,
        })
    }

    // -----------------------------------------------------------------------
    // Builder methods
    // -----------------------------------------------------------------------

    /// Return a new provider configured for a different chat model.
    pub fn with_model(mut self, model: &str) -> Self {
        self.model = model.to_string();
        self.inner = self.inner.with_model(model);
        self
    }

    /// Return a new provider configured for a different embedding model.
    pub fn with_embedding_model(mut self, model: &str) -> Self {
        self.embedding_model = model.to_string();
        self
    }

    // -----------------------------------------------------------------------
    // Model catalog helpers
    // -----------------------------------------------------------------------

    /// Context length for a given model ID.
    ///
    /// Falls back to 32 768 if the model is not in the static catalog.
    pub fn context_length(model: &str) -> usize {
        MISTRAL_CHAT_MODELS
            .iter()
            .find(|(id, _, _, _, _)| *id == model)
            .map(|(_, _, ctx, _, _)| *ctx)
            .unwrap_or(32768)
    }

    /// List the statically-known chat models.
    pub fn available_models() -> Vec<(&'static str, &'static str, usize)> {
        MISTRAL_CHAT_MODELS
            .iter()
            .map(|(id, name, ctx, _, _)| (*id, *name, *ctx))
            .collect()
    }

    // -----------------------------------------------------------------------
    // Model listing (live API)
    // -----------------------------------------------------------------------

    /// Fetch available models from the Mistral API.
    ///
    /// Calls `GET {base_url}/models` and returns the raw response.
    pub async fn list_models(&self) -> Result<MistralModelsResponse> {
        let url = format!("{}/models", self.base_url.trim_end_matches('/'));

        let response = self
            .client
            .get(&url)
            .header("Authorization", format!("Bearer {}", self.api_key))
            .header("Accept", "application/json")
            .send()
            .await
            .map_err(|e| LlmError::NetworkError(format!("Failed to list Mistral models: {}", e)))?;

        let status = response.status();
        let body = response.text().await.map_err(|e| {
            LlmError::NetworkError(format!("Failed to read model list response: {}", e))
        })?;

        if !status.is_success() {
            return Err(LlmError::ApiError(format!(
                "Mistral models list failed ({status}): {body}"
            )));
        }

        serde_json::from_str(&body)
            .map_err(|e| LlmError::ApiError(format!("Failed to parse models response: {e}")))
    }

    /// Text-to-speech via `POST /v1/audio/speech`.
    pub async fn speech(
        &self,
        request: &MistralSpeechRequest<'_>,
    ) -> Result<MistralSpeechResponse> {
        if request.input.trim().is_empty() {
            return Err(LlmError::InvalidRequest(
                "Mistral speech requires non-empty input text".to_string(),
            ));
        }
        if request.voice_id.is_none() && request.ref_audio.is_none() {
            return Err(LlmError::InvalidRequest(
                "Mistral speech requires either voice_id or ref_audio".to_string(),
            ));
        }
        if request.stream == Some(true) {
            return Err(LlmError::InvalidRequest(
                "Use speech_stream_raw() when stream=true".to_string(),
            ));
        }

        let url = format!("{}/audio/speech", self.base_url.trim_end_matches('/'));
        let response = self
            .client
            .post(&url)
            .header("Authorization", format!("Bearer {}", self.api_key))
            .header("Content-Type", "application/json")
            .json(request)
            .send()
            .await
            .map_err(|e| {
                LlmError::NetworkError(format!("Failed to call Mistral speech API: {e}"))
            })?;

        let status = response.status();
        let body = response
            .text()
            .await
            .map_err(|e| LlmError::NetworkError(format!("Failed to read speech response: {e}")))?;

        if !status.is_success() {
            return Err(LlmError::ApiError(format!(
                "Mistral speech API error ({status}): {body}"
            )));
        }

        serde_json::from_str(&body)
            .map_err(|e| LlmError::ApiError(format!("Failed to parse speech response: {e}")))
    }

    /// Text-to-speech streaming via `POST /v1/audio/speech` (event-stream body passthrough).
    pub async fn speech_stream_raw(&self, request: &MistralSpeechRequest<'_>) -> Result<String> {
        if request.input.trim().is_empty() {
            return Err(LlmError::InvalidRequest(
                "Mistral speech requires non-empty input text".to_string(),
            ));
        }
        if request.voice_id.is_none() && request.ref_audio.is_none() {
            return Err(LlmError::InvalidRequest(
                "Mistral speech requires either voice_id or ref_audio".to_string(),
            ));
        }
        if request.stream != Some(true) {
            return Err(LlmError::InvalidRequest(
                "speech_stream_raw requires stream=true".to_string(),
            ));
        }

        let url = format!("{}/audio/speech", self.base_url.trim_end_matches('/'));
        let response = self
            .client
            .post(&url)
            .header("Authorization", format!("Bearer {}", self.api_key))
            .header("Content-Type", "application/json")
            .header("Accept", "text/event-stream")
            .json(request)
            .send()
            .await
            .map_err(|e| {
                LlmError::NetworkError(format!("Failed to call Mistral speech API: {e}"))
            })?;

        let status = response.status();
        let body = response.text().await.map_err(|e| {
            LlmError::NetworkError(format!("Failed to read speech stream response: {e}"))
        })?;

        if !status.is_success() {
            return Err(LlmError::ApiError(format!(
                "Mistral speech stream API error ({status}): {body}"
            )));
        }

        Ok(body)
    }

    /// Audio transcription via `POST /v1/audio/transcriptions`.
    pub async fn transcribe(
        &self,
        request: &MistralTranscriptionRequest<'_>,
    ) -> Result<MistralTranscriptionResponse> {
        if request.stream == Some(true) {
            return Err(LlmError::InvalidRequest(
                "Use transcribe_stream_raw() when stream=true".to_string(),
            ));
        }
        if request.file_url.is_some() == request.file_id.is_some() {
            return Err(LlmError::InvalidRequest(
                "Mistral transcription requires exactly one source: file_url or file_id"
                    .to_string(),
            ));
        }

        let url = format!(
            "{}/audio/transcriptions",
            self.base_url.trim_end_matches('/')
        );

        // Mistral transcription expects multipart/form-data (even when using file_url).
        let mut form = multipart::Form::new().text("model", request.model.to_string());

        if let Some(file_url) = request.file_url {
            form = form.text("file_url", file_url.to_string());
        }
        if let Some(file_id) = request.file_id {
            form = form.text("file_id", file_id.to_string());
        }

        if let Some(language) = request.language {
            form = form.text("language", language.to_string());
        }
        if let Some(stream) = request.stream {
            form = form.text("stream", stream.to_string());
        }

        let response = self
            .client
            .post(&url)
            .header("Authorization", format!("Bearer {}", self.api_key))
            .multipart(form)
            .send()
            .await
            .map_err(|e| {
                LlmError::NetworkError(format!("Failed to call Mistral transcription API: {e}"))
            })?;

        let status = response.status();
        let body = response.text().await.map_err(|e| {
            LlmError::NetworkError(format!("Failed to read transcription response: {e}"))
        })?;

        if !status.is_success() {
            return Err(LlmError::ApiError(format!(
                "Mistral transcription API error ({status}): {body}"
            )));
        }

        serde_json::from_str(&body)
            .map_err(|e| LlmError::ApiError(format!("Failed to parse transcription response: {e}")))
    }

    /// Multipart file-upload transcription via `POST /v1/audio/transcriptions`.
    pub async fn transcribe_file_upload(
        &self,
        model: &str,
        filename: &str,
        bytes: Vec<u8>,
        language: Option<&str>,
    ) -> Result<MistralTranscriptionResponse> {
        if model.trim().is_empty() {
            return Err(LlmError::InvalidRequest(
                "Mistral transcription requires non-empty model".to_string(),
            ));
        }
        if filename.trim().is_empty() {
            return Err(LlmError::InvalidRequest(
                "Mistral transcription requires non-empty filename".to_string(),
            ));
        }
        if bytes.is_empty() {
            return Err(LlmError::InvalidRequest(
                "Mistral transcription file bytes must not be empty".to_string(),
            ));
        }

        let url = format!(
            "{}/audio/transcriptions",
            self.base_url.trim_end_matches('/')
        );
        let file_part = multipart::Part::bytes(bytes).file_name(filename.to_string());
        let mut form = multipart::Form::new()
            .text("model", model.to_string())
            .part("file", file_part);
        if let Some(lang) = language {
            form = form.text("language", lang.to_string());
        }

        let response = self
            .client
            .post(&url)
            .header("Authorization", format!("Bearer {}", self.api_key))
            .multipart(form)
            .send()
            .await
            .map_err(|e| {
                LlmError::NetworkError(format!("Failed to call Mistral transcription API: {e}"))
            })?;

        let status = response.status();
        let body = response.text().await.map_err(|e| {
            LlmError::NetworkError(format!("Failed to read transcription response: {e}"))
        })?;

        if !status.is_success() {
            return Err(LlmError::ApiError(format!(
                "Mistral transcription API error ({status}): {body}"
            )));
        }

        serde_json::from_str(&body)
            .map_err(|e| LlmError::ApiError(format!("Failed to parse transcription response: {e}")))
    }

    /// Audio transcription streaming via `POST /v1/audio/transcriptions`.
    /// Returns the raw event-stream payload.
    pub async fn transcribe_stream_raw(
        &self,
        request: &MistralTranscriptionRequest<'_>,
    ) -> Result<String> {
        if request.stream != Some(true) {
            return Err(LlmError::InvalidRequest(
                "transcribe_stream_raw requires stream=true".to_string(),
            ));
        }
        if request.file_url.is_some() == request.file_id.is_some() {
            return Err(LlmError::InvalidRequest(
                "Mistral transcription requires exactly one source: file_url or file_id"
                    .to_string(),
            ));
        }

        let url = format!(
            "{}/audio/transcriptions",
            self.base_url.trim_end_matches('/')
        );
        let mut form = multipart::Form::new().text("model", request.model.to_string());
        if let Some(file_url) = request.file_url {
            form = form.text("file_url", file_url.to_string());
        }
        if let Some(file_id) = request.file_id {
            form = form.text("file_id", file_id.to_string());
        }
        if let Some(language) = request.language {
            form = form.text("language", language.to_string());
        }
        form = form.text("stream", "true".to_string());

        let response = self
            .client
            .post(&url)
            .header("Authorization", format!("Bearer {}", self.api_key))
            .header("Accept", "text/event-stream")
            .multipart(form)
            .send()
            .await
            .map_err(|e| {
                LlmError::NetworkError(format!("Failed to call Mistral transcription API: {e}"))
            })?;

        let status = response.status();
        let body = response.text().await.map_err(|e| {
            LlmError::NetworkError(format!("Failed to read transcription stream response: {e}"))
        })?;

        if !status.is_success() {
            return Err(LlmError::ApiError(format!(
                "Mistral transcription stream API error ({status}): {body}"
            )));
        }

        Ok(body)
    }

    /// OCR processing via `POST /v1/ocr`.
    pub async fn ocr(&self, request: &MistralOcrRequest<'_>) -> Result<MistralOcrResponse> {
        let url = format!("{}/ocr", self.base_url.trim_end_matches('/'));
        let response = self
            .client
            .post(&url)
            .header("Authorization", format!("Bearer {}", self.api_key))
            .header("Content-Type", "application/json")
            .json(request)
            .send()
            .await
            .map_err(|e| LlmError::NetworkError(format!("Failed to call Mistral OCR API: {e}")))?;

        let status = response.status();
        let body = response
            .text()
            .await
            .map_err(|e| LlmError::NetworkError(format!("Failed to read OCR response: {e}")))?;

        if !status.is_success() {
            return Err(LlmError::ApiError(format!(
                "Mistral OCR API error ({status}): {body}"
            )));
        }

        serde_json::from_str(&body)
            .map_err(|e| LlmError::ApiError(format!("Failed to parse OCR response: {e}")))
    }

    /// List voices via `GET /v1/audio/voices`.
    pub async fn list_audio_voices(&self) -> Result<MistralVoicesListResponse> {
        let url = format!("{}/audio/voices", self.base_url.trim_end_matches('/'));

        let response = self
            .client
            .get(&url)
            .header("Authorization", format!("Bearer {}", self.api_key))
            .header("Accept", "application/json")
            .send()
            .await
            .map_err(|e| LlmError::NetworkError(format!("Failed to list Mistral voices: {}", e)))?;

        let status = response.status();
        let body = response.text().await.map_err(|e| {
            LlmError::NetworkError(format!("Failed to read voices list response: {}", e))
        })?;

        if !status.is_success() {
            return Err(LlmError::ApiError(format!(
                "Mistral voices list failed ({status}): {body}"
            )));
        }

        serde_json::from_str(&body)
            .map_err(|e| LlmError::ApiError(format!("Failed to parse voices response: {e}")))
    }

    /// Get a voice metadata record by id.
    pub async fn get_audio_voice(&self, voice_id: &str) -> Result<MistralVoice> {
        if voice_id.trim().is_empty() {
            return Err(LlmError::InvalidRequest(
                "voice_id must not be empty".to_string(),
            ));
        }

        let url = format!(
            "{}/audio/voices/{}",
            self.base_url.trim_end_matches('/'),
            voice_id
        );
        let response = self
            .client
            .get(&url)
            .header("Authorization", format!("Bearer {}", self.api_key))
            .header("Accept", "application/json")
            .send()
            .await
            .map_err(|e| LlmError::NetworkError(format!("Failed to get Mistral voice: {}", e)))?;

        let status = response.status();
        let body = response
            .text()
            .await
            .map_err(|e| LlmError::NetworkError(format!("Failed to read voice response: {}", e)))?;

        if !status.is_success() {
            return Err(LlmError::ApiError(format!(
                "Mistral get voice failed ({status}): {body}"
            )));
        }

        serde_json::from_str(&body)
            .map_err(|e| LlmError::ApiError(format!("Failed to parse voice response: {e}")))
    }

    /// Get a base64 voice sample by id.
    pub async fn get_audio_voice_sample(&self, voice_id: &str) -> Result<String> {
        if voice_id.trim().is_empty() {
            return Err(LlmError::InvalidRequest(
                "voice_id must not be empty".to_string(),
            ));
        }

        let url = format!(
            "{}/audio/voices/{}/sample",
            self.base_url.trim_end_matches('/'),
            voice_id
        );
        let response = self
            .client
            .get(&url)
            .header("Authorization", format!("Bearer {}", self.api_key))
            .header("Accept", "application/json")
            .send()
            .await
            .map_err(|e| {
                LlmError::NetworkError(format!("Failed to get Mistral voice sample: {}", e))
            })?;

        let status = response.status();
        let body = response.text().await.map_err(|e| {
            LlmError::NetworkError(format!("Failed to read voice sample response: {}", e))
        })?;

        if !status.is_success() {
            return Err(LlmError::ApiError(format!(
                "Mistral get voice sample failed ({status}): {body}"
            )));
        }

        // Some deployments return a JSON string (`"..."`), others return
        // raw base64 text. Accept both formats for compatibility.
        if let Ok(sample) = serde_json::from_str::<String>(&body) {
            return Ok(sample);
        }

        let trimmed = body.trim().to_string();
        if trimmed.is_empty() {
            return Err(LlmError::ApiError(
                "Voice sample response was empty".to_string(),
            ));
        }
        Ok(trimmed)
    }

    /// Create a new voice with a base64 encoded sample.
    pub async fn create_audio_voice(
        &self,
        request: &MistralCreateVoiceRequest<'_>,
    ) -> Result<MistralVoice> {
        if request.name.trim().is_empty() {
            return Err(LlmError::InvalidRequest(
                "Mistral voice create requires non-empty name".to_string(),
            ));
        }
        if request.sample_audio.trim().is_empty() {
            return Err(LlmError::InvalidRequest(
                "Mistral voice create requires non-empty sample_audio".to_string(),
            ));
        }

        let url = format!("{}/audio/voices", self.base_url.trim_end_matches('/'));
        let response = self
            .client
            .post(&url)
            .header("Authorization", format!("Bearer {}", self.api_key))
            .header("Content-Type", "application/json")
            .json(request)
            .send()
            .await
            .map_err(|e| {
                LlmError::NetworkError(format!("Failed to create Mistral voice metadata: {}", e))
            })?;

        let status = response.status();
        let body = response.text().await.map_err(|e| {
            LlmError::NetworkError(format!("Failed to read voice create response: {}", e))
        })?;

        if !status.is_success() {
            return Err(LlmError::ApiError(format!(
                "Mistral create voice failed ({status}): {body}"
            )));
        }

        serde_json::from_str(&body)
            .map_err(|e| LlmError::ApiError(format!("Failed to parse create voice response: {e}")))
    }

    /// Update a voice metadata record.
    pub async fn update_audio_voice(
        &self,
        voice_id: &str,
        request: &MistralUpdateVoiceRequest<'_>,
    ) -> Result<MistralVoice> {
        if voice_id.trim().is_empty() {
            return Err(LlmError::InvalidRequest(
                "voice_id must not be empty".to_string(),
            ));
        }

        let url = format!(
            "{}/audio/voices/{}",
            self.base_url.trim_end_matches('/'),
            voice_id
        );
        let response = self
            .client
            .patch(&url)
            .header("Authorization", format!("Bearer {}", self.api_key))
            .header("Content-Type", "application/json")
            .json(request)
            .send()
            .await
            .map_err(|e| {
                LlmError::NetworkError(format!("Failed to update Mistral voice metadata: {}", e))
            })?;

        let status = response.status();
        let body = response.text().await.map_err(|e| {
            LlmError::NetworkError(format!("Failed to read voice update response: {}", e))
        })?;

        if !status.is_success() {
            return Err(LlmError::ApiError(format!(
                "Mistral update voice failed ({status}): {body}"
            )));
        }

        serde_json::from_str(&body)
            .map_err(|e| LlmError::ApiError(format!("Failed to parse update voice response: {e}")))
    }

    /// Delete a voice record.
    pub async fn delete_audio_voice(&self, voice_id: &str) -> Result<MistralVoice> {
        if voice_id.trim().is_empty() {
            return Err(LlmError::InvalidRequest(
                "voice_id must not be empty".to_string(),
            ));
        }

        let url = format!(
            "{}/audio/voices/{}",
            self.base_url.trim_end_matches('/'),
            voice_id
        );
        let response = self
            .client
            .delete(&url)
            .header("Authorization", format!("Bearer {}", self.api_key))
            .header("Accept", "application/json")
            .send()
            .await
            .map_err(|e| {
                LlmError::NetworkError(format!("Failed to delete Mistral voice: {}", e))
            })?;

        let status = response.status();
        let body = response.text().await.map_err(|e| {
            LlmError::NetworkError(format!("Failed to read delete voice response: {}", e))
        })?;

        if !status.is_success() {
            return Err(LlmError::ApiError(format!(
                "Mistral delete voice failed ({status}): {body}"
            )));
        }

        serde_json::from_str(&body)
            .map_err(|e| LlmError::ApiError(format!("Failed to parse delete voice response: {e}")))
    }

    // -----------------------------------------------------------------------
    // Internal helpers
    // -----------------------------------------------------------------------

    /// Construct the [`ProviderConfig`] used by the inner [`OpenAICompatibleProvider`].
    fn build_provider_config(
        api_key: &str,
        model: &str,
        embedding_model: &str,
        base_url: &str,
    ) -> ProviderConfig {
        let models: Vec<ModelCard> = MISTRAL_CHAT_MODELS
            .iter()
            .map(|(id, display, ctx, vision, fc)| {
                // Frontier large/medium models support up to 16 384 output tokens;
                // smaller / legacy models default to 4 096.
                let max_output = if id.contains("large") || id.contains("medium") {
                    MISTRAL_FRONTIER_MAX_OUTPUT_TOKENS
                } else {
                    MISTRAL_DEFAULT_MAX_OUTPUT_TOKENS
                };
                ModelCard {
                    name: id.to_string(),
                    display_name: display.to_string(),
                    model_type: ModelType::Llm,
                    capabilities: ModelCapabilities {
                        context_length: *ctx,
                        max_output_tokens: max_output,
                        supports_vision: *vision,
                        supports_function_calling: *fc,
                        supports_json_mode: true,
                        supports_streaming: true,
                        supports_system_message: true,
                        ..Default::default()
                    },
                    ..Default::default()
                }
            })
            .chain(std::iter::once(ModelCard {
                name: MISTRAL_DEFAULT_EMBEDDING_MODEL.to_string(),
                display_name: "Mistral Embed".to_string(),
                model_type: ModelType::Embedding,
                capabilities: ModelCapabilities {
                    context_length: MISTRAL_EMBED_MAX_TOKENS,
                    embedding_dimension: MISTRAL_EMBED_DIMENSION,
                    ..Default::default()
                },
                ..Default::default()
            }))
            .collect();

        ProviderConfig {
            name: MISTRAL_PROVIDER_NAME.to_string(),
            display_name: "Mistral AI".to_string(),
            provider_type: ConfigProviderType::OpenAICompatible,
            // Inject the literal key so OpenAICompatibleProvider does not need
            // to read from the environment (avoids std::env::set_var races).
            api_key: Some(api_key.to_string()),
            api_key_env: Some("MISTRAL_API_KEY".to_string()),
            base_url: Some(base_url.to_string()),
            base_url_env: Some("MISTRAL_BASE_URL".to_string()),
            default_llm_model: Some(model.to_string()),
            default_embedding_model: Some(embedding_model.to_string()),
            models,
            enabled: true,
            ..Default::default()
        }
    }
}

// ============================================================================
// LLMProvider trait implementation (delegates to inner OpenAICompatibleProvider)
// ============================================================================

#[async_trait]
impl LLMProvider for MistralProvider {
    fn name(&self) -> &str {
        MISTRAL_PROVIDER_NAME
    }

    fn model(&self) -> &str {
        &self.model
    }

    fn max_context_length(&self) -> usize {
        Self::context_length(&self.model)
    }

    async fn complete(&self, prompt: &str) -> Result<LLMResponse> {
        self.inner.complete(prompt).await
    }

    async fn complete_with_options(
        &self,
        prompt: &str,
        options: &CompletionOptions,
    ) -> Result<LLMResponse> {
        self.inner.complete_with_options(prompt, options).await
    }

    async fn chat(
        &self,
        messages: &[ChatMessage],
        options: Option<&CompletionOptions>,
    ) -> Result<LLMResponse> {
        self.inner.chat(messages, options).await
    }

    async fn chat_with_tools(
        &self,
        messages: &[ChatMessage],
        tools: &[ToolDefinition],
        tool_choice: Option<ToolChoice>,
        options: Option<&CompletionOptions>,
    ) -> Result<LLMResponse> {
        self.inner
            .chat_with_tools(messages, tools, tool_choice, options)
            .await
    }

    async fn chat_with_tools_stream(
        &self,
        messages: &[ChatMessage],
        tools: &[ToolDefinition],
        tool_choice: Option<ToolChoice>,
        options: Option<&CompletionOptions>,
    ) -> Result<BoxStream<'static, Result<StreamChunk>>> {
        self.inner
            .chat_with_tools_stream(messages, tools, tool_choice, options)
            .await
    }

    async fn stream(&self, prompt: &str) -> Result<BoxStream<'static, Result<String>>> {
        self.inner.stream(prompt).await
    }

    fn supports_streaming(&self) -> bool {
        true
    }

    fn supports_function_calling(&self) -> bool {
        // All "latest" models and nemo support function calling
        let fc_capable = MISTRAL_CHAT_MODELS
            .iter()
            .find(|(id, _, _, _, _)| *id == self.model.as_str())
            .map(|(_, _, _, _, fc)| *fc)
            .unwrap_or(true);
        fc_capable
    }

    fn supports_json_mode(&self) -> bool {
        true
    }

    fn supports_tool_streaming(&self) -> bool {
        self.inner.supports_tool_streaming()
    }
}

// ============================================================================
// EmbeddingProvider trait implementation (native reqwest)
// ============================================================================

#[async_trait]
impl EmbeddingProvider for MistralProvider {
    fn name(&self) -> &str {
        MISTRAL_PROVIDER_NAME
    }

    #[allow(clippy::misnamed_getters)]
    fn model(&self) -> &str {
        &self.embedding_model
    }

    fn dimension(&self) -> usize {
        // Only `mistral-embed` is currently offered, which has 1024 dimensions.
        // Return the standard dimension regardless of the configured model name.
        MISTRAL_EMBED_DIMENSION
    }

    fn max_tokens(&self) -> usize {
        MISTRAL_EMBED_MAX_TOKENS
    }

    /// Embed a batch of texts using `POST /v1/embeddings`.
    ///
    /// The returned vectors are in the same order as the input slice.
    async fn embed(&self, texts: &[String]) -> Result<Vec<Vec<f32>>> {
        if texts.is_empty() {
            return Ok(Vec::new());
        }

        let url = format!("{}/embeddings", self.base_url.trim_end_matches('/'));

        let request_body = EmbeddingRequest {
            model: &self.embedding_model,
            input: texts,
            // Use "float" explicitly so that the response always contains a
            // `Vec<f32>` rather than a base64-encoded blob.  This keeps the
            // deserialization path simple and avoids a client-side decode step.
            encoding_format: Some("float"),
        };

        debug!(
            model = self.embedding_model,
            count = texts.len(),
            "Mistral embed request"
        );

        let response = self
            .client
            .post(&url)
            .header("Authorization", format!("Bearer {}", self.api_key))
            .header("Content-Type", "application/json")
            .json(&request_body)
            .send()
            .await
            .map_err(|e| {
                LlmError::NetworkError(format!("Mistral embeddings request failed: {}", e))
            })?;

        let status = response.status();
        let body = response.text().await.map_err(|e| {
            LlmError::NetworkError(format!("Failed to read Mistral embeddings response: {}", e))
        })?;

        if !status.is_success() {
            return Err(LlmError::ApiError(format!(
                "Mistral embeddings API error ({status}): {body}"
            )));
        }

        let embedding_response: EmbeddingResponse = serde_json::from_str(&body).map_err(|e| {
            LlmError::ApiError(format!(
                "Failed to parse Mistral embeddings response: {e} | body: {}",
                &body[..body.len().min(500)]
            ))
        })?;

        // Sort by index to preserve input order and extract vectors
        let mut data = embedding_response.data;
        data.sort_by_key(|d| d.index);

        let embeddings: Vec<Vec<f32>> = data.into_iter().map(|d| d.embedding).collect();

        // Validate that we got the right number of embeddings
        if embeddings.len() != texts.len() {
            return Err(LlmError::ApiError(format!(
                "Mistral returned {} embeddings for {} inputs",
                embeddings.len(),
                texts.len()
            )));
        }

        Ok(embeddings)
    }
}

// ============================================================================
// Tests
// ============================================================================

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

    // -----------------------------------------------------------------------
    // Static catalog tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_available_models_not_empty() {
        let models = MistralProvider::available_models();
        assert!(!models.is_empty());
    }

    #[test]
    fn test_available_models_contains_expected_ids() {
        let models = MistralProvider::available_models();
        let ids: Vec<&str> = models.iter().map(|(id, _, _)| *id).collect();
        // Current frontier aliases (April 2026)
        assert!(
            ids.contains(&"mistral-large-latest"),
            "Should contain mistral-large-latest"
        );
        assert!(
            ids.contains(&"mistral-medium-latest"),
            "Should contain mistral-medium-latest"
        );
        assert!(
            ids.contains(&"mistral-small-latest"),
            "Should contain mistral-small-latest"
        );
        assert!(
            ids.contains(&"codestral-latest"),
            "Should contain codestral-latest"
        );
        assert!(
            ids.contains(&"devstral-latest"),
            "Should contain devstral-latest"
        );
        // Ministral edge models
        assert!(
            ids.contains(&"ministral-3b-latest"),
            "Should contain ministral-3b-latest"
        );
        assert!(
            ids.contains(&"ministral-8b-latest"),
            "Should contain ministral-8b-latest"
        );
        assert!(
            ids.contains(&"ministral-14b-latest"),
            "Should contain ministral-14b-latest"
        );
        // Reasoning models
        assert!(
            ids.contains(&"magistral-medium-latest"),
            "Should contain magistral-medium-latest"
        );
        assert!(
            ids.contains(&"magistral-small-latest"),
            "Should contain magistral-small-latest"
        );
    }

    #[test]
    fn test_context_length_known_models() {
        // Frontier models — 256 K (262_144)
        assert_eq!(
            MistralProvider::context_length("mistral-large-latest"),
            262_144
        );
        assert_eq!(
            MistralProvider::context_length("mistral-small-latest"),
            262_144
        );
        assert_eq!(MistralProvider::context_length("codestral-latest"), 262_144);
        // Medium — 128 K (131_072)
        assert_eq!(
            MistralProvider::context_length("mistral-medium-latest"),
            131_072
        );
        assert_eq!(
            MistralProvider::context_length("open-mistral-nemo"),
            131_072
        );
        // Ministral edge models — 128 K
        assert_eq!(
            MistralProvider::context_length("ministral-3b-latest"),
            131_072
        );
        assert_eq!(
            MistralProvider::context_length("ministral-8b-latest"),
            131_072
        );
        assert_eq!(
            MistralProvider::context_length("ministral-14b-latest"),
            131_072
        );
    }

    #[test]
    fn test_context_length_unknown_model() {
        // Unknown models should fall back to 32 768
        assert_eq!(MistralProvider::context_length("unknown-model"), 32768);
    }

    #[test]
    fn test_all_models_have_positive_context() {
        for (id, _, ctx) in MistralProvider::available_models() {
            assert!(ctx > 0, "Model {} must have positive context length", id);
        }
    }

    // -----------------------------------------------------------------------
    // Constants tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_provider_name_constant() {
        assert_eq!(MISTRAL_PROVIDER_NAME, "mistral");
    }

    #[test]
    fn test_default_model_constant() {
        assert_eq!(MISTRAL_DEFAULT_MODEL, "mistral-small-latest");
    }

    #[test]
    fn test_default_embedding_model_constant() {
        assert_eq!(MISTRAL_DEFAULT_EMBEDDING_MODEL, "mistral-embed");
    }

    #[test]
    fn test_embed_dimension_constant() {
        assert_eq!(MISTRAL_EMBED_DIMENSION, 1024);
    }

    #[test]
    fn test_base_url_constant() {
        assert_eq!(MISTRAL_BASE_URL, "https://api.mistral.ai/v1");
    }

    // -----------------------------------------------------------------------
    // ProviderConfig construction
    // -----------------------------------------------------------------------

    #[test]
    fn test_build_provider_config_name() {
        let cfg = MistralProvider::build_provider_config(
            "key",
            "mistral-small-latest",
            "mistral-embed",
            MISTRAL_BASE_URL,
        );
        assert_eq!(cfg.name, "mistral");
        assert_eq!(cfg.display_name, "Mistral AI");
    }

    #[test]
    fn test_build_provider_config_base_url() {
        let cfg = MistralProvider::build_provider_config(
            "key",
            "mistral-small-latest",
            "mistral-embed",
            "https://custom.api/v1",
        );
        assert_eq!(cfg.base_url, Some("https://custom.api/v1".to_string()));
    }

    #[test]
    fn test_build_provider_config_models_include_embedding() {
        let cfg = MistralProvider::build_provider_config(
            "key",
            "mistral-small-latest",
            "mistral-embed",
            MISTRAL_BASE_URL,
        );
        let embedding_cards: Vec<_> = cfg
            .models
            .iter()
            .filter(|m| m.model_type == ModelType::Embedding)
            .collect();
        assert_eq!(embedding_cards.len(), 1);
        assert_eq!(embedding_cards[0].name, "mistral-embed");
        assert_eq!(
            embedding_cards[0].capabilities.embedding_dimension,
            MISTRAL_EMBED_DIMENSION
        );
    }

    #[test]
    fn test_build_provider_config_default_models() {
        let cfg = MistralProvider::build_provider_config(
            "key",
            "mistral-small-latest",
            "mistral-embed",
            MISTRAL_BASE_URL,
        );
        assert_eq!(
            cfg.default_llm_model,
            Some("mistral-small-latest".to_string())
        );
        assert_eq!(
            cfg.default_embedding_model,
            Some("mistral-embed".to_string())
        );
    }

    #[test]
    fn test_build_provider_config_api_key_env() {
        let cfg = MistralProvider::build_provider_config(
            "key",
            "mistral-small-latest",
            "mistral-embed",
            MISTRAL_BASE_URL,
        );
        assert_eq!(cfg.api_key_env, Some("MISTRAL_API_KEY".to_string()));
    }

    // -----------------------------------------------------------------------
    // from_env failures
    // -----------------------------------------------------------------------

    #[test]
    fn test_from_env_missing_api_key() {
        std::env::remove_var("MISTRAL_API_KEY");
        let result = MistralProvider::from_env();
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("MISTRAL_API_KEY"));
    }

    // -----------------------------------------------------------------------
    // EmbeddingProvider trait surface (no live calls)
    // -----------------------------------------------------------------------

    #[test]
    fn test_embedding_dimension() {
        std::env::set_var("MISTRAL_API_KEY", "test-key-for-unit-test");
        let p = MistralProvider::new(
            "test-key".to_string(),
            MISTRAL_DEFAULT_MODEL.to_string(),
            MISTRAL_DEFAULT_EMBEDDING_MODEL.to_string(),
            None,
        )
        .unwrap();
        assert_eq!(EmbeddingProvider::dimension(&p), MISTRAL_EMBED_DIMENSION);
        assert_eq!(EmbeddingProvider::max_tokens(&p), MISTRAL_EMBED_MAX_TOKENS);
        assert_eq!(EmbeddingProvider::name(&p), "mistral");
        assert_eq!(EmbeddingProvider::model(&p), "mistral-embed");
        std::env::remove_var("MISTRAL_API_KEY");
    }

    // -----------------------------------------------------------------------
    // LLMProvider trait surface (no live calls)
    // -----------------------------------------------------------------------

    #[test]
    fn test_llm_provider_surface() {
        std::env::set_var("MISTRAL_API_KEY", "test-key-for-unit-test");
        let p = MistralProvider::new(
            "test-key".to_string(),
            "mistral-large-latest".to_string(),
            MISTRAL_DEFAULT_EMBEDDING_MODEL.to_string(),
            None,
        )
        .unwrap();
        assert_eq!(LLMProvider::name(&p), "mistral");
        assert_eq!(LLMProvider::model(&p), "mistral-large-latest");
        assert_eq!(p.max_context_length(), 262_144); // Mistral Large 3 = 256 K
        assert!(p.supports_streaming());
        assert!(p.supports_function_calling());
        assert!(p.supports_json_mode());
        std::env::remove_var("MISTRAL_API_KEY");
    }

    #[test]
    fn test_with_model_builder() {
        std::env::set_var("MISTRAL_API_KEY", "test-key-for-unit-test");
        let p = MistralProvider::new(
            "test-key".to_string(),
            MISTRAL_DEFAULT_MODEL.to_string(),
            MISTRAL_DEFAULT_EMBEDDING_MODEL.to_string(),
            None,
        )
        .unwrap()
        .with_model("codestral-latest");
        assert_eq!(p.model, "codestral-latest");
        assert_eq!(p.max_context_length(), 262144);
        std::env::remove_var("MISTRAL_API_KEY");
    }

    #[test]
    fn test_with_embedding_model_builder() {
        std::env::set_var("MISTRAL_API_KEY", "test-key-for-unit-test");
        let p = MistralProvider::new(
            "test-key".to_string(),
            MISTRAL_DEFAULT_MODEL.to_string(),
            MISTRAL_DEFAULT_EMBEDDING_MODEL.to_string(),
            None,
        )
        .unwrap()
        .with_embedding_model("custom-embed");
        assert_eq!(p.embedding_model, "custom-embed");
        std::env::remove_var("MISTRAL_API_KEY");
    }

    // -----------------------------------------------------------------------
    // Serialization / deserialization helpers
    // -----------------------------------------------------------------------

    #[test]
    fn test_embedding_request_serialization() {
        let texts = vec!["hello world".to_string(), "foo bar".to_string()];
        let req = EmbeddingRequest {
            model: "mistral-embed",
            input: &texts,
            encoding_format: Some("float"),
        };
        let json = serde_json::to_value(&req).unwrap();
        assert_eq!(json["model"], "mistral-embed");
        assert_eq!(json["input"][0], "hello world");
        assert_eq!(json["input"][1], "foo bar");
        assert_eq!(json["encoding_format"], "float");
    }

    #[test]
    fn test_embedding_request_serialization_no_encoding_format() {
        let texts = vec!["hello".to_string()];
        let req = EmbeddingRequest {
            model: "mistral-embed",
            input: &texts,
            encoding_format: None,
        };
        let json = serde_json::to_value(&req).unwrap();
        // `encoding_format` must be absent when None (skip_serializing_if)
        assert!(
            json.get("encoding_format").is_none(),
            "encoding_format should be absent when None"
        );
    }

    #[test]
    fn test_embedding_response_deserialization() {
        let raw = r#"{
            "id": "embd-1",
            "object": "list",
            "data": [
                {"object": "embedding", "embedding": [0.1, 0.2, 0.3], "index": 0},
                {"object": "embedding", "embedding": [0.4, 0.5, 0.6], "index": 1}
            ],
            "model": "mistral-embed",
            "usage": {"prompt_tokens": 10, "total_tokens": 10}
        }"#;
        let resp: EmbeddingResponse = serde_json::from_str(raw).unwrap();
        assert_eq!(resp.data.len(), 2);
        assert_eq!(resp.data[0].embedding, vec![0.1, 0.2, 0.3]);
        assert_eq!(resp.data[1].index, 1);
    }

    #[test]
    fn test_model_info_deserialization() {
        let raw = r#"{
            "data": [
                {
                    "id": "mistral-small-latest",
                    "created": 1735689600,
                    "owned_by": "mistralai",
                    "description": "Mistral Small",
                    "max_context_length": 32768,
                    "capabilities": {
                        "completion_chat": true,
                        "completion_fim": false,
                        "function_calling": true,
                        "fine_tuning": false,
                        "vision": false
                    }
                }
            ]
        }"#;
        let resp: MistralModelsResponse = serde_json::from_str(raw).unwrap();
        assert_eq!(resp.data.len(), 1);
        let m = &resp.data[0];
        assert_eq!(m.id, "mistral-small-latest");
        assert_eq!(m.max_context_length, Some(32768));
        let caps = m.capabilities.as_ref().unwrap();
        assert!(caps.function_calling);
        assert!(!caps.vision);
    }

    #[test]
    fn test_vision_model_capabilities() {
        // Mistral Large 3 has vision support in latest catalog
        let cfg = MistralProvider::build_provider_config(
            "key",
            "mistral-large-latest",
            "mistral-embed",
            MISTRAL_BASE_URL,
        );
        let large = cfg
            .models
            .iter()
            .find(|m| m.name == "mistral-large-latest")
            .unwrap();
        assert!(large.capabilities.supports_vision);
        assert!(large.capabilities.supports_function_calling);
    }

    #[test]
    fn test_ministral_models_in_catalog() {
        let cfg = MistralProvider::build_provider_config(
            "key",
            "mistral-small-latest",
            "mistral-embed",
            MISTRAL_BASE_URL,
        );
        for id in &[
            "ministral-3b-latest",
            "ministral-8b-latest",
            "ministral-14b-latest",
        ] {
            let card = cfg.models.iter().find(|m| m.name == *id);
            assert!(card.is_some(), "Missing model: {id}");
            let card = card.unwrap();
            assert_eq!(
                card.capabilities.context_length, 131_072,
                "Wrong context for {id}"
            );
            assert!(
                card.capabilities.supports_vision,
                "{id} should support vision"
            );
            assert!(
                card.capabilities.supports_function_calling,
                "{id} should support FC"
            );
        }
    }

    #[test]
    fn test_reasoning_models_in_catalog() {
        let cfg = MistralProvider::build_provider_config(
            "key",
            "magistral-medium-latest",
            "mistral-embed",
            MISTRAL_BASE_URL,
        );
        for id in &["magistral-medium-latest", "magistral-small-latest"] {
            let card = cfg.models.iter().find(|m| m.name == *id);
            assert!(card.is_some(), "Missing reasoning model: {id}");
            let card = card.unwrap();
            assert_eq!(
                card.capabilities.context_length, 131_072,
                "Wrong context for {id}"
            );
            assert!(
                card.capabilities.supports_function_calling,
                "{id} should support FC"
            );
        }
    }

    #[test]
    fn test_frontier_models_have_256k_context() {
        for id in &[
            "mistral-large-latest",
            "mistral-small-latest",
            "codestral-latest",
        ] {
            assert_eq!(
                MistralProvider::context_length(id),
                262_144,
                "Expected 256K context for {id}"
            );
        }
    }

    #[test]
    fn test_provider_config_sets_api_key_directly() {
        // Ensure the literal api_key field is set so OpenAICompatibleProvider
        // does not need to mutate the environment (thread-safety guarantee).
        let cfg = MistralProvider::build_provider_config(
            "secret-api-key",
            "mistral-small-latest",
            "mistral-embed",
            MISTRAL_BASE_URL,
        );
        assert_eq!(cfg.api_key.as_deref(), Some("secret-api-key"));
    }

    #[test]
    fn test_new_does_not_require_env_var() {
        // MistralProvider::new() must not need MISTRAL_API_KEY in the env.
        std::env::remove_var("MISTRAL_API_KEY");
        let result = MistralProvider::new(
            "explicit-key".to_string(),
            MISTRAL_DEFAULT_MODEL.to_string(),
            MISTRAL_DEFAULT_EMBEDDING_MODEL.to_string(),
            None,
        );
        assert!(
            result.is_ok(),
            "MistralProvider::new() should succeed without env var when key is provided directly"
        );
    }

    #[test]
    fn test_frontier_max_output_tokens() {
        let cfg = MistralProvider::build_provider_config(
            "key",
            "mistral-large-latest",
            "mistral-embed",
            MISTRAL_BASE_URL,
        );
        let large = cfg
            .models
            .iter()
            .find(|m| m.name == "mistral-large-latest")
            .unwrap();
        assert_eq!(
            large.capabilities.max_output_tokens, MISTRAL_FRONTIER_MAX_OUTPUT_TOKENS,
            "Frontier large model should have 16 384 output tokens"
        );
        let medium = cfg
            .models
            .iter()
            .find(|m| m.name == "mistral-medium-latest")
            .unwrap();
        assert_eq!(
            medium.capabilities.max_output_tokens, MISTRAL_FRONTIER_MAX_OUTPUT_TOKENS,
            "Frontier medium model should have 16 384 output tokens"
        );
    }

    #[test]
    fn test_speech_request_serialization() {
        let req = MistralSpeechRequest {
            model: "voxtral-mini-tts-latest",
            input: "hello world",
            voice_id: Some("voice_123"),
            ref_audio: None,
            response_format: Some("mp3"),
            stream: Some(false),
        };
        let json = serde_json::to_value(&req).unwrap();
        assert_eq!(json["model"], "voxtral-mini-tts-latest");
        assert_eq!(json["input"], "hello world");
        assert_eq!(json["voice_id"], "voice_123");
        assert_eq!(json["response_format"], "mp3");
    }

    #[test]
    fn test_transcription_request_serialization() {
        let req = MistralTranscriptionRequest {
            model: "voxtral-mini-transcribe-26-02",
            file_url: Some("https://example.com/sample.wav"),
            file_id: None,
            language: Some("en"),
            stream: Some(false),
        };
        let json = serde_json::to_value(&req).unwrap();
        assert_eq!(json["model"], "voxtral-mini-transcribe-26-02");
        assert_eq!(json["file_url"], "https://example.com/sample.wav");
        assert!(json.get("file_id").is_none());
        assert_eq!(json["language"], "en");
    }

    #[test]
    fn test_transcription_request_serialization_with_file_id() {
        let req = MistralTranscriptionRequest {
            model: "voxtral-mini-transcribe-2507",
            file_url: None,
            file_id: Some("file-123"),
            language: None,
            stream: Some(false),
        };
        let json = serde_json::to_value(&req).unwrap();
        assert_eq!(json["file_id"], "file-123");
        assert!(json.get("file_url").is_none());
    }

    #[test]
    fn test_ocr_request_serialization() {
        let req = MistralOcrRequest {
            model: "mistral-ocr-latest",
            document: MistralOcrDocument {
                doc_type: "document_url",
                document_url: "https://example.com/file.pdf",
            },
        };
        let json = serde_json::to_value(&req).unwrap();
        assert_eq!(json["model"], "mistral-ocr-latest");
        assert_eq!(json["document"]["type"], "document_url");
        assert_eq!(
            json["document"]["document_url"],
            "https://example.com/file.pdf"
        );
    }

    #[test]
    fn test_create_voice_request_serialization() {
        let req = MistralCreateVoiceRequest {
            name: "Edgequake Voice",
            sample_audio: "dGVzdA==",
            sample_filename: Some("sample.wav"),
            languages: Some(vec!["en".to_string()]),
            gender: Some("female"),
            age: Some(30),
            color: None,
            slug: None,
            tags: Some(vec!["ci".to_string()]),
            retention_notice: Some(30),
        };
        let json = serde_json::to_value(&req).unwrap();
        assert_eq!(json["name"], "Edgequake Voice");
        assert_eq!(json["sample_audio"], "dGVzdA==");
        assert_eq!(json["sample_filename"], "sample.wav");
        assert_eq!(json["languages"][0], "en");
    }

    #[test]
    fn test_speech_requires_voice_or_ref_audio_validation() {
        let req = MistralSpeechRequest {
            model: "voxtral-mini-tts-latest",
            input: "hello",
            voice_id: None,
            ref_audio: None,
            response_format: Some("mp3"),
            stream: Some(false),
        };
        std::env::set_var("MISTRAL_API_KEY", "test-key");
        let p = MistralProvider::new(
            "test-key".to_string(),
            MISTRAL_DEFAULT_MODEL.to_string(),
            MISTRAL_DEFAULT_EMBEDDING_MODEL.to_string(),
            None,
        )
        .unwrap();
        let rt = tokio::runtime::Runtime::new().unwrap();
        let err = rt.block_on(async { p.speech(&req).await.unwrap_err() });
        assert!(matches!(err, LlmError::InvalidRequest(_)));
        std::env::remove_var("MISTRAL_API_KEY");
    }

    #[test]
    fn test_transcribe_requires_exactly_one_source_validation() {
        std::env::set_var("MISTRAL_API_KEY", "test-key");
        let p = MistralProvider::new(
            "test-key".to_string(),
            MISTRAL_DEFAULT_MODEL.to_string(),
            MISTRAL_DEFAULT_EMBEDDING_MODEL.to_string(),
            None,
        )
        .unwrap();
        let req_none = MistralTranscriptionRequest {
            model: "voxtral-mini-transcribe-2507",
            file_url: None,
            file_id: None,
            language: None,
            stream: Some(false),
        };
        let rt = tokio::runtime::Runtime::new().unwrap();
        let err_none = rt.block_on(async { p.transcribe(&req_none).await.unwrap_err() });
        assert!(matches!(err_none, LlmError::InvalidRequest(_)));

        let req_both = MistralTranscriptionRequest {
            model: "voxtral-mini-transcribe-2507",
            file_url: Some("https://example.com/a.wav"),
            file_id: Some("file-1"),
            language: None,
            stream: Some(false),
        };
        let err_both = rt.block_on(async { p.transcribe(&req_both).await.unwrap_err() });
        assert!(matches!(err_both, LlmError::InvalidRequest(_)));
        std::env::remove_var("MISTRAL_API_KEY");
    }
}