rainy-sdk 0.6.14

Official Rust SDK for Rainy API by Enosis Labs v0.6.14 - OpenAI/GPT-5 parity, native streaming events, and legacy static model cleanup
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
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
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::collections::HashMap;

fn map_is_empty(value: &HashMap<String, serde_json::Value>) -> bool {
    value.is_empty()
}

/// Represents a single message in a chat conversation.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ChatMessage {
    /// The role of the message author.
    pub role: MessageRole,
    /// The content of the message.
    pub content: String,
}

/// The role of a message's author.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum MessageRole {
    /// A message from the system, setting the context or instructions for the assistant.
    System,
    /// A message from the user.
    User,
    /// A message from the assistant.
    Assistant,
}

/// The role of an OpenAI-compatible chat message author.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum OpenAIMessageRole {
    /// A message from the system.
    System,
    /// A message from the user.
    User,
    /// A message from the assistant.
    Assistant,
    /// A tool result message.
    Tool,
}

/// OpenAI-compatible chat message content.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum OpenAIMessageContent {
    /// Plain text content.
    Text(String),
    /// Multimodal content parts.
    Parts(Vec<OpenAIContentPart>),
}

/// OpenAI-compatible multimodal content part.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum OpenAIContentPart {
    /// Text content part.
    Text {
        /// The text content.
        text: String,
    },
    /// Image URL content part.
    ImageUrl {
        /// Image URL payload.
        image_url: OpenAIImageUrl,
    },
}

/// OpenAI-compatible image URL payload.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct OpenAIImageUrl {
    /// Image URL or data URI.
    pub url: String,
    /// Optional detail level hint.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub detail: Option<String>,
}

/// OpenAI-compatible function call payload.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct OpenAIFunctionCall {
    /// Function name.
    pub name: String,
    /// JSON-encoded function arguments.
    pub arguments: String,
}

/// OpenAI-compatible tool call payload.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct OpenAIToolCall {
    /// Tool call ID.
    pub id: String,
    /// Tool type (typically `function`).
    pub r#type: String,
    /// Optional provider-specific metadata.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extra_content: Option<serde_json::Value>,
    /// Function call details.
    pub function: OpenAIFunctionCall,
}

/// OpenAI-compatible chat message with full tool-call replay support.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct OpenAIChatMessage {
    /// The role of the message author.
    pub role: OpenAIMessageRole,
    /// Message content. Assistant tool-call messages may omit content.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content: Option<OpenAIMessageContent>,
    /// Optional display name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Assistant tool calls attached to this message.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_calls: Option<Vec<OpenAIToolCall>>,
    /// Tool call ID associated with a `tool` role message.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_call_id: Option<String>,
}

/// The search provider to use for web research.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "lowercase")]
pub enum ResearchProvider {
    /// Use Exa (formerly Metaphor) for high-quality semantic search.
    #[default]
    Exa,
    /// Use Tavily for comprehensive web search and content extraction.
    Tavily,
    /// Automatically select the best provider based on the query.
    Auto,
}

/// The depth of the research operation.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "lowercase")]
pub enum ResearchDepth {
    /// Basic search (faster, lower cost).
    #[default]
    Basic,
    /// Deep search (more thorough, higher cost, includes more context).
    Advanced,
}

/// Represents a request to create a chat completion.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatCompletionRequest {
    /// The identifier of the model to use for the completion (e.g., "gpt-4o", "claude-sonnet-4").
    pub model: String,

    /// A list of messages that form the conversation history.
    pub messages: Vec<ChatMessage>,

    /// The sampling temperature to use, between 0.0 and 2.0. Higher values will make the output
    /// more random, while lower values will make it more focused and deterministic.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f32>,

    /// The maximum number of tokens to generate in the completion.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_tokens: Option<u32>,

    /// Upper bound for completion tokens in modern OpenAI-compatible payloads.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_completion_tokens: Option<u32>,

    /// The nucleus sampling parameter. The model considers the results of the tokens with `top_p`
    /// probability mass. So, 0.1 means only the tokens comprising the top 10% probability mass are considered.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_p: Option<f32>,

    /// A penalty applied to new tokens based on their frequency in the text so far.
    /// It decreases the model's likelihood to repeat the same line verbatim.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub frequency_penalty: Option<f32>,

    /// A penalty applied to new tokens based on whether they appear in the text so far.
    /// It increases the model's likelihood to talk about new topics.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub presence_penalty: Option<f32>,

    /// A list of sequences that will cause the model to stop generating further tokens.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop: Option<Vec<String>>,

    /// A unique identifier representing your end-user, which can help in monitoring and
    /// tracking conversations.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user: Option<String>,

    /// A hint to the router about which provider to use for the model.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub provider: Option<String>,

    /// If set to `true`, the response will be streamed as a series of events.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream: Option<bool>,

    /// Stream behavior options (`include_usage`, etc.).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream_options: Option<serde_json::Value>,

    /// Modify the likelihood of specified tokens appearing in the completion.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub logit_bias: Option<serde_json::Value>,

    /// Whether to return log probabilities of the output tokens.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub logprobs: Option<bool>,

    /// An integer between 0 and 20 specifying the number of most likely tokens to return at each token position.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_logprobs: Option<u32>,

    /// How many chat completion choices to generate for each input message.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub n: Option<u32>,

    /// An object specifying the format that the model must output.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_format: Option<ResponseFormat>,

    /// A list of tools the model may call.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tools: Option<Vec<Tool>>,

    /// Controls which (if any) tool is called by the model.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_choice: Option<ToolChoice>,

    /// Whether multiple tool calls may be emitted in parallel.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parallel_tool_calls: Option<bool>,

    /// Seed for deterministic sampling where supported.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub seed: Option<i64>,

    /// Prompt cache key for provider routing/cache optimization.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prompt_cache_key: Option<String>,

    /// Provider-specific option bag.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub provider_options: Option<serde_json::Value>,

    /// Prompt cache retention mode.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prompt_cache_retention: Option<String>,

    /// Reasoning settings (bool/object depending on provider).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning: Option<serde_json::Value>,

    /// Include reasoning traces where supported.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub include_reasoning: Option<bool>,

    /// Arbitrary request metadata.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<HashMap<String, String>>,

    /// Requested service tier (`auto`, `default`, `flex`, etc.).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub service_tier: Option<String>,

    /// Persist request/response server-side when supported.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub store: Option<bool>,

    /// Stable identifier used by safety systems.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub safety_identifier: Option<String>,

    /// Requested output modalities.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub modalities: Option<Vec<String>>,

    /// Audio generation options.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub audio: Option<serde_json::Value>,

    /// Prediction optimization payload.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prediction: Option<serde_json::Value>,

    /// Verbosity hint for some models.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub verbosity: Option<String>,

    /// Native web search options.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub web_search_options: Option<serde_json::Value>,

    /// Legacy functions field accepted by compat layers.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub functions: Option<Vec<serde_json::Value>>,

    /// Legacy function-call directive accepted by compat layers.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub function_call: Option<serde_json::Value>,

    /// Configuration for thinking capabilities (Gemini 3 and 2.5 series).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thinking_config: Option<ThinkingConfig>,
}

/// OpenAI-compatible request payload with full message replay support.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpenAIChatCompletionRequest {
    /// The identifier of the model to use for the completion.
    pub model: String,

    /// Full OpenAI-compatible message history.
    pub messages: Vec<OpenAIChatMessage>,

    /// The sampling temperature to use.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f32>,

    /// The maximum number of tokens to generate in the completion.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_tokens: Option<u32>,

    /// Upper bound for completion tokens in modern OpenAI-compatible payloads.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_completion_tokens: Option<u32>,

    /// Nucleus sampling parameter.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_p: Option<f32>,

    /// Frequency penalty.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub frequency_penalty: Option<f32>,

    /// Presence penalty.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub presence_penalty: Option<f32>,

    /// Stop sequences.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop: Option<Vec<String>>,

    /// End-user identifier.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user: Option<String>,

    /// Router/provider hint.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub provider: Option<String>,

    /// If true, the response will be streamed as SSE events.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream: Option<bool>,

    /// Stream behavior options (`include_usage`, etc.).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream_options: Option<serde_json::Value>,

    /// Logit bias map.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub logit_bias: Option<serde_json::Value>,

    /// Whether to return log probabilities.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub logprobs: Option<bool>,

    /// Number of top log probabilities to return.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_logprobs: Option<u32>,

    /// Number of completion choices to generate.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub n: Option<u32>,

    /// Structured response format.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_format: Option<ResponseFormat>,

    /// Tools available to the model.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tools: Option<Vec<Tool>>,

    /// Tool selection strategy.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_choice: Option<ToolChoice>,

    /// Whether multiple tool calls may be emitted in parallel.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parallel_tool_calls: Option<bool>,

    /// Seed for deterministic sampling where supported.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub seed: Option<i64>,

    /// Prompt cache key for provider routing/cache optimization.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prompt_cache_key: Option<String>,

    /// Provider-specific option bag.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub provider_options: Option<serde_json::Value>,

    /// Prompt cache retention mode.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prompt_cache_retention: Option<String>,

    /// Reasoning settings (bool/object depending on provider).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning: Option<serde_json::Value>,

    /// Include reasoning traces where supported.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub include_reasoning: Option<bool>,

    /// Arbitrary request metadata.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<HashMap<String, String>>,

    /// Requested service tier (`auto`, `default`, `flex`, etc.).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub service_tier: Option<String>,

    /// Persist request/response server-side when supported.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub store: Option<bool>,

    /// Stable identifier used by safety systems.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub safety_identifier: Option<String>,

    /// Requested output modalities.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub modalities: Option<Vec<String>>,

    /// Audio generation options.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub audio: Option<serde_json::Value>,

    /// Prediction optimization payload.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prediction: Option<serde_json::Value>,

    /// Verbosity hint for some models.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub verbosity: Option<String>,

    /// Native web search options.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub web_search_options: Option<serde_json::Value>,

    /// Legacy functions field accepted by compat layers.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub functions: Option<Vec<serde_json::Value>>,

    /// Legacy function-call directive accepted by compat layers.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub function_call: Option<serde_json::Value>,

    /// Gemini thinking configuration.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thinking_config: Option<ThinkingConfig>,

    /// Anthropic extended-thinking configuration (`thinking.budget_tokens`).
    /// Serialised as the `thinking` top-level field so it is passed through to
    /// OpenRouter/Anthropic as `{"type":"enabled","budget_tokens":N}`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thinking: Option<serde_json::Value>,
}

