blazen-cabi 0.5.2

Hand-rolled C ABI over blazen-uniffi for the Ruby gem (via cbindgen + FFI gem) and any other FFI host
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
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
//! Cabi `CustomProvider` — opaque handle wrapping `blazen_llm`'s typed-trait
//! [`CustomProviderHandle`] plus a C-callable vtable that Ruby (or any other
//! cbindgen-driven host) fills out to provide a *typed* foreign implementation
//! of the 16-method `CustomProvider` trait.
//!
//! ## V2 model (Phase B-Pivot.1)
//!
//! `blazen_llm::providers::custom::CustomProvider` is a foreign-implementable
//! trait with 16 typed async methods (completion + streaming + embedding plus
//! 13 compute/media capabilities). Every method has a default
//! `BlazenError::Unsupported` implementation; users override only the methods
//! their provider actually supports.
//!
//! The cabi exposes that trait via [`BlazenCustomProviderVTable`] — a flat
//! `#[repr(C)]` struct of 16 `extern "C"` function pointers plus a
//! `user_data` opaque context and a `drop_user_data` thunk. Each function
//! pointer accepts typed `*const`/`*mut` opaque request handles (mirroring the
//! types already exposed in [`crate::compute_requests`] and
//! [`crate::llm_records`]) and writes a typed result back into a caller-
//! supplied out-pointer (or an error into `out_err`).
//!
//! The Rust-side adapter [`CabiCustomProviderAdapter`] implements the typed
//! trait by dispatching each call to the vtable on a `spawn_blocking` worker
//! (so foreign callbacks may block while doing their own async work without
//! starving the tokio scheduler — same pattern as
//! [`crate::step_handler::CStepHandler`] / [`crate::stream_sink::CStreamSink`]).
//!
//! ## Factories exposed
//!
//! - [`blazen_custom_provider_from_vtable`] — fully-typed foreign override.
//!   Caller supplies a [`BlazenCustomProviderVTable`] (the vtable is consumed
//!   and its `user_data` ownership transfers to the adapter; the adapter
//!   invokes `drop_user_data` exactly once on drop).
//! - [`blazen_custom_provider_openai_compat`] — arbitrary OpenAI-compatible
//!   server (clones the supplied [`crate::provider_api_protocol::BlazenOpenAiCompatConfig`]).
//! - [`blazen_custom_provider_ollama`] — convenience for Ollama servers.
//! - [`blazen_custom_provider_lm_studio`] — convenience for LM Studio servers.
//!
//! ## Async return values
//!
//! Each typed-call C entry point returns a `*mut BlazenFuture` that the host
//! polls / waits on, then pops the typed result with the matching
//! `blazen_future_take_custom_*` entry point. The typed-take wrappers in this
//! module monomorphise [`crate::future::BlazenFuture::take_typed`] onto the
//! result types from [`blazen_llm::compute`].
//!
//! ## Ownership conventions
//!
//! - Every `blazen_custom_provider_*` factory returns a heap-allocated handle
//!   the caller owns; release with [`blazen_custom_provider_free`].
//! - Compute methods take the request handle by value (consuming, via
//!   `Box::from_raw`) so they can move the inner request into the async task
//!   without an extra clone. After the call, the caller must NOT free the
//!   request handle — ownership transferred.
//! - [`blazen_custom_provider_as_base_provider`] clones the inner
//!   `CustomProviderHandle` (which implements `CompletionModel`) into a brand-
//!   new `BaseProvider` whose inner `CompletionModel` is an
//!   `Arc<CustomProviderHandle>`. The returned `BlazenBaseProvider` is
//!   independent of the source handle's lifetime and must be freed with
//!   [`crate::provider_base::blazen_base_provider_free`].

#![allow(dead_code)]
// `blazen_uniffi::BlazenError` is ~168 bytes (every variant inlined). Boxing
// it would force allocations on every `?` and break `From<...> for InnerError`
// patterns; the existing `step_handler` / `stream_sink` modules accept the
// same lint locally. We follow the same convention here.
#![allow(clippy::result_large_err)]

use std::ffi::{c_char, c_void};
use std::pin::Pin;
use std::sync::Arc;

use async_trait::async_trait;
use futures_util::stream::{self, Stream};

use blazen_llm::compute::{
    AudioResult as InnerAudioResult, BackgroundRemovalRequest as InnerBackgroundRemovalRequest,
    ImageRequest as InnerImageRequest, ImageResult as InnerImageResult,
    MusicRequest as InnerMusicRequest, SpeechRequest as InnerSpeechRequest,
    ThreeDRequest as InnerThreeDRequest, ThreeDResult as InnerThreeDResult,
    TranscriptionRequest as InnerTranscriptionRequest,
    TranscriptionResult as InnerTranscriptionResult, UpscaleRequest as InnerUpscaleRequest,
    VideoRequest as InnerVideoRequest, VideoResult as InnerVideoResult,
    VoiceCloneRequest as InnerVoiceCloneRequest, VoiceHandle as InnerVoiceHandle,
};
use blazen_llm::error::BlazenError as LlmError;
use blazen_llm::providers::base::BaseProvider as InnerBaseProvider;
use blazen_llm::providers::custom::{
    self as inner_custom, CustomProvider as InnerCustomProviderTrait,
    CustomProviderHandle as InnerCustomProviderHandle,
};
use blazen_llm::traits::CompletionModel;
use blazen_llm::types::{
    CompletionRequest as LlmCompletionRequest, CompletionResponse as LlmCompletionResponse,
    EmbeddingResponse as LlmEmbeddingResponse, StreamChunk as LlmStreamChunk,
};

use blazen_uniffi::errors::BlazenError as UniffiError;
use blazen_uniffi::llm::{
    CompletionRequest as UniffiCompletionRequest, CompletionResponse as UniffiCompletionResponse,
    EmbeddingResponse as UniffiEmbeddingResponse,
};
use blazen_uniffi::streaming::StreamChunk as UniffiStreamChunk;

use crate::compute_requests::{
    BlazenBackgroundRemovalRequest, BlazenImageRequest, BlazenMusicRequest, BlazenSpeechRequest,
    BlazenThreeDRequest, BlazenTranscriptionRequest, BlazenUpscaleRequest, BlazenVideoRequest,
    BlazenVoiceCloneRequest,
};
use crate::compute_results::{
    BlazenAudioResult, BlazenImageResult, BlazenThreeDResult, BlazenTranscriptionResult,
    BlazenVideoResult, BlazenVoiceHandle,
};
use crate::error::BlazenError;
use crate::future::BlazenFuture;
use crate::llm_records::{
    BlazenCompletionRequest, BlazenCompletionResponse, BlazenEmbeddingResponse,
};
use crate::provider_api_protocol::BlazenOpenAiCompatConfig;
use crate::provider_base::BlazenBaseProvider;
use crate::streaming_records::BlazenStreamChunk;
use crate::string::{alloc_cstring, cstr_to_str};

// ---------------------------------------------------------------------------
// Handle
// ---------------------------------------------------------------------------

/// Opaque handle wrapping an `Arc<blazen_llm::providers::CustomProviderHandle>`.
///
/// `Arc` so the compute methods can clone-and-move the handle into the
/// spawned async task without forcing a deep copy of the inner defaults
/// bundle on every call.
#[repr(C)]
pub struct BlazenCustomProvider(pub(crate) Arc<InnerCustomProviderHandle>);

impl BlazenCustomProvider {
    pub(crate) fn into_ptr(self) -> *mut BlazenCustomProvider {
        Box::into_raw(Box::new(self))
    }
}

impl From<Arc<InnerCustomProviderHandle>> for BlazenCustomProvider {
    fn from(inner: Arc<InnerCustomProviderHandle>) -> Self {
        Self(inner)
    }
}

impl From<InnerCustomProviderHandle> for BlazenCustomProvider {
    fn from(inner: InnerCustomProviderHandle) -> Self {
        Self(Arc::new(inner))
    }
}

// ---------------------------------------------------------------------------
// Error bridging
// ---------------------------------------------------------------------------

/// Bridge a `blazen_uniffi` error (the error type the cabi surface speaks
/// natively) back into the `blazen_llm` error type expected by the
/// `CustomProvider` trait. Lossy by design — variants the upstream trait
/// can't represent collapse to `Request` / `Provider`.
fn uniffi_err_to_llm(err: UniffiError) -> LlmError {
    match err {
        UniffiError::Auth { message } => LlmError::Auth { message },
        UniffiError::RateLimit { retry_after_ms, .. } => LlmError::RateLimit { retry_after_ms },
        UniffiError::Timeout { elapsed_ms, .. } => LlmError::Timeout { elapsed_ms },
        UniffiError::Validation { message } => LlmError::Validation {
            field: None,
            message,
        },
        UniffiError::ContentPolicy { message } => LlmError::ContentPolicy { message },
        UniffiError::Unsupported { message } => LlmError::Unsupported { message },
        UniffiError::Tool { message } => LlmError::Tool {
            name: None,
            message,
        },
        UniffiError::Provider {
            provider,
            message,
            status,
            ..
        } => LlmError::Provider {
            provider: provider.unwrap_or_default(),
            message,
            status_code: status.and_then(|s| u16::try_from(s).ok()),
        },
        UniffiError::Compute { message }
        | UniffiError::Media { message }
        | UniffiError::Internal { message }
        | UniffiError::Workflow { message }
        | UniffiError::Peer { message, .. }
        | UniffiError::Persist { message }
        | UniffiError::Prompt { message, .. }
        | UniffiError::Memory { message, .. }
        | UniffiError::Cache { message, .. } => LlmError::request(message),
        UniffiError::Cancelled => LlmError::request("cancelled"),
    }
}

// ---------------------------------------------------------------------------
// VTable
// ---------------------------------------------------------------------------

/// C-side vtable for a foreign [`InnerCustomProviderTrait`] implementation.
///
/// All 18 fields are required (no nullable function pointers): the four
/// metadata fields (`provider_id`, `model_id`, `user_data`, `drop_user_data`),
/// and 16 typed-method fn-pointers — one per [`InnerCustomProviderTrait`]
/// method.
///
/// The vtable is consumed by [`blazen_custom_provider_from_vtable`] which
/// takes ownership of `user_data` for the lifetime of the resulting handle.
/// `drop_user_data` is invoked exactly once when the wrapping
/// [`CabiCustomProviderAdapter`] drops.
///
/// ## Ownership conventions per call
///
/// - **Request pointers** (e.g. `*mut BlazenSpeechRequest`): caller-owned and
///   the foreign callback OWNS them — must free via the matching `_free`
///   function before returning, OR consume them into a derivative structure.
/// - **Result pointers** (e.g. `out_result: *mut *mut BlazenAudioResult`):
///   on a success return (status 0) the foreign callback writes a fresh
///   caller-owned `*mut BlazenAudioResult` (produced by one of the
///   `compute_results` constructors) into the slot; ownership transfers back
///   to the Rust adapter.
/// - **Error pointers** (`out_err: *mut *mut BlazenError`): on a failure
///   return (status -1) the foreign callback writes a fresh caller-owned
///   `*mut BlazenError` into the slot; ownership transfers back to the Rust
///   adapter.
///
/// Every callback returns `0` on success or `-1` on failure. A `-1` return
/// surfaces as the corresponding [`InnerCustomProviderTrait`] method's
/// `Err(BlazenError)`.
///
/// ## Thread safety
///
/// The Rust adapter dispatches each callback through
/// `tokio::task::spawn_blocking`, so the foreign side is invoked from a
/// blocking-pool worker that may differ from the thread that registered the
/// vtable. The foreign side guarantees `user_data` and the function pointers
/// are safe to invoke from any thread (Ruby's `ffi` gem reacquires the GVL
/// automatically for declared `FFI::Callback` signatures; native hosts must
/// opt into thread-safety in their own runtime model).
#[repr(C)]
pub struct BlazenCustomProviderVTable {
    /// Opaque foreign-side context handed back to each function pointer.
    /// Owned by this vtable struct (released via `drop_user_data` on drop).
    pub user_data: *mut c_void,

    /// Called exactly once when the wrapping `CabiCustomProviderAdapter`
    /// drops. Implementations should reclaim and release `user_data`.
    pub drop_user_data: extern "C" fn(user_data: *mut c_void),

    /// Stable provider identifier (NUL-terminated UTF-8) — fed back by
    /// [`InnerCustomProviderTrait::provider_id`]. Owned by the vtable
    /// struct (NOT released by the adapter; the foreign side is expected to
    /// either return a static string or to free this pointer in
    /// `drop_user_data`).
    pub provider_id: *const c_char,

    /// Model identifier (NUL-terminated UTF-8) — fed back by
    /// [`InnerCustomProviderTrait::model_id`]. Same ownership as
    /// `provider_id`. A null pointer reuses `provider_id`.
    pub model_id: *const c_char,

    // ---- Completion / streaming / embedding ----
    /// Non-streaming chat completion.
    pub complete: extern "C" fn(
        user_data: *mut c_void,
        request: *mut BlazenCompletionRequest,
        out_response: *mut *mut BlazenCompletionResponse,
        out_err: *mut *mut BlazenError,
    ) -> i32,

    /// Streaming chat completion. The foreign callback receives an opaque
    /// [`BlazenStreamPusher`] sink it pushes chunks into via
    /// [`blazen_stream_pusher_push`], finishing with either
    /// [`blazen_stream_pusher_end`] or [`blazen_stream_pusher_error`].
    ///
    /// Returns `0` on a successful stream START (chunks are then pushed
    /// asynchronously); `-1` if the stream can't be opened at all (writing a
    /// fresh `*mut BlazenError` into `*out_err`).
    pub stream: extern "C" fn(
        user_data: *mut c_void,
        request: *mut BlazenCompletionRequest,
        pusher: *mut BlazenStreamPusher,
        out_err: *mut *mut BlazenError,
    ) -> i32,

    /// Batch text embedding. `texts` is an array of `count` NUL-terminated
    /// UTF-8 buffers — caller-owned for the duration of the call; the
    /// foreign callback MUST NOT free the underlying string memory (the
    /// adapter releases the backing Rust allocations after the call returns).
    pub embed: extern "C" fn(
        user_data: *mut c_void,
        texts: *const *const c_char,
        count: usize,
        out_response: *mut *mut BlazenEmbeddingResponse,
        out_err: *mut *mut BlazenError,
    ) -> i32,

    // ---- Audio ----
    pub text_to_speech: extern "C" fn(
        user_data: *mut c_void,
        request: *mut BlazenSpeechRequest,
        out_result: *mut *mut BlazenAudioResult,
        out_err: *mut *mut BlazenError,
    ) -> i32,

    pub generate_music: extern "C" fn(
        user_data: *mut c_void,
        request: *mut BlazenMusicRequest,
        out_result: *mut *mut BlazenAudioResult,
        out_err: *mut *mut BlazenError,
    ) -> i32,

    pub generate_sfx: extern "C" fn(
        user_data: *mut c_void,
        request: *mut BlazenMusicRequest,
        out_result: *mut *mut BlazenAudioResult,
        out_err: *mut *mut BlazenError,
    ) -> i32,

    // ---- Voice cloning ----
    pub clone_voice: extern "C" fn(
        user_data: *mut c_void,
        request: *mut BlazenVoiceCloneRequest,
        out_result: *mut *mut BlazenVoiceHandle,
        out_err: *mut *mut BlazenError,
    ) -> i32,

    /// On success, the callback writes a fresh array of caller-owned
    /// `*mut BlazenVoiceHandle` pointers (count in `*out_count`). The Rust
    /// adapter takes ownership of each element and frees the outer pointer-
    /// array via [`blazen_voice_handle_list_free`].
    pub list_voices: extern "C" fn(
        user_data: *mut c_void,
        out_array: *mut *mut *mut BlazenVoiceHandle,
        out_count: *mut usize,
        out_err: *mut *mut BlazenError,
    ) -> i32,

    pub delete_voice: extern "C" fn(
        user_data: *mut c_void,
        voice: *mut BlazenVoiceHandle,
        out_err: *mut *mut BlazenError,
    ) -> i32,

    // ---- Image / video ----
    pub generate_image: extern "C" fn(
        user_data: *mut c_void,
        request: *mut BlazenImageRequest,
        out_result: *mut *mut BlazenImageResult,
        out_err: *mut *mut BlazenError,
    ) -> i32,

    pub upscale_image: extern "C" fn(
        user_data: *mut c_void,
        request: *mut BlazenUpscaleRequest,
        out_result: *mut *mut BlazenImageResult,
        out_err: *mut *mut BlazenError,
    ) -> i32,

    pub text_to_video: extern "C" fn(
        user_data: *mut c_void,
        request: *mut BlazenVideoRequest,
        out_result: *mut *mut BlazenVideoResult,
        out_err: *mut *mut BlazenError,
    ) -> i32,

    pub image_to_video: extern "C" fn(
        user_data: *mut c_void,
        request: *mut BlazenVideoRequest,
        out_result: *mut *mut BlazenVideoResult,
        out_err: *mut *mut BlazenError,
    ) -> i32,

    pub transcribe: extern "C" fn(
        user_data: *mut c_void,
        request: *mut BlazenTranscriptionRequest,
        out_result: *mut *mut BlazenTranscriptionResult,
        out_err: *mut *mut BlazenError,
    ) -> i32,

    pub generate_3d: extern "C" fn(
        user_data: *mut c_void,
        request: *mut BlazenThreeDRequest,
        out_result: *mut *mut BlazenThreeDResult,
        out_err: *mut *mut BlazenError,
    ) -> i32,

    pub remove_background: extern "C" fn(
        user_data: *mut c_void,
        request: *mut BlazenBackgroundRemovalRequest,
        out_result: *mut *mut BlazenImageResult,
        out_err: *mut *mut BlazenError,
    ) -> i32,
}

// SAFETY: the foreign side guarantees thread-safety of `user_data` and the
// function pointers, as documented on `BlazenCustomProviderVTable`. Ruby's
// `ffi` gem automatically reacquires the GVL for callbacks; Dart's
// `NativeCallable.listener` marshals back to the isolate event loop; native
// hosts must opt into thread-safety in their own runtime model.
unsafe impl Send for BlazenCustomProviderVTable {}
// SAFETY: see the `Send` impl above — same foreign-side guarantee covers
// shared-reference access from multiple threads.
unsafe impl Sync for BlazenCustomProviderVTable {}

// ---------------------------------------------------------------------------
// Stream pusher (one-shot channel from foreign callback into a Rust stream)
// ---------------------------------------------------------------------------

/// Opaque handle the foreign `stream` callback writes chunks into. The Rust
/// adapter wraps the receiving end in a `Pin<Box<dyn Stream<...>>>` that
/// drives the typed [`InnerCustomProviderTrait::stream`] return value.
///
/// Each pushed chunk is a `*mut BlazenStreamChunk` whose ownership transfers
/// into the pusher. The foreign caller terminates the stream with EXACTLY
/// ONE of:
/// - [`blazen_stream_pusher_end`] — graceful EOF.
/// - [`blazen_stream_pusher_error`] — fatal error.
///
/// Sending after termination is silently ignored (the channel half is
/// already closed).
pub struct BlazenStreamPusher {
    tx: tokio::sync::mpsc::UnboundedSender<Result<LlmStreamChunk, UniffiError>>,
}

impl BlazenStreamPusher {
    fn into_ptr(self) -> *mut BlazenStreamPusher {
        Box::into_raw(Box::new(self))
    }
}

/// Pushes a single `BlazenStreamChunk` into the stream. Consumes `chunk`.
/// No-op on null `pusher` or `chunk`. Returns `0` on success, `-1` if the
/// receiver has already been dropped (the stream consumer disappeared).
///
/// # Safety
///
/// `pusher` must be null OR a live `BlazenStreamPusher` previously handed to
/// the foreign callback by the cabi adapter. `chunk` must be null OR a live
/// `BlazenStreamChunk`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_stream_pusher_push(
    pusher: *mut BlazenStreamPusher,
    chunk: *mut BlazenStreamChunk,
) -> i32 {
    if pusher.is_null() || chunk.is_null() {
        if !chunk.is_null() {
            // SAFETY: per the contract, `chunk` came from `Box::into_raw`.
            drop(unsafe { Box::from_raw(chunk) });
        }
        return -1;
    }
    // SAFETY: caller guarantees `pusher` is live.
    let p = unsafe { &*pusher };
    // SAFETY: caller transferred ownership of `chunk`.
    let chunk_box = unsafe { Box::from_raw(chunk) };
    // The cabi handle holds a `blazen_uniffi::streaming::StreamChunk`;
    // convert to the `blazen_llm::types::StreamChunk` shape that the
    // upstream stream consumes.
    let uniffi_chunk: UniffiStreamChunk = chunk_box.0;
    let delta = if uniffi_chunk.content_delta.is_empty() {
        None
    } else {
        Some(uniffi_chunk.content_delta)
    };
    let finish_reason = if uniffi_chunk.is_final {
        Some("stop".to_owned())
    } else {
        None
    };
    // Tool-call deltas don't round-trip via the cabi wire today; the
    // foreign side that needs them can synthesise them through the
    // upstream `complete_streaming` path instead.
    let llm_chunk = LlmStreamChunk {
        delta,
        finish_reason,
        ..LlmStreamChunk::default()
    };
    match p.tx.send(Ok(llm_chunk)) {
        Ok(()) => 0,
        Err(_) => -1,
    }
}

/// Signals end-of-stream gracefully. After this call the pusher is closed —
/// further pushes are no-ops. No-op on null `pusher`.
///
/// The foreign callback MUST call exactly one of `blazen_stream_pusher_end`
/// or [`blazen_stream_pusher_error`] before returning ownership of the
/// pusher to the Rust adapter; the adapter assumes the channel is sealed
/// once the synchronous callback returns success.
///
/// # Safety
///
/// `pusher` must be null OR a live `BlazenStreamPusher`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_stream_pusher_end(pusher: *mut BlazenStreamPusher) {
    if pusher.is_null() {
        return;
    }
    // SAFETY: per the contract, `pusher` came from `Box::into_raw` inside the
    // adapter's stream-method dispatch. Reclaiming via `Box::from_raw` drops
    // the sender, which closes the channel.
    drop(unsafe { Box::from_raw(pusher) });
}