/// Represents the response from a chat completion request.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatCompletionResponse {
    /// A unique identifier for the chat completion.
    pub id: String,

    /// The type of object, which is always "chat.completion".
    pub object: String,

    /// The Unix timestamp (in seconds) of when the completion was created.
    pub created: u64,

    /// The model that was used for the completion.
    pub model: String,

    /// A list of chat completion choices.
    pub choices: Vec<ChatChoice>,

    /// Information about the token usage for this completion.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub usage: Option<Usage>,
}

/// OpenAI-compatible chat completion response with tool-call aware messages.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpenAIChatCompletionResponse {
    /// A unique identifier for the chat completion.
    pub id: String,

    /// The type of object, which is always `chat.completion`.
    pub object: String,

    /// The Unix timestamp (in seconds) of when the completion was created.
    pub created: u64,

    /// The model that was used for the completion.
    pub model: String,

    /// A list of chat completion choices.
    pub choices: Vec<OpenAIChatChoice>,

    /// Token usage information for this completion.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub usage: Option<Usage>,
}

/// Represents a single choice in a chat completion response.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatChoice {
    /// The index of the choice in the list of choices.
    pub index: u32,

    /// The message generated by the model.
    pub message: ChatMessage,

    /// The reason the model stopped generating tokens.
    pub finish_reason: String,
}

/// Represents a single choice in an OpenAI-compatible chat completion response.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpenAIChatChoice {
    /// The index of the choice in the list of choices.
    pub index: u32,

    /// The tool-call aware message generated by the model.
    pub message: OpenAIChatMessage,

    /// The reason the model stopped generating tokens.
    pub finish_reason: String,
}

/// Represents the token usage statistics for a chat completion.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Usage {
    /// The number of tokens in the prompt.
    pub prompt_tokens: u32,

    /// The number of tokens in the generated completion.
    pub completion_tokens: u32,

    /// The total number of tokens used in the request (prompt + completion).
    pub total_tokens: u32,
}

/// Represents the health status of the Rainy API.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthStatus {
    /// The overall status of the API (e.g., "healthy", "degraded").
    pub status: String,

    /// The timestamp of when the health check was performed.
    pub timestamp: String,

    /// The uptime of the system in seconds.
    pub uptime: f64,

    /// The status of individual services.
    pub services: ServiceStatus,
}

/// Represents the status of individual backend services.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceStatus {
    /// The status of the database connection.
    pub database: bool,

    /// The status of the Redis connection, if applicable.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub redis: Option<bool>,

    /// The overall status of the connections to AI providers.
    pub providers: bool,
}

/// Represents the available models and providers.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct AvailableModels {
    /// A map where keys are provider names and values are lists of model names.
    #[serde(default)]
    pub providers: HashMap<String, Vec<String>>,

    /// The total number of available models across all providers.
    #[serde(default)]
    pub total_models: usize,

    /// A list of provider names that are currently active and available.
    #[serde(default)]
    pub active_providers: Vec<String>,
}

/// Represents information about credit usage for a request.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreditInfo {
    /// The number of credits available before the request.
    pub current_credits: f64,

    /// The estimated number of credits that the request will cost.
    pub estimated_cost: f64,

    /// The estimated number of credits remaining after the request.
    pub credits_after_request: f64,

    /// The date when the credit balance is next scheduled to be reset.
    pub reset_date: String,
}

/// Represents metadata extracted from the response headers of an API request.
#[derive(Debug, Clone)]
pub struct RequestMetadata {
    /// The time taken for the request to complete, in milliseconds.
    pub response_time: Option<u64>,

    /// The AI provider that handled the request.
    pub provider: Option<String>,

    /// The number of tokens used in the request.
    pub tokens_used: Option<u32>,

    /// The number of credits used for the request.
    pub credits_used: Option<f64>,

    /// The number of credits remaining after the request.
    pub credits_remaining: Option<f64>,

    /// The unique ID of the request, for tracking and debugging.
    pub request_id: Option<String>,

    /// Count of non-blocking compatibility warnings returned by Rainy.
    pub compat_warnings: Option<u32>,

    /// Response mode selected by Rainy (`raw` or `envelope`).
    pub response_mode: Option<String>,

    /// Billing plan used for this request.
    pub billing_plan: Option<String>,

    /// Credits charged for the request.
    pub rainy_credits_charged: Option<f64>,

    /// Remaining daily credits reported by Rainy.
    pub rainy_daily_credits_remaining: Option<String>,

    /// Sanitized parameters removed/rewritten by the compatibility layer.
    pub rainy_sanitized_params: Option<String>,

    /// Billing reconciliation status (`exact`, `refunded`, `undercharged`, etc.).
    pub rainy_billing_adjustment: Option<String>,

    /// Outstanding credits that could not be charged in post-processing.
    pub rainy_billing_outstanding_credits: Option<f64>,
}

/// OpenRouter/Rainy Responses API request payload.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResponsesRequest {
    /// The identifier of the model to use.
    pub model: String,

    /// Input payload accepted by the Responses API (string, object, or array).
    pub input: serde_json::Value,

    /// If true, the response will be streamed as SSE events.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream: Option<bool>,

    /// Responses tool definitions and/or custom tools.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tools: Option<Vec<serde_json::Value>>,

    /// Tool selection strategy.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_choice: Option<serde_json::Value>,

    /// Structured output format.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_format: Option<serde_json::Value>,

    /// Sampling temperature.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f32>,

    /// Top-p nucleus sampling.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_p: Option<f32>,

    /// Maximum number of output tokens.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_output_tokens: Option<u32>,

    /// End-user identifier (legacy fallback accepted by Rainy).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user: Option<String>,

    /// Prompt cache key for routing/cache optimization.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prompt_cache_key: Option<String>,

    /// Reasoning configuration object (provider/model dependent).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning: Option<serde_json::Value>,

    /// Include reasoning traces where supported.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub include_reasoning: Option<bool>,

    /// Whether multiple tool calls may be emitted in parallel.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parallel_tool_calls: Option<bool>,

    /// Arbitrary request metadata.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<HashMap<String, String>>,

    /// Requested service tier (`auto`, `default`, `flex`, etc.).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub service_tier: Option<String>,

    /// Persist request/response server-side when supported.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub store: Option<bool>,

    /// Stable identifier used by safety systems.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub safety_identifier: Option<String>,

    /// Provider-specific option bag.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub provider_options: Option<serde_json::Value>,

    /// Prompt cache retention mode.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prompt_cache_retention: Option<String>,

    /// Text/output controls supported by modern Responses API.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<serde_json::Value>,

    /// System-level instructions.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub instructions: Option<String>,

    /// Include directives for response rendering/metadata.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub include: Option<Vec<String>>,

    /// Previous response ID for multi-turn server-side continuation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub previous_response_id: Option<String>,

    /// Conversation identifier or object.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub conversation: Option<serde_json::Value>,

    /// Prompt object for hosted prompts/templates.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prompt: Option<serde_json::Value>,

    /// Request asynchronous/background processing where supported.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub background: Option<bool>,

    /// Context management directives.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub context_management: Option<Vec<serde_json::Value>>,

    /// Truncation strategy (`auto` or `disabled`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub truncation: Option<String>,

    /// Forward-compatible extra parameters.
    #[serde(flatten, skip_serializing_if = "map_is_empty", default)]
    pub extra: HashMap<String, serde_json::Value>,
}

impl ResponsesRequest {
    /// Creates a new Responses request from an arbitrary input payload.
    pub fn new(model: impl Into<String>, input: serde_json::Value) -> Self {
        Self {
            model: model.into(),
            input,
            stream: None,
            tools: None,
            tool_choice: None,
            response_format: None,
            temperature: None,
            top_p: None,
            max_output_tokens: None,
            user: None,
            prompt_cache_key: None,
            reasoning: None,
            include_reasoning: None,
            parallel_tool_calls: None,
            metadata: None,
            service_tier: None,
            store: None,
            safety_identifier: None,
            provider_options: None,
            prompt_cache_retention: None,
            text: None,
            instructions: None,
            include: None,
            previous_response_id: None,
            conversation: None,
            prompt: None,
            background: None,
            context_management: None,
            truncation: None,
            extra: HashMap::new(),
        }
    }

    /// Convenience constructor for plain text input.
    pub fn text(model: impl Into<String>, input_text: impl Into<String>) -> Self {
        Self::new(model, serde_json::Value::String(input_text.into()))
    }

    /// Sets streaming mode.
    pub fn with_stream(mut self, stream: bool) -> Self {
        self.stream = Some(stream);
        self
    }

    /// Sets reasoning configuration object.
    pub fn with_reasoning(mut self, reasoning: serde_json::Value) -> Self {
        self.reasoning = Some(reasoning);
        self
    }

    /// Requests explicit reasoning traces when supported.
    pub fn with_include_reasoning(mut self, include_reasoning: bool) -> Self {
        self.include_reasoning = Some(include_reasoning);
        self
    }