/// Signals a fatal stream error. Consumes `err`. No-op on null `pusher`
/// (still frees `err` if non-null).
///
/// # Safety
///
/// `pusher` must be null OR a live `BlazenStreamPusher`. `err` must be null
/// OR a live `BlazenError` produced by the cabi surface; ownership transfers
/// into the pusher (or is dropped on a null-pusher path).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_stream_pusher_error(
    pusher: *mut BlazenStreamPusher,
    err: *mut BlazenError,
) {
    if pusher.is_null() {
        if !err.is_null() {
            // SAFETY: per the contract, `err` came from `Box::into_raw`.
            drop(unsafe { Box::from_raw(err) });
        }
        return;
    }
    // SAFETY: per the contract, `pusher` came from `Box::into_raw`.
    let p_box = unsafe { Box::from_raw(pusher) };
    let inner_err = if err.is_null() {
        UniffiError::Internal {
            message: "stream pusher closed without an error payload".into(),
        }
    } else {
        // SAFETY: per the contract, `err` came from `Box::into_raw`.
        unsafe { Box::from_raw(err) }.inner
    };
    // Send the error then let the pusher box drop, sealing the channel.
    let _ = p_box.tx.send(Err(inner_err));
}

// ---------------------------------------------------------------------------
// Adapter — bridges the C vtable into `InnerCustomProviderTrait`
// ---------------------------------------------------------------------------

/// Rust-side trampoline wrapping a foreign [`BlazenCustomProviderVTable`].
/// Implements [`InnerCustomProviderTrait`] by dispatching each method to the
/// vtable's function pointers on a `tokio::task::spawn_blocking` worker.
///
/// Owns the vtable's `user_data` — drops it via `drop_user_data` exactly
/// once when this adapter drops.
pub struct BlazenCabiCustomProviderAdapter {
    vtable: BlazenCustomProviderVTable,
    provider_id_cached: String,
    model_id_cached: String,
}

impl Drop for BlazenCabiCustomProviderAdapter {
    fn drop(&mut self) {
        (self.vtable.drop_user_data)(self.vtable.user_data);
    }
}

impl std::fmt::Debug for BlazenCabiCustomProviderAdapter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BlazenCabiCustomProviderAdapter")
            .field("provider_id", &self.provider_id_cached)
            .field("model_id", &self.model_id_cached)
            .finish_non_exhaustive()
    }
}

impl BlazenCabiCustomProviderAdapter {
    /// Reads `provider_id` / `model_id` out of the vtable's C-string fields
    /// and caches them as owned `String`s. Falls back to sensible defaults
    /// when the foreign side supplies null pointers (e.g. `model_id == NULL`
    /// reuses `provider_id`).
    fn new(vtable: BlazenCustomProviderVTable) -> Self {
        // SAFETY: per the vtable contract, `provider_id` is either null or a
        // valid NUL-terminated UTF-8 buffer.
        let provider_id_cached = unsafe { cstr_to_str(vtable.provider_id) }
            .unwrap_or("custom")
            .to_owned();
        // SAFETY: same contract for `model_id`.
        let model_id_cached = unsafe { cstr_to_str(vtable.model_id) }
            .map_or_else(|| provider_id_cached.clone(), str::to_owned);
        Self {
            vtable,
            provider_id_cached,
            model_id_cached,
        }
    }
}

// Helpers --------------------------------------------------------------------

/// Spawn-blocking a typed foreign call that consumes a request handle and
/// produces a typed result handle. Bridges the C ABI (status + out-ptrs)
/// into a Rust `Result<Inner, LlmError>`.
///
/// `req_addr` is the `usize`-encoded address of a `*mut Req` (a raw pointer
/// is `!Send`; passing the address as a primitive lets the closure cross
/// the spawn-blocking boundary cleanly). The foreign callback takes
/// ownership of the request and must free it.
async fn dispatch_typed_with_request<Req, ResHandle, Res>(
    user_data_addr: usize,
    func: extern "C" fn(*mut c_void, *mut Req, *mut *mut ResHandle, *mut *mut BlazenError) -> i32,
    req_addr: usize,
) -> Result<Res, LlmError>
where
    Req: 'static,
    ResHandle: 'static,
    Res: Send + 'static,
    Box<ResHandle>: TakeInner<Res>,
{
    // SAFETY: foreign side guarantees thread-safe access to `user_data`
    // (see the `BlazenCustomProviderVTable` docs). Function pointer is
    // `Copy + Send + Sync`. The request pointer was just minted from
    // `Box::into_raw` upstream so it's a unique allocation we're handing
    // off to the callback.
    let join =
        tokio::task::spawn_blocking(move || -> Result<Res, UniffiError> {
            let user_data = user_data_addr as *mut c_void;
            let req_ptr = req_addr as *mut Req;
            let mut out_handle: *mut ResHandle = std::ptr::null_mut();
            let mut out_err: *mut BlazenError = std::ptr::null_mut();

            let status = func(user_data, req_ptr, &raw mut out_handle, &raw mut out_err);
            if status == 0 {
                if out_handle.is_null() {
                    return Err(UniffiError::Internal {
                        message: "CustomProvider vtable returned 0 without writing out_handle"
                            .into(),
                    });
                }
                // SAFETY: per the vtable contract, on success the foreign
                // callback wrote a fresh caller-owned `*mut ResHandle` produced
                // by one of the cabi result constructors. Ownership transfers
                // to us; reclaim via `Box::from_raw` and unwrap to the inner.
                let boxed = unsafe { Box::from_raw(out_handle) };
                Ok(<Box<ResHandle> as TakeInner<Res>>::take_inner(boxed))
            } else {
                if out_err.is_null() {
                    return Err(UniffiError::Internal {
                        message: format!(
                            "CustomProvider vtable returned -1 (status={status}) without writing out_err"
                        ),
                    });
                }
                // SAFETY: per the vtable contract, on failure the foreign
                // callback wrote a fresh `*mut BlazenError`.
                let be = unsafe { Box::from_raw(out_err) };
                Err(be.inner)
            }
        })
        .await;

    match join {
        Ok(Ok(v)) => Ok(v),
        Ok(Err(e)) => Err(uniffi_err_to_llm(e)),
        Err(join_err) => Err(LlmError::request(format!(
            "CustomProvider vtable task panicked: {join_err}"
        ))),
    }
}

/// Helper trait letting `dispatch_typed_with_request` unbox cabi result
/// handles into their inner `blazen_llm` core types without leaking the
/// `Box<BlazenXxx>(InnerXxx)` shape into the generic signature.
trait TakeInner<Out> {
    fn take_inner(self) -> Out;
}

impl TakeInner<InnerAudioResult> for Box<BlazenAudioResult> {
    fn take_inner(self) -> InnerAudioResult {
        self.0
    }
}
impl TakeInner<InnerImageResult> for Box<BlazenImageResult> {
    fn take_inner(self) -> InnerImageResult {
        self.0
    }
}
impl TakeInner<InnerVideoResult> for Box<BlazenVideoResult> {
    fn take_inner(self) -> InnerVideoResult {
        self.0
    }
}
impl TakeInner<InnerTranscriptionResult> for Box<BlazenTranscriptionResult> {
    fn take_inner(self) -> InnerTranscriptionResult {
        self.0
    }
}
impl TakeInner<InnerThreeDResult> for Box<BlazenThreeDResult> {
    fn take_inner(self) -> InnerThreeDResult {
        self.0
    }
}
impl TakeInner<InnerVoiceHandle> for Box<BlazenVoiceHandle> {
    fn take_inner(self) -> InnerVoiceHandle {
        self.0
    }
}

#[async_trait]
impl InnerCustomProviderTrait for BlazenCabiCustomProviderAdapter {
    fn provider_id(&self) -> &str {
        &self.provider_id_cached
    }

    fn model_id(&self) -> &str {
        &self.model_id_cached
    }

    async fn complete(
        &self,
        request: LlmCompletionRequest,
    ) -> Result<LlmCompletionResponse, LlmError> {
        // Convert from the blazen_llm typed request into the cabi wire shape
        // (which the foreign side already understands via `blazen_completion_request_*`).
        let uniffi_req: UniffiCompletionRequest = llm_to_uniffi_request(request);
        let req_handle = BlazenCompletionRequest::from(uniffi_req).into_ptr();

        let user_data_addr = self.vtable.user_data as usize;
        let complete_fn = self.vtable.complete;
        let req_addr = req_handle as usize;

        // SAFETY: foreign side guarantees thread-safe access; the request
        // pointer was just minted from `Box::into_raw`.
        let join = tokio::task::spawn_blocking(move || -> Result<LlmCompletionResponse, UniffiError> {
            let user_data = user_data_addr as *mut c_void;
            let req_ptr = req_addr as *mut BlazenCompletionRequest;
            let mut out_response: *mut BlazenCompletionResponse = std::ptr::null_mut();
            let mut out_err: *mut BlazenError = std::ptr::null_mut();

            let status = complete_fn(user_data, req_ptr, &raw mut out_response, &raw mut out_err);
            if status == 0 {
                if out_response.is_null() {
                    return Err(UniffiError::Internal {
                        message: "CustomProvider complete returned 0 without writing out_response".into(),
                    });
                }
                // SAFETY: per the vtable contract, ownership transferred to us.
                let resp_box = unsafe { Box::from_raw(out_response) };
                Ok(uniffi_to_llm_response(resp_box.0))
            } else {
                if out_err.is_null() {
                    return Err(UniffiError::Internal {
                        message: format!("CustomProvider complete returned -1 (status={status}) without writing out_err"),
                    });
                }
                // SAFETY: per the vtable contract.
                let be = unsafe { Box::from_raw(out_err) };
                Err(be.inner)
            }
        })
        .await;

        match join {
            Ok(Ok(v)) => Ok(v),
            Ok(Err(e)) => Err(uniffi_err_to_llm(e)),
            Err(join_err) => Err(LlmError::request(format!(
                "CustomProvider complete task panicked: {join_err}"
            ))),
        }
    }