    /// Convenience helper to set reasoning effort (`low`, `medium`, `high`).
    pub fn with_reasoning_effort(mut self, effort: impl Into<String>) -> Self {
        self.reasoning = Some(serde_json::json!({ "effort": effort.into() }));
        self
    }

    /// Sets max output tokens.
    pub fn with_max_output_tokens(mut self, max_output_tokens: u32) -> Self {
        self.max_output_tokens = Some(max_output_tokens);
        self
    }

    /// Sets prompt cache key.
    pub fn with_prompt_cache_key(mut self, prompt_cache_key: impl Into<String>) -> Self {
        self.prompt_cache_key = Some(prompt_cache_key.into());
        self
    }

    /// Sets user identifier.
    pub fn with_user(mut self, user: impl Into<String>) -> Self {
        self.user = Some(user.into());
        self
    }

    /// Sets provider option bag (forwarded as-is).
    pub fn with_provider_options(mut self, provider_options: serde_json::Value) -> Self {
        self.provider_options = Some(provider_options);
        self
    }

    /// Sets system-level instructions.
    pub fn with_instructions(mut self, instructions: impl Into<String>) -> Self {
        self.instructions = Some(instructions.into());
        self
    }

    /// Sets previous response identifier for stateful continuation.
    pub fn with_previous_response_id(mut self, previous_response_id: impl Into<String>) -> Self {
        self.previous_response_id = Some(previous_response_id.into());
        self
    }

    /// Sets service tier hint (`auto`, `default`, `flex`, etc.).
    pub fn with_service_tier(mut self, service_tier: impl Into<String>) -> Self {
        self.service_tier = Some(service_tier.into());
        self
    }

    /// Sets metadata map.
    pub fn with_metadata(mut self, metadata: HashMap<String, String>) -> Self {
        self.metadata = Some(metadata);
        self
    }

    /// Sets tool definitions array directly.
    pub fn with_tools(mut self, tools: Vec<serde_json::Value>) -> Self {
        self.tools = Some(tools);
        self
    }

    /// Adds a function tool using Responses-style shape.
    pub fn add_function_tool(
        mut self,
        name: impl Into<String>,
        description: impl Into<String>,
        parameters: serde_json::Value,
    ) -> Self {
        let mut tools = self.tools.unwrap_or_default();
        tools.push(serde_json::json!({
            "type": "function",
            "name": name.into(),
            "description": description.into(),
            "parameters": parameters
        }));
        self.tools = Some(tools);
        self
    }

    /// Adds a custom extra parameter for forward compatibility.
    pub fn with_extra(mut self, key: impl Into<String>, value: serde_json::Value) -> Self {
        self.extra.insert(key.into(), value);
        self
    }
}

/// Responses API usage object (partial, forward-compatible).
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ResponsesUsage {
    /// Number of input tokens consumed.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub input_tokens: Option<u32>,
    /// Number of output tokens generated.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub output_tokens: Option<u32>,
    /// Number of tokens used for cache creation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_creation_input_tokens: Option<u32>,
    /// Number of tokens read from cache.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_read_input_tokens: Option<u32>,
    /// Detailed breakdown of output tokens.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub output_tokens_details: Option<serde_json::Value>,
    /// Detailed breakdown of completion tokens.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub completion_tokens_details: Option<serde_json::Value>,
    /// Additional provider-specific usage fields.
    #[serde(flatten, default)]
    pub extra: HashMap<String, serde_json::Value>,
}

/// Responses API raw response payload.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ResponsesApiResponse {
    /// Unique identifier for the response.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// Object type identifier.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub object: Option<String>,
    /// Model used for the response.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,
    /// Plain text output content.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub output_text: Option<String>,
    /// Structured output items.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub output: Option<Vec<serde_json::Value>>,
    /// Response lifecycle status (`completed`, `in_progress`, etc.).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
    /// Error object for failed responses.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<serde_json::Value>,
    /// Incomplete details payload when response is partial/interrupted.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub incomplete_details: Option<serde_json::Value>,
    /// Token usage information.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub usage: Option<ResponsesUsage>,
    /// Additional provider-specific response fields.
    #[serde(flatten, default)]
    pub extra: HashMap<String, serde_json::Value>,
}

/// Non-blocking compatibility warning emitted by Rainy in envelope mode.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompatWarning {
    /// Warning code identifier.
    pub code: String,
    /// Human-readable warning message.
    pub message: String,
    /// JSON path to the field that triggered the warning.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub path: Option<String>,
}

/// Features used by request (reported in envelope mode).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FeaturesUsed {
    /// Whether reasoning/thinking was used.
    pub reasoning: bool,
    /// Whether image input was provided.
    #[serde(rename = "imageInput")]
    pub image_input: bool,
    /// Whether tool calling was used.
    pub tools: bool,
    /// Whether structured output was requested.
    #[serde(rename = "structuredOutput")]
    pub structured_output: bool,
}

/// Reasoning summary metadata reported by Rainy in envelope mode.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReasoningMeta {
    /// Whether reasoning was present in the response.
    pub present: bool,
    /// Whether a reasoning summary was provided.
    pub summary_present: bool,
    /// Number of tokens used for reasoning.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tokens: Option<u32>,
}

/// Rainy envelope metadata (partial, forward-compatible).
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct RainyEnvelopeMeta {
    /// Billing plan identifier.
    #[serde(
        rename = "billingPlan",
        alias = "billing_plan",
        skip_serializing_if = "Option::is_none"
    )]
    pub billing_plan: Option<String>,
    /// Credits charged for the request.
    #[serde(
        rename = "creditsCharged",
        alias = "credits_charged",
        skip_serializing_if = "Option::is_none"
    )]
    pub credits_charged: Option<f64>,
    /// Markup percentage applied to pricing.
    #[serde(
        rename = "markupPercent",
        alias = "markup_percent",
        skip_serializing_if = "Option::is_none"
    )]
    pub markup_percent: Option<f64>,
    /// Daily credits remaining for the user.
    #[serde(
        rename = "dailyCreditsRemaining",
        alias = "daily_credits_remaining",
        skip_serializing_if = "Option::is_none"
    )]
    pub daily_credits_remaining: Option<String>,
    /// Compatibility warnings emitted during processing.
    #[serde(
        rename = "compatWarnings",
        alias = "compat_warnings",
        skip_serializing_if = "Option::is_none"
    )]
    pub compat_warnings: Option<Vec<CompatWarning>>,
    /// Features used by the request.
    #[serde(
        rename = "featuresUsed",
        alias = "features_used",
        skip_serializing_if = "Option::is_none"
    )]
    pub features_used: Option<FeaturesUsed>,
    /// Reasoning metadata for the response.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning: Option<ReasoningMeta>,
    /// Additional envelope metadata fields.
    #[serde(flatten, default)]
    pub extra: HashMap<String, serde_json::Value>,
}

/// Standard Rainy success envelope.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RainyEnvelope<T> {
    /// Whether the request was successful.
    pub success: bool,
    /// The response data payload.
    pub data: T,
    /// Optional envelope metadata.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub meta: Option<RainyEnvelopeMeta>,
}

/// Response stream SSE event payload (dynamic by design).
pub type ResponsesStreamEvent = serde_json::Value;

/// Model architecture metadata returned by `/models/catalog`.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ModelArchitecture {
    /// Supported input modalities (e.g., "text", "image").
    #[serde(skip_serializing_if = "Option::is_none")]
    pub input_modalities: Option<Vec<String>>,
    /// Supported output modalities (e.g., "text").
    #[serde(skip_serializing_if = "Option::is_none")]
    pub output_modalities: Option<Vec<String>>,
    /// Tokenizer used by the model.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tokenizer: Option<String>,
    /// Instruction type supported by the model.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub instruct_type: Option<String>,
}

/// Capability flag can be boolean or `"unknown"`.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CapabilityFlag {
    /// Boolean capability flag.
    Bool(bool),
    /// Text-based capability flag (e.g., "unknown").
    Text(String),
}

/// Rainy capability hints returned by `/models/catalog`.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct RainyCapabilities {
    /// Whether the model supports reasoning/thinking.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning: Option<CapabilityFlag>,
    /// Whether the model supports image input.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub image_input: Option<CapabilityFlag>,
    /// Whether the model supports tool calling.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tools: Option<CapabilityFlag>,
    /// Whether the model supports structured output formats.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_format: Option<CapabilityFlag>,
}

/// Provider-specific reasoning profile.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum ReasoningProvider {
    /// OpenAI provider.
    Openai,
    /// Google provider.
    Google,
    /// Anthropic provider.
    Anthropic,
    /// Other providers.
    Other,
}

/// Thinking budget range metadata.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct ThinkingBudget {
    /// Minimum budget value.
    pub min: i32,
    /// Maximum budget value.
    pub max: i32,
    /// Dynamic budget value if applicable.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dynamic_value: Option<i32>,
    /// Value that disables thinking budget.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub disable_value: Option<i32>,
}

/// Reasoning controls available for a model.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct ReasoningControls {
    /// Parameters observed for reasoning control.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub observed_parameters: Option<Vec<String>>,
    /// Whether reasoning toggle is supported.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning_toggle: Option<bool>,
    /// Whether reasoning effort control is supported.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning_effort: Option<bool>,
    /// Available effort levels.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub effort: Option<Vec<String>>,
    /// Available thinking levels.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thinking_level: Option<Vec<String>>,
    /// Thinking budget configuration.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thinking_budget: Option<ThinkingBudget>,
}