    async fn stream(
        &self,
        request: LlmCompletionRequest,
    ) -> Result<Pin<Box<dyn Stream<Item = Result<LlmStreamChunk, LlmError>> + Send>>, LlmError>
    {
        // Build cabi request handle.
        let uniffi_req: UniffiCompletionRequest = llm_to_uniffi_request(request);
        let req_handle = BlazenCompletionRequest::from(uniffi_req).into_ptr();

        // Build the pusher: the channel's RX side becomes the returned
        // Stream; the TX side is moved into the BlazenStreamPusher handle
        // we hand to the foreign callback.
        let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
        let pusher_ptr = BlazenStreamPusher { tx }.into_ptr();

        let user_data_addr = self.vtable.user_data as usize;
        let stream_fn = self.vtable.stream;
        let req_addr = req_handle as usize;
        let pusher_addr = pusher_ptr as usize;

        // Drive the foreign callback synchronously on a blocking worker. The
        // foreign side is expected to either push chunks inline before
        // returning OR spawn its own producer that holds the pusher pointer
        // for the duration of the stream. Either way: `status` reports
        // whether the stream STARTED successfully.
        //
        // SAFETY: foreign side guarantees thread-safe access; the request
        // and pusher pointers were just minted from `Box::into_raw`.
        let start_status = tokio::task::spawn_blocking(move || -> Result<(), UniffiError> {
            let user_data = user_data_addr as *mut c_void;
            let req_ptr = req_addr as *mut BlazenCompletionRequest;
            let pusher = pusher_addr as *mut BlazenStreamPusher;
            let mut out_err: *mut BlazenError = std::ptr::null_mut();

            let status = stream_fn(user_data, req_ptr, pusher, &raw mut out_err);
            if status == 0 {
                Ok(())
            } else {
                if out_err.is_null() {
                    return Err(UniffiError::Internal {
                        message: format!(
                            "CustomProvider stream returned -1 (status={status}) without writing out_err"
                        ),
                    });
                }
                // SAFETY: per the vtable contract.
                let be = unsafe { Box::from_raw(out_err) };
                Err(be.inner)
            }
        })
        .await;

        match start_status {
            Ok(Ok(())) => {}
            Ok(Err(e)) => return Err(uniffi_err_to_llm(e)),
            Err(join_err) => {
                return Err(LlmError::request(format!(
                    "CustomProvider stream start task panicked: {join_err}"
                )));
            }
        }

        // Build the stream that pulls from the channel and converts each
        // item from the uniffi-error to the llm-error type. After a fatal
        // error the foreign side has already dropped the pusher (via
        // `blazen_stream_pusher_error`), so `recv` returns `None` on the
        // next iteration and the stream ends.
        let s = stream::unfold(rx, |mut rx| async move {
            match rx.recv().await {
                Some(Ok(chunk)) => Some((Ok(chunk), rx)),
                Some(Err(e)) => Some((Err(uniffi_err_to_llm(e)), rx)),
                None => None,
            }
        });
        Ok(Box::pin(s))
    }

    async fn embed(&self, texts: Vec<String>) -> Result<LlmEmbeddingResponse, LlmError> {
        // Build the owning `CString` vector in the outer scope; move it
        // (and only `Send`-safe values) into the blocking closure. The
        // pointer array gets rebuilt inside the closure right before the
        // foreign call — raw pointers are `!Send`, so we never carry them
        // across the closure boundary.
        let owned: Vec<std::ffi::CString> = texts
            .into_iter()
            .map(|t| {
                std::ffi::CString::new(t).unwrap_or_else(|_| std::ffi::CString::new("").unwrap())
            })
            .collect();

        let user_data_addr = self.vtable.user_data as usize;
        let embed_fn = self.vtable.embed;

        // SAFETY: foreign side guarantees thread-safe access. The CString
        // backing store lives inside the closure (moved by value) so it
        // outlives the foreign call.
        let join = tokio::task::spawn_blocking(
            move || -> Result<LlmEmbeddingResponse, UniffiError> {
                let user_data = user_data_addr as *mut c_void;
                // Build the ptr array INSIDE the closure so we never need
                // to send raw pointers across the `Send` boundary.
                let ptrs: Vec<*const c_char> = owned.iter().map(|s| s.as_ptr()).collect();
                let count = ptrs.len();

                let mut out_response: *mut BlazenEmbeddingResponse = std::ptr::null_mut();
                let mut out_err: *mut BlazenError = std::ptr::null_mut();

                let status = embed_fn(
                    user_data,
                    ptrs.as_ptr(),
                    count,
                    &raw mut out_response,
                    &raw mut out_err,
                );

                // Keep `owned` alive across the call by referencing it after.
                let _keepalive = &owned;

                if status == 0 {
                    if out_response.is_null() {
                        return Err(UniffiError::Internal {
                            message: "CustomProvider embed returned 0 without writing out_response"
                                .into(),
                        });
                    }
                    // SAFETY: per the vtable contract.
                    let resp_box = unsafe { Box::from_raw(out_response) };
                    Ok(uniffi_to_llm_embedding_response(resp_box.0))
                } else {
                    if out_err.is_null() {
                        return Err(UniffiError::Internal {
                            message: format!(
                                "CustomProvider embed returned -1 (status={status}) without writing out_err"
                            ),
                        });
                    }
                    // SAFETY: per the vtable contract.
                    let be = unsafe { Box::from_raw(out_err) };
                    Err(be.inner)
                }
            },
        )
        .await;

        match join {
            Ok(Ok(v)) => Ok(v),
            Ok(Err(e)) => Err(uniffi_err_to_llm(e)),
            Err(join_err) => Err(LlmError::request(format!(
                "CustomProvider embed task panicked: {join_err}"
            ))),
        }
    }

    async fn text_to_speech(&self, req: InnerSpeechRequest) -> Result<InnerAudioResult, LlmError> {
        let user_data_addr = self.vtable.user_data as usize;
        let f = self.vtable.text_to_speech;
        let req_ptr = BlazenSpeechRequest::from(req).into_ptr();
        dispatch_typed_with_request::<BlazenSpeechRequest, BlazenAudioResult, InnerAudioResult>(
            user_data_addr,
            f,
            req_ptr as usize,
        )
        .await
    }

    async fn generate_music(&self, req: InnerMusicRequest) -> Result<InnerAudioResult, LlmError> {
        let user_data_addr = self.vtable.user_data as usize;
        let f = self.vtable.generate_music;
        let req_ptr = BlazenMusicRequest::from(req).into_ptr();
        dispatch_typed_with_request::<BlazenMusicRequest, BlazenAudioResult, InnerAudioResult>(
            user_data_addr,
            f,
            req_ptr as usize,
        )
        .await
    }

    async fn generate_sfx(&self, req: InnerMusicRequest) -> Result<InnerAudioResult, LlmError> {
        let user_data_addr = self.vtable.user_data as usize;
        let f = self.vtable.generate_sfx;
        let req_ptr = BlazenMusicRequest::from(req).into_ptr();
        dispatch_typed_with_request::<BlazenMusicRequest, BlazenAudioResult, InnerAudioResult>(
            user_data_addr,
            f,
            req_ptr as usize,
        )
        .await
    }

    async fn clone_voice(&self, req: InnerVoiceCloneRequest) -> Result<InnerVoiceHandle, LlmError> {
        let user_data_addr = self.vtable.user_data as usize;
        let f = self.vtable.clone_voice;
        let req_ptr = BlazenVoiceCloneRequest::from(req).into_ptr();
        dispatch_typed_with_request::<BlazenVoiceCloneRequest, BlazenVoiceHandle, InnerVoiceHandle>(
            user_data_addr,
            f,
            req_ptr as usize,
        )
        .await
    }

    async fn list_voices(&self) -> Result<Vec<InnerVoiceHandle>, LlmError> {
        let user_data_addr = self.vtable.user_data as usize;
        let f = self.vtable.list_voices;

        // SAFETY: foreign side guarantees thread-safe access to `user_data`.
        let join = tokio::task::spawn_blocking(move || -> Result<Vec<InnerVoiceHandle>, UniffiError> {
            let user_data = user_data_addr as *mut c_void;
            let mut out_array: *mut *mut BlazenVoiceHandle = std::ptr::null_mut();
            let mut out_count: usize = 0;
            let mut out_err: *mut BlazenError = std::ptr::null_mut();

            let status = f(user_data, &raw mut out_array, &raw mut out_count, &raw mut out_err);
            if status == 0 {
                if out_count == 0 {
                    // Empty list path: foreign side may legitimately return
                    // a null array with count 0. Anything else is treated as
                    // an empty list.
                    return Ok(Vec::new());
                }
                if out_array.is_null() {
                    return Err(UniffiError::Internal {
                        message: "CustomProvider list_voices returned non-zero count with null array".into(),
                    });
                }
                // Reclaim the outer pointer array as a Box<[*mut _]> so we
                // can free it correctly later. The foreign side allocated
                // the array with `blazen_voice_handle_list_alloc`, which is
                // the dual of `blazen_voice_handle_list_free`.
                let mut out: Vec<InnerVoiceHandle> = Vec::with_capacity(out_count);
                // SAFETY: caller wrote `out_count` element pointers into a
                // fresh array (as documented on the vtable). Walk the slice
                // and reclaim each handle.
                for i in 0..out_count {
                    // SAFETY: `i < out_count`, and the array spans `out_count`
                    // valid pointer slots per the vtable contract.
                    let h_ptr = unsafe { *out_array.add(i) };
                    if h_ptr.is_null() {
                        return Err(UniffiError::Internal {
                            message: "CustomProvider list_voices: null entry in voice array".into(),
                        });
                    }
                    // SAFETY: per the vtable contract, each entry came from
                    // `Box::into_raw`. Ownership transfers here.
                    let h_box = unsafe { Box::from_raw(h_ptr) };
                    out.push(h_box.0);
                }
                // Free the outer pointer array as a boxed slice. The foreign
                // side allocates it; the cabi reclaims it via the shared
                // `blazen_voice_handle_list_free` shape (boxed slice from
                // `Box::into_raw`).
                let slice_ptr: *mut [*mut BlazenVoiceHandle] =
                    std::ptr::slice_from_raw_parts_mut(out_array, out_count);
                // SAFETY: same boxed-slice round-trip as documented on
                // `blazen_voice_handle_list_free`; the foreign side must
                // allocate the array as a boxed slice for this to be sound.
                drop(unsafe { Box::from_raw(slice_ptr) });
                Ok(out)
            } else {
                if out_err.is_null() {
                    return Err(UniffiError::Internal {
                        message: format!(
                            "CustomProvider list_voices returned -1 (status={status}) without writing out_err"
                        ),
                    });
                }
                // SAFETY: per the vtable contract.
                let be = unsafe { Box::from_raw(out_err) };
                Err(be.inner)
            }
        })
        .await;

        match join {
            Ok(Ok(v)) => Ok(v),
            Ok(Err(e)) => Err(uniffi_err_to_llm(e)),
            Err(join_err) => Err(LlmError::request(format!(
                "CustomProvider list_voices task panicked: {join_err}"
            ))),
        }
    }

    async fn delete_voice(&self, voice: InnerVoiceHandle) -> Result<(), LlmError> {
        let user_data_addr = self.vtable.user_data as usize;
        let f = self.vtable.delete_voice;
        let v_ptr = BlazenVoiceHandle::from(voice).into_ptr();
        let v_addr = v_ptr as usize;

        // SAFETY: foreign side guarantees thread-safe access. The voice
        // pointer was just minted from `Box::into_raw`.
        let join = tokio::task::spawn_blocking(move || -> Result<(), UniffiError> {
            let user_data = user_data_addr as *mut c_void;
            let v_ptr = v_addr as *mut BlazenVoiceHandle;
            let mut out_err: *mut BlazenError = std::ptr::null_mut();

            let status = f(user_data, v_ptr, &raw mut out_err);
            if status == 0 {
                Ok(())
            } else {
                if out_err.is_null() {
                    return Err(UniffiError::Internal {
                        message: format!(
                            "CustomProvider delete_voice returned -1 (status={status}) without writing out_err"
                        ),
                    });
                }
                // SAFETY: per the vtable contract.
                let be = unsafe { Box::from_raw(out_err) };
                Err(be.inner)
            }
        })
        .await;

        match join {
            Ok(Ok(())) => Ok(()),
            Ok(Err(e)) => Err(uniffi_err_to_llm(e)),
            Err(join_err) => Err(LlmError::request(format!(
                "CustomProvider delete_voice task panicked: {join_err}"
            ))),
        }
    }