/// Provider profile for reasoning/thinking.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ReasoningProfile {
    /// The provider this profile applies to.
    pub provider: ReasoningProvider,
    /// JSON path to the reasoning parameter.
    pub parameter_path: String,
    /// Available values for the parameter.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub values: Option<Vec<String>>,
    /// Additional notes about this profile.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub notes: Option<String>,
}

/// Reasoning toggle paths for clients.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct ReasoningToggle {
    /// Parameter path to enable reasoning.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enable_param: Option<String>,
    /// Parameter path to include reasoning in response.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub include_reasoning_param: Option<String>,
}

/// Reasoning capability block in `rainy_capabilities_v2`.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct RainyReasoningCapabilitiesV2 {
    /// Whether reasoning is supported.
    pub supported: bool,
    /// Available reasoning controls.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub controls: Option<ReasoningControls>,
    /// Provider-specific reasoning profiles.
    #[serde(default)]
    pub profiles: Vec<ReasoningProfile>,
    /// Reasoning toggle configuration.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub toggle: Option<ReasoningToggle>,
}

/// Multimodal capability block in `rainy_capabilities_v2`.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct RainyMultimodalCapabilitiesV2 {
    /// Supported input modalities.
    #[serde(default)]
    pub input: Vec<String>,
    /// Supported output modalities.
    #[serde(default)]
    pub output: Vec<String>,
}

/// Parameter capability block in `rainy_capabilities_v2`.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct RainyParametersCapabilitiesV2 {
    /// Accepted parameter names.
    #[serde(default)]
    pub accepted: Vec<String>,
}

/// Full v2 capability block returned by `/models/catalog`.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct RainyCapabilitiesV2 {
    /// Multimodal capabilities.
    pub multimodal: RainyMultimodalCapabilitiesV2,
    /// Reasoning capabilities.
    pub reasoning: RainyReasoningCapabilitiesV2,
    /// Parameter capabilities.
    pub parameters: RainyParametersCapabilitiesV2,
}

/// Pricing metadata for model ranking helpers.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct ModelPricing {
    /// Prompt token pricing.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prompt: Option<String>,
    /// Completion token pricing.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub completion: Option<String>,
}

/// Model entry returned by `/models/catalog`.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ModelCatalogItem {
    /// Unique model identifier.
    pub id: String,
    /// Human-readable model name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Maximum context length in tokens.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub context_length: Option<u32>,
    /// Model pricing information.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pricing: Option<ModelPricing>,
    /// Supported API parameters.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub supported_parameters: Option<Vec<String>>,
    /// Model architecture metadata.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub architecture: Option<ModelArchitecture>,
    /// Rainy capability hints (v1).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rainy_capabilities: Option<RainyCapabilities>,
    /// Rainy capability hints (v2).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rainy_capabilities_v2: Option<RainyCapabilitiesV2>,
    /// Additional model metadata.
    #[serde(flatten, default)]
    pub extra: HashMap<String, serde_json::Value>,
}

/// Reasoning mode expected by the caller when selecting models.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum ReasoningMode {
    /// Effort-based reasoning control.
    Effort,
    /// Thinking level-based reasoning control.
    ThinkingLevel,
    /// Thinking budget-based reasoning control.
    ThinkingBudget,
}

/// Selector criteria for model discovery from `/models/catalog`.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct ModelSelectionCriteria {
    /// Required input modalities.
    #[serde(default)]
    pub required_input_modalities: Vec<String>,
    /// Required output modalities.
    #[serde(default)]
    pub required_output_modalities: Vec<String>,
    /// Whether tool calling is required.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub require_tools: Option<bool>,
    /// Whether structured output is required.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub require_structured_output: Option<bool>,
    /// Required reasoning mode.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning_mode: Option<ReasoningMode>,
    /// Reasoning value to match.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning_value: Option<String>,
}

/// Builder preference for reasoning payload generation.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ReasoningPreference {
    /// Reasoning mode to use.
    pub mode: ReasoningMode,
    /// Reasoning value to apply.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<String>,
    /// Thinking budget value.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub budget: Option<i32>,
}

fn parse_price(value: Option<&str>) -> f64 {
    value
        .and_then(|raw| raw.parse::<f64>().ok())
        .filter(|v| v.is_finite())
        .unwrap_or(f64::MAX)
}

fn has_required_modalities(available: &[String], required: &[String]) -> bool {
    if required.is_empty() {
        return true;
    }

    required.iter().all(|modality| {
        available
            .iter()
            .any(|candidate| candidate.eq_ignore_ascii_case(modality))
    })
}

fn supports_reasoning_preference(
    capabilities: &RainyCapabilitiesV2,
    mode: &ReasoningMode,
    reasoning_value: Option<&str>,
) -> bool {
    if !capabilities.reasoning.supported {
        return false;
    }

    let controls = capabilities.reasoning.controls.as_ref();
    match mode {
        ReasoningMode::Effort => controls
            .map(|c| {
                c.reasoning_effort == Some(true) || c.effort.as_ref().is_some_and(|v| !v.is_empty())
            })
            .filter(|supported| *supported)
            .map(|_| {
                controls
                    .and_then(|c| c.effort.as_ref())
                    .map(|values| {
                        reasoning_value.is_none_or(|value| {
                            values
                                .iter()
                                .any(|candidate| candidate.eq_ignore_ascii_case(value))
                        })
                    })
                    .unwrap_or(reasoning_value.is_none())
            })
            .unwrap_or(false),
        ReasoningMode::ThinkingLevel => controls
            .and_then(|c| c.thinking_level.as_ref())
            .map(|values| {
                reasoning_value.is_none_or(|value| {
                    values
                        .iter()
                        .any(|candidate| candidate.eq_ignore_ascii_case(value))
                })
            })
            .unwrap_or(false),
        ReasoningMode::ThinkingBudget => {
            controls.and_then(|c| c.thinking_budget.as_ref()).is_some()
        }
    }
}

fn catalog_item_supports(item: &ModelCatalogItem, parameter: &str) -> bool {
    if let Some(v2) = &item.rainy_capabilities_v2 {
        return v2
            .parameters
            .accepted
            .iter()
            .any(|candidate| candidate == parameter);
    }

    item.supported_parameters
        .as_ref()
        .map(|params| params.iter().any(|candidate| candidate == parameter))
        .unwrap_or(false)
}

/// Select models from catalog and rank by prompt price, completion price, then context length desc.
pub fn select_models(
    models: &[ModelCatalogItem],
    criteria: &ModelSelectionCriteria,
) -> Vec<ModelCatalogItem> {
    let required_inputs: Vec<String> = criteria
        .required_input_modalities
        .iter()
        .map(|v| v.to_lowercase())
        .collect();
    let required_outputs: Vec<String> = criteria
        .required_output_modalities
        .iter()
        .map(|v| v.to_lowercase())
        .collect();

    let mut filtered: Vec<ModelCatalogItem> = models
        .iter()
        .filter(|item| {
            let Some(v2) = item.rainy_capabilities_v2.as_ref() else {
                return false;
            };
            let input: Vec<String> = v2
                .multimodal
                .input
                .iter()
                .map(|v| v.to_lowercase())
                .collect();
            let output: Vec<String> = v2
                .multimodal
                .output
                .iter()
                .map(|v| v.to_lowercase())
                .collect();

            if !has_required_modalities(&input, &required_inputs) {
                return false;
            }

            if !has_required_modalities(&output, &required_outputs) {
                return false;
            }

            if criteria.require_tools == Some(true) && !catalog_item_supports(item, "tools") {
                return false;
            }

            if criteria.require_structured_output == Some(true)
                && !catalog_item_supports(item, "response_format")
                && !catalog_item_supports(item, "structured_outputs")
            {
                return false;
            }

            if let Some(mode) = &criteria.reasoning_mode {
                let reasoning_value = criteria.reasoning_value.as_deref();
                if !supports_reasoning_preference(v2, mode, reasoning_value) {
                    return false;
                }
            }

            true
        })
        .cloned()
        .collect();

    filtered.sort_by(|a, b| {
        let a_prompt = parse_price(a.pricing.as_ref().and_then(|p| p.prompt.as_deref()));
        let b_prompt = parse_price(b.pricing.as_ref().and_then(|p| p.prompt.as_deref()));
        let prompt_cmp = a_prompt.partial_cmp(&b_prompt).unwrap_or(Ordering::Equal);
        if prompt_cmp != Ordering::Equal {
            return prompt_cmp;
        }

        let a_completion = parse_price(a.pricing.as_ref().and_then(|p| p.completion.as_deref()));
        let b_completion = parse_price(b.pricing.as_ref().and_then(|p| p.completion.as_deref()));
        let completion_cmp = a_completion
            .partial_cmp(&b_completion)
            .unwrap_or(Ordering::Equal);
        if completion_cmp != Ordering::Equal {
            return completion_cmp;
        }

        let a_context = a.context_length.unwrap_or_default();
        let b_context = b.context_length.unwrap_or_default();
        b_context.cmp(&a_context)
    });

    filtered
}

/// Build provider-aware reasoning payload from `rainy_capabilities_v2`.
pub fn build_reasoning_config(
    model: &ModelCatalogItem,
    preference: &ReasoningPreference,
) -> Option<serde_json::Value> {
    let v2 = model.rainy_capabilities_v2.as_ref()?;
    if !v2.reasoning.supported {
        return None;
    }

    let profiles = &v2.reasoning.profiles;
    let controls = v2.reasoning.controls.as_ref();
    match preference.mode {
        ReasoningMode::Effort => {
            let value = preference.value.clone()?;
            let supports_effort = controls
                .map(|c| {
                    c.reasoning_effort == Some(true)
                        || c.effort.as_ref().is_some_and(|v| !v.is_empty())
                })
                .unwrap_or(false);
            if !supports_effort {
                return None;
            }
            if let Some(efforts) = controls.and_then(|c| c.effort.as_ref()) {
                if !efforts.iter().any(|v| v.eq_ignore_ascii_case(&value)) {
                    return None;
                }
            }

            let effort_profile = profiles
                .iter()
                .find(|p| p.parameter_path == "reasoning.effort")?;
            match effort_profile.parameter_path.as_str() {
                "reasoning.effort" => Some(serde_json::json!({
                    "reasoning": { "effort": value }
                })),
                _ => None,
            }
        }
        ReasoningMode::ThinkingLevel => {
            let value = preference.value.clone()?;
            let supports = controls
                .and_then(|c| c.thinking_level.as_ref())
                .map(|levels| levels.iter().any(|v| v.eq_ignore_ascii_case(&value)))
                .unwrap_or(false);
            if !supports {
                return None;
            }
            let level_profile = profiles
                .iter()
                .find(|p| p.parameter_path == "thinking_config.thinking_level")?;
            if let Some(values) = &level_profile.values {
                if !values.iter().any(|v| v.eq_ignore_ascii_case(&value)) {
                    return None;
                }
            }
            Some(serde_json::json!({
                "thinking_config": { "thinking_level": value }
            }))
        }
        ReasoningMode::ThinkingBudget => {
            let budget = preference.budget?;
            let supports = controls.and_then(|c| c.thinking_budget.as_ref())?;
            if budget < supports.min || budget > supports.max {
                return None;
            }
            let budget_profile = profiles.iter().find(|p| {
                p.parameter_path == "thinking.budget_tokens"
                    || p.parameter_path == "thinking_config.thinking_budget"
            })?;

            if budget_profile.parameter_path == "thinking.budget_tokens" {
                return Some(serde_json::json!({
                    "thinking": { "budget_tokens": budget }
                }));
            }
            if budget_profile.parameter_path == "thinking_config.thinking_budget" {
                return Some(serde_json::json!({
                "thinking_config": { "thinking_budget": budget }
                }));
            }
            None
        }
    }
}

#[cfg(feature = "legacy")]
pub mod model_constants;

/// A collection of predefined provider name constants for convenience.
pub mod providers {
    /// Constant for the OpenAI provider.
    pub const OPENAI: &str = "openai";
    /// Constant for the Anthropic provider.
    pub const ANTHROPIC: &str = "anthropic";
    /// Constant for the Groq provider.
    pub const GROQ: &str = "groq";
    /// Constant for the Cerebras provider.
    pub const CEREBRAS: &str = "cerebras";
    /// Constant for the Gemini provider.
    pub const GEMINI: &str = "gemini";
    /// Constant for the Enosis Labs provider.
    pub const ENOSISLABS: &str = "enosislabs";
}

impl ChatCompletionRequest {
    /// Creates a new `ChatCompletionRequest` with the given model and messages.
    ///
    /// # Arguments
    ///
    /// * `model` - The identifier of the model to use.
    /// * `messages` - The list of messages for the conversation.
    pub fn new(model: impl Into<String>, messages: Vec<ChatMessage>) -> Self {
        Self {
            model: model.into(),
            messages,
            temperature: None,
            max_tokens: None,
            max_completion_tokens: None,
            top_p: None,
            frequency_penalty: None,
            presence_penalty: None,
            stop: None,
            user: None,
            provider: None,
            stream: None,
            stream_options: None,
            logit_bias: None,
            logprobs: None,
            top_logprobs: None,
            n: None,
            response_format: None,
            tools: None,
            tool_choice: None,
            parallel_tool_calls: None,
            seed: None,
            prompt_cache_key: None,
            provider_options: None,
            prompt_cache_retention: None,
            reasoning: None,
            include_reasoning: None,
            metadata: None,
            service_tier: None,
            store: None,
            safety_identifier: None,
            modalities: None,
            audio: None,
            prediction: None,
            verbosity: None,
            web_search_options: None,
            functions: None,
            function_call: None,
            thinking_config: None,
        }
    }

    /// Sets the temperature for the chat completion.
    ///
    /// The temperature is clamped between 0.0 and 2.0.
    ///
    /// # Arguments
    ///
    /// * `temperature` - The sampling temperature.
    pub fn with_temperature(mut self, temperature: f32) -> Self {
        self.temperature = Some(temperature.clamp(0.0, 2.0));
        self
    }

    /// Sets the maximum number of tokens to generate.
    ///
    /// # Arguments
    ///
    /// * `max_tokens` - The maximum number of tokens.
    pub fn with_max_tokens(mut self, max_tokens: u32) -> Self {
        self.max_tokens = Some(max_tokens);
        self
    }

    /// Sets the maximum number of completion tokens.
    pub fn with_max_completion_tokens(mut self, max_completion_tokens: u32) -> Self {
        self.max_completion_tokens = Some(max_completion_tokens);
        self
    }

    /// Sets the user identifier for the chat completion.
    ///
    /// # Arguments
    ///
    /// * `user` - A unique identifier for the end-user.
    pub fn with_user(mut self, user: impl Into<String>) -> Self {
        self.user = Some(user.into());
        self
    }

    /// Sets a provider hint for the request.
    ///
    /// # Arguments
    ///
    /// * `provider` - The name of the provider to use.
    pub fn with_provider(mut self, provider: impl Into<String>) -> Self {
        self.provider = Some(provider.into());
        self
    }

    /// Enables or disables streaming for the response.
    ///
    /// # Arguments
    ///
    /// * `stream` - `true` to enable streaming, `false` to disable.
    pub fn with_stream(mut self, stream: bool) -> Self {
        self.stream = Some(stream);
        self
    }

    /// Sets stream options payload.
    pub fn with_stream_options(mut self, stream_options: serde_json::Value) -> Self {
        self.stream_options = Some(stream_options);
        self
    }

    /// Sets the logit bias for the chat completion.
    ///
    /// # Arguments
    ///
    /// * `logit_bias` - A map of token IDs to bias values.
    pub fn with_logit_bias(mut self, logit_bias: serde_json::Value) -> Self {
        self.logit_bias = Some(logit_bias);
        self
    }

    /// Enables or disables log probabilities for the response.
    ///
    /// # Arguments
    ///
    /// * `logprobs` - `true` to include log probabilities.
    pub fn with_logprobs(mut self, logprobs: bool) -> Self {
        self.logprobs = Some(logprobs);
        self
    }

    /// Sets the number of most likely tokens to return at each position.
    ///
    /// # Arguments
    ///
    /// * `top_logprobs` - The number of top log probabilities to return.
    pub fn with_top_logprobs(mut self, top_logprobs: u32) -> Self {
        self.top_logprobs = Some(top_logprobs);
        self
    }

    /// Sets the number of chat completion choices to generate.
    ///
    /// # Arguments
    ///
    /// * `n` - The number of completions to generate.
    pub fn with_n(mut self, n: u32) -> Self {
        self.n = Some(n);
        self
    }

    /// Sets the response format for the chat completion.
    ///
    /// # Arguments
    ///
    /// * `response_format` - The format the model must output.
    pub fn with_response_format(mut self, response_format: ResponseFormat) -> Self {
        self.response_format = Some(response_format);
        self
    }

    /// Sets the tools available to the model.
    ///
    /// # Arguments
    ///
    /// * `tools` - A list of tools the model can use.
    pub fn with_tools(mut self, tools: Vec<Tool>) -> Self {
        self.tools = Some(tools);
        self
    }

    /// Sets the tool choice for the chat completion.
    ///
    /// # Arguments
    ///
    /// * `tool_choice` - Controls which tool the model uses.
    pub fn with_tool_choice(mut self, tool_choice: ToolChoice) -> Self {
        self.tool_choice = Some(tool_choice);
        self
    }

    /// Sets the thinking configuration for Gemini 3 and 2.5 series models.
    ///
    /// # Arguments
    ///
    /// * `thinking_config` - Configuration for thinking capabilities.
    pub fn with_thinking_config(mut self, thinking_config: ThinkingConfig) -> Self {
        self.thinking_config = Some(thinking_config);
        self
    }

    /// Sets reasoning configuration object used by modern compat routes.
    pub fn with_reasoning(mut self, reasoning: serde_json::Value) -> Self {
        self.reasoning = Some(reasoning);
        self
    }

    /// Requests reasoning traces where supported.
    pub fn with_include_reasoning(mut self, include_reasoning: bool) -> Self {
        self.include_reasoning = Some(include_reasoning);
        self
    }

    /// Sets service tier hint (`auto`, `default`, `flex`, etc.).
    pub fn with_service_tier(mut self, service_tier: impl Into<String>) -> Self {
        self.service_tier = Some(service_tier.into());
        self
    }

    /// Sets metadata map.
    pub fn with_metadata(mut self, metadata: HashMap<String, String>) -> Self {
        self.metadata = Some(metadata);
        self
    }

    /// Enables thought summaries in the response (Gemini 3 and 2.5 series).
    ///
    /// # Arguments
    ///
    /// * `include_thoughts` - Whether to include thought summaries.
    pub fn with_include_thoughts(mut self, include_thoughts: bool) -> Self {
        let mut config = self.thinking_config.unwrap_or_default();
        config.include_thoughts = Some(include_thoughts);
        self.thinking_config = Some(config);
        self
    }

    /// Sets the thinking level for Gemini 3 models.
    ///
    /// # Arguments
    ///
    /// * `thinking_level` - The thinking level (minimal, low, medium, high).
    pub fn with_thinking_level(mut self, thinking_level: ThinkingLevel) -> Self {
        let mut config = self.thinking_config.unwrap_or_default();
        config.thinking_level = Some(thinking_level);
        self.thinking_config = Some(config);
        self
    }