    async fn generate_image(&self, req: InnerImageRequest) -> Result<InnerImageResult, LlmError> {
        let user_data_addr = self.vtable.user_data as usize;
        let f = self.vtable.generate_image;
        let req_ptr = BlazenImageRequest::from(req).into_ptr();
        dispatch_typed_with_request::<BlazenImageRequest, BlazenImageResult, InnerImageResult>(
            user_data_addr,
            f,
            req_ptr as usize,
        )
        .await
    }

    async fn upscale_image(&self, req: InnerUpscaleRequest) -> Result<InnerImageResult, LlmError> {
        let user_data_addr = self.vtable.user_data as usize;
        let f = self.vtable.upscale_image;
        let req_ptr = BlazenUpscaleRequest::from(req).into_ptr();
        dispatch_typed_with_request::<BlazenUpscaleRequest, BlazenImageResult, InnerImageResult>(
            user_data_addr,
            f,
            req_ptr as usize,
        )
        .await
    }

    async fn text_to_video(&self, req: InnerVideoRequest) -> Result<InnerVideoResult, LlmError> {
        let user_data_addr = self.vtable.user_data as usize;
        let f = self.vtable.text_to_video;
        let req_ptr = BlazenVideoRequest::from(req).into_ptr();
        dispatch_typed_with_request::<BlazenVideoRequest, BlazenVideoResult, InnerVideoResult>(
            user_data_addr,
            f,
            req_ptr as usize,
        )
        .await
    }

    async fn image_to_video(&self, req: InnerVideoRequest) -> Result<InnerVideoResult, LlmError> {
        let user_data_addr = self.vtable.user_data as usize;
        let f = self.vtable.image_to_video;
        let req_ptr = BlazenVideoRequest::from(req).into_ptr();
        dispatch_typed_with_request::<BlazenVideoRequest, BlazenVideoResult, InnerVideoResult>(
            user_data_addr,
            f,
            req_ptr as usize,
        )
        .await
    }

    async fn transcribe(
        &self,
        req: InnerTranscriptionRequest,
    ) -> Result<InnerTranscriptionResult, LlmError> {
        let user_data_addr = self.vtable.user_data as usize;
        let f = self.vtable.transcribe;
        let req_ptr = BlazenTranscriptionRequest::from(req).into_ptr();
        dispatch_typed_with_request::<
            BlazenTranscriptionRequest,
            BlazenTranscriptionResult,
            InnerTranscriptionResult,
        >(user_data_addr, f, req_ptr as usize)
        .await
    }

    async fn generate_3d(&self, req: InnerThreeDRequest) -> Result<InnerThreeDResult, LlmError> {
        let user_data_addr = self.vtable.user_data as usize;
        let f = self.vtable.generate_3d;
        let req_ptr = BlazenThreeDRequest::from(req).into_ptr();
        dispatch_typed_with_request::<BlazenThreeDRequest, BlazenThreeDResult, InnerThreeDResult>(
            user_data_addr,
            f,
            req_ptr as usize,
        )
        .await
    }

    async fn remove_background(
        &self,
        req: InnerBackgroundRemovalRequest,
    ) -> Result<InnerImageResult, LlmError> {
        let user_data_addr = self.vtable.user_data as usize;
        let f = self.vtable.remove_background;
        let req_ptr = BlazenBackgroundRemovalRequest::from(req).into_ptr();
        dispatch_typed_with_request::<
            BlazenBackgroundRemovalRequest,
            BlazenImageResult,
            InnerImageResult,
        >(user_data_addr, f, req_ptr as usize)
        .await
    }
}

// ---------------------------------------------------------------------------
// blazen_llm <-> blazen_uniffi request/response/embedding conversions
// ---------------------------------------------------------------------------

/// Convert a `blazen_llm::types::CompletionRequest` (core trait input) into
/// the `blazen_uniffi::llm::CompletionRequest` shape consumed by the cabi
/// handle. Loss is bounded to fields that have no first-class slot in the
/// uniffi wire format (`stop`, `frequency_penalty`, `presence_penalty`,
/// `seed`, modality / image / audio configs, etc.) — the foreign side that
/// implements `complete` doesn't see them anyway; if it needs them it can
/// add them through the framework's existing extensibility.
fn llm_to_uniffi_request(req: LlmCompletionRequest) -> UniffiCompletionRequest {
    let response_format_json = req.response_format.map(|v| v.to_string());

    let messages = req
        .messages
        .into_iter()
        .map(llm_message_to_uniffi)
        .collect();

    let tools = req
        .tools
        .into_iter()
        .map(|t| blazen_uniffi::llm::Tool {
            name: t.name,
            description: t.description,
            parameters_json: t.parameters.to_string(),
        })
        .collect();

    UniffiCompletionRequest {
        messages,
        tools,
        temperature: req.temperature.map(f64::from),
        max_tokens: req.max_tokens,
        top_p: req.top_p.map(f64::from),
        model: req.model,
        response_format_json,
        system: None,
    }
}

fn llm_message_to_uniffi(msg: blazen_llm::types::ChatMessage) -> blazen_uniffi::llm::ChatMessage {
    use blazen_llm::types::{ContentPart, ImageSource, MessageContent, Role};

    let role = match msg.role {
        Role::System => "system",
        Role::User => "user",
        Role::Assistant => "assistant",
        Role::Tool => "tool",
    }
    .to_owned();

    let (content, media_parts) = match msg.content {
        MessageContent::Text(s) => (s, Vec::new()),
        MessageContent::Image(img) => (String::new(), vec![image_content_to_media(img)]),
        MessageContent::Parts(parts) => {
            let mut text = String::new();
            let mut media = Vec::new();
            for part in parts {
                match part {
                    ContentPart::Text { text: t } => {
                        if !text.is_empty() {
                            text.push('\n');
                        }
                        text.push_str(&t);
                    }
                    ContentPart::Image(img) => {
                        media.push(image_content_to_media(img));
                    }
                    ContentPart::Audio(a) => {
                        let m = blazen_uniffi::llm::Media {
                            kind: "audio".into(),
                            mime_type: a.media_type.unwrap_or_default(),
                            data_base64: match a.source {
                                ImageSource::Base64 { data } => data,
                                other => serde_json::to_string(&other).unwrap_or_default(),
                            },
                        };
                        media.push(m);
                    }
                    ContentPart::Video(v) => {
                        let m = blazen_uniffi::llm::Media {
                            kind: "video".into(),
                            mime_type: v.media_type.unwrap_or_default(),
                            data_base64: match v.source {
                                ImageSource::Base64 { data } => data,
                                other => serde_json::to_string(&other).unwrap_or_default(),
                            },
                        };
                        media.push(m);
                    }
                    ContentPart::File(_) => {}
                }
            }
            (text, media)
        }
    };

    let tool_calls = msg
        .tool_calls
        .into_iter()
        .map(|tc| blazen_uniffi::llm::ToolCall {
            id: tc.id,
            name: tc.name,
            arguments_json: tc.arguments.to_string(),
        })
        .collect();

    blazen_uniffi::llm::ChatMessage {
        role,
        content,
        media_parts,
        tool_calls,
        tool_call_id: msg.tool_call_id,
        name: msg.name,
    }
}

fn image_content_to_media(img: blazen_llm::types::ImageContent) -> blazen_uniffi::llm::Media {
    use blazen_llm::types::ImageSource;
    let data_base64 = match img.source {
        ImageSource::Base64 { data } => data,
        other => serde_json::to_string(&other).unwrap_or_default(),
    };
    blazen_uniffi::llm::Media {
        kind: "image".into(),
        mime_type: img.media_type.unwrap_or_default(),
        data_base64,
    }
}

/// Convert the uniffi-flavoured response back into the core llm shape.
fn uniffi_to_llm_response(resp: UniffiCompletionResponse) -> LlmCompletionResponse {
    let finish_reason = if resp.finish_reason.is_empty() {
        None
    } else {
        Some(resp.finish_reason)
    };
    let usage = if resp.usage.total_tokens == 0
        && resp.usage.prompt_tokens == 0
        && resp.usage.completion_tokens == 0
    {
        None
    } else {
        Some(blazen_llm::types::TokenUsage {
            prompt_tokens: u32::try_from(resp.usage.prompt_tokens).unwrap_or(u32::MAX),
            completion_tokens: u32::try_from(resp.usage.completion_tokens).unwrap_or(u32::MAX),
            total_tokens: u32::try_from(resp.usage.total_tokens).unwrap_or(u32::MAX),
            cached_input_tokens: u32::try_from(resp.usage.cached_input_tokens).unwrap_or(u32::MAX),
            reasoning_tokens: u32::try_from(resp.usage.reasoning_tokens).unwrap_or(u32::MAX),
            audio_input_tokens: 0,
            audio_output_tokens: 0,
        })
    };
    let tool_calls = resp
        .tool_calls
        .into_iter()
        .map(|tc| blazen_llm::types::ToolCall {
            id: tc.id,
            name: tc.name,
            arguments: serde_json::from_str(&tc.arguments_json).unwrap_or_default(),
        })
        .collect();

    LlmCompletionResponse {
        content: if resp.content.is_empty() {
            None
        } else {
            Some(resp.content)
        },
        tool_calls,
        reasoning: None,
        citations: Vec::new(),
        artifacts: Vec::new(),
        usage,
        model: resp.model,
        finish_reason,
        cost: None,
        timing: None,
        images: Vec::new(),
        audio: Vec::new(),
        videos: Vec::new(),
        metadata: serde_json::Value::Null,
    }
}

fn uniffi_to_llm_embedding_response(resp: UniffiEmbeddingResponse) -> LlmEmbeddingResponse {
    // Narrowing f64 -> f32 is intentional: blazen_llm's `EmbeddingResponse`
    // stores f32 (saving 50% memory and matching native model precision);
    // uniffi widens to f64 for cross-language uniformity. Round-trip loss
    // is bounded to the bottom 29 mantissa bits — irrelevant for cosine
    // similarity over typical embedding vectors.
    #[allow(clippy::cast_possible_truncation)]
    let embeddings: Vec<Vec<f32>> = resp
        .embeddings
        .into_iter()
        .map(|v| v.into_iter().map(|d| d as f32).collect())
        .collect();
    let usage = blazen_llm::types::TokenUsage {
        prompt_tokens: u32::try_from(resp.usage.prompt_tokens).unwrap_or(u32::MAX),
        completion_tokens: u32::try_from(resp.usage.completion_tokens).unwrap_or(u32::MAX),
        total_tokens: u32::try_from(resp.usage.total_tokens).unwrap_or(u32::MAX),
        cached_input_tokens: u32::try_from(resp.usage.cached_input_tokens).unwrap_or(u32::MAX),
        reasoning_tokens: u32::try_from(resp.usage.reasoning_tokens).unwrap_or(u32::MAX),
        audio_input_tokens: 0,
        audio_output_tokens: 0,
    };
    let usage =
        if usage.total_tokens == 0 && usage.prompt_tokens == 0 && usage.completion_tokens == 0 {
            None
        } else {
            Some(usage)
        };
    LlmEmbeddingResponse {
        embeddings,
        model: resp.model,
        usage,
        cost: None,
        timing: None,
        metadata: serde_json::Value::Null,
    }
}