    /// Sets the thinking budget for Gemini 2.5 models.
    ///
    /// # Arguments
    ///
    /// * `thinking_budget` - Number of thinking tokens (-1 for dynamic, 0 to disable).
    pub fn with_thinking_budget(mut self, thinking_budget: i32) -> Self {
        let mut config = self.thinking_config.unwrap_or_default();
        config.thinking_budget = Some(thinking_budget);
        self.thinking_config = Some(config);
        self
    }

    /// Validates that the request parameters are compatible with OpenAI standards.
    ///
    /// This method checks parameter ranges and values to ensure they match OpenAI's API specifications.
    /// Also validates Gemini 3 specific parameters like thinking configuration.
    ///
    /// # Returns
    ///
    /// A `Result` indicating whether the request is valid for OpenAI compatibility.
    pub fn validate_openai_compatibility(&self) -> Result<(), String> {
        // Validate temperature
        if let Some(temp) = self.temperature {
            if !(0.0..=2.0).contains(&temp) {
                return Err(format!(
                    "Temperature must be between 0.0 and 2.0, got {}",
                    temp
                ));
            }
        }

        // Validate top_p
        if let Some(top_p) = self.top_p {
            if !(0.0..=1.0).contains(&top_p) {
                return Err(format!("Top-p must be between 0.0 and 1.0, got {}", top_p));
            }
        }

        // Validate frequency_penalty
        if let Some(fp) = self.frequency_penalty {
            if !(-2.0..=2.0).contains(&fp) {
                return Err(format!(
                    "Frequency penalty must be between -2.0 and 2.0, got {}",
                    fp
                ));
            }
        }

        // Validate presence_penalty
        if let Some(pp) = self.presence_penalty {
            if !(-2.0..=2.0).contains(&pp) {
                return Err(format!(
                    "Presence penalty must be between -2.0 and 2.0, got {}",
                    pp
                ));
            }
        }

        // Validate max_tokens
        if let Some(mt) = self.max_tokens {
            if mt == 0 {
                return Err("Max tokens must be greater than 0".to_string());
            }
        }

        // Validate max_completion_tokens
        if let Some(mct) = self.max_completion_tokens {
            if mct == 0 {
                return Err("Max completion tokens must be greater than 0".to_string());
            }
        }

        // Validate top_logprobs
        if let Some(tlp) = self.top_logprobs {
            if !(0..=20).contains(&tlp) {
                return Err(format!(
                    "Top logprobs must be between 0 and 20, got {}",
                    tlp
                ));
            }
        }

        // Validate n
        if let Some(n) = self.n {
            if n == 0 {
                return Err("n must be greater than 0".to_string());
            }
        }

        // Validate stop sequences
        if let Some(stop) = &self.stop {
            if stop.len() > 4 {
                return Err("Cannot have more than 4 stop sequences".to_string());
            }
            for seq in stop {
                if seq.is_empty() {
                    return Err("Stop sequences cannot be empty".to_string());
                }
                if seq.len() > 64 {
                    return Err("Stop sequences cannot be longer than 64 characters".to_string());
                }
            }
        }

        // Validate thinking configuration for Gemini models
        if let Some(thinking_config) = &self.thinking_config {
            self.validate_thinking_config(thinking_config)?;
        }

        Ok(())
    }

    /// Validates thinking configuration parameters for Gemini models.
    fn validate_thinking_config(&self, config: &ThinkingConfig) -> Result<(), String> {
        let is_gemini_3 = self.model.contains("gemini-3");
        let is_gemini_2_5 = self.model.contains("gemini-2.5");
        let is_gemini_3_pro = self.model.contains("gemini-3-pro");

        // Validate thinking level (Gemini 3 only)
        if let Some(level) = &config.thinking_level {
            if !is_gemini_3 {
                return Err("thinking_level is only supported for Gemini 3 models".to_string());
            }

            match level {
                ThinkingLevel::Minimal | ThinkingLevel::Medium => {
                    if is_gemini_3_pro {
                        return Err(
                            "Gemini 3 Pro only supports 'low' and 'high' thinking levels"
                                .to_string(),
                        );
                    }
                }
                _ => {}
            }
        }

        // Validate thinking budget (Gemini 2.5 only)
        if let Some(budget) = config.thinking_budget {
            if !is_gemini_2_5 {
                return Err("thinking_budget is only supported for Gemini 2.5 models".to_string());
            }

            // Validate budget ranges based on model
            if self.model.contains("2.5-pro") {
                if budget != -1 && !(128..=32768).contains(&budget) {
                    return Err(
                        "Gemini 2.5 Pro thinking budget must be -1 (dynamic) or between 128-32768"
                            .to_string(),
                    );
                }
            } else if self.model.contains("2.5-flash")
                && budget != -1
                && !(0..=24576).contains(&budget)
            {
                return Err(
                    "Gemini 2.5 Flash thinking budget must be -1 (dynamic) or between 0-24576"
                        .to_string(),
                );
            }
        }

        // Warn about conflicting parameters
        if config.thinking_level.is_some() && config.thinking_budget.is_some() {
            return Err("Cannot specify both thinking_level (Gemini 3) and thinking_budget (Gemini 2.5) in the same request".to_string());
        }

        Ok(())
    }

    /// Checks if the model supports thinking capabilities.
    pub fn supports_thinking(&self) -> bool {
        self.model.contains("gemini-3") || self.model.contains("gemini-2.5")
    }

    /// Checks if the model requires thought signatures for function calling.
    pub fn requires_thought_signatures(&self) -> bool {
        self.model.contains("gemini-3")
    }
}

impl OpenAIChatCompletionRequest {
    /// Creates a new OpenAI-compatible chat completion request.
    pub fn new(model: impl Into<String>, messages: Vec<OpenAIChatMessage>) -> Self {
        Self {
            model: model.into(),
            messages,
            temperature: None,
            max_tokens: None,
            max_completion_tokens: None,
            top_p: None,
            frequency_penalty: None,
            presence_penalty: None,
            stop: None,
            user: None,
            provider: None,
            stream: None,
            stream_options: None,
            logit_bias: None,
            logprobs: None,
            top_logprobs: None,
            n: None,
            response_format: None,
            tools: None,
            tool_choice: None,
            parallel_tool_calls: None,
            seed: None,
            prompt_cache_key: None,
            provider_options: None,
            prompt_cache_retention: None,
            reasoning: None,
            include_reasoning: None,
            metadata: None,
            service_tier: None,
            store: None,
            safety_identifier: None,
            modalities: None,
            audio: None,
            prediction: None,
            verbosity: None,
            web_search_options: None,
            functions: None,
            function_call: None,
            thinking_config: None,
            thinking: None,
        }
    }

    /// Sets the sampling temperature.
    pub fn with_temperature(mut self, temperature: f32) -> Self {
        self.temperature = Some(temperature.clamp(0.0, 2.0));
        self
    }

    /// Sets the maximum number of tokens to generate.
    pub fn with_max_tokens(mut self, max_tokens: u32) -> Self {
        self.max_tokens = Some(max_tokens);
        self
    }

    /// Sets the maximum number of completion tokens.
    pub fn with_max_completion_tokens(mut self, max_completion_tokens: u32) -> Self {
        self.max_completion_tokens = Some(max_completion_tokens);
        self
    }

    /// Sets the end-user identifier.
    pub fn with_user(mut self, user: impl Into<String>) -> Self {
        self.user = Some(user.into());
        self
    }

    /// Sets a provider hint.
    pub fn with_provider(mut self, provider: impl Into<String>) -> Self {
        self.provider = Some(provider.into());
        self
    }

    /// Enables or disables streaming.
    pub fn with_stream(mut self, stream: bool) -> Self {
        self.stream = Some(stream);
        self
    }

    /// Sets stream options payload.
    pub fn with_stream_options(mut self, stream_options: serde_json::Value) -> Self {
        self.stream_options = Some(stream_options);
        self
    }

    /// Sets nucleus sampling.
    pub fn with_top_p(mut self, top_p: f32) -> Self {
        self.top_p = Some(top_p.clamp(0.0, 1.0));
        self
    }

    /// Sets frequency penalty.
    pub fn with_frequency_penalty(mut self, frequency_penalty: f32) -> Self {
        self.frequency_penalty = Some(frequency_penalty.clamp(-2.0, 2.0));
        self
    }

    /// Sets presence penalty.
    pub fn with_presence_penalty(mut self, presence_penalty: f32) -> Self {
        self.presence_penalty = Some(presence_penalty.clamp(-2.0, 2.0));
        self
    }

    /// Sets stop sequences.
    pub fn with_stop(mut self, stop: Vec<String>) -> Self {
        self.stop = Some(stop);
        self
    }

    /// Sets logit bias.
    pub fn with_logit_bias(mut self, logit_bias: serde_json::Value) -> Self {
        self.logit_bias = Some(logit_bias);
        self
    }

    /// Enables or disables log probabilities.
    pub fn with_logprobs(mut self, logprobs: bool) -> Self {
        self.logprobs = Some(logprobs);
        self
    }

    /// Sets the top log probabilities count.
    pub fn with_top_logprobs(mut self, top_logprobs: u32) -> Self {
        self.top_logprobs = Some(top_logprobs);
        self
    }

    /// Sets the number of choices to generate.
    pub fn with_n(mut self, n: u32) -> Self {
        self.n = Some(n);
        self
    }

    /// Sets the response format.
    pub fn with_response_format(mut self, response_format: ResponseFormat) -> Self {
        self.response_format = Some(response_format);
        self
    }

    /// Sets the available tools.
    pub fn with_tools(mut self, tools: Vec<Tool>) -> Self {
        self.tools = Some(tools);
        self
    }

    /// Sets the tool choice strategy.
    pub fn with_tool_choice(mut self, tool_choice: ToolChoice) -> Self {
        self.tool_choice = Some(tool_choice);
        self
    }