// ---------------------------------------------------------------------------
// Factories
// ---------------------------------------------------------------------------

/// Wraps a foreign-supplied [`BlazenCustomProviderVTable`] in an
/// [`InnerCustomProviderHandle`] and hands back a caller-owned handle.
///
/// The vtable's `user_data` ownership transfers into the wrapping
/// [`BlazenCabiCustomProviderAdapter`] for the lifetime of the handle.
/// `drop_user_data` is invoked exactly once when the handle is freed via
/// [`blazen_custom_provider_free`].
///
/// # Safety
///
/// The vtable's function pointers must satisfy the contracts documented on
/// [`BlazenCustomProviderVTable`]. `user_data` ownership transfers; foreign
/// callers must NOT call `drop_user_data` themselves after this returns.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_custom_provider_from_vtable(
    vtable: BlazenCustomProviderVTable,
) -> *mut BlazenCustomProvider {
    let adapter =
        Arc::new(BlazenCabiCustomProviderAdapter::new(vtable)) as Arc<dyn InnerCustomProviderTrait>;
    let handle = InnerCustomProviderHandle::new(adapter);
    BlazenCustomProvider::from(handle).into_ptr()
}

/// Constructs a `CustomProvider` speaking the `OpenAI` Chat Completions wire
/// protocol against an arbitrary `OpenAI`-compatible server.
///
/// Clones the supplied [`BlazenOpenAiCompatConfig`] into the new provider —
/// caller retains ownership of the source config handle. Returns null if
/// `provider_id` or `config` is null / non-UTF-8.
///
/// # Safety
///
/// `provider_id` must be a valid NUL-terminated UTF-8 buffer. `config` must
/// be null OR a live `BlazenOpenAiCompatConfig`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_custom_provider_openai_compat(
    provider_id: *const c_char,
    config: *const BlazenOpenAiCompatConfig,
) -> *mut BlazenCustomProvider {
    if config.is_null() {
        return std::ptr::null_mut();
    }
    // SAFETY: caller upholds the NUL-termination + UTF-8 contract.
    let Some(provider_id) = (unsafe { cstr_to_str(provider_id) }) else {
        return std::ptr::null_mut();
    };
    // SAFETY: caller guarantees live config handle.
    let cfg = unsafe { &*config }.0.clone();
    let handle = inner_custom::openai_compat(provider_id.to_owned(), cfg);
    BlazenCustomProvider::from(handle).into_ptr()
}

/// Convenience factory for an Ollama server.
///
/// Builds the canonical `http://<host>:<port>/v1` base URL and constructs an
/// OpenAI-protocol `CustomProviderHandle` with `provider_id = "ollama"`.
/// `host` being null is treated as `"localhost"`. `port` of `0` is treated
/// as the upstream default (`11434`).
///
/// # Safety
///
/// `host` must be null OR a valid NUL-terminated UTF-8 buffer. `model` must
/// be a valid NUL-terminated UTF-8 buffer.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_custom_provider_ollama(
    model: *const c_char,
    host: *const c_char,
    port: u16,
) -> *mut BlazenCustomProvider {
    // SAFETY: caller upholds the NUL-termination + UTF-8 contract.
    let Some(model) = (unsafe { cstr_to_str(model) }) else {
        return std::ptr::null_mut();
    };
    // SAFETY: caller upholds the NUL-termination + UTF-8 contract on `host`.
    let host_str = unsafe { cstr_to_str(host) }.unwrap_or("localhost");
    let resolved_port = if port == 0 { 11434 } else { port };
    let handle = inner_custom::ollama(host_str, resolved_port, model.to_owned());
    BlazenCustomProvider::from(handle).into_ptr()
}

/// Convenience factory for an LM Studio server.
///
/// Builds the canonical `http://<host>:<port>/v1` base URL and constructs an
/// OpenAI-protocol `CustomProviderHandle` with `provider_id = "lm_studio"`.
/// `host` being null is treated as `"localhost"`. `port` of `0` is treated
/// as the upstream default (`1234`).
///
/// # Safety
///
/// `host` must be null OR a valid NUL-terminated UTF-8 buffer. `model` must
/// be a valid NUL-terminated UTF-8 buffer.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_custom_provider_lm_studio(
    model: *const c_char,
    host: *const c_char,
    port: u16,
) -> *mut BlazenCustomProvider {
    // SAFETY: caller upholds the NUL-termination + UTF-8 contract.
    let Some(model) = (unsafe { cstr_to_str(model) }) else {
        return std::ptr::null_mut();
    };
    // SAFETY: caller upholds the NUL-termination + UTF-8 contract on `host`.
    let host_str = unsafe { cstr_to_str(host) }.unwrap_or("localhost");
    let resolved_port = if port == 0 { 1234 } else { port };
    let handle = inner_custom::lm_studio(host_str, resolved_port, model.to_owned());
    BlazenCustomProvider::from(handle).into_ptr()
}

// ---------------------------------------------------------------------------
// Cross-handle bridges
// ---------------------------------------------------------------------------

/// Returns the inner provider's `provider_id` as a caller-owned C string.
/// Free with [`crate::string::blazen_string_free`]. Returns null on null
/// `p`.
///
/// # Safety
///
/// `p` must be null OR a live `BlazenCustomProvider`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_custom_provider_provider_id(
    p: *const BlazenCustomProvider,
) -> *mut c_char {
    if p.is_null() {
        return std::ptr::null_mut();
    }
    // SAFETY: caller guarantees live handle.
    let h = unsafe { &*p };
    alloc_cstring(h.0.provider_id_str())
}

/// Returns the inner provider's `model_id` as a caller-owned C string. Free
/// with [`crate::string::blazen_string_free`]. Returns null on null `p`.
///
/// # Safety
///
/// `p` must be null OR a live `BlazenCustomProvider`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_custom_provider_model_id(
    p: *const BlazenCustomProvider,
) -> *mut c_char {
    if p.is_null() {
        return std::ptr::null_mut();
    }
    // SAFETY: caller guarantees live handle.
    let h = unsafe { &*p };
    alloc_cstring(<InnerCustomProviderHandle as CompletionModel>::model_id(
        &h.0,
    ))
}

/// Wraps the underlying [`InnerCustomProviderHandle`] inside a brand-new
/// [`InnerBaseProvider`] and returns it as a caller-owned
/// `BlazenBaseProvider` handle.
///
/// This lets the host attach base-provider builder mutations (system
/// prompts, default tools, defaults bundles) without reaching into the
/// private `base` field on `CustomProviderHandle`. The returned
/// `BaseProvider` holds an `Arc<CustomProviderHandle>` as its inner
/// `CompletionModel`, so `complete()` / `stream()` on the result delegate
/// back through the `CustomProviderHandle` (including any vtable dispatch).
///
/// Returns null if `p` is null.
///
/// # Safety
///
/// `p` must be null OR a live `BlazenCustomProvider`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_custom_provider_as_base_provider(
    p: *const BlazenCustomProvider,
) -> *mut BlazenBaseProvider {
    if p.is_null() {
        return std::ptr::null_mut();
    }
    // SAFETY: caller guarantees live handle.
    let inner: Arc<dyn CompletionModel> = unsafe { (*p).0.clone() };
    let bp = InnerBaseProvider::new(inner);
    BlazenBaseProvider::from(bp).into_ptr()
}

/// Frees a `BlazenCustomProvider`. No-op on null.
///
/// # Safety
///
/// `p` must be null OR a pointer produced by one of the
/// `blazen_custom_provider_*` factories. Double-free is undefined behavior.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_custom_provider_free(p: *mut BlazenCustomProvider) {
    if p.is_null() {
        return;
    }
    // SAFETY: per the contract above, `p` came from `Box::into_raw`.
    drop(unsafe { Box::from_raw(p) });
}

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

/// Clones the inner `Arc<CustomProviderHandle>` out of a live handle. The
/// caller is responsible for confirming `p.is_null()` first.
///
/// # Safety
///
/// `p` must be a live, non-null pointer to a `BlazenCustomProvider`.
#[inline]
unsafe fn clone_provider(p: *const BlazenCustomProvider) -> Arc<InnerCustomProviderHandle> {
    // SAFETY: caller guarantees live handle.
    Arc::clone(unsafe { &(*p).0 })
}

// ---------------------------------------------------------------------------
// 13 compute methods + completion/embedding C entry points
// ---------------------------------------------------------------------------

/// Spawns an async `text_to_speech` against the host's `CustomProvider`.
///
/// Consumes the request handle (the inner [`InnerSpeechRequest`] is moved
/// into the async task). On completion the typed [`InnerAudioResult`] is
/// popped with [`blazen_future_take_custom_audio_result`].
///
/// Returns null if either argument is null.
///
/// # Safety
///
/// `p` must be null OR a live `BlazenCustomProvider`. `request` must be null
/// OR a live `BlazenSpeechRequest`. Ownership of `request` transfers to the
/// spawned task — do NOT call `blazen_speech_request_free` afterwards.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_custom_provider_text_to_speech(
    p: *const BlazenCustomProvider,
    request: *mut BlazenSpeechRequest,
) -> *mut BlazenFuture {
    if p.is_null() || request.is_null() {
        return std::ptr::null_mut();
    }
    // SAFETY: caller guarantees live handle.
    let provider = unsafe { clone_provider(p) };
    // SAFETY: per the contract, `request` came from `Box::into_raw`.
    let req = unsafe { Box::from_raw(request) }.0;
    BlazenFuture::spawn::<InnerAudioResult, _>(async move {
        <InnerCustomProviderHandle as InnerCustomProviderTrait>::text_to_speech(&provider, req)
            .await
            .map_err(Into::into)
    })
}

/// Spawns an async `generate_music`. See
/// [`blazen_custom_provider_text_to_speech`] for ownership semantics.
///
/// # Safety
///
/// Same as [`blazen_custom_provider_text_to_speech`] but `request` must be
/// a `BlazenMusicRequest`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_custom_provider_generate_music(
    p: *const BlazenCustomProvider,
    request: *mut BlazenMusicRequest,
) -> *mut BlazenFuture {
    if p.is_null() || request.is_null() {
        return std::ptr::null_mut();
    }
    // SAFETY: caller guarantees live handle.
    let provider = unsafe { clone_provider(p) };
    // SAFETY: per the contract, `request` came from `Box::into_raw`.
    let req = unsafe { Box::from_raw(request) }.0;
    BlazenFuture::spawn::<InnerAudioResult, _>(async move {
        <InnerCustomProviderHandle as InnerCustomProviderTrait>::generate_music(&provider, req)
            .await
            .map_err(Into::into)
    })
}

/// Spawns an async `generate_sfx`. See
/// [`blazen_custom_provider_text_to_speech`] for ownership semantics.
///
/// # Safety
///
/// Same as [`blazen_custom_provider_text_to_speech`] but `request` must be
/// a `BlazenMusicRequest`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_custom_provider_generate_sfx(
    p: *const BlazenCustomProvider,
    request: *mut BlazenMusicRequest,
) -> *mut BlazenFuture {
    if p.is_null() || request.is_null() {
        return std::ptr::null_mut();
    }
    // SAFETY: caller guarantees live handle.
    let provider = unsafe { clone_provider(p) };
    // SAFETY: per the contract, `request` came from `Box::into_raw`.
    let req = unsafe { Box::from_raw(request) }.0;
    BlazenFuture::spawn::<InnerAudioResult, _>(async move {
        <InnerCustomProviderHandle as InnerCustomProviderTrait>::generate_sfx(&provider, req)
            .await
            .map_err(Into::into)
    })
}

/// Spawns an async `clone_voice`. See
/// [`blazen_custom_provider_text_to_speech`] for ownership semantics.
///
/// # Safety
///
/// Same as [`blazen_custom_provider_text_to_speech`] but `request` must be
/// a `BlazenVoiceCloneRequest`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_custom_provider_clone_voice(
    p: *const BlazenCustomProvider,
    request: *mut BlazenVoiceCloneRequest,
) -> *mut BlazenFuture {
    if p.is_null() || request.is_null() {
        return std::ptr::null_mut();
    }
    // SAFETY: caller guarantees live handle.
    let provider = unsafe { clone_provider(p) };
    // SAFETY: per the contract, `request` came from `Box::into_raw`.
    let req = unsafe { Box::from_raw(request) }.0;
    BlazenFuture::spawn::<InnerVoiceHandle, _>(async move {
        <InnerCustomProviderHandle as InnerCustomProviderTrait>::clone_voice(&provider, req)
            .await
            .map_err(Into::into)
    })
}

/// Spawns an async `list_voices`. Takes no request handle.
///
/// On completion, pop the resulting `Vec<VoiceHandle>` via
/// [`blazen_future_take_custom_voice_list`].
///
/// Returns null if `p` is null.
///
/// # Safety
///
/// `p` must be null OR a live `BlazenCustomProvider`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_custom_provider_list_voices(
    p: *const BlazenCustomProvider,
) -> *mut BlazenFuture {
    if p.is_null() {
        return std::ptr::null_mut();
    }
    // SAFETY: caller guarantees live handle.
    let provider = unsafe { clone_provider(p) };
    BlazenFuture::spawn::<Vec<InnerVoiceHandle>, _>(async move {
        <InnerCustomProviderHandle as InnerCustomProviderTrait>::list_voices(&provider)
            .await
            .map_err(Into::into)
    })
}

/// Spawns an async `delete_voice`. Consumes `voice` — ownership of the
/// handle transfers into the spawned task. After this call, the caller must
/// NOT call `blazen_voice_handle_free`.
///
/// Returns a future whose typed result is unit; observers should drain via
/// [`blazen_future_take_custom_unit`].
///
/// Returns null if `p` or `voice` is null.
///
/// # Safety
///
/// `p` must be null OR a live `BlazenCustomProvider`. `voice` must be null
/// OR a live `BlazenVoiceHandle`; ownership transfers.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_custom_provider_delete_voice(
    p: *const BlazenCustomProvider,
    voice: *mut BlazenVoiceHandle,
) -> *mut BlazenFuture {
    if p.is_null() || voice.is_null() {
        return std::ptr::null_mut();
    }
    // SAFETY: caller guarantees live handle.
    let provider = unsafe { clone_provider(p) };
    // SAFETY: per the contract, `voice` came from `Box::into_raw`.
    let v = unsafe { Box::from_raw(voice) }.0;
    BlazenFuture::spawn::<(), _>(async move {
        <InnerCustomProviderHandle as InnerCustomProviderTrait>::delete_voice(&provider, v)
            .await
            .map_err(Into::into)
    })
}

/// Spawns an async `generate_image`. See
/// [`blazen_custom_provider_text_to_speech`] for ownership semantics.
///
/// # Safety
///
/// Same as [`blazen_custom_provider_text_to_speech`] but `request` must be
/// a `BlazenImageRequest`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_custom_provider_generate_image(
    p: *const BlazenCustomProvider,
    request: *mut BlazenImageRequest,
) -> *mut BlazenFuture {
    if p.is_null() || request.is_null() {
        return std::ptr::null_mut();
    }
    // SAFETY: caller guarantees live handle.
    let provider = unsafe { clone_provider(p) };
    // SAFETY: per the contract, `request` came from `Box::into_raw`.
    let req = unsafe { Box::from_raw(request) }.0;
    BlazenFuture::spawn::<InnerImageResult, _>(async move {
        <InnerCustomProviderHandle as InnerCustomProviderTrait>::generate_image(&provider, req)
            .await
            .map_err(Into::into)
    })
}

/// Spawns an async `upscale_image`. See
/// [`blazen_custom_provider_text_to_speech`] for ownership semantics.
///
/// # Safety
///
/// Same as [`blazen_custom_provider_text_to_speech`] but `request` must be
/// a `BlazenUpscaleRequest`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_custom_provider_upscale_image(
    p: *const BlazenCustomProvider,
    request: *mut BlazenUpscaleRequest,
) -> *mut BlazenFuture {
    if p.is_null() || request.is_null() {
        return std::ptr::null_mut();
    }
    // SAFETY: caller guarantees live handle.
    let provider = unsafe { clone_provider(p) };
    // SAFETY: per the contract, `request` came from `Box::into_raw`.
    let req = unsafe { Box::from_raw(request) }.0;
    BlazenFuture::spawn::<InnerImageResult, _>(async move {
        <InnerCustomProviderHandle as InnerCustomProviderTrait>::upscale_image(&provider, req)
            .await
            .map_err(Into::into)
    })
}

/// Spawns an async `text_to_video`. See
/// [`blazen_custom_provider_text_to_speech`] for ownership semantics.
///
/// # Safety
///
/// Same as [`blazen_custom_provider_text_to_speech`] but `request` must be
/// a `BlazenVideoRequest`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_custom_provider_text_to_video(
    p: *const BlazenCustomProvider,
    request: *mut BlazenVideoRequest,
) -> *mut BlazenFuture {
    if p.is_null() || request.is_null() {
        return std::ptr::null_mut();
    }
    // SAFETY: caller guarantees live handle.
    let provider = unsafe { clone_provider(p) };
    // SAFETY: per the contract, `request` came from `Box::into_raw`.
    let req = unsafe { Box::from_raw(request) }.0;
    BlazenFuture::spawn::<InnerVideoResult, _>(async move {
        <InnerCustomProviderHandle as InnerCustomProviderTrait>::text_to_video(&provider, req)
            .await
            .map_err(Into::into)
    })
}

/// Spawns an async `image_to_video`. See
/// [`blazen_custom_provider_text_to_speech`] for ownership semantics.
///
/// # Safety
///
/// Same as [`blazen_custom_provider_text_to_speech`] but `request` must be
/// a `BlazenVideoRequest`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_custom_provider_image_to_video(
    p: *const BlazenCustomProvider,
    request: *mut BlazenVideoRequest,
) -> *mut BlazenFuture {
    if p.is_null() || request.is_null() {
        return std::ptr::null_mut();
    }
    // SAFETY: caller guarantees live handle.
    let provider = unsafe { clone_provider(p) };
    // SAFETY: per the contract, `request` came from `Box::into_raw`.
    let req = unsafe { Box::from_raw(request) }.0;
    BlazenFuture::spawn::<InnerVideoResult, _>(async move {
        <InnerCustomProviderHandle as InnerCustomProviderTrait>::image_to_video(&provider, req)
            .await
            .map_err(Into::into)
    })
}

/// Spawns an async `transcribe`. See
/// [`blazen_custom_provider_text_to_speech`] for ownership semantics.
///
/// # Safety
///
/// Same as [`blazen_custom_provider_text_to_speech`] but `request` must be
/// a `BlazenTranscriptionRequest`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_custom_provider_transcribe(
    p: *const BlazenCustomProvider,
    request: *mut BlazenTranscriptionRequest,
) -> *mut BlazenFuture {
    if p.is_null() || request.is_null() {
        return std::ptr::null_mut();
    }
    // SAFETY: caller guarantees live handle.
    let provider = unsafe { clone_provider(p) };
    // SAFETY: per the contract, `request` came from `Box::into_raw`.
    let req = unsafe { Box::from_raw(request) }.0;
    BlazenFuture::spawn::<InnerTranscriptionResult, _>(async move {
        <InnerCustomProviderHandle as InnerCustomProviderTrait>::transcribe(&provider, req)
            .await
            .map_err(Into::into)
    })
}

/// Spawns an async `generate_3d`. See
/// [`blazen_custom_provider_text_to_speech`] for ownership semantics.
///
/// # Safety
///
/// Same as [`blazen_custom_provider_text_to_speech`] but `request` must be
/// a `BlazenThreeDRequest`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_custom_provider_generate_3d(
    p: *const BlazenCustomProvider,
    request: *mut BlazenThreeDRequest,
) -> *mut BlazenFuture {
    if p.is_null() || request.is_null() {
        return std::ptr::null_mut();
    }
    // SAFETY: caller guarantees live handle.
    let provider = unsafe { clone_provider(p) };
    // SAFETY: per the contract, `request` came from `Box::into_raw`.
    let req = unsafe { Box::from_raw(request) }.0;
    BlazenFuture::spawn::<InnerThreeDResult, _>(async move {
        <InnerCustomProviderHandle as InnerCustomProviderTrait>::generate_3d(&provider, req)
            .await
            .map_err(Into::into)
    })
}

/// Spawns an async `remove_background`. See
/// [`blazen_custom_provider_text_to_speech`] for ownership semantics.
///
/// # Safety
///
/// Same as [`blazen_custom_provider_text_to_speech`] but `request` must be
/// a `BlazenBackgroundRemovalRequest`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_custom_provider_remove_background(
    p: *const BlazenCustomProvider,
    request: *mut BlazenBackgroundRemovalRequest,
) -> *mut BlazenFuture {
    if p.is_null() || request.is_null() {
        return std::ptr::null_mut();
    }
    // SAFETY: caller guarantees live handle.
    let provider = unsafe { clone_provider(p) };
    // SAFETY: per the contract, `request` came from `Box::into_raw`.
    let req = unsafe { Box::from_raw(request) }.0;
    BlazenFuture::spawn::<InnerImageResult, _>(async move {
        <InnerCustomProviderHandle as InnerCustomProviderTrait>::remove_background(&provider, req)
            .await
            .map_err(Into::into)
    })
}

// ---------------------------------------------------------------------------
// Typed future-take entry points
// ---------------------------------------------------------------------------