    /// Sets the Gemini thinking configuration.
    pub fn with_thinking_config(mut self, thinking_config: ThinkingConfig) -> Self {
        self.thinking_config = Some(thinking_config);
        self
    }

    /// Sets reasoning configuration object used by modern compat routes.
    pub fn with_reasoning(mut self, reasoning: serde_json::Value) -> Self {
        self.reasoning = Some(reasoning);
        self
    }

    /// Requests reasoning traces where supported.
    pub fn with_include_reasoning(mut self, include_reasoning: bool) -> Self {
        self.include_reasoning = Some(include_reasoning);
        self
    }

    /// Sets service tier hint (`auto`, `default`, `flex`, etc.).
    pub fn with_service_tier(mut self, service_tier: impl Into<String>) -> Self {
        self.service_tier = Some(service_tier.into());
        self
    }

    /// Sets metadata map.
    pub fn with_metadata(mut self, metadata: HashMap<String, String>) -> Self {
        self.metadata = Some(metadata);
        self
    }

    /// Enables or disables thought summaries.
    pub fn with_include_thoughts(mut self, include_thoughts: bool) -> Self {
        let mut config = self.thinking_config.unwrap_or_default();
        config.include_thoughts = Some(include_thoughts);
        self.thinking_config = Some(config);
        self
    }

    /// Sets the Gemini 3 thinking level.
    pub fn with_thinking_level(mut self, thinking_level: ThinkingLevel) -> Self {
        let mut config = self.thinking_config.unwrap_or_default();
        config.thinking_level = Some(thinking_level);
        self.thinking_config = Some(config);
        self
    }

    /// Sets the Gemini 2.5 thinking budget.
    pub fn with_thinking_budget(mut self, thinking_budget: i32) -> Self {
        let mut config = self.thinking_config.unwrap_or_default();
        config.thinking_budget = Some(thinking_budget);
        self.thinking_config = Some(config);
        self
    }

    /// Sets Anthropic extended-thinking configuration.
    ///
    /// Serialised as `thinking: {"type":"enabled","budget_tokens":N}` — the format
    /// expected by Anthropic's API via OpenRouter/Rainy API.
    pub fn with_anthropic_thinking(mut self, budget_tokens: i32) -> Self {
        self.thinking =
            Some(serde_json::json!({"type": "enabled", "budget_tokens": budget_tokens}));
        self
    }

    /// Validates compatibility using the same parameter rules as the simple chat request.
    pub fn validate_openai_compatibility(&self) -> Result<(), String> {
        ChatCompletionRequest {
            model: self.model.clone(),
            messages: vec![],
            temperature: self.temperature,
            max_tokens: self.max_tokens,
            max_completion_tokens: self.max_completion_tokens,
            top_p: self.top_p,
            frequency_penalty: self.frequency_penalty,
            presence_penalty: self.presence_penalty,
            stop: self.stop.clone(),
            user: self.user.clone(),
            provider: self.provider.clone(),
            stream: self.stream,
            stream_options: self.stream_options.clone(),
            logit_bias: self.logit_bias.clone(),
            logprobs: self.logprobs,
            top_logprobs: self.top_logprobs,
            n: self.n,
            response_format: self.response_format.clone(),
            tools: self.tools.clone(),
            tool_choice: self.tool_choice.clone(),
            parallel_tool_calls: self.parallel_tool_calls,
            seed: self.seed,
            prompt_cache_key: self.prompt_cache_key.clone(),
            provider_options: self.provider_options.clone(),
            prompt_cache_retention: self.prompt_cache_retention.clone(),
            reasoning: self.reasoning.clone(),
            include_reasoning: self.include_reasoning,
            metadata: self.metadata.clone(),
            service_tier: self.service_tier.clone(),
            store: self.store,
            safety_identifier: self.safety_identifier.clone(),
            modalities: self.modalities.clone(),
            audio: self.audio.clone(),
            prediction: self.prediction.clone(),
            verbosity: self.verbosity.clone(),
            web_search_options: self.web_search_options.clone(),
            functions: self.functions.clone(),
            function_call: self.function_call.clone(),
            thinking_config: self.thinking_config.clone(),
        }
        .validate_openai_compatibility()
    }

    /// Checks whether the selected model supports thinking features.
    pub fn supports_thinking(&self) -> bool {
        self.model.contains("gemini-3") || self.model.contains("gemini-2.5")
    }

    /// Checks whether the selected model requires thought signatures for function calling.
    pub fn requires_thought_signatures(&self) -> bool {
        self.model.contains("gemini-3")
    }
}

impl ChatMessage {
    /// Creates a new message with the `System` role.
    ///
    /// # Arguments
    ///
    /// * `content` - The content of the system message.
    pub fn system(content: impl Into<String>) -> Self {
        Self {
            role: MessageRole::System,
            content: content.into(),
        }
    }

    /// Creates a new message with the `User` role.
    ///
    /// # Arguments
    ///
    /// * `content` - The content of the user message.
    pub fn user(content: impl Into<String>) -> Self {
        Self {
            role: MessageRole::User,
            content: content.into(),
        }
    }

    /// Creates a new message with the `Assistant` role.
    ///
    /// # Arguments
    ///
    /// * `content` - The content of the assistant message.
    pub fn assistant(content: impl Into<String>) -> Self {
        Self {
            role: MessageRole::Assistant,
            content: content.into(),
        }
    }
}

impl OpenAIMessageContent {
    /// Creates text content.
    pub fn text(content: impl Into<String>) -> Self {
        Self::Text(content.into())
    }

    /// Creates multimodal content parts.
    pub fn parts(parts: Vec<OpenAIContentPart>) -> Self {
        Self::Parts(parts)
    }
}

impl OpenAIContentPart {
    /// Creates a text content part.
    pub fn text(content: impl Into<String>) -> Self {
        Self::Text {
            text: content.into(),
        }
    }

    /// Creates an image URL part.
    pub fn image_url(url: impl Into<String>) -> Self {
        Self::ImageUrl {
            image_url: OpenAIImageUrl {
                url: url.into(),
                detail: None,
            },
        }
    }

    /// Creates an image URL part with a specific detail hint.
    pub fn image_url_with_detail(url: impl Into<String>, detail: impl Into<String>) -> Self {
        Self::ImageUrl {
            image_url: OpenAIImageUrl {
                url: url.into(),
                detail: Some(detail.into()),
            },
        }
    }
}

impl OpenAIChatMessage {
    /// Creates a new system message.
    pub fn system(content: impl Into<OpenAIMessageContent>) -> Self {
        Self {
            role: OpenAIMessageRole::System,
            content: Some(content.into()),
            name: None,
            tool_calls: None,
            tool_call_id: None,
        }
    }

    /// Creates a new user message.
    pub fn user(content: impl Into<OpenAIMessageContent>) -> Self {
        Self {
            role: OpenAIMessageRole::User,
            content: Some(content.into()),
            name: None,
            tool_calls: None,
            tool_call_id: None,
        }
    }

    /// Creates a new assistant message.
    pub fn assistant(content: impl Into<OpenAIMessageContent>) -> Self {
        Self {
            role: OpenAIMessageRole::Assistant,
            content: Some(content.into()),
            name: None,
            tool_calls: None,
            tool_call_id: None,
        }
    }

    /// Creates an assistant message that only carries tool calls.
    pub fn assistant_with_tool_calls(tool_calls: Vec<OpenAIToolCall>) -> Self {
        Self {
            role: OpenAIMessageRole::Assistant,
            content: None,
            name: None,
            tool_calls: Some(tool_calls),
            tool_call_id: None,
        }
    }

    /// Creates a tool result message.
    pub fn tool(tool_call_id: impl Into<String>, content: impl Into<OpenAIMessageContent>) -> Self {
        Self {
            role: OpenAIMessageRole::Tool,
            content: Some(content.into()),
            name: None,
            tool_calls: None,
            tool_call_id: Some(tool_call_id.into()),
        }
    }

    /// Creates a message with full control over optional OpenAI-compatible fields.
    pub fn with_parts(
        role: OpenAIMessageRole,
        content: Option<OpenAIMessageContent>,
        tool_calls: Option<Vec<OpenAIToolCall>>,
        tool_call_id: Option<String>,
    ) -> Self {
        Self {
            role,
            content,
            name: None,
            tool_calls,
            tool_call_id,
        }
    }
}

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

impl From<&str> for OpenAIMessageContent {
    fn from(value: &str) -> Self {
        Self::Text(value.to_string())
    }
}

#[cfg(feature = "legacy")]
mod legacy_types;
#[cfg(feature = "legacy")]
pub use legacy_types::{
    ApiKey, ChatRole, ChatUsage, CreditTransaction, DailyUsage, HealthCheck, HealthServices,
    HealthStatusEnum, TransactionType, UsageStats, User,
};

/// Represents the format that the model must output.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ResponseFormat {
    /// The model can return text.
    Text,
    /// The model must return a valid JSON object.
    JsonObject,
    /// The model must return a JSON object that matches the provided schema.
    JsonSchema {
        /// The JSON Schema that the model's output must conform to.
        json_schema: serde_json::Value,
    },
}

/// Represents a tool that the model can use.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Tool {
    /// The type of the tool (currently only "function" is supported).
    pub r#type: ToolType,
    /// The function definition describing the tool's capabilities.
    pub function: FunctionDefinition,
}

/// The type of tool.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ToolType {
    /// A function tool.
    Function,
}

/// Represents a function definition for a tool.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FunctionDefinition {
    /// The name of the function.
    pub name: String,
    /// A description of what the function does.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// The parameters the function accepts, described as a JSON Schema object.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parameters: Option<serde_json::Value>,
}

/// Controls which tool is called by the model.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ToolChoice {
    /// No tool is called.
    None,
    /// The model chooses which tool to call.
    Auto,
    /// A specific tool is called.
    Tool {
        /// The type of the tool being called.
        r#type: ToolType,
        /// The function to call within the tool.
        function: ToolFunction,
    },
}