/// Pops a typed [`InnerAudioResult`] (the `blazen_llm::compute::AudioResult`
/// returned by `text_to_speech`, `generate_music`, `generate_sfx`) out of
/// `fut`.
///
/// On success returns `0` and writes a caller-owned `*mut BlazenAudioResult`
/// into `out`; on failure returns `-1` and writes a caller-owned
/// `*mut BlazenError` into `err`. Either out-pointer may be null.
///
/// # Safety
///
/// `fut` must be a non-null pointer produced by one of the audio-result
/// `blazen_custom_provider_*` entry points, not yet freed. `out` / `err`
/// must be null OR writable pointers.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_future_take_custom_audio_result(
    fut: *mut BlazenFuture,
    out: *mut *mut BlazenAudioResult,
    err: *mut *mut BlazenError,
) -> i32 {
    // SAFETY: caller upholds the live-future-pointer contract.
    match unsafe { BlazenFuture::take_typed::<InnerAudioResult>(fut) } {
        Ok(v) => {
            if !out.is_null() {
                // SAFETY: caller has guaranteed `out` is writable.
                unsafe {
                    *out = BlazenAudioResult::from(v).into_ptr();
                }
            }
            0
        }
        Err(e) => {
            if !err.is_null() {
                // SAFETY: caller has guaranteed `err` is writable.
                unsafe {
                    *err = BlazenError::from(e).into_ptr();
                }
            }
            -1
        }
    }
}

/// Pops a typed [`InnerImageResult`] (`generate_image`, `upscale_image`,
/// `remove_background`) out of `fut`. Mirrors
/// [`blazen_future_take_custom_audio_result`] semantics.
///
/// # Safety
///
/// `fut` must be a non-null pointer produced by one of the image-result
/// `blazen_custom_provider_*` entry points. `out` / `err` must be null OR
/// writable pointers.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_future_take_custom_image_result(
    fut: *mut BlazenFuture,
    out: *mut *mut BlazenImageResult,
    err: *mut *mut BlazenError,
) -> i32 {
    // SAFETY: caller upholds the live-future-pointer contract.
    match unsafe { BlazenFuture::take_typed::<InnerImageResult>(fut) } {
        Ok(v) => {
            if !out.is_null() {
                // SAFETY: caller has guaranteed `out` is writable.
                unsafe {
                    *out = BlazenImageResult::from(v).into_ptr();
                }
            }
            0
        }
        Err(e) => {
            if !err.is_null() {
                // SAFETY: caller has guaranteed `err` is writable.
                unsafe {
                    *err = BlazenError::from(e).into_ptr();
                }
            }
            -1
        }
    }
}

/// Pops a typed [`InnerVideoResult`] (`text_to_video`, `image_to_video`) out
/// of `fut`. Mirrors [`blazen_future_take_custom_audio_result`] semantics.
///
/// # Safety
///
/// `fut` must be a non-null pointer produced by one of the video-result
/// `blazen_custom_provider_*` entry points. `out` / `err` must be null OR
/// writable pointers.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_future_take_custom_video_result(
    fut: *mut BlazenFuture,
    out: *mut *mut BlazenVideoResult,
    err: *mut *mut BlazenError,
) -> i32 {
    // SAFETY: caller upholds the live-future-pointer contract.
    match unsafe { BlazenFuture::take_typed::<InnerVideoResult>(fut) } {
        Ok(v) => {
            if !out.is_null() {
                // SAFETY: caller has guaranteed `out` is writable.
                unsafe {
                    *out = BlazenVideoResult::from(v).into_ptr();
                }
            }
            0
        }
        Err(e) => {
            if !err.is_null() {
                // SAFETY: caller has guaranteed `err` is writable.
                unsafe {
                    *err = BlazenError::from(e).into_ptr();
                }
            }
            -1
        }
    }
}

/// Pops a typed [`InnerTranscriptionResult`] (`transcribe`) out of `fut`.
/// Mirrors [`blazen_future_take_custom_audio_result`] semantics.
///
/// # Safety
///
/// `fut` must be a non-null pointer produced by
/// [`blazen_custom_provider_transcribe`]. `out` / `err` must be null OR
/// writable pointers.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_future_take_custom_transcription_result(
    fut: *mut BlazenFuture,
    out: *mut *mut BlazenTranscriptionResult,
    err: *mut *mut BlazenError,
) -> i32 {
    // SAFETY: caller upholds the live-future-pointer contract.
    match unsafe { BlazenFuture::take_typed::<InnerTranscriptionResult>(fut) } {
        Ok(v) => {
            if !out.is_null() {
                // SAFETY: caller has guaranteed `out` is writable.
                unsafe {
                    *out = BlazenTranscriptionResult::from(v).into_ptr();
                }
            }
            0
        }
        Err(e) => {
            if !err.is_null() {
                // SAFETY: caller has guaranteed `err` is writable.
                unsafe {
                    *err = BlazenError::from(e).into_ptr();
                }
            }
            -1
        }
    }
}

/// Pops a typed [`InnerThreeDResult`] (`generate_3d`) out of `fut`. Mirrors
/// [`blazen_future_take_custom_audio_result`] semantics.
///
/// # Safety
///
/// `fut` must be a non-null pointer produced by
/// [`blazen_custom_provider_generate_3d`]. `out` / `err` must be null OR
/// writable pointers.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_future_take_custom_three_d_result(
    fut: *mut BlazenFuture,
    out: *mut *mut BlazenThreeDResult,
    err: *mut *mut BlazenError,
) -> i32 {
    // SAFETY: caller upholds the live-future-pointer contract.
    match unsafe { BlazenFuture::take_typed::<InnerThreeDResult>(fut) } {
        Ok(v) => {
            if !out.is_null() {
                // SAFETY: caller has guaranteed `out` is writable.
                unsafe {
                    *out = BlazenThreeDResult::from(v).into_ptr();
                }
            }
            0
        }
        Err(e) => {
            if !err.is_null() {
                // SAFETY: caller has guaranteed `err` is writable.
                unsafe {
                    *err = BlazenError::from(e).into_ptr();
                }
            }
            -1
        }
    }
}

/// Pops a typed [`InnerVoiceHandle`] (`clone_voice`) out of `fut`. Mirrors
/// [`blazen_future_take_custom_audio_result`] semantics.
///
/// # Safety
///
/// `fut` must be a non-null pointer produced by
/// [`blazen_custom_provider_clone_voice`]. `out` / `err` must be null OR
/// writable pointers.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_future_take_custom_voice_handle(
    fut: *mut BlazenFuture,
    out: *mut *mut BlazenVoiceHandle,
    err: *mut *mut BlazenError,
) -> i32 {
    // SAFETY: caller upholds the live-future-pointer contract.
    match unsafe { BlazenFuture::take_typed::<InnerVoiceHandle>(fut) } {
        Ok(v) => {
            if !out.is_null() {
                // SAFETY: caller has guaranteed `out` is writable.
                unsafe {
                    *out = BlazenVoiceHandle::from(v).into_ptr();
                }
            }
            0
        }
        Err(e) => {
            if !err.is_null() {
                // SAFETY: caller has guaranteed `err` is writable.
                unsafe {
                    *err = BlazenError::from(e).into_ptr();
                }
            }
            -1
        }
    }
}

/// Pops the typed `Vec<InnerVoiceHandle>` result of `list_voices` out of
/// `fut`. On success returns `0`, writes the count into `*out_count`, and
/// writes a caller-owned `*mut *mut BlazenVoiceHandle` array into
/// `*out_array`.
///
/// On failure returns `-1` and writes a caller-owned `*mut BlazenError`
/// into `err`. Any of `out_array` / `out_count` / `err` may be null.
///
/// ## Releasing the returned array
///
/// Free each `BlazenVoiceHandle` element with
/// [`crate::compute_results::blazen_voice_handle_free`], then release the
/// outer pointer array with [`blazen_voice_handle_list_free`].
///
/// # Safety
///
/// `fut` must be a non-null pointer produced by
/// [`blazen_custom_provider_list_voices`]. `out_array` / `out_count` / `err`
/// must each be null OR writable pointers.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_future_take_custom_voice_list(
    fut: *mut BlazenFuture,
    out_array: *mut *mut *mut BlazenVoiceHandle,
    out_count: *mut usize,
    err: *mut *mut BlazenError,
) -> i32 {
    // SAFETY: caller upholds the live-future-pointer contract.
    match unsafe { BlazenFuture::take_typed::<Vec<InnerVoiceHandle>>(fut) } {
        Ok(v) => {
            let count = v.len();
            // Convert each `VoiceHandle` into a caller-owned
            // `*mut BlazenVoiceHandle`, then leak the boxed slice so the
            // caller owns the array.
            let boxed_slice: Box<[*mut BlazenVoiceHandle]> = v
                .into_iter()
                .map(|h| BlazenVoiceHandle::from(h).into_ptr())
                .collect();
            let raw_slice = Box::into_raw(boxed_slice);
            let ptr: *mut *mut BlazenVoiceHandle = raw_slice.cast::<*mut BlazenVoiceHandle>();
            if out_array.is_null() {
                // Caller doesn't care about the array — leak it to avoid
                // freeing the inner handles. Documented contract for
                // "discard the result" callers.
                let _ = ptr;
            } else {
                // SAFETY: caller has guaranteed `out_array` is writable.
                unsafe {
                    *out_array = ptr;
                }
            }
            if !out_count.is_null() {
                // SAFETY: caller has guaranteed `out_count` is writable.
                unsafe {
                    *out_count = count;
                }
            }
            0
        }
        Err(e) => {
            if !err.is_null() {
                // SAFETY: caller has guaranteed `err` is writable.
                unsafe {
                    *err = BlazenError::from(e).into_ptr();
                }
            }
            -1
        }
    }
}

/// Frees the array of `*mut BlazenVoiceHandle` returned by
/// [`blazen_future_take_custom_voice_list`]. Does NOT free the individual
/// `BlazenVoiceHandle` elements — call
/// [`crate::compute_results::blazen_voice_handle_free`] for each pointer
/// first. No-op on null.
///
/// # Safety
///
/// `array` must be null OR a pointer-array produced by
/// [`blazen_future_take_custom_voice_list`]. `count` must be the exact
/// length reported by that call. Double-free is undefined behavior.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_voice_handle_list_free(
    array: *mut *mut BlazenVoiceHandle,
    count: usize,
) {
    if array.is_null() {
        return;
    }
    // SAFETY: per the contract, `array` was leaked from a boxed slice with
    // length `count`. Reconstructing the `Box<[_]>` here is sound and
    // `drop` releases the outer allocation. The element pointers remain
    // owned by the caller.
    let slice_ptr: *mut [*mut BlazenVoiceHandle] = std::ptr::slice_from_raw_parts_mut(array, count);
    // SAFETY: see comment above.
    drop(unsafe { Box::from_raw(slice_ptr) });
}

/// Pops the unit `()` result of `delete_voice` out of `fut`. On success
/// returns `0`; on failure returns `-1` and writes a caller-owned
/// `*mut BlazenError` into `err`. `err` may be null.
///
/// # Safety
///
/// `fut` must be a non-null pointer produced by
/// [`blazen_custom_provider_delete_voice`]. `err` must be null OR a
/// writable pointer.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_future_take_custom_unit(
    fut: *mut BlazenFuture,
    err: *mut *mut BlazenError,
) -> i32 {
    // SAFETY: caller upholds the live-future-pointer contract.
    match unsafe { BlazenFuture::take_typed::<()>(fut) } {
        Ok(()) => 0,
        Err(e) => {
            if !err.is_null() {
                // SAFETY: caller has guaranteed `err` is writable.
                unsafe {
                    *err = BlazenError::from(e).into_ptr();
                }
            }
            -1
        }
    }
}