/// Represents a tool function call.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolFunction {
    /// The name of the function to call.
    pub name: String,
}

/// Configuration for thinking capabilities in Gemini 3 and 2.5 series models.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ThinkingConfig {
    /// Whether to include thought summaries in the response.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub include_thoughts: Option<bool>,

    /// The thinking level for Gemini 3 models (low, high for Pro; minimal, low, medium, high for Flash).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thinking_level: Option<ThinkingLevel>,

    /// The thinking budget for Gemini 2.5 models (number of thinking tokens).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thinking_budget: Option<i32>,
}

/// Thinking levels for Gemini 3 models.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ThinkingLevel {
    /// Minimal thinking (Gemini 3 Flash only) - model likely won't think.
    Minimal,
    /// Low thinking level - faster responses with basic reasoning.
    Low,
    /// Medium thinking level (Gemini 3 Flash only) - balanced reasoning and speed.
    Medium,
    /// High thinking level - deep reasoning for complex tasks (default).
    High,
}

/// Represents a content part that may include thought signatures.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ContentPart {
    /// The text content of the part.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,

    /// Function call information if this part contains a function call.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub function_call: Option<FunctionCall>,

    /// Function response information if this part contains a function response.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub function_response: Option<FunctionResponse>,

    /// Indicates if this part contains thought content.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thought: Option<bool>,

    /// Encrypted thought signature for preserving reasoning context across turns.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thought_signature: Option<String>,
}

/// Represents a function call in the content.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct FunctionCall {
    /// The name of the function being called.
    pub name: String,
    /// The arguments for the function call as a JSON object.
    pub args: serde_json::Value,
}

/// Represents a function response in the content.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct FunctionResponse {
    /// The name of the function that was called.
    pub name: String,
    /// The response from the function call.
    pub response: serde_json::Value,
}

/// Enhanced chat message that supports Gemini 3 thinking capabilities.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct EnhancedChatMessage {
    /// The role of the message author.
    pub role: MessageRole,
    /// The content parts of the message (supports text, function calls, and thought signatures).
    pub parts: Vec<ContentPart>,
}

/// Enhanced usage statistics that include thinking tokens.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnhancedUsage {
    /// The number of tokens in the prompt.
    pub prompt_tokens: u32,
    /// The number of tokens in the generated completion.
    pub completion_tokens: u32,
    /// The total number of tokens used in the request (prompt + completion).
    pub total_tokens: u32,
    /// The number of thinking tokens used (Gemini 3 and 2.5 series).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thoughts_token_count: Option<u32>,
}

impl ThinkingConfig {
    /// Creates a new thinking configuration with default values.
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a configuration for Gemini 3 models with specified thinking level.
    ///
    /// # Arguments
    ///
    /// * `level` - The thinking level to use.
    /// * `include_thoughts` - Whether to include thought summaries.
    pub fn gemini_3(level: ThinkingLevel, include_thoughts: bool) -> Self {
        Self {
            thinking_level: Some(level),
            include_thoughts: Some(include_thoughts),
            thinking_budget: None,
        }
    }

    /// Creates a configuration for Gemini 2.5 models with specified thinking budget.
    ///
    /// # Arguments
    ///
    /// * `budget` - The thinking budget (-1 for dynamic, 0 to disable, or specific token count).
    /// * `include_thoughts` - Whether to include thought summaries.
    pub fn gemini_2_5(budget: i32, include_thoughts: bool) -> Self {
        Self {
            thinking_budget: Some(budget),
            include_thoughts: Some(include_thoughts),
            thinking_level: None,
        }
    }

    /// Creates a configuration optimized for complex reasoning tasks.
    pub fn high_reasoning() -> Self {
        Self {
            thinking_level: Some(ThinkingLevel::High),
            include_thoughts: Some(true),
            thinking_budget: Some(-1), // Dynamic for 2.5 models
        }
    }

    /// Creates a configuration optimized for fast responses.
    pub fn fast_response() -> Self {
        Self {
            thinking_level: Some(ThinkingLevel::Low),
            include_thoughts: Some(false),
            thinking_budget: Some(512), // Low budget for 2.5 models
        }
    }
}

impl ContentPart {
    /// Creates a new text content part.
    pub fn text(content: impl Into<String>) -> Self {
        Self {
            text: Some(content.into()),
            function_call: None,
            function_response: None,
            thought: None,
            thought_signature: None,
        }
    }

    /// Creates a new function call content part.
    pub fn function_call(name: impl Into<String>, args: serde_json::Value) -> Self {
        Self {
            text: None,
            function_call: Some(FunctionCall {
                name: name.into(),
                args,
            }),
            function_response: None,
            thought: None,
            thought_signature: None,
        }
    }

    /// Creates a new function response content part.
    pub fn function_response(name: impl Into<String>, response: serde_json::Value) -> Self {
        Self {
            text: None,
            function_call: None,
            function_response: Some(FunctionResponse {
                name: name.into(),
                response,
            }),
            thought: None,
            thought_signature: None,
        }
    }

    /// Adds a thought signature to this content part.
    pub fn with_thought_signature(mut self, signature: impl Into<String>) -> Self {
        self.thought_signature = Some(signature.into());
        self
    }

    /// Marks this content part as containing thought content.
    pub fn as_thought(mut self) -> Self {
        self.thought = Some(true);
        self
    }
}

impl EnhancedChatMessage {
    /// Creates a new enhanced message with the `System` role.
    pub fn system(content: impl Into<String>) -> Self {
        Self {
            role: MessageRole::System,
            parts: vec![ContentPart::text(content)],
        }
    }

    /// Creates a new enhanced message with the `User` role.
    pub fn user(content: impl Into<String>) -> Self {
        Self {
            role: MessageRole::User,
            parts: vec![ContentPart::text(content)],
        }
    }

    /// Creates a new enhanced message with the `Assistant` role.
    pub fn assistant(content: impl Into<String>) -> Self {
        Self {
            role: MessageRole::Assistant,
            parts: vec![ContentPart::text(content)],
        }
    }

    /// Creates a new enhanced message with multiple content parts.
    pub fn with_parts(role: MessageRole, parts: Vec<ContentPart>) -> Self {
        Self { role, parts }
    }
}

/// Billing usage payload emitted via `event: rainy.billing` in chat streams.
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct RainyBillingUsage {
    /// Prompt token count.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prompt_tokens: Option<u32>,
    /// Completion token count.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub completion_tokens: Option<u32>,
    /// Reasoning token count when available.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning_tokens: Option<u32>,
    /// Image units when available.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub image_units: Option<u32>,
}

/// Rainy native billing event emitted during chat streaming.
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct RainyBillingStreamEvent {
    /// Billing plan identifier.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub plan_id: Option<String>,
    /// Charged credits so far.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub charged_credits: Option<f64>,
    /// Usage snapshot included by the server.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub usage: Option<RainyBillingUsage>,
}

/// Typed event emitted by chat streaming endpoints.
#[derive(Debug, Clone)]
pub enum ChatStreamEvent {
    /// Standard OpenAI-compatible chat chunk.
    Chunk(ChatCompletionStreamResponse),
    /// Rainy native billing event.
    Billing(RainyBillingStreamEvent),
    /// Unknown event payload kept verbatim for forward compatibility.
    Raw(serde_json::Value),
}

impl ChatStreamEvent {
    /// Build a typed stream event from a raw JSON value.
    pub fn from_value(value: serde_json::Value) -> Self {
        if let Ok(chunk) = serde_json::from_value::<ChatCompletionStreamResponse>(value.clone()) {
            return Self::Chunk(chunk);
        }

        if let Ok(billing) = serde_json::from_value::<RainyBillingStreamEvent>(value.clone()) {
            if billing.plan_id.is_some()
                || billing.charged_credits.is_some()
                || billing.usage.is_some()
            {
                return Self::Billing(billing);
            }
        }

        Self::Raw(value)
    }
}

/// Represents a streaming chat completion response (OpenAI delta format).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatCompletionStreamResponse {
    /// A unique identifier for the chat completion.
    pub id: String,
    /// The type of object, which is always "chat.completion.chunk".
    pub object: String,
    /// The Unix timestamp (in seconds) of when the completion was created.
    pub created: u64,
    /// The model that was used for the completion.
    pub model: String,
    /// A list of chat completion choices.
    pub choices: Vec<ChatCompletionStreamChoice>,
    /// Information about the token usage for this completion (only present in the final chunk).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub usage: Option<Usage>,
}

/// Represents a single choice in a streaming chat completion response.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatCompletionStreamChoice {
    /// The index of the choice in the list of choices.
    pub index: u32,
    /// The delta containing the new content for this choice.
    pub delta: ChatCompletionStreamDelta,
    /// The reason the model stopped generating tokens (only present in the final chunk).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub finish_reason: Option<String>,
}

/// Represents the delta (change) in a streaming chat completion response.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatCompletionStreamDelta {
    /// The role of the message (only present in the first chunk).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub role: Option<String>,
    /// The new content for this chunk.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content: Option<String>,
    /// The thinking/reasoning content for this chunk (if any).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thought: Option<String>,
    /// Tool calls for this chunk (if any).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_calls: Option<Vec<ToolCall>>,
}

/// Represents a tool call in a streaming response.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCall {
    /// The index of the tool call.
    pub index: u32,
    /// The ID of the tool call.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// The type of the tool call.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub r#type: Option<String>,
    /// The function being called.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub function: Option<ToolCallFunction>,
}

/// Represents a function call in a tool call.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCallFunction {
    /// The name of the function.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// The arguments for the function.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub arguments: Option<String>,
}