memvid-ask-model 2.0.140

LLM inference module for Memvid Q&A with local and cloud model support
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
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
use std::fmt;
use std::io::{IsTerminal, Write, stderr};
use std::path::PathBuf;
use std::sync::{
    Arc,
    atomic::{AtomicBool, Ordering},
};
use std::thread;
use std::time::Duration;

use memvid_core::types::SearchHit;

#[derive(Debug, Clone)]
pub struct ModelAnswer {
    pub requested: String,
    pub model: String,
    pub answer: String,
}

#[derive(Debug, Clone)]
pub struct ModelInference {
    pub answer: ModelAnswer,
    pub context_body: String,
    pub context_fragments: Vec<ModelContextFragment>,
    pub usage: Option<TokenUsage>,
    pub grounding: Option<GroundingResult>,
    /// True if this result came from the cache
    pub cached: bool,
}

/// Token usage and cost information from LLM inference
#[derive(Debug, Clone, Default)]
pub struct TokenUsage {
    /// Input/prompt tokens
    pub input_tokens: u32,
    /// Output/completion tokens
    pub output_tokens: u32,
    /// Total tokens (input + output)
    pub total_tokens: u32,
    /// Estimated cost in USD (based on model pricing)
    pub cost_usd: f64,
}

/// Cache for LLM answers to avoid redundant API calls
/// Uses Blake3 hash of (query + context) as the key
pub mod cache {
    use std::collections::HashMap;
    use std::sync::Mutex;

    /// Cached answer entry
    #[derive(Debug, Clone)]
    pub struct CacheEntry {
        pub answer: String,
        pub model: String,
        pub input_tokens: u32,
        pub output_tokens: u32,
        pub cost_usd: f64,
        pub grounding_score: f32,
        pub created_at: std::time::SystemTime,
    }

    /// In-memory LRU cache for answers
    /// Thread-safe with a simple mutex
    pub struct AnswerCache {
        entries: Mutex<HashMap<[u8; 32], CacheEntry>>,
        max_size: usize,
        hits: std::sync::atomic::AtomicU64,
        misses: std::sync::atomic::AtomicU64,
    }

    impl AnswerCache {
        /// Create a new cache with the specified maximum size
        pub fn new(max_size: usize) -> Self {
            Self {
                entries: Mutex::new(HashMap::new()),
                max_size,
                hits: std::sync::atomic::AtomicU64::new(0),
                misses: std::sync::atomic::AtomicU64::new(0),
            }
        }

        /// Generate a cache key from query and context
        pub fn make_key(query: &str, context: &str, model: &str) -> [u8; 32] {
            use std::io::Write;
            let mut hasher = blake3::Hasher::new();
            let _ = write!(hasher, "{}|{}|{}", model, query, context);
            *hasher.finalize().as_bytes()
        }

        /// Look up an entry in the cache
        pub fn get(&self, key: &[u8; 32]) -> Option<CacheEntry> {
            let entries = self.entries.lock().ok()?;
            let result = entries.get(key).cloned();
            if result.is_some() {
                self.hits.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            } else {
                self.misses
                    .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            }
            result
        }

        /// Insert an entry into the cache
        pub fn insert(&self, key: [u8; 32], entry: CacheEntry) {
            if let Ok(mut entries) = self.entries.lock() {
                // Simple LRU: if at capacity, remove oldest entry
                if entries.len() >= self.max_size {
                    let oldest_key = entries
                        .iter()
                        .min_by_key(|(_, v)| v.created_at)
                        .map(|(k, _)| *k);
                    if let Some(k) = oldest_key {
                        entries.remove(&k);
                    }
                }
                entries.insert(key, entry);
            }
        }

        /// Clear the cache
        pub fn clear(&self) {
            if let Ok(mut entries) = self.entries.lock() {
                entries.clear();
            }
        }

        /// Get cache statistics
        pub fn stats(&self) -> CacheStats {
            let entries = self.entries.lock().map(|e| e.len()).unwrap_or(0);
            let hits = self.hits.load(std::sync::atomic::Ordering::Relaxed);
            let misses = self.misses.load(std::sync::atomic::Ordering::Relaxed);
            CacheStats {
                entries,
                hits,
                misses,
                hit_rate: if hits + misses > 0 {
                    hits as f64 / (hits + misses) as f64
                } else {
                    0.0
                },
            }
        }

        /// Estimated cost savings from cache hits
        pub fn estimated_savings(&self) -> f64 {
            if let Ok(entries) = self.entries.lock() {
                let hits = self.hits.load(std::sync::atomic::Ordering::Relaxed);
                let avg_cost =
                    entries.values().map(|e| e.cost_usd).sum::<f64>() / entries.len().max(1) as f64;
                hits as f64 * avg_cost
            } else {
                0.0
            }
        }
    }

    impl Default for AnswerCache {
        fn default() -> Self {
            Self::new(100) // Default to 100 entries
        }
    }

    #[derive(Debug, Clone)]
    pub struct CacheStats {
        pub entries: usize,
        pub hits: u64,
        pub misses: u64,
        pub hit_rate: f64,
    }

    // Global cache instance
    lazy_static::lazy_static! {
        pub static ref GLOBAL_CACHE: AnswerCache = AnswerCache::new(500);
    }

    /// Check cache and return cached result if available
    pub fn check_cache(query: &str, context: &str, model: &str) -> Option<CacheEntry> {
        let key = AnswerCache::make_key(query, context, model);
        GLOBAL_CACHE.get(&key)
    }

    /// Store result in cache
    pub fn store_in_cache(query: &str, context: &str, model: &str, entry: CacheEntry) {
        let key = AnswerCache::make_key(query, context, model);
        GLOBAL_CACHE.insert(key, entry);
    }

    /// Get global cache statistics
    pub fn global_stats() -> CacheStats {
        GLOBAL_CACHE.stats()
    }

    /// Clear the global cache
    pub fn clear_global_cache() {
        GLOBAL_CACHE.clear();
    }
}

/// Result of grounding/hallucination verification
#[derive(Debug, Clone, Default)]
pub struct GroundingResult {
    /// Overall grounding score (0.0 to 1.0)
    /// Higher = more grounded in context, less likely to hallucinate
    pub score: f32,
    /// Number of sentences in the answer
    pub sentence_count: usize,
    /// Number of sentences with at least one grounded claim
    pub grounded_sentences: usize,
    /// Individual sentence scores
    pub sentence_scores: Vec<f32>,
    /// Warning flag: true if potential hallucination detected
    pub has_warning: bool,
    /// Explanation of the warning (if any)
    pub warning_reason: Option<String>,
}

impl GroundingResult {
    /// Returns a human-readable grade based on grounding score
    pub fn grade(&self) -> &'static str {
        match self.score {
            s if s >= 0.8 => "A",
            s if s >= 0.6 => "B",
            s if s >= 0.4 => "C",
            s if s >= 0.2 => "D",
            _ => "F",
        }
    }

    /// Returns a label like "HIGH", "MEDIUM", "LOW"
    pub fn label(&self) -> &'static str {
        match self.score {
            s if s >= 0.7 => "HIGH",
            s if s >= 0.4 => "MEDIUM",
            _ => "LOW",
        }
    }
}

#[derive(Debug, Clone)]
pub struct ModelContextFragment {
    pub rank: usize,
    pub uri: String,
    pub title: Option<String>,
    pub score: Option<f32>,
    pub matches: usize,
    pub frame_id: u64,
    pub range: (usize, usize),
    pub chunk_range: Option<(usize, usize)>,
    pub text: String,
    pub kind: ModelContextFragmentKind,
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum ModelContextFragmentKind {
    Full,
    Summary,
}

impl ModelContextFragment {
    fn from_record(record: context::ContextRecord) -> Self {
        let kind = match record.mode {
            context::ContextMode::Full => ModelContextFragmentKind::Full,
            context::ContextMode::Summary => ModelContextFragmentKind::Summary,
        };
        Self {
            rank: record.rank,
            uri: record.uri,
            title: record.title,
            score: record.score,
            matches: record.matches,
            frame_id: record.frame_id,
            range: record.range,
            chunk_range: record.chunk_range,
            text: record.text,
            kind,
        }
    }
}

#[derive(Debug)]
pub enum ModelRunError {
    UnsupportedModel(String),
    AssetsMissing {
        model: String,
        missing: Vec<PathBuf>,
    },
    Runtime(anyhow::Error),
}

impl fmt::Display for ModelRunError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnsupportedModel(model) => write!(f, "unsupported model '{model}'"),
            Self::AssetsMissing { model, missing } => {
                let paths: Vec<_> = missing
                    .iter()
                    .map(|path| path.display().to_string())
                    .collect();
                write!(
                    f,
                    "model '{model}' missing required assets: {}",
                    paths.join(", ")
                )
            }
            Self::Runtime(err) => write!(f, "model runtime error: {err}"),
        }
    }
}

impl std::error::Error for ModelRunError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Runtime(err) => Some(err.root_cause()),
            _ => None,
        }
    }
}

const LOCAL_CONTEXT_CHARS: usize = 32_768;
const MAX_QUESTION_CHARS: usize = 512;
const LOCAL_MAX_OUTPUT_TOKENS: usize = 256;
const REMOTE_MAX_OUTPUT_TOKENS: usize = 768;
const SYSTEM_PROMPT: &str = r#"You are a precise, intelligent assistant that answers questions using ONLY the provided retrieval context.

## Core Principles
1. GROUND EVERY CLAIM in the context. If asked for a number, quote it exactly.
2. NEVER hallucinate or use external knowledge. If unsure, say "Based on the context..."
3. BE CONCISE but complete. One clear answer is better than verbose hedging.

## CRITICAL: Correction Handling (MANDATORY)
**STOP AND READ THIS FIRST.** Before answering ANY question:
1. Scan ALL sources for "Correction:" in the title or "mv2://correction/" in the URI
2. If ANY correction exists that relates to the question, USE THAT ANSWER ONLY
3. IGNORE ALL OTHER SOURCES that contradict the correction - they are OUTDATED
4. If multiple corrections exist, use the FIRST one listed (most recent)

**VIOLATION OF THIS RULE IS A CRITICAL ERROR.** Example:
- Question: "Where does Ben live?"
- Correction says: "Ben lives in Kenya"
- Other doc says: "Ben lives in Germany"
- CORRECT ANSWER: "Kenya" (from correction)
- WRONG ANSWER: "Germany" (ignores correction = FAIL)

## Answer Strategy
- For NUMERIC questions: Extract the exact value. If multiple values exist, identify which is most relevant (usually the most recent or most specific match).
- For YES/NO questions: Answer directly, then briefly explain why.
- For COMPARISON questions: Present both sides with their values.
- For LIST questions: Use bullet points or numbered lists.
- For TEMPORAL questions: Note that later timestamps = more current information. State WHEN data is from.
- For CALCULATION questions: Show your work step-by-step.
- For ANALYTICAL/PATTERN questions (e.g., "reverted", "changed back", "any differences over time"):
  1. TRACE each attribute's value across ALL time periods in the context
  2. Look for A→B→A patterns where a value changes then returns to its original state
  3. Terms like "consolidated", "same as", "unified", or "aligned" often indicate returning to a prior arrangement
  4. Compare explicit state changes: if Period 1 says "X was same as Y", Period 2 says "X different from Y", and Period 3 says "X consolidated/same as Y again", that IS a reversion
  5. Create a timeline table if helpful to track changes

## Handling Ambiguity
- If the question is ambiguous, interpret it reasonably and state your interpretation.
- If multiple valid answers exist, present the most likely one first, then mention alternatives.
- If context is insufficient, say what IS known, then note what's missing.

## Quality Standards
- PREFER specific answers over vague ones ("$1,234.56" not "around a thousand")
- CITE context when helpful ("[Source: ...]")
- CORRECT obvious typos in your interpretation (e.g., "teh" → "the")
- For percentages/ratios, include the actual numbers when available"#;
const TINYLLAMA_LABEL: &str = "tinyllama-1.1b";
const LOCAL_PROMPT_MARGIN_CHARS: usize = 2_048;
const REMOTE_PROMPT_MARGIN_CHARS: usize = 4_096;
const OLLAMA_PROMPT_CHARS: usize = 110_000;
const OPENAI_PROMPT_CHARS: usize = 240_000;
const NVIDIA_PROMPT_CHARS: usize = 240_000;
const GEMINI_PROMPT_CHARS: usize = 320_000;
const CLAUDE_PROMPT_CHARS: usize = 360_000;
const XAI_PROMPT_CHARS: usize = 260_000; // Grok models: ~131K tokens
const GROQ_PROMPT_CHARS: usize = 260_000; // LLaMA 3.3 70B: 128K tokens
const MISTRAL_PROMPT_CHARS: usize = 260_000; // Mistral Large: 128K tokens

#[derive(Debug, Clone, Copy)]
struct ModelContextBudget {
    total_chars: usize,
    reserved_chars: usize,
}

impl ModelContextBudget {
    const fn new(total_chars: usize, reserved_chars: usize) -> Self {
        Self {
            total_chars,
            reserved_chars,
        }
    }

    fn context_chars(&self) -> usize {
        self.total_chars.saturating_sub(self.reserved_chars)
    }

    fn question_limit(&self) -> usize {
        MAX_QUESTION_CHARS
            .min(self.reserved_chars.max(1))
            .min(self.total_chars.max(1))
    }

    fn apply_override(self, override_context_chars: usize) -> Self {
        let total = override_context_chars.saturating_add(self.reserved_chars);
        Self {
            total_chars: total.max(self.reserved_chars + 1),
            reserved_chars: self.reserved_chars,
        }
    }

    fn prompt_ceiling(&self) -> usize {
        self.total_chars
    }
}

pub struct PromptParts {
    completion_prompt: String,
    user_message: String,
    max_output_tokens: usize,
}

impl PromptParts {
    pub fn completion_prompt(&self) -> &str {
        &self.completion_prompt
    }

    pub fn user_message(&self) -> &str {
        &self.user_message
    }

    pub fn max_output_tokens(&self) -> usize {
        self.max_output_tokens
    }
}

/// Normalize and enhance a question for optimal LLM interpretation.
///
/// This function:
/// 1. Ensures questions end with `?` for consistent interpretation
/// 2. Fixes common typos and abbreviations
/// 3. Clarifies ambiguous phrasing
/// 4. Expands common abbreviations for better matching
fn normalize_question(question: &str) -> String {
    let trimmed = question.trim();
    if trimmed.is_empty() {
        return trimmed.to_string();
    }

    // Step 1: Fix common typos and normalize spacing
    let mut normalized = fix_common_typos(trimmed);

    // Step 2: Expand common abbreviations for clarity
    normalized = expand_abbreviations(&normalized);

    // Step 3: Ensure proper punctuation
    normalized = ensure_question_punctuation(&normalized);

    normalized
}

/// Fix common typos that affect query interpretation
fn fix_common_typos(text: &str) -> String {
    let mut result = text.to_string();

    // Common typo patterns (case-insensitive replacements)
    let typos: &[(&str, &str)] = &[
        // Common misspellings
        ("teh ", "the "),
        ("hte ", "the "),
        ("adn ", "and "),
        ("taht ", "that "),
        ("wiht ", "with "),
        ("thier ", "their "),
        ("recieve", "receive"),
        ("occured", "occurred"),
        ("seperate", "separate"),
        // Question word typos
        ("waht ", "what "),
        ("hwat ", "what "),
        ("wehn ", "when "),
        ("whre ", "where "),
        ("wher ", "where "),
        ("howm ", "how "),
        ("hwo ", "who "),
        // Common finger slips
        ("amoutn", "amount"),
        ("totla", "total"),
        ("nubmer", "number"),
        ("vlaue", "value"),
        ("prive", "price"),
        ("proce", "price"),
        ("revneue", "revenue"),
        ("reveneu", "revenue"),
    ];

    for (typo, correction) in typos {
        // Case-insensitive replacement
        let lower = result.to_lowercase();
        if lower.contains(*typo) {
            let start = lower.find(*typo).unwrap();
            let end = start + typo.len();
            result = format!("{}{}{}", &result[..start], correction, &result[end..]);
        }
    }

    // Normalize multiple spaces to single
    let mut prev_space = false;
    result = result
        .chars()
        .filter(|c| {
            if c.is_whitespace() {
                if prev_space {
                    false
                } else {
                    prev_space = true;
                    true
                }
            } else {
                prev_space = false;
                true
            }
        })
        .collect();

    result
}

/// Generate optimized search keywords from a question using LLM
/// Returns the original question plus extracted search terms for better retrieval
pub fn generate_search_query(
    question: &str,
    model: &str,
    api_key: &str,
) -> Result<String, ModelRunError> {
    // For lexical search, we need to be careful - adding too many terms can hurt
    // because Tantivy uses AND logic. We need a very short, focused query.
    // IMPORTANT: Keep abbreviations as-is since documents often use the abbreviation form.
    let prompt = format!(
        r#"Extract 2 key search terms from this question.
KEEP abbreviations exactly as written (QPS, API, SDK, etc.) - don't expand them.
Output only the main topic and one key term.

Question: {}

Examples:
- "What is the QPS for memvid?" → "memvid QPS"
- "How many queries per second?" → "QPS throughput"
- "What's the API rate limit?" → "API rate"
- "How much does it cost?" → "cost pricing"

Output exactly 2 words, nothing else."#,
        question
    );

    // Use a fast model for keyword extraction
    // The model passed in is already the fast variant (gpt-4o-mini or claude-haiku)
    let extraction_model =
        if model.starts_with("gpt") || model.starts_with("o1") || model.contains("openai") {
            "gpt-4o-mini"
        } else if model.starts_with("claude") || model.contains("anthropic") {
            "claude-haiku-4-5"
        } else if model.contains("llama") || model.contains("groq") || model.contains("mixtral") {
            "llama-3.1-8b-instant" // Fast Groq model for keyword extraction
        } else if model.contains("grok") || model.contains("xai") {
            "grok-4-fast"
        } else if model.contains("mistral") {
            "mistral-small-latest" // Fast Mistral model
        } else {
            // For other models, just return the original question
            return Ok(question.to_string());
        };

    // Make a quick API call for query rewriting
    let rewritten = call_llm_for_keywords(&prompt, extraction_model, api_key)?;

    // If we got a good rewritten query, use it; otherwise fall back to original
    let rewritten = rewritten.trim();
    if rewritten.is_empty() || rewritten.len() > 100 {
        // LLM returned empty or too long - use original
        Ok(question.to_string())
    } else {
        // Use the short, focused rewritten query for search
        Ok(rewritten.to_string())
    }
}

/// Quick LLM call for keyword extraction (lightweight, fast model)
fn call_llm_for_keywords(
    prompt: &str,
    model: &str,
    api_key: &str,
) -> Result<String, ModelRunError> {
    use reqwest::blocking::Client;
    use reqwest::header::{AUTHORIZATION, CONTENT_TYPE, HeaderMap, HeaderValue};

    let client = Client::builder()
        .timeout(std::time::Duration::from_secs(10)) // Fast timeout for keyword extraction
        .build()
        .map_err(|e| ModelRunError::Runtime(anyhow::anyhow!("HTTP client error: {e}")))?;

    // Determine API endpoint and headers based on model
    let (url, is_anthropic) = if model.starts_with("gpt") || model.starts_with("o1") {
        ("https://api.openai.com/v1/chat/completions", false)
    } else if model.starts_with("claude") {
        ("https://api.anthropic.com/v1/messages", true)
    } else if model.contains("llama") || model.contains("mixtral") {
        ("https://api.groq.com/openai/v1/chat/completions", false)
    } else if model.contains("grok") {
        ("https://api.x.ai/v1/chat/completions", false)
    } else if model.contains("mistral") {
        ("https://api.mistral.ai/v1/chat/completions", false)
    } else {
        return Err(ModelRunError::UnsupportedModel(model.to_string()));
    };

    let response = if is_anthropic {
        let mut headers = HeaderMap::new();
        headers.insert(
            reqwest::header::HeaderName::from_static("x-api-key"),
            HeaderValue::from_str(api_key)
                .map_err(|e| ModelRunError::Runtime(anyhow::anyhow!("Invalid API key: {e}")))?,
        );
        headers.insert(
            reqwest::header::HeaderName::from_static("anthropic-version"),
            HeaderValue::from_static("2023-06-01"),
        );
        headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));

        client
            .post(url)
            .headers(headers)
            .json(&serde_json::json!({
                "model": model,
                "max_tokens": 100,
                "messages": [{"role": "user", "content": prompt}]
            }))
            .send()
    } else {
        // OpenAI-compatible API (OpenAI, Groq, XAI, Mistral)
        let mut headers = HeaderMap::new();
        headers.insert(
            AUTHORIZATION,
            HeaderValue::from_str(&format!("Bearer {}", api_key))
                .map_err(|e| ModelRunError::Runtime(anyhow::anyhow!("Invalid API key: {e}")))?,
        );
        headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));

        client
            .post(url)
            .headers(headers)
            .json(&serde_json::json!({
                "model": model,
                "messages": [{"role": "user", "content": prompt}],
                "max_tokens": 100,
                "temperature": 0.0
            }))
            .send()
    };

    match response {
        Ok(resp) => {
            let json: serde_json::Value = resp
                .json()
                .map_err(|e| ModelRunError::Runtime(anyhow::anyhow!("JSON parse error: {e}")))?;

            // Extract text from response
            let text = if model.starts_with("claude") {
                json["content"][0]["text"].as_str().unwrap_or("")
            } else {
                json["choices"][0]["message"]["content"]
                    .as_str()
                    .unwrap_or("")
            };

            Ok(text.to_string())
        }
        Err(_) => {
            // On error, just return empty (fall back to original query)
            // This is not a fatal error - we just use the original query
            Ok(String::new())
        }
    }
}

/// Expand common abbreviations for better context matching (fallback when LLM not available)
fn expand_abbreviations(text: &str) -> String {
    // This is now a simple fallback - the main expansion happens via LLM
    text.to_string()
}

/// Ensure the question ends with proper punctuation
fn ensure_question_punctuation(text: &str) -> String {
    let trimmed = text.trim();

    // Already ends with punctuation - don't modify
    if trimmed.ends_with('?') || trimmed.ends_with('.') || trimmed.ends_with('!') {
        return trimmed.to_string();
    }

    // Check if it looks like a question (starts with question word or auxiliary verb)
    let lower = trimmed.to_lowercase();
    let question_starters = [
        "how", "what", "where", "when", "why", "which", "who", "whom", "whose", "is", "are", "was",
        "were", "will", "would", "can", "could", "should", "do", "does", "did", "have", "has",
        "had", "may", "might", "shall", "tell me", "show me", "find", "list", "give me", "explain",
    ];

    let is_question = question_starters.iter().any(|starter| {
        lower.starts_with(starter)
            && (lower.len() == starter.len()
                || !lower[starter.len()..].starts_with(|c: char| c.is_alphanumeric()))
    });

    if is_question {
        format!("{}?", trimmed)
    } else {
        trimmed.to_string()
    }
}

fn build_prompt_parts(
    question: &str,
    context: &str,
    budget: &ModelContextBudget,
    max_output_tokens: usize,
) -> PromptParts {
    let mut context_section = context.to_string();
    let normalized_question = normalize_question(question);
    let trimmed_question = trim_to(&normalized_question, budget.question_limit());

    // Detect question type for better prompting
    let question_type = detect_question_type(&trimmed_question);
    let type_hint = question_type.hint();

    let system_section = format!("### System\n{SYSTEM_PROMPT}");
    let question_section = format!("### Question\n{trimmed_question}");
    let answer_stub = "### Answer\n";

    let overhead = system_section.len() + 2 + question_section.len() + 2 + answer_stub.len();
    if budget.prompt_ceiling() > overhead {
        let max_context_len = budget
            .prompt_ceiling()
            .saturating_sub(overhead)
            .min(budget.context_chars());
        if context_section.len() > max_context_len {
            context_section = clamp_to(&context_section, max_context_len);
        }
    } else {
        context_section = String::new();
    }

    // Handle empty context gracefully
    let context_instruction = if context_section.trim().is_empty() {
        "Note: No relevant context was found. Answer based on what you know, but clearly state this limitation."
    } else {
        ""
    };

    let completion_prompt =
        format!("{system_section}\n\n{context_section}\n\n{question_section}\n\n### Answer\n");

    // Enhanced user message with type-specific guidance
    let user_message = if context_instruction.is_empty() {
        format!(
            "{context_section}\n\n---\nQuestion: {trimmed_question}\n{type_hint}\nProvide a direct, accurate answer using only the context above."
        )
    } else {
        format!("{context_instruction}\n\nQuestion: {trimmed_question}\n{type_hint}")
    };

    PromptParts {
        completion_prompt,
        user_message,
        max_output_tokens,
    }
}

/// Question type classification for better prompting
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum QuestionType {
    Numeric,     // "how much", "how many", "what is the value"
    YesNo,       // "is", "are", "does", "can", "will"
    List,        // "list", "what are the", "show all"
    Comparison,  // "compare", "difference", "vs", "versus"
    Temporal,    // "when", "what date", "how long"
    Explanation, // "why", "explain", "how does"
    Factual,     // "what", "who", "where"
    Other,
}

impl QuestionType {
    fn hint(&self) -> &'static str {
        match self {
            Self::Numeric => "(Expected: a specific number or value)",
            Self::YesNo => "(Expected: yes/no with brief explanation)",
            Self::List => "(Expected: a list of items)",
            Self::Comparison => "(Expected: comparison of two or more items)",
            Self::Temporal => "(Expected: a date, time, or duration)",
            Self::Explanation => "(Expected: reasoning or explanation)",
            Self::Factual => "(Expected: a factual answer)",
            Self::Other => "",
        }
    }
}

fn detect_question_type(question: &str) -> QuestionType {
    let lower = question.to_lowercase();

    // Numeric patterns
    if lower.contains("how much")
        || lower.contains("how many")
        || lower.contains("what is the value")
        || lower.contains("what's the value")
        || lower.contains("total")
        || lower.contains("sum")
        || lower.contains("average")
        || lower.contains("percentage")
        || lower.contains("rate")
        || lower.contains("amount")
        || lower.contains("price")
        || lower.contains("cost")
        || lower.contains("revenue")
        || lower.contains("profit")
    {
        return QuestionType::Numeric;
    }

    // Yes/No patterns
    let yes_no_starters = [
        "is ", "are ", "does ", "do ", "can ", "will ", "has ", "have ", "was ", "were ",
    ];
    if yes_no_starters.iter().any(|s| lower.starts_with(s)) {
        return QuestionType::YesNo;
    }

    // List patterns
    if lower.contains("list")
        || lower.contains("show all")
        || lower.contains("what are the")
        || lower.contains("name all")
        || lower.contains("enumerate")
    {
        return QuestionType::List;
    }

    // Comparison patterns
    if lower.contains("compare")
        || lower.contains("difference between")
        || lower.contains(" vs ")
        || lower.contains("versus")
        || lower.contains("better than")
        || lower.contains("worse than")
    {
        return QuestionType::Comparison;
    }

    // Temporal patterns
    if lower.starts_with("when")
        || lower.contains("what date")
        || lower.contains("how long")
        || lower.contains("how old")
        || lower.contains("since when")
    {
        return QuestionType::Temporal;
    }

    // Explanation patterns
    if lower.starts_with("why")
        || lower.starts_with("explain")
        || lower.contains("how does")
        || lower.contains("reason for")
        || lower.contains("cause of")
    {
        return QuestionType::Explanation;
    }

    // Factual patterns
    if lower.starts_with("what") || lower.starts_with("who") || lower.starts_with("where") {
        return QuestionType::Factual;
    }

    QuestionType::Other
}

/// Post-process the LLM answer for quality
pub fn postprocess_answer(answer: &str) -> String {
    let mut result = answer.trim().to_string();

    // Remove common LLM artifacts
    let artifacts = [
        "Based on the provided context,",
        "According to the context,",
        "From the context provided,",
        "The context shows that",
        "Based on the information provided,",
    ];
    for artifact in artifacts {
        if result.starts_with(artifact) {
            result = result[artifact.len()..].trim_start().to_string();
            // Capitalize first letter
            if let Some(first) = result.chars().next() {
                result = first.to_uppercase().chain(result.chars().skip(1)).collect();
            }
        }
    }

    // Normalize whitespace
    result = result.split_whitespace().collect::<Vec<_>>().join(" ");

    // Ensure the answer doesn't start with lowercase
    if let Some(first) = result.chars().next() {
        if first.is_lowercase() && !result.starts_with("i ") {
            result = first.to_uppercase().chain(result.chars().skip(1)).collect();
        }
    }

    result
}

fn trim_to(text: &str, limit: usize) -> String {
    if text.len() <= limit {
        text.to_string()
    } else {
        let mut truncated = text[..limit].to_string();
        truncated.push_str("...");
        truncated
    }
}

fn clamp_to(text: &str, limit: usize) -> String {
    if text.len() <= limit {
        text.to_string()
    } else if limit <= 3 {
        "...".chars().take(limit).collect()
    } else {
        let mut end = limit.saturating_sub(3);
        // Find valid UTF-8 char boundary (curly quotes, emojis, etc. are multi-byte)
        while end > 0 && !text.is_char_boundary(end) {
            end -= 1;
        }
        if end == 0 {
            return "...".to_string();
        }
        let mut truncated = text[..end].to_string();
        truncated.push_str("...");
        truncated
    }
}

struct ThinkingSpinner {
    flag: Arc<AtomicBool>,
    handle: Option<thread::JoinHandle<()>>,
}

impl ThinkingSpinner {
    fn start() -> Self {
        let flag = Arc::new(AtomicBool::new(true));
        let thread_flag = flag.clone();

        // Only show spinner if stderr is a TTY (interactive terminal).
        // This prevents control characters from polluting output when
        // stderr is redirected or combined with stdout (e.g., `2>&1`).
        let is_tty = stderr().is_terminal();

        let handle = thread::spawn(move || {
            if !is_tty {
                // Not a TTY, don't show spinner - just wait for stop signal
                while thread_flag.load(Ordering::Relaxed) {
                    thread::sleep(Duration::from_millis(200));
                }
                return;
            }

            let frames = [
                "Thinking    ",
                "Thinking.   ",
                "Thinking..  ",
                "Thinking... ",
                "Thinking .. ",
                "Thinking  . ",
            ];
            let mut idx = 0;
            let mut err = stderr();
            while thread_flag.load(Ordering::Relaxed) {
                let frame = frames[idx % frames.len()];
                let _ = write!(err, "\r{frame}");
                let _ = err.flush();
                idx = idx.wrapping_add(1);
                thread::sleep(Duration::from_millis(200));
            }
            let _ = write!(err, "\r             \r");
            let _ = err.flush();
        });

        Self {
            flag,
            handle: Some(handle),
        }
    }

    fn stop(&mut self) {
        if let Some(handle) = self.handle.take() {
            self.flag.store(false, Ordering::Relaxed);
            let _ = handle.join();
        }
    }
}

impl Drop for ThinkingSpinner {
    fn drop(&mut self) {
        self.stop();
    }
}

#[derive(Debug, Clone)]
enum ModelKind {
    TinyLlama,
    Ghost { pack_path: PathBuf },
    Ollama { model: String },
    OpenAi { model: String },
    Nvidia { model: String },
    Gemini { model: String },
    Claude { model: String },
    Xai { model: String },
    Groq { model: String },
    Mistral { model: String },
}

impl ModelKind {
    fn parse(raw: &str) -> Option<Self> {
        let trimmed = raw.trim();
        if trimmed.is_empty() {
            return None;
        }

        let (provider, explicit_model) = if let Some((p, rest)) = trimmed.split_once(':') {
            let value = rest.trim();
            let explicit = if value.is_empty() {
                None
            } else {
                Some(value.to_string())
            };
            (p.trim().to_ascii_lowercase(), explicit)
        } else {
            (trimmed.to_ascii_lowercase(), None)
        };

        match provider.as_str() {
            "tinyllama" | "tiny-llama" | "tinyllama-1.1b" => Some(Self::TinyLlama),
            "ghost" => explicit_model.map(|value| Self::Ghost {
                pack_path: PathBuf::from(value),
            }),
            "ollama" => Some(Self::Ollama {
                model: explicit_model.unwrap_or_else(|| "ollama1.5".to_string()),
            }),
            "ollama1.5" | "ollama1-5" => Some(Self::Ollama {
                model: "ollama1.5".to_string(),
            }),
            "openai" => Some(Self::OpenAi {
                model: normalize_openai_model(explicit_model),
            }),
            "nvidia" | "nv" => Some(Self::Nvidia {
                model: normalize_nvidia_model(explicit_model),
            }),
            "gemini" | "google" => Some(Self::Gemini {
                model: normalize_gemini_model(explicit_model),
            }),
            "claude" | "anthropic" => Some(Self::Claude {
                model: normalize_claude_model(explicit_model),
            }),
            "xai" | "grok" => Some(Self::Xai {
                model: normalize_xai_model(explicit_model),
            }),
            "groq" => Some(Self::Groq {
                model: normalize_groq_model(explicit_model),
            }),
            "mistral" => Some(Self::Mistral {
                model: normalize_mistral_model(explicit_model),
            }),
            // Auto-detect provider from model name prefix
            // For Ollama models with colons in the name (e.g., qwen2.5:1.5b),
            // we need to use the full original name, not just the provider prefix
            _ => Self::infer_from_model_name_full(trimmed, &provider),
        }
    }

    /// Infer the provider from a model name, using the full original name for Ollama models.
    /// This handles model names with colons like "qwen2.5:1.5b" by using the full name.
    fn infer_from_model_name_full(full_name: &str, prefix: &str) -> Option<Self> {
        let lowered = prefix.to_ascii_lowercase();

        // Gemini models: gemini-*, models/gemini-*
        if lowered.starts_with("gemini") || lowered.starts_with("models/gemini") {
            return Some(Self::Gemini {
                model: full_name.to_string(),
            });
        }

        // OpenAI models: gpt-*, o1-*, chatgpt-*, text-davinci-*, etc.
        if lowered.starts_with("gpt-")
            || lowered.starts_with("o1-")
            || lowered.starts_with("o3-")
            || lowered.starts_with("chatgpt-")
            || lowered.starts_with("text-")
        {
            return Some(Self::OpenAi {
                model: full_name.to_string(),
            });
        }

        // Claude/Anthropic models: claude-*
        if lowered.starts_with("claude-") {
            return Some(Self::Claude {
                model: full_name.to_string(),
            });
        }

        // xAI Grok models: grok-*
        if lowered.starts_with("grok-") {
            return Some(Self::Xai {
                model: full_name.to_string(),
            });
        }

        // Mistral API models: mistral-*
        if lowered.starts_with("mistral-") {
            return Some(Self::Mistral {
                model: full_name.to_string(),
            });
        }

        // Groq models: llama-* (via Groq), mixtral-*
        if lowered.starts_with("llama-") || lowered.starts_with("mixtral-") {
            return Some(Self::Groq {
                model: full_name.to_string(),
            });
        }

        // Ollama models: llama*, phi*, qwen*, gemma*, etc.
        // Use the full name to preserve version tags like ":1.5b"
        if lowered.starts_with("llama")
            || lowered.starts_with("phi")
            || lowered.starts_with("codellama")
            || lowered.starts_with("deepseek")
            || lowered.starts_with("qwen")
            || lowered.starts_with("gemma")
        {
            return Some(Self::Ollama {
                model: full_name.to_string(),
            });
        }

        None
    }

    fn label(&self) -> String {
        match self {
            Self::TinyLlama => TINYLLAMA_LABEL.to_string(),
            Self::Ghost { pack_path } => format!("ghost:{}", pack_path.display()),
            Self::Ollama { model } => format!("ollama:{model}"),
            Self::OpenAi { model } => format!("openai:{model}"),
            Self::Nvidia { model } => format!("nvidia:{model}"),
            Self::Gemini { model } => format!("gemini:{model}"),
            Self::Claude { model } => format!("claude:{model}"),
            Self::Xai { model } => format!("xai:{model}"),
            Self::Groq { model } => format!("groq:{model}"),
            Self::Mistral { model } => format!("mistral:{model}"),
        }
    }

    fn context_budget(&self) -> ModelContextBudget {
        match self {
            Self::TinyLlama => {
                ModelContextBudget::new(LOCAL_CONTEXT_CHARS, LOCAL_PROMPT_MARGIN_CHARS)
            }
            Self::Ghost { .. } => {
                ModelContextBudget::new(LOCAL_CONTEXT_CHARS, LOCAL_PROMPT_MARGIN_CHARS)
            }
            Self::Ollama { .. } => {
                ModelContextBudget::new(OLLAMA_PROMPT_CHARS, REMOTE_PROMPT_MARGIN_CHARS)
            }
            Self::OpenAi { .. } => {
                ModelContextBudget::new(OPENAI_PROMPT_CHARS, REMOTE_PROMPT_MARGIN_CHARS)
            }
            Self::Nvidia { .. } => {
                ModelContextBudget::new(NVIDIA_PROMPT_CHARS, REMOTE_PROMPT_MARGIN_CHARS)
            }
            Self::Gemini { .. } => {
                ModelContextBudget::new(GEMINI_PROMPT_CHARS, REMOTE_PROMPT_MARGIN_CHARS)
            }
            Self::Claude { .. } => {
                ModelContextBudget::new(CLAUDE_PROMPT_CHARS, REMOTE_PROMPT_MARGIN_CHARS)
            }
            Self::Xai { .. } => {
                ModelContextBudget::new(XAI_PROMPT_CHARS, REMOTE_PROMPT_MARGIN_CHARS)
            }
            Self::Groq { .. } => {
                ModelContextBudget::new(GROQ_PROMPT_CHARS, REMOTE_PROMPT_MARGIN_CHARS)
            }
            Self::Mistral { .. } => {
                ModelContextBudget::new(MISTRAL_PROMPT_CHARS, REMOTE_PROMPT_MARGIN_CHARS)
            }
        }
    }

    fn max_output_tokens(&self) -> usize {
        match self {
            Self::TinyLlama => LOCAL_MAX_OUTPUT_TOKENS,
            Self::Ghost { .. } => LOCAL_MAX_OUTPUT_TOKENS,
            Self::Ollama { .. }
            | Self::OpenAi { .. }
            | Self::Nvidia { .. }
            | Self::Gemini { .. }
            | Self::Claude { .. }
            | Self::Xai { .. }
            | Self::Groq { .. }
            | Self::Mistral { .. } => REMOTE_MAX_OUTPUT_TOKENS,
        }
    }
}

fn normalize_openai_model(explicit: Option<String>) -> String {
    match explicit {
        Some(raw) if !raw.trim().is_empty() => raw,
        _ => "gpt-4o-mini".to_string(),
    }
}

fn normalize_nvidia_model(explicit: Option<String>) -> String {
    match explicit {
        Some(raw) if !raw.trim().is_empty() => raw,
        _ => std::env::var("NVIDIA_LLM_MODEL")
            .or_else(|_| std::env::var("NVIDIA_MODEL"))
            .ok()
            .map(|value| value.trim().to_string())
            .filter(|value| !value.is_empty())
            .unwrap_or_default(),
    }
}

fn normalize_gemini_model(explicit: Option<String>) -> String {
    let default_model = "gemini-2.5-flash".to_string();
    let Some(raw) = explicit else {
        return default_model;
    };

    let lowered = raw.to_ascii_lowercase();
    match lowered.as_str() {
        "gemini-pro" | "gemini-1.5-pro" | "gemini-1.5-flash" | "gemini-2.0-pro-exp" => raw,
        _ => raw,
    }
}

fn normalize_claude_model(explicit: Option<String>) -> String {
    let default_model = "claude-sonnet-4-5".to_string();
    let Some(raw) = explicit else {
        return default_model;
    };

    // Map old model names to new ones
    match raw.as_str() {
        "claude-3-5-sonnet-20241022" | "claude-3.5-sonnet" | "sonnet" => {
            "claude-sonnet-4-5".to_string()
        }
        "claude-3-haiku-20240307" | "claude-3-haiku" | "haiku" => "claude-haiku-4-5".to_string(),
        "claude-3-opus-20240229" | "claude-3-opus" | "opus" => "claude-opus-4".to_string(),
        _ => raw,
    }
}

fn normalize_xai_model(explicit: Option<String>) -> String {
    let default_model = "grok-4-fast".to_string();
    let Some(raw) = explicit else {
        return default_model;
    };

    // Map common aliases to actual model names
    match raw.to_lowercase().as_str() {
        "grok" | "grok-fast" => "grok-4-fast".to_string(),
        "grok-4" | "grok-3" | "grok-4-fast" => raw, // Keep explicit versions
        _ => raw,
    }
}

fn normalize_groq_model(explicit: Option<String>) -> String {
    let default_model = "llama-3.3-70b-versatile".to_string();
    let Some(raw) = explicit else {
        return default_model;
    };

    // Map common aliases to actual model names
    match raw.to_lowercase().as_str() {
        "llama" | "llama3" | "llama-3" => "llama-3.3-70b-versatile".to_string(),
        "llama-70b" | "llama3-70b" => "llama-3.3-70b-versatile".to_string(),
        "llama-8b" | "llama3-8b" => "llama-3.1-8b-instant".to_string(),
        "mixtral" => "mixtral-8x7b-32768".to_string(),
        _ => raw,
    }
}

fn normalize_mistral_model(explicit: Option<String>) -> String {
    let default_model = "mistral-large-latest".to_string();
    let Some(raw) = explicit else {
        return default_model;
    };

    // Map common aliases to actual model names
    match raw.to_lowercase().as_str() {
        "mistral" | "large" | "mistral-large" => "mistral-large-latest".to_string(),
        "medium" | "mistral-medium" => "mistral-medium-latest".to_string(),
        "small" | "mistral-small" => "mistral-small-latest".to_string(),
        _ => raw,
    }
}

/// Calculate cost for a given model based on token usage.
/// Prices are per 1M tokens in USD (December 2025 pricing).
pub fn calculate_cost(model: &str, input_tokens: u32, output_tokens: u32) -> f64 {
    let (input_price, output_price) = match model.to_lowercase().as_str() {
        // OpenAI pricing (per 1M tokens) - Dec 2025
        m if m.contains("gpt-4o-mini") => (0.15, 0.60),
        m if m.contains("gpt-4o") => (2.50, 10.00),
        m if m.contains("gpt-4.5") => (75.00, 150.00),
        m if m.contains("gpt-4.1-mini") => (0.40, 1.60),
        m if m.contains("gpt-4.1") => (2.00, 8.00),
        m if m.contains("gpt-5.2") => (1.75, 14.00),
        m if m.contains("gpt-5") => (1.75, 14.00),
        m if m.contains("gpt-4-turbo") => (10.00, 30.00),
        m if m.contains("gpt-4") => (30.00, 60.00),
        m if m.contains("gpt-3.5") => (0.50, 1.50),
        m if m.contains("o1") || m.contains("o3") => (15.00, 60.00),

        // Claude/Anthropic pricing (per 1M tokens) - Dec 2025
        m if m.contains("claude-4-opus") || m.contains("claude-opus-4") => (15.00, 75.00),
        m if m.contains("claude-4-sonnet") || m.contains("claude-sonnet-4") => (3.00, 15.00),
        m if m.contains("claude-4-haiku") || m.contains("claude-haiku-4") => (0.25, 1.25),
        m if m.contains("claude-3-5-sonnet") || m.contains("claude-3.5-sonnet") => (3.00, 15.00),
        m if m.contains("claude-3-opus") => (15.00, 75.00),
        m if m.contains("claude-3-sonnet") => (3.00, 15.00),
        m if m.contains("claude-3-haiku") => (0.25, 1.25),
        m if m.contains("claude") => (3.00, 15.00), // Default to Sonnet pricing

        // Gemini/Google pricing (per 1M tokens) - Dec 2025
        m if m.contains("gemini-2.5-flash") => (0.15, 3.50),
        m if m.contains("gemini-2.5-pro") => (1.25, 10.00),
        m if m.contains("gemini-2.0") => (0.10, 0.40),
        m if m.contains("gemini-1.5-pro") => (1.25, 5.00),
        m if m.contains("gemini-1.5-flash") => (0.075, 0.30),
        m if m.contains("gemini") => (0.15, 3.50), // Default to 2.5 Flash

        // xAI Grok pricing (per 1M tokens) - Dec 2025
        m if m.contains("grok-4-fast") => (0.20, 0.50),
        m if m.contains("grok-4") => (3.00, 15.00),
        m if m.contains("grok-3") => (3.00, 15.00),
        m if m.contains("grok") => (3.00, 15.00),

        // Groq pricing (per 1M tokens) - Dec 2025
        m if m.contains("llama-3.3-70b") => (0.59, 0.79),
        m if m.contains("llama-3.1-70b") => (0.59, 0.79),
        m if m.contains("llama-3.1-8b") => (0.05, 0.08),
        m if m.contains("mixtral-8x7b") => (0.24, 0.24),

        // Mistral pricing (per 1M tokens) - Dec 2025
        m if m.contains("mistral-large-3") || m.contains("mistral-large-latest") => (0.50, 1.50),
        m if m.contains("mistral-large") => (2.00, 6.00),
        m if m.contains("mistral-medium") => (0.40, 1.20),
        m if m.contains("mistral-small") => (0.10, 0.30),
        m if m.contains("mistral") => (0.50, 1.50),

        // DeepSeek pricing (per 1M tokens) - Dec 2025
        m if m.contains("deepseek-v3") || m.contains("deepseek") => (0.27, 1.10),

        // NVIDIA NIM pricing (per 1M tokens)
        m if m.contains("nvidia") => (1.00, 3.00),

        // Local/free models
        m if m.contains("ollama") || m.contains("tinyllama") => (0.0, 0.0),

        // Default pricing (conservative estimate)
        _ => (1.00, 3.00),
    };

    let input_cost = (input_tokens as f64 / 1_000_000.0) * input_price;
    let output_cost = (output_tokens as f64 / 1_000_000.0) * output_price;
    input_cost + output_cost
}

/// Internal result type for provider runs
struct ProviderResult {
    answer: String,
    usage: Option<TokenUsage>,
}

/// Minimum score threshold for relevance. Below this, we say "no relevant info found".
/// Set to 0.0 to only block clearly irrelevant queries (negative scores).
const RELEVANCE_THRESHOLD: f32 = 0.0;

pub fn run_model_inference(
    requested_model: &str,
    question: &str,
    fallback_context: &str,
    hits: &[SearchHit],
    context_override: Option<usize>,
    api_key: Option<&str>,
    system_prompt_override: Option<&str>,
) -> Result<ModelInference, ModelRunError> {
    let Some(model_kind) = ModelKind::parse(requested_model) else {
        return Err(ModelRunError::UnsupportedModel(requested_model.to_string()));
    };

    // Check if top hit score is below relevance threshold
    let top_score = hits.first().and_then(|h| h.score).unwrap_or(0.0);
    if hits.is_empty() || top_score < RELEVANCE_THRESHOLD {
        // Extract unique topics from available hits for suggestions
        let mut topics: Vec<String> = hits
            .iter()
            .take(5)
            .filter_map(|h| h.title.clone())
            .collect();
        topics.dedup();

        let suggestions = if topics.is_empty() {
            "Try asking about the topics in your memory file.".to_string()
        } else {
            format!(
                "Your memory contains information about: {}. Try asking about these topics.",
                topics.join(", ")
            )
        };

        let no_match_answer = format!(
            "No relevant information found for your question.\n\n{}\n\nRelevance score: {:.2} (threshold: {:.2})",
            suggestions, top_score, RELEVANCE_THRESHOLD
        );

        return Ok(ModelInference {
            answer: ModelAnswer {
                requested: requested_model.to_string(),
                model: "none".to_string(),
                answer: no_match_answer,
            },
            context_body: String::new(),
            context_fragments: Vec::new(),
            usage: Some(TokenUsage {
                input_tokens: 0,
                output_tokens: 0,
                total_tokens: 0,
                cost_usd: 0.0,
            }),
            grounding: Some(GroundingResult {
                score: 0.0,
                sentence_count: 0,
                grounded_sentences: 0,
                sentence_scores: Vec::new(),
                has_warning: true,
                warning_reason: Some(
                    "No relevant information found - retrieval score below threshold".to_string(),
                ),
            }),
            cached: false,
        });
    }

    let mut budget = model_kind.context_budget();
    if let Some(override_chars) = context_override {
        budget = budget.apply_override(override_chars);
    }

    let context_plan = context::assemble_context(hits, fallback_context, &budget);

    // Check cache first
    if let Some(cached) = cache::check_cache(question, &context_plan.body, &model_kind.label()) {
        let grounding = Some(GroundingResult {
            score: cached.grounding_score,
            sentence_count: 0,
            grounded_sentences: 0,
            sentence_scores: Vec::new(),
            has_warning: cached.grounding_score < 0.4,
            warning_reason: if cached.grounding_score < 0.4 {
                Some("Cached answer - original grounding was low".to_string())
            } else {
                None
            },
        });

        let context_fragments = context_plan
            .records
            .into_iter()
            .map(ModelContextFragment::from_record)
            .collect();

        return Ok(ModelInference {
            answer: ModelAnswer {
                requested: requested_model.to_string(),
                model: cached.model.clone(),
                answer: cached.answer.clone(),
            },
            context_body: context_plan.body,
            context_fragments,
            usage: Some(TokenUsage {
                input_tokens: cached.input_tokens,
                output_tokens: cached.output_tokens,
                total_tokens: cached.input_tokens + cached.output_tokens,
                cost_usd: 0.0, // Cached = no cost
            }),
            grounding,
            cached: true,
        });
    }

    let prompt = build_prompt_parts(
        question,
        &context_plan.body,
        &budget,
        model_kind.max_output_tokens(),
    );

    let result = match &model_kind {
        ModelKind::TinyLlama => {
            #[cfg(feature = "llama-cpp")]
            {
                ProviderResult {
                    answer: tinyllama::run(&prompt)?,
                    usage: None, // Local models don't track tokens
                }
            }
            #[cfg(not(feature = "llama-cpp"))]
            {
                return Err(ModelRunError::UnsupportedModel(
                    "tinyllama (llama-cpp feature not enabled)".to_string(),
                ));
            }
        }
        ModelKind::Ghost { pack_path } => {
            return Err(ModelRunError::UnsupportedModel(format!(
                "ghost model '{}' (ghost runtime not yet available)",
                pack_path.display()
            )));
        }
        ModelKind::Ollama { model } => ProviderResult {
            answer: ollama::run(model, &prompt)?,
            usage: None, // Ollama doesn't always return usage
        },
        ModelKind::OpenAi { model } => {
            openai::run(model, &prompt, api_key, system_prompt_override)?
        }
        ModelKind::Nvidia { model } => ProviderResult {
            answer: nvidia::run(model, &prompt, api_key, system_prompt_override)?,
            usage: None, // NVIDIA NIM doesn't consistently return usage
        },
        ModelKind::Gemini { model } => {
            gemini::run(model, &prompt, api_key, system_prompt_override)?
        }
        ModelKind::Claude { model } => {
            claude::run(model, &prompt, api_key, system_prompt_override)?
        }
        ModelKind::Xai { model } => xai::run(model, &prompt, api_key, system_prompt_override)?,
        ModelKind::Groq { model } => groq::run(model, &prompt, api_key, system_prompt_override)?,
        ModelKind::Mistral { model } => {
            mistral::run(model, &prompt, api_key, system_prompt_override)?
        }
    };

    let context::ContextAggregation {
        body: context_body,
        records,
    } = context_plan;
    let context_fragments = records
        .into_iter()
        .map(ModelContextFragment::from_record)
        .collect();

    // Verify grounding of the answer against the context
    let grounding = Some(verify_grounding(&result.answer, &context_body));

    // Store in cache for future use
    let grounding_score = grounding.as_ref().map(|g| g.score).unwrap_or(0.5);
    let (input_tokens, output_tokens, cost_usd) = result
        .usage
        .as_ref()
        .map(|u| (u.input_tokens, u.output_tokens, u.cost_usd))
        .unwrap_or((0, 0, 0.0));

    cache::store_in_cache(
        question,
        &context_body,
        &model_kind.label(),
        cache::CacheEntry {
            answer: result.answer.clone(),
            model: model_kind.label(),
            input_tokens,
            output_tokens,
            cost_usd,
            grounding_score,
            created_at: std::time::SystemTime::now(),
        },
    );

    // Apply post-processing to clean up the answer
    let processed_answer = postprocess_answer(&result.answer);

    Ok(ModelInference {
        answer: ModelAnswer {
            requested: requested_model.to_string(),
            model: model_kind.label(),
            answer: processed_answer,
        },
        context_body,
        context_fragments,
        usage: result.usage,
        grounding,
        cached: false,
    })
}

/// Verify how well the answer is grounded in the provided context.
/// Returns a GroundingResult with a score (0.0 to 1.0) indicating
/// how well the answer is supported by the context.
pub fn verify_grounding(answer: &str, context: &str) -> GroundingResult {
    use std::collections::HashSet;

    if answer.is_empty() {
        return GroundingResult {
            score: 1.0, // Empty answer = no hallucination
            sentence_count: 0,
            grounded_sentences: 0,
            sentence_scores: Vec::new(),
            has_warning: false,
            warning_reason: None,
        };
    }

    if context.is_empty() {
        return GroundingResult {
            score: 0.0,
            sentence_count: 1,
            grounded_sentences: 0,
            sentence_scores: vec![0.0],
            has_warning: true,
            warning_reason: Some("No context provided - answer may be hallucinated".to_string()),
        };
    }

    // Normalize context for comparison
    let context_lower = context.to_lowercase();
    let context_words: HashSet<&str> = context_lower
        .split(|c: char| !c.is_alphanumeric())
        .filter(|w| w.len() > 2)
        .collect();

    // Split answer into sentences
    let sentences: Vec<&str> = answer
        .split(|c| c == '.' || c == '!' || c == '?')
        .map(|s| s.trim())
        .filter(|s| !s.is_empty() && s.len() > 10)
        .collect();

    if sentences.is_empty() {
        return GroundingResult {
            score: 0.5, // Can't verify
            sentence_count: 0,
            grounded_sentences: 0,
            sentence_scores: Vec::new(),
            has_warning: false,
            warning_reason: None,
        };
    }

    let mut sentence_scores = Vec::with_capacity(sentences.len());
    let mut grounded_count = 0;

    for sentence in &sentences {
        let sentence_lower = sentence.to_lowercase();
        let sentence_words: HashSet<&str> = sentence_lower
            .split(|c: char| !c.is_alphanumeric())
            .filter(|w| w.len() > 2)
            .collect();

        if sentence_words.is_empty() {
            sentence_scores.push(0.5);
            continue;
        }

        // Calculate word overlap
        let overlap: usize = sentence_words.intersection(&context_words).count();
        let score = (overlap as f32) / (sentence_words.len() as f32).max(1.0);

        // Also check for exact phrase matches (stronger signal)
        let phrase_bonus = if context_lower.contains(&sentence_lower) {
            0.3
        } else {
            // Check for significant substring matches
            let words: Vec<&str> = sentence_lower.split_whitespace().collect();
            if words.len() >= 3 {
                let phrase = words[..3.min(words.len())].join(" ");
                if context_lower.contains(&phrase) {
                    0.15
                } else {
                    0.0
                }
            } else {
                0.0
            }
        };

        let final_score = (score + phrase_bonus).min(1.0);
        sentence_scores.push(final_score);

        if final_score >= 0.3 {
            grounded_count += 1;
        }
    }

    let overall_score = if sentence_scores.is_empty() {
        0.5
    } else {
        sentence_scores.iter().sum::<f32>() / sentence_scores.len() as f32
    };

    // Determine warning
    let (has_warning, warning_reason) = if overall_score < 0.2 {
        (
            true,
            Some("Answer appears to be poorly grounded in context".to_string()),
        )
    } else if overall_score < 0.4 && grounded_count < sentences.len() / 2 {
        (
            true,
            Some("Some statements may not be supported by context".to_string()),
        )
    } else {
        (false, None)
    };

    GroundingResult {
        score: overall_score,
        sentence_count: sentences.len(),
        grounded_sentences: grounded_count,
        sentence_scores,
        has_warning,
        warning_reason,
    }
}

mod context {
    use super::{ModelContextBudget, clamp_to};
    use memvid_core::types::SearchHit;

    const CONTEXT_HEADER: &str = "## Retrieval Context\n";
    const PRIMARY_HEADER: &str = "### Primary Hit\n";
    const CORRECTION_WARNING: &str = r#"
╔══════════════════════════════════════════════════════════════════╗
║  🔴 USER CORRECTION - THIS IS THE AUTHORITATIVE ANSWER          ║
║  Any contradicting information below is OUTDATED and WRONG.     ║
║  YOU MUST USE THE ANSWER FROM THIS CORRECTION.                  ║
╚══════════════════════════════════════════════════════════════════╝
"#;
    const SUPPORT_HEADER: &str = "### Supporting Hits\n";
    const SUMMARY_HEADER: &str = "### Overflow Summaries\n";
    const SUMMARY_HIGHLIGHT_CHARS: usize = 240;
    /// Minimum chars for a micro-summary when budget is tight (title + rank info)
    #[allow(dead_code)]
    const MICRO_SUMMARY_CHARS: usize = 80;

    #[derive(Debug, Clone)]
    pub(super) struct ContextAggregation {
        pub body: String,
        pub records: Vec<ContextRecord>,
    }

    impl ContextAggregation {
        fn from_fallback(fallback: &str, limit: usize) -> Self {
            let body = if limit == 0 || fallback.is_empty() {
                String::new()
            } else if fallback.len() <= limit {
                fallback.to_string()
            } else {
                clamp_to(fallback, limit)
            };
            Self {
                body,
                records: Vec::new(),
            }
        }
    }

    #[derive(Debug, Clone)]
    pub(super) struct ContextRecord {
        pub rank: usize,
        pub uri: String,
        pub title: Option<String>,
        pub score: Option<f32>,
        pub matches: usize,
        pub frame_id: u64,
        pub range: (usize, usize),
        pub chunk_range: Option<(usize, usize)>,
        pub text: String,
        pub mode: ContextMode,
    }

    #[derive(Debug, Clone, Copy, Eq, PartialEq)]
    pub(super) enum ContextMode {
        Full,
        Summary,
    }

    #[derive(Debug, Clone)]
    pub(super) struct ContextAssemblyPlan {
        primary: Option<ContextRecord>,
        supporting: Vec<ContextRecord>,
        summaries: Vec<ContextRecord>,
    }

    pub(super) fn assemble_context(
        hits: &[SearchHit],
        fallback: &str,
        budget: &ModelContextBudget,
    ) -> ContextAggregation {
        if hits.is_empty() {
            return ContextAggregation::from_fallback(fallback, budget.context_chars());
        }

        let plan = assemble_plan(hits, budget.context_chars());
        let mut body = String::new();
        let mut records = Vec::new();

        body.push_str(CONTEXT_HEADER);
        // Check if primary is a correction BEFORE moving it
        let primary_is_correction = plan
            .primary
            .as_ref()
            .map(|p| p.uri.contains("mv2://correction/"))
            .unwrap_or(false);
        if let Some(primary) = plan.primary {
            body.push_str(PRIMARY_HEADER);
            // Add correction warning if primary hit is a correction
            if primary_is_correction {
                body.push_str(CORRECTION_WARNING);
            }
            body.push_str(&primary.text);
            body.push_str("\n\n");
            records.push(primary);
        }

        if !plan.supporting.is_empty() {
            body.push_str(SUPPORT_HEADER);
            if primary_is_correction {
                body.push_str("⚠️ **WARNING: The following sources may contain OUTDATED information. Use the correction above.**\n\n");
            }
            for record in plan.supporting {
                // If primary is a correction, skip older corrections in supporting hits
                // to avoid confusing the LLM with conflicting correction data
                if primary_is_correction && record.uri.contains("mv2://correction/") {
                    continue;
                }
                body.push_str(&record.text);
                body.push_str("\n\n");
                records.push(record);
            }
        }

        if !plan.summaries.is_empty() {
            body.push_str(SUMMARY_HEADER);
            for record in plan.summaries {
                body.push_str(&record.text);
                body.push_str("\n\n");
                records.push(record);
            }
        }

        ContextAggregation { body, records }
    }

    fn assemble_plan(hits: &[SearchHit], mut remaining_chars: usize) -> ContextAssemblyPlan {
        let mut records = Vec::new();
        for hit in hits.iter().take(32) {
            let full_record = build_record(hit, render_full(hit), ContextMode::Full);
            let summary_record = build_record(hit, render_summary(hit), ContextMode::Summary);
            let micro_record = build_record(hit, render_micro_summary(hit), ContextMode::Summary);
            records.push((full_record, summary_record, micro_record));
        }

        let mut plan = ContextAssemblyPlan {
            primary: None,
            supporting: Vec::new(),
            summaries: Vec::new(),
        };

        // Handle primary hit (rank #1) - always include at least a summary
        if let Some((primary_full, primary_summary, primary_micro)) = records.first() {
            if primary_full.text.len() <= remaining_chars {
                remaining_chars = remaining_chars.saturating_sub(primary_full.text.len());
                plan.primary = Some(primary_full.clone());
            } else if primary_summary.text.len() <= remaining_chars {
                // Primary as summary (unusual but possible with very tight budget)
                remaining_chars = remaining_chars.saturating_sub(primary_summary.text.len());
                plan.primary = Some(primary_summary.clone());
            } else if primary_micro.text.len() <= remaining_chars {
                // At minimum, include micro-summary for primary
                remaining_chars = remaining_chars.saturating_sub(primary_micro.text.len());
                plan.primary = Some(primary_micro.clone());
            }
        }

        // Process remaining hits with fallback to micro-summaries
        for (idx, (full, summary, micro)) in records.iter().enumerate() {
            if idx == 0 {
                continue;
            }

            if full.text.len() <= remaining_chars {
                remaining_chars = remaining_chars.saturating_sub(full.text.len());
                plan.supporting.push(full.clone());
            } else if summary.text.len() <= remaining_chars {
                remaining_chars = remaining_chars.saturating_sub(summary.text.len());
                plan.summaries.push(summary.clone());
            } else if micro.text.len() <= remaining_chars {
                // Fallback: include at least a micro-summary to preserve ranking info
                // This ensures high-ranked hits are never completely dropped
                remaining_chars = remaining_chars.saturating_sub(micro.text.len());
                plan.summaries.push(micro.clone());
            }
            // If even micro-summary doesn't fit, the budget is truly exhausted
        }

        plan
    }

    fn render_full(hit: &SearchHit) -> String {
        let content = hit
            .chunk_text
            .clone()
            .or_else(|| Some(hit.text.clone()))
            .unwrap_or_default();

        // Clean up the content for better LLM comprehension
        let clean_content = clean_text_for_llm(&content);

        // Use a cleaner format that LLMs parse better
        let title = hit.title.clone().unwrap_or_default();
        let source_info = if title.is_empty() {
            format!("[Source #{}]", hit.rank)
        } else {
            format!("[Source #{}: {}]", hit.rank, title)
        };

        // Include relevance indicator for context
        let relevance = match hit.score {
            Some(s) if s > 0.8 => "⬤ High relevance",
            Some(s) if s > 0.5 => "◐ Medium relevance",
            _ => "",
        };

        if relevance.is_empty() {
            format!("{}\n{}", source_info, clean_content)
        } else {
            format!("{} ({})\n{}", source_info, relevance, clean_content)
        }
    }

    fn render_summary(hit: &SearchHit) -> String {
        let snippet = hit
            .chunk_text
            .clone()
            .or_else(|| Some(hit.text.clone()))
            .unwrap_or_default();
        let snippet = trim_highlight(&snippet, SUMMARY_HIGHLIGHT_CHARS);
        let clean_snippet = clean_text_for_llm(&snippet);
        format!("[Source #{}] {}", hit.rank, clean_snippet)
    }

    /// Create a minimal summary when budget is very tight.
    /// Always fits within MICRO_SUMMARY_CHARS to ensure important hits are never dropped.
    fn render_micro_summary(hit: &SearchHit) -> String {
        let title = hit.title.clone().unwrap_or_else(|| "untitled".to_string());
        let title_truncated = clamp_to(&title, 40);
        // Format: "[#2: document.pdf] ..." - ~50-60 chars max
        format!("[#{}: {}] ...", hit.rank, title_truncated)
    }

    /// Clean text to improve LLM comprehension
    fn clean_text_for_llm(text: &str) -> String {
        let mut result = text.to_string();

        // Remove excessive whitespace while preserving paragraph structure
        result = result
            .lines()
            .map(|line| line.trim())
            .filter(|line| !line.is_empty())
            .collect::<Vec<_>>()
            .join("\n");

        // Normalize unicode quotes and dashes (using string patterns for multi-byte chars)
        result = result
            .replace("\u{2018}", "'") // Left single quote '
            .replace("\u{2019}", "'") // Right single quote '
            .replace("\u{201C}", "\"") // Left double quote "
            .replace("\u{201D}", "\"") // Right double quote "
            .replace("\u{2013}", "-") // En dash –
            .replace("\u{2014}", "-"); // Em dash —

        // Remove null bytes and other control characters
        result = result
            .chars()
            .filter(|c| !c.is_control() || *c == '\n' || *c == '\t')
            .collect();

        result
    }

    fn trim_highlight(text: &str, limit: usize) -> String {
        let clean = text.replace('\n', " ");
        clamp_to(&clean, limit)
    }

    fn build_record(hit: &SearchHit, text: String, mode: ContextMode) -> ContextRecord {
        ContextRecord {
            rank: hit.rank,
            uri: hit.uri.clone(),
            title: hit.title.clone(),
            score: hit.score,
            matches: hit.matches,
            frame_id: hit.frame_id,
            range: hit.range,
            chunk_range: hit.chunk_range,
            text,
            mode,
        }
    }
}

#[cfg(feature = "llama-cpp")]
mod tinyllama {
    use super::{ModelRunError, PromptParts, TINYLLAMA_LABEL, ThinkingSpinner};
    use anyhow::anyhow;
    use llama_cpp::standard_sampler::StandardSampler;
    use llama_cpp::{LlamaModel, LlamaParams, SessionParams};
    use tokio::runtime::Builder;

    use std::path::{Path, PathBuf};

    const MODEL_DIR: &str = "models/tinyllama";
    const GGUF_HINT: &str = "*.gguf";

    pub(super) fn run(prompt: &PromptParts) -> Result<String, ModelRunError> {
        let base_dir = Path::new(MODEL_DIR);
        let assets = RequiredAssets::new(base_dir);

        if let Some(missing) = assets.missing_paths() {
            return Err(ModelRunError::AssetsMissing {
                model: TINYLLAMA_LABEL.to_string(),
                missing,
            });
        }

        let gguf_path = assets.gguf_path.clone().ok_or_else(|| {
            ModelRunError::Runtime(anyhow!(
                "no GGUF model file found in {}",
                base_dir.display()
            ))
        })?;

        unsafe {
            std::env::set_var("GGML_LOG_LEVEL", "ERROR");
            std::env::set_var("LLAMA_LOG_LEVEL", "ERROR");
        }

        let model =
            LlamaModel::load_from_file(&gguf_path, LlamaParams::default()).map_err(|err| {
                ModelRunError::Runtime(anyhow!(
                    "failed to load TinyLlama weights from {}: {err}",
                    gguf_path.display()
                ))
            })?;

        let mut session_params = SessionParams::default();
        if session_params.n_ctx == 0 {
            session_params.n_ctx = 2048;
        }
        session_params.n_batch = session_params.n_ctx.min(512);
        if session_params.n_ubatch == 0 {
            session_params.n_ubatch = 512;
        }
        let max_tokens = session_params.n_ctx as usize;
        let mut session = model.create_session(session_params).map_err(|err| {
            ModelRunError::Runtime(anyhow!("failed to create TinyLlama session: {err}"))
        })?;

        let mut priming_tokens = model
            .tokenize_bytes(prompt.completion_prompt().as_bytes(), true, true)
            .map_err(|err| {
                ModelRunError::Runtime(anyhow!("failed to tokenize TinyLlama prompt: {err}"))
            })?;

        let requested_tokens = prompt.max_output_tokens();
        if max_tokens > 0 {
            let reserved = requested_tokens + 64;
            if priming_tokens.len() >= max_tokens.saturating_sub(reserved) {
                let target = max_tokens.saturating_sub(reserved).max(1);
                let tail_start = priming_tokens.len().saturating_sub(target);
                priming_tokens = priming_tokens.split_off(tail_start);
            }
        }

        session
            .advance_context_with_tokens(&priming_tokens)
            .map_err(|err| {
                ModelRunError::Runtime(anyhow!("failed to prime TinyLlama context: {err}"))
            })?;

        let handle = session
            .start_completing_with(StandardSampler::default(), requested_tokens)
            .map_err(|err| ModelRunError::Runtime(anyhow!("completion failed to start: {err}")))?;

        let runtime = Builder::new_current_thread()
            .enable_all()
            .build()
            .map_err(|err| {
                ModelRunError::Runtime(anyhow!("failed to build tokio runtime: {err}"))
            })?;

        let mut spinner = ThinkingSpinner::start();
        let generated = runtime.block_on(async { handle.into_string_async().await });
        spinner.stop();

        let answer = generated.trim().to_string();

        if answer.is_empty() {
            Ok("No answer generated by TinyLlama.".to_string())
        } else {
            Ok(answer)
        }
    }

    struct RequiredAssets {
        gguf_path: Option<PathBuf>,
        base_dir: PathBuf,
    }

    impl RequiredAssets {
        fn new(base_dir: &Path) -> Self {
            let gguf_path = find_first_gguf(base_dir);
            Self {
                gguf_path,
                base_dir: base_dir.to_path_buf(),
            }
        }

        fn missing_paths(&self) -> Option<Vec<PathBuf>> {
            if self.gguf_path.is_some() {
                None
            } else {
                Some(vec![self.base_dir.join(GGUF_HINT)])
            }
        }
    }

    fn find_first_gguf(base_dir: &Path) -> Option<PathBuf> {
        let mut entries: Vec<PathBuf> = std::fs::read_dir(base_dir)
            .ok()?
            .filter_map(|entry| entry.ok().map(|e| e.path()))
            .filter(|path| path.is_file() && path.extension().map_or(false, |ext| ext == "gguf"))
            .collect();
        entries.sort();
        entries.into_iter().next()
    }
}

mod ollama {
    use super::{ModelRunError, PromptParts, ThinkingSpinner};
    use anyhow::anyhow;
    use reqwest::blocking::Client;
    use serde::Deserialize;
    use serde_json::json;

    const ENDPOINT: &str = "http://127.0.0.1:11434/api/generate";

    pub(super) fn run(model: &str, prompt: &PromptParts) -> Result<String, ModelRunError> {
        let client = Client::builder()
            .timeout(std::time::Duration::from_secs(60))
            .build()
            .map_err(|err| ModelRunError::Runtime(anyhow!("failed to build HTTP client: {err}")))?;

        let mut spinner = ThinkingSpinner::start();
        let response = client
            .post(ENDPOINT)
            .json(&json!({
                "model": model,
                "prompt": prompt.completion_prompt(),
                "stream": false
            }))
            .send()
            .map_err(|err| ModelRunError::Runtime(anyhow!("ollama request failed: {err}")))?
            .error_for_status()
            .map_err(|err| {
                ModelRunError::Runtime(anyhow!("ollama returned error status: {err}"))
            })?;

        let body: GenerateResponse = response.json().map_err(|err| {
            ModelRunError::Runtime(anyhow!("failed to decode ollama response: {err}"))
        })?;
        spinner.stop();

        let text = body.response.trim().to_string();
        if text.is_empty() {
            Ok("No answer returned by Ollama.".to_string())
        } else {
            Ok(text)
        }
    }

    #[derive(Debug, Deserialize)]
    struct GenerateResponse {
        #[serde(default)]
        response: String,
    }
}

mod openai {
    use super::{
        ModelRunError, PromptParts, ProviderResult, SYSTEM_PROMPT, ThinkingSpinner, TokenUsage,
        calculate_cost,
    };
    use anyhow::anyhow;
    use reqwest::blocking::Client;
    use reqwest::header::{AUTHORIZATION, CONTENT_TYPE, HeaderMap, HeaderValue};
    use serde::Deserialize;
    use serde_json::json;

    const CHAT_ENDPOINT: &str = "https://api.openai.com/v1/chat/completions";
    const RESPONSES_ENDPOINT: &str = "https://api.openai.com/v1/responses";

    pub(super) fn run(
        model: &str,
        prompt: &PromptParts,
        override_key: Option<&str>,
        system_prompt_override: Option<&str>,
    ) -> Result<ProviderResult, ModelRunError> {
        let system_prompt = system_prompt_override.unwrap_or(SYSTEM_PROMPT);
        let key = override_key
            .map(|value| value.to_string())
            .or_else(|| std::env::var("OPENAI_API_KEY").ok())
            .ok_or_else(|| {
                ModelRunError::Runtime(anyhow!(
                    "OPENAI_API_KEY environment variable is required for OpenAI models"
                ))
            })?;

        let mut headers = HeaderMap::new();
        headers.insert(
            AUTHORIZATION,
            HeaderValue::from_str(&format!("Bearer {key}")).map_err(|err| {
                ModelRunError::Runtime(anyhow!("invalid OPENAI_API_KEY header value: {err}"))
            })?,
        );
        headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));

        let client = Client::builder()
            .no_proxy()
            .timeout(std::time::Duration::from_secs(60))
            .default_headers(headers)
            .build()
            .map_err(|err| ModelRunError::Runtime(anyhow!("failed to build HTTP client: {err}")))?;

        let mut spinner = ThinkingSpinner::start();
        let (text, usage) = if requires_responses_api(model) {
            let combined_prompt = format!(
                "System instructions:\n{}\n\nUser query:\n{}",
                system_prompt,
                prompt.user_message()
            );
            let payload = json!({
                "model": model,
                "input": combined_prompt,
                "max_output_tokens": prompt.max_output_tokens() as u32,
                "reasoning": {
                    "effort": "low"
                }
            });

            let response = client
                .post(RESPONSES_ENDPOINT)
                .json(&payload)
                .send()
                .map_err(|err| ModelRunError::Runtime(anyhow!("OpenAI request failed: {err}")))?;

            let status = response.status();
            if !status.is_success() {
                let body = response
                    .text()
                    .unwrap_or_else(|_| "<failed to read body>".to_string());
                return Err(ModelRunError::Runtime(anyhow!(
                    "OpenAI returned error status {status}: {body}"
                )));
            }

            let body: ResponsesResponse = response.json().map_err(|err| {
                ModelRunError::Runtime(anyhow!("failed to decode OpenAI response: {err}"))
            })?;

            let usage = body.usage.as_ref().map(|u| {
                let input = u.input_tokens.unwrap_or(0);
                let output = u.output_tokens.unwrap_or(0);
                TokenUsage {
                    input_tokens: input,
                    output_tokens: output,
                    total_tokens: input + output,
                    cost_usd: calculate_cost(model, input, output),
                }
            });
            (extract_responses_text(&body), usage)
        } else {
            let payload = json!({
                "model": model,
                "messages": [
                    {"role": "system", "content": system_prompt},
                    {"role": "user", "content": prompt.user_message()}
                ],
                "temperature": 0.2,
                "max_tokens": prompt.max_output_tokens() as u32
            });

            let response = client
                .post(CHAT_ENDPOINT)
                .json(&payload)
                .send()
                .map_err(|err| ModelRunError::Runtime(anyhow!("OpenAI request failed: {err}")))?;

            let status = response.status();
            if !status.is_success() {
                let body = response
                    .text()
                    .unwrap_or_else(|_| "<failed to read body>".to_string());
                return Err(ModelRunError::Runtime(anyhow!(
                    "OpenAI returned error status {status}: {body}"
                )));
            }

            let body: ChatResponse = response.json().map_err(|err| {
                ModelRunError::Runtime(anyhow!("failed to decode OpenAI response: {err}"))
            })?;

            let usage = body.usage.as_ref().map(|u| TokenUsage {
                input_tokens: u.prompt_tokens,
                output_tokens: u.completion_tokens,
                total_tokens: u.total_tokens,
                cost_usd: calculate_cost(model, u.prompt_tokens, u.completion_tokens),
            });
            (extract_chat_text(&body), usage)
        };
        spinner.stop();
        Ok(ProviderResult {
            answer: text,
            usage,
        })
    }

    #[derive(Debug, Deserialize)]
    struct ChatResponse {
        choices: Vec<Choice>,
        #[serde(default)]
        usage: Option<ChatUsage>,
    }

    #[derive(Debug, Deserialize)]
    struct ChatUsage {
        prompt_tokens: u32,
        completion_tokens: u32,
        total_tokens: u32,
    }

    #[derive(Debug, Deserialize)]
    struct Choice {
        message: ChatMessage,
    }

    #[derive(Debug, Deserialize)]
    struct ChatMessage {
        #[serde(default)]
        content: Option<String>,
    }

    #[derive(Debug, Deserialize)]
    struct ResponsesResponse {
        #[serde(default)]
        output: Vec<ResponseItem>,
        #[serde(default)]
        output_text: Vec<String>,
        #[serde(default)]
        usage: Option<ResponsesUsage>,
    }

    #[derive(Debug, Deserialize)]
    struct ResponsesUsage {
        #[serde(default)]
        input_tokens: Option<u32>,
        #[serde(default)]
        output_tokens: Option<u32>,
    }

    #[derive(Debug, Deserialize)]
    struct ResponseItem {
        #[serde(default)]
        content: Vec<ResponseContent>,
    }

    #[derive(Debug, Deserialize)]
    struct ResponseContent {
        #[serde(rename = "type")]
        kind: String,
        #[serde(default)]
        text: Option<String>,
    }

    fn extract_chat_text(body: &ChatResponse) -> String {
        body.choices
            .iter()
            .find_map(|choice| choice.message.content.clone())
            .map(|value| value.trim().to_string())
            .unwrap_or_else(|| "No answer returned by OpenAI.".to_string())
    }

    fn extract_responses_text(body: &ResponsesResponse) -> String {
        if !body.output_text.is_empty() {
            let text = body
                .output_text
                .iter()
                .find(|value| !value.trim().is_empty());
            if let Some(text) = text {
                return text.trim().to_string();
            }
        }
        for item in &body.output {
            for segment in &item.content {
                match segment.kind.as_str() {
                    "output_text" | "text" => {
                        if let Some(text) = &segment.text {
                            let trimmed = text.trim();
                            if !trimmed.is_empty() {
                                return trimmed.to_string();
                            }
                        }
                    }
                    _ => {}
                }
            }
        }
        "No answer returned by OpenAI.".to_string()
    }

    fn requires_responses_api(model: &str) -> bool {
        let lowered = model.to_ascii_lowercase();
        lowered.starts_with("gpt-5") || lowered.contains("gpt-4.1")
    }
}

mod nvidia {
    use super::{ModelRunError, PromptParts, SYSTEM_PROMPT, ThinkingSpinner};
    use anyhow::anyhow;
    use reqwest::blocking::Client;
    use reqwest::header::{AUTHORIZATION, CONTENT_TYPE, HeaderMap, HeaderValue};
    use serde::Deserialize;
    use serde_json::json;

    pub(super) fn run(
        model: &str,
        prompt: &PromptParts,
        override_key: Option<&str>,
        system_prompt_override: Option<&str>,
    ) -> Result<String, ModelRunError> {
        let system_prompt = system_prompt_override.unwrap_or(SYSTEM_PROMPT);
        let key = override_key
            .map(|value| value.to_string())
            .or_else(|| std::env::var("NVIDIA_API_KEY").ok())
            .ok_or_else(|| {
                ModelRunError::Runtime(anyhow!(
                    "NVIDIA_API_KEY environment variable is required for NVIDIA models"
                ))
            })?;

        let model = model.trim();
        if model.is_empty() {
            return Err(ModelRunError::Runtime(anyhow!(
                "NVIDIA model name required. Use `nvidia:<model>` or set NVIDIA_LLM_MODEL."
            )));
        }

        let base_url = std::env::var("NVIDIA_BASE_URL")
            .ok()
            .map(|value| value.trim().trim_end_matches('/').to_string())
            .filter(|value| !value.is_empty())
            .unwrap_or_else(|| "https://integrate.api.nvidia.com".to_string());
        let endpoint = format!("{base_url}/v1/chat/completions");

        let mut headers = HeaderMap::new();
        headers.insert(
            AUTHORIZATION,
            HeaderValue::from_str(&format!("Bearer {key}")).map_err(|err| {
                ModelRunError::Runtime(anyhow!("invalid NVIDIA_API_KEY header value: {err}"))
            })?,
        );
        headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));

        let client = Client::builder()
            .timeout(std::time::Duration::from_secs(60))
            .default_headers(headers)
            .build()
            .map_err(|err| ModelRunError::Runtime(anyhow!("failed to build HTTP client: {err}")))?;

        let payload = json!({
            "model": model,
            "messages": [
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": prompt.user_message()}
            ],
            "temperature": 0.2,
            "max_tokens": prompt.max_output_tokens() as u32
        });

        let mut spinner = ThinkingSpinner::start();
        let response = client
            .post(endpoint)
            .json(&payload)
            .send()
            .map_err(|err| ModelRunError::Runtime(anyhow!("NVIDIA request failed: {err}")))?;

        let status = response.status();
        if !status.is_success() {
            let body = response
                .text()
                .unwrap_or_else(|_| "<failed to read body>".to_string());
            spinner.stop();
            return Err(ModelRunError::Runtime(anyhow!(
                "NVIDIA returned error status {status}: {body}"
            )));
        }

        let body: ChatResponse = response.json().map_err(|err| {
            ModelRunError::Runtime(anyhow!("failed to decode NVIDIA response: {err}"))
        })?;
        spinner.stop();

        let text = body
            .choices
            .into_iter()
            .find_map(|choice| choice.message.content)
            .map(|value| value.trim().to_string())
            .unwrap_or_else(|| "No answer returned by NVIDIA.".to_string());

        Ok(text)
    }

    #[derive(Debug, Deserialize)]
    struct ChatResponse {
        choices: Vec<Choice>,
    }

    #[derive(Debug, Deserialize)]
    struct Choice {
        message: ChatMessage,
    }

    #[derive(Debug, Deserialize)]
    struct ChatMessage {
        #[serde(default)]
        content: Option<String>,
    }
}

mod gemini {
    use super::{
        ModelRunError, PromptParts, ProviderResult, SYSTEM_PROMPT, ThinkingSpinner, TokenUsage,
        calculate_cost,
    };
    use anyhow::anyhow;
    use reqwest::blocking::Client;
    use reqwest::header::{CONTENT_TYPE, HeaderMap, HeaderName, HeaderValue};
    use serde::Deserialize;
    use serde_json::json;

    pub(super) fn run(
        model: &str,
        prompt: &PromptParts,
        override_key: Option<&str>,
        system_prompt_override: Option<&str>,
    ) -> Result<ProviderResult, ModelRunError> {
        let system_prompt = system_prompt_override.unwrap_or(SYSTEM_PROMPT);
        let key = override_key
            .map(|value| value.to_string())
            .or_else(|| std::env::var("GEMINI_API_KEY").ok())
            .ok_or_else(|| {
                ModelRunError::Runtime(anyhow!(
                    "GEMINI_API_KEY environment variable is required for Gemini models"
                ))
            })?;

        let url = format!(
            "https://generativelanguage.googleapis.com/v1beta/models/{}:generateContent",
            model
        );

        let mut headers = HeaderMap::new();
        headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
        headers.insert(
            HeaderName::from_static("x-goog-api-key"),
            HeaderValue::from_str(&key).map_err(|err| {
                ModelRunError::Runtime(anyhow!("invalid GEMINI_API_KEY header value: {err}"))
            })?,
        );

        let client = Client::builder()
            .timeout(std::time::Duration::from_secs(60))
            .default_headers(headers)
            .build()
            .map_err(|err| ModelRunError::Runtime(anyhow!("failed to build HTTP client: {err}")))?;

        let payload = json!({
            "contents": [{
                "parts": [
                    { "text": system_prompt },
                    { "text": prompt.user_message() }
                ]
            }],
            "generationConfig": {
                "temperature": 0.2,
                "maxOutputTokens": prompt.max_output_tokens() as u32,
                "topK": 40,
                "topP": 0.95
            }
        });

        let mut spinner = ThinkingSpinner::start();
        let response = client
            .post(url)
            .json(&payload)
            .send()
            .map_err(|err| ModelRunError::Runtime(anyhow!("Gemini request failed: {err}")))?
            .error_for_status()
            .map_err(|err| {
                ModelRunError::Runtime(anyhow!("Gemini returned error status: {err}"))
            })?;

        let body: GenerateResponse = response.json().map_err(|err| {
            ModelRunError::Runtime(anyhow!("failed to decode Gemini response: {err}"))
        })?;
        spinner.stop();

        let text = body
            .candidates
            .iter()
            .flat_map(|candidate| candidate.content.parts.iter())
            .find_map(|part| part.text.clone())
            .map(|value| value.trim().to_string())
            .unwrap_or_else(|| "No answer returned by Gemini.".to_string());

        let usage = body.usage_metadata.as_ref().map(|u| {
            let input = u.prompt_token_count.unwrap_or(0);
            let output = u.candidates_token_count.unwrap_or(0);
            TokenUsage {
                input_tokens: input,
                output_tokens: output,
                total_tokens: input + output,
                cost_usd: calculate_cost(model, input, output),
            }
        });

        Ok(ProviderResult {
            answer: text,
            usage,
        })
    }

    #[derive(Debug, Deserialize)]
    struct GenerateResponse {
        candidates: Vec<Candidate>,
        #[serde(default, rename = "usageMetadata")]
        usage_metadata: Option<GeminiUsage>,
    }

    #[derive(Debug, Deserialize)]
    struct GeminiUsage {
        #[serde(default, rename = "promptTokenCount")]
        prompt_token_count: Option<u32>,
        #[serde(default, rename = "candidatesTokenCount")]
        candidates_token_count: Option<u32>,
    }

    #[derive(Debug, Deserialize)]
    struct Candidate {
        content: CandidateContent,
    }

    #[derive(Debug, Deserialize)]
    struct CandidateContent {
        parts: Vec<CandidatePart>,
    }

    #[derive(Debug, Deserialize)]
    struct CandidatePart {
        #[serde(default)]
        text: Option<String>,
    }
}

mod claude {
    use super::{
        ModelRunError, PromptParts, ProviderResult, SYSTEM_PROMPT, ThinkingSpinner, TokenUsage,
        calculate_cost,
    };
    use anyhow::anyhow;
    use reqwest::blocking::Client;
    use reqwest::header::{CONTENT_TYPE, HeaderMap, HeaderName, HeaderValue};
    use serde::Deserialize;
    use serde_json::json;

    const ENDPOINT: &str = "https://api.anthropic.com/v1/messages";
    const API_VERSION: &str = "2023-06-01";

    pub(super) fn run(
        model: &str,
        prompt: &PromptParts,
        override_key: Option<&str>,
        system_prompt_override: Option<&str>,
    ) -> Result<ProviderResult, ModelRunError> {
        let system_prompt = system_prompt_override.unwrap_or(SYSTEM_PROMPT);
        let key = override_key
            .map(|value| value.to_string())
            .or_else(|| std::env::var("ANTHROPIC_API_KEY").ok())
            .or_else(|| std::env::var("CLAUDE_API_KEY").ok())
            .ok_or_else(|| {
                ModelRunError::Runtime(anyhow!(
                    "ANTHROPIC_API_KEY environment variable is required for Claude models"
                ))
            })?;

        let mut headers = HeaderMap::new();
        headers.insert(
            HeaderName::from_static("x-api-key"),
            HeaderValue::from_str(&key).map_err(|err| {
                ModelRunError::Runtime(anyhow!("invalid ANTHROPIC_API_KEY header value: {err}"))
            })?,
        );
        headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
        headers.insert(
            HeaderName::from_static("anthropic-version"),
            HeaderValue::from_static(API_VERSION),
        );

        let client = Client::builder()
            .timeout(std::time::Duration::from_secs(60))
            .default_headers(headers)
            .build()
            .map_err(|err| ModelRunError::Runtime(anyhow!("failed to build HTTP client: {err}")))?;

        let payload = json!({
            "model": model,
            "max_tokens": prompt.max_output_tokens() as u32,
            "temperature": 0.2,
            "system": system_prompt,
            "messages": [{
                "role": "user",
                "content": [{"type": "text", "text": prompt.user_message()}]
            }]
        });

        let mut spinner = ThinkingSpinner::start();
        let response = client
            .post(ENDPOINT)
            .json(&payload)
            .send()
            .map_err(|err| ModelRunError::Runtime(anyhow!("Claude request failed: {err}")))?
            .error_for_status()
            .map_err(|err| {
                ModelRunError::Runtime(anyhow!("Claude returned error status: {err}"))
            })?;

        let body: ClaudeResponse = response.json().map_err(|err| {
            ModelRunError::Runtime(anyhow!("failed to decode Claude response: {err}"))
        })?;
        spinner.stop();

        let text = body
            .content
            .iter()
            .find_map(|part| match part {
                ContentBlock::Text { text } if !text.trim().is_empty() => {
                    Some(text.trim().to_string())
                }
                _ => None,
            })
            .unwrap_or_else(|| "No answer returned by Claude.".to_string());

        let usage = body.usage.as_ref().map(|u| TokenUsage {
            input_tokens: u.input_tokens,
            output_tokens: u.output_tokens,
            total_tokens: u.input_tokens + u.output_tokens,
            cost_usd: calculate_cost(model, u.input_tokens, u.output_tokens),
        });

        Ok(ProviderResult {
            answer: text,
            usage,
        })
    }

    #[derive(Debug, Deserialize)]
    struct ClaudeResponse {
        #[serde(default)]
        content: Vec<ContentBlock>,
        #[serde(default)]
        usage: Option<ClaudeUsage>,
    }

    #[derive(Debug, Deserialize)]
    struct ClaudeUsage {
        input_tokens: u32,
        output_tokens: u32,
    }

    #[derive(Debug, Deserialize)]
    #[serde(tag = "type", rename_all = "lowercase")]
    enum ContentBlock {
        Text {
            text: String,
        },
        #[serde(other)]
        Other,
    }
}

mod xai {
    use super::{
        ModelRunError, PromptParts, ProviderResult, SYSTEM_PROMPT, ThinkingSpinner, TokenUsage,
        calculate_cost,
    };
    use anyhow::anyhow;
    use reqwest::blocking::Client;
    use reqwest::header::{AUTHORIZATION, CONTENT_TYPE, HeaderMap, HeaderValue};
    use serde::Deserialize;
    use serde_json::json;

    const ENDPOINT: &str = "https://api.x.ai/v1/chat/completions";

    pub(super) fn run(
        model: &str,
        prompt: &PromptParts,
        override_key: Option<&str>,
        system_prompt_override: Option<&str>,
    ) -> Result<ProviderResult, ModelRunError> {
        let system_prompt = system_prompt_override.unwrap_or(SYSTEM_PROMPT);
        let key = override_key
            .map(|value| value.to_string())
            .or_else(|| std::env::var("XAI_API_KEY").ok())
            .or_else(|| std::env::var("GROK_API_KEY").ok())
            .ok_or_else(|| {
                ModelRunError::Runtime(anyhow!(
                    "XAI_API_KEY environment variable is required for Grok models"
                ))
            })?;

        let mut headers = HeaderMap::new();
        headers.insert(
            AUTHORIZATION,
            HeaderValue::from_str(&format!("Bearer {key}")).map_err(|err| {
                ModelRunError::Runtime(anyhow!("invalid XAI_API_KEY header value: {err}"))
            })?,
        );
        headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));

        let client = Client::builder()
            .timeout(std::time::Duration::from_secs(120))
            .default_headers(headers)
            .build()
            .map_err(|err| ModelRunError::Runtime(anyhow!("failed to build HTTP client: {err}")))?;

        let payload = json!({
            "model": model,
            "max_tokens": prompt.max_output_tokens() as u32,
            "temperature": 0.2,
            "messages": [
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": prompt.user_message()}
            ]
        });

        let mut spinner = ThinkingSpinner::start();
        let response = client
            .post(ENDPOINT)
            .json(&payload)
            .send()
            .map_err(|err| ModelRunError::Runtime(anyhow!("xAI request failed: {err}")))?
            .error_for_status()
            .map_err(|err| ModelRunError::Runtime(anyhow!("xAI returned error status: {err}")))?;

        let body: XaiResponse = response.json().map_err(|err| {
            ModelRunError::Runtime(anyhow!("failed to decode xAI response: {err}"))
        })?;
        spinner.stop();

        let text = body
            .choices
            .first()
            .and_then(|c| c.message.content.as_ref())
            .map(|s| s.trim().to_string())
            .unwrap_or_else(|| "No answer returned by Grok.".to_string());

        let usage = body.usage.as_ref().map(|u| TokenUsage {
            input_tokens: u.prompt_tokens,
            output_tokens: u.completion_tokens,
            total_tokens: u
                .total_tokens
                .unwrap_or(u.prompt_tokens + u.completion_tokens),
            cost_usd: calculate_cost(model, u.prompt_tokens, u.completion_tokens),
        });

        Ok(ProviderResult {
            answer: text,
            usage,
        })
    }

    #[derive(Debug, Deserialize)]
    struct XaiResponse {
        #[serde(default)]
        choices: Vec<XaiChoice>,
        #[serde(default)]
        usage: Option<XaiUsage>,
    }

    #[derive(Debug, Deserialize)]
    struct XaiChoice {
        message: XaiMessage,
    }

    #[derive(Debug, Deserialize)]
    struct XaiMessage {
        content: Option<String>,
    }

    #[derive(Debug, Deserialize)]
    struct XaiUsage {
        prompt_tokens: u32,
        completion_tokens: u32,
        total_tokens: Option<u32>,
    }
}

mod groq {
    use super::{
        ModelRunError, PromptParts, ProviderResult, SYSTEM_PROMPT, ThinkingSpinner, TokenUsage,
        calculate_cost,
    };
    use anyhow::anyhow;
    use reqwest::blocking::Client;
    use reqwest::header::{AUTHORIZATION, CONTENT_TYPE, HeaderMap, HeaderValue};
    use serde::Deserialize;
    use serde_json::json;

    const ENDPOINT: &str = "https://api.groq.com/openai/v1/chat/completions";

    pub(super) fn run(
        model: &str,
        prompt: &PromptParts,
        override_key: Option<&str>,
        system_prompt_override: Option<&str>,
    ) -> Result<ProviderResult, ModelRunError> {
        let system_prompt = system_prompt_override.unwrap_or(SYSTEM_PROMPT);
        let key = override_key
            .map(|value| value.to_string())
            .or_else(|| std::env::var("GROQ_API_KEY").ok())
            .ok_or_else(|| {
                ModelRunError::Runtime(anyhow!(
                    "GROQ_API_KEY environment variable is required for Groq models"
                ))
            })?;

        let mut headers = HeaderMap::new();
        headers.insert(
            AUTHORIZATION,
            HeaderValue::from_str(&format!("Bearer {key}")).map_err(|err| {
                ModelRunError::Runtime(anyhow!("invalid GROQ_API_KEY header value: {err}"))
            })?,
        );
        headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));

        let client = Client::builder()
            .timeout(std::time::Duration::from_secs(60))
            .default_headers(headers)
            .build()
            .map_err(|err| ModelRunError::Runtime(anyhow!("failed to build HTTP client: {err}")))?;

        let payload = json!({
            "model": model,
            "max_tokens": prompt.max_output_tokens() as u32,
            "temperature": 0.2,
            "messages": [
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": prompt.user_message()}
            ]
        });

        let mut spinner = ThinkingSpinner::start();
        let response = client
            .post(ENDPOINT)
            .json(&payload)
            .send()
            .map_err(|err| ModelRunError::Runtime(anyhow!("Groq request failed: {err}")))?
            .error_for_status()
            .map_err(|err| ModelRunError::Runtime(anyhow!("Groq returned error status: {err}")))?;

        let body: GroqResponse = response.json().map_err(|err| {
            ModelRunError::Runtime(anyhow!("failed to decode Groq response: {err}"))
        })?;
        spinner.stop();

        let text = body
            .choices
            .first()
            .and_then(|c| c.message.content.as_ref())
            .map(|s| s.trim().to_string())
            .unwrap_or_else(|| "No answer returned by Groq.".to_string());

        let usage = body.usage.as_ref().map(|u| TokenUsage {
            input_tokens: u.prompt_tokens,
            output_tokens: u.completion_tokens,
            total_tokens: u
                .total_tokens
                .unwrap_or(u.prompt_tokens + u.completion_tokens),
            cost_usd: calculate_cost(model, u.prompt_tokens, u.completion_tokens),
        });

        Ok(ProviderResult {
            answer: text,
            usage,
        })
    }

    #[derive(Debug, Deserialize)]
    struct GroqResponse {
        #[serde(default)]
        choices: Vec<GroqChoice>,
        #[serde(default)]
        usage: Option<GroqUsage>,
    }

    #[derive(Debug, Deserialize)]
    struct GroqChoice {
        message: GroqMessage,
    }

    #[derive(Debug, Deserialize)]
    struct GroqMessage {
        content: Option<String>,
    }

    #[derive(Debug, Deserialize)]
    struct GroqUsage {
        prompt_tokens: u32,
        completion_tokens: u32,
        total_tokens: Option<u32>,
    }
}

mod mistral {
    use super::{
        ModelRunError, PromptParts, ProviderResult, SYSTEM_PROMPT, ThinkingSpinner, TokenUsage,
        calculate_cost,
    };
    use anyhow::anyhow;
    use reqwest::blocking::Client;
    use reqwest::header::{AUTHORIZATION, CONTENT_TYPE, HeaderMap, HeaderValue};
    use serde::Deserialize;
    use serde_json::json;

    const ENDPOINT: &str = "https://api.mistral.ai/v1/chat/completions";

    pub(super) fn run(
        model: &str,
        prompt: &PromptParts,
        override_key: Option<&str>,
        system_prompt_override: Option<&str>,
    ) -> Result<ProviderResult, ModelRunError> {
        let system_prompt = system_prompt_override.unwrap_or(SYSTEM_PROMPT);
        let key = override_key
            .map(|value| value.to_string())
            .or_else(|| std::env::var("MISTRAL_API_KEY").ok())
            .ok_or_else(|| {
                ModelRunError::Runtime(anyhow!(
                    "MISTRAL_API_KEY environment variable is required for Mistral models"
                ))
            })?;

        let mut headers = HeaderMap::new();
        headers.insert(
            AUTHORIZATION,
            HeaderValue::from_str(&format!("Bearer {key}")).map_err(|err| {
                ModelRunError::Runtime(anyhow!("invalid MISTRAL_API_KEY header value: {err}"))
            })?,
        );
        headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));

        let client = Client::builder()
            .timeout(std::time::Duration::from_secs(60))
            .default_headers(headers)
            .build()
            .map_err(|err| ModelRunError::Runtime(anyhow!("failed to build HTTP client: {err}")))?;

        let payload = json!({
            "model": model,
            "max_tokens": prompt.max_output_tokens() as u32,
            "temperature": 0.2,
            "messages": [
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": prompt.user_message()}
            ]
        });

        let mut spinner = ThinkingSpinner::start();
        let response = client
            .post(ENDPOINT)
            .json(&payload)
            .send()
            .map_err(|err| ModelRunError::Runtime(anyhow!("Mistral request failed: {err}")))?
            .error_for_status()
            .map_err(|err| {
                ModelRunError::Runtime(anyhow!("Mistral returned error status: {err}"))
            })?;

        let body: MistralResponse = response.json().map_err(|err| {
            ModelRunError::Runtime(anyhow!("failed to decode Mistral response: {err}"))
        })?;
        spinner.stop();

        let text = body
            .choices
            .first()
            .and_then(|c| c.message.content.as_ref())
            .map(|s| s.trim().to_string())
            .unwrap_or_else(|| "No answer returned by Mistral.".to_string());

        let usage = body.usage.as_ref().map(|u| TokenUsage {
            input_tokens: u.prompt_tokens,
            output_tokens: u.completion_tokens,
            total_tokens: u
                .total_tokens
                .unwrap_or(u.prompt_tokens + u.completion_tokens),
            cost_usd: calculate_cost(model, u.prompt_tokens, u.completion_tokens),
        });

        Ok(ProviderResult {
            answer: text,
            usage,
        })
    }

    #[derive(Debug, Deserialize)]
    struct MistralResponse {
        #[serde(default)]
        choices: Vec<MistralChoice>,
        #[serde(default)]
        usage: Option<MistralUsage>,
    }

    #[derive(Debug, Deserialize)]
    struct MistralChoice {
        message: MistralMessage,
    }

    #[derive(Debug, Deserialize)]
    struct MistralMessage {
        content: Option<String>,
    }

    #[derive(Debug, Deserialize)]
    struct MistralUsage {
        prompt_tokens: u32,
        completion_tokens: u32,
        total_tokens: Option<u32>,
    }
}

// ============================================================================
// Entity Extraction API
// ============================================================================

/// Default system prompt for entity extraction
pub const ENTITY_EXTRACTION_PROMPT: &str = r#"Extract named entities from the provided text. Return a JSON object with an "entities" array.

Each entity should have:
- "name": The entity name as it appears in the text
- "type": One of "PERSON", "ORG", "LOCATION", "DATE", "PRODUCT", "EVENT", or "OTHER"
- "confidence": A number between 0.0 and 1.0 indicating your confidence

Guidelines:
1. Only include entities you're confident about (confidence >= 0.7)
2. Preserve the original capitalization of entity names
3. For organizations, include full names (e.g., "S&P Global" not just "S&P")
4. For people, include full names when available
5. Deduplicate: if an entity appears multiple times, include it only once

Return format:
{"entities": [{"name": "...", "type": "...", "confidence": 0.9}, ...]}"#;

/// Extracted entity from text
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ExtractedEntity {
    pub name: String,
    #[serde(rename = "type")]
    pub entity_type: String,
    pub confidence: f32,
}

/// Response from entity extraction
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct EntityExtractionResponse {
    pub entities: Vec<ExtractedEntity>,
    pub model: String,
    pub text_chars: usize,
}

/// Extract entities from text using an LLM
///
/// # Arguments
/// * `model` - Model identifier (e.g., "openai:gpt-4o-mini", "claude:claude-3-5-sonnet")
/// * `text` - The text to extract entities from
/// * `system_prompt` - Optional custom system prompt (uses default if None)
/// * `api_key` - Optional API key (uses environment variable if None)
///
/// # Returns
/// An `EntityExtractionResponse` with the extracted entities
///
/// # Example
/// ```ignore
/// let response = extract_entities(
///     "openai:gpt-4o-mini",
///     "John Smith met with Microsoft CEO Satya Nadella in Seattle.",
///     None,  // use default prompt
///     None,  // use OPENAI_API_KEY env var
/// )?;
/// for entity in response.entities {
///     println!("{}: {} ({:.0}%)", entity.name, entity.entity_type, entity.confidence * 100.0);
/// }
/// ```
pub fn extract_entities(
    model: &str,
    text: &str,
    system_prompt: Option<&str>,
    api_key: Option<&str>,
) -> Result<EntityExtractionResponse, ModelRunError> {
    let prompt = system_prompt.unwrap_or(ENTITY_EXTRACTION_PROMPT);
    let text_chars = text.len();

    // Determine model provider and make API call
    let (provider, model_name) = parse_model_spec(model);

    let json_response = match provider.as_str() {
        "openai" => extract_entities_openai(&model_name, text, prompt, api_key)?,
        "claude" | "anthropic" => extract_entities_claude(&model_name, text, prompt, api_key)?,
        "gemini" | "google" => extract_entities_gemini(&model_name, text, prompt, api_key)?,
        _ => {
            return Err(ModelRunError::UnsupportedModel(format!(
                "Entity extraction not supported for provider '{}'. Use openai:, claude:, or gemini:",
                provider
            )));
        }
    };

    // Parse the JSON response
    let entities = parse_entity_response(&json_response)?;

    Ok(EntityExtractionResponse {
        entities,
        model: model.to_string(),
        text_chars,
    })
}

fn parse_model_spec(model: &str) -> (String, String) {
    if let Some((provider, name)) = model.split_once(':') {
        (provider.to_lowercase(), name.to_string())
    } else {
        // Default to OpenAI if no provider specified
        ("openai".to_string(), model.to_string())
    }
}

fn parse_entity_response(json_str: &str) -> Result<Vec<ExtractedEntity>, ModelRunError> {
    // Try to parse the response, handling various formats
    let trimmed = json_str.trim();

    // Handle markdown code blocks
    let clean_json = if trimmed.starts_with("```json") {
        trimmed
            .strip_prefix("```json")
            .and_then(|s| s.strip_suffix("```"))
            .unwrap_or(trimmed)
            .trim()
    } else if trimmed.starts_with("```") {
        trimmed
            .strip_prefix("```")
            .and_then(|s| s.strip_suffix("```"))
            .unwrap_or(trimmed)
            .trim()
    } else {
        trimmed
    };

    // Try parsing as {"entities": [...]}
    #[derive(serde::Deserialize)]
    struct EntityResponse {
        entities: Vec<ExtractedEntity>,
    }

    if let Ok(response) = serde_json::from_str::<EntityResponse>(clean_json) {
        return Ok(response.entities);
    }

    // Try parsing as a direct array [...]
    if let Ok(entities) = serde_json::from_str::<Vec<ExtractedEntity>>(clean_json) {
        return Ok(entities);
    }

    Err(ModelRunError::Runtime(anyhow::anyhow!(
        "Failed to parse entity extraction response as JSON: {}",
        &clean_json[..clean_json.len().min(200)]
    )))
}

fn extract_entities_openai(
    model: &str,
    text: &str,
    system_prompt: &str,
    api_key: Option<&str>,
) -> Result<String, ModelRunError> {
    use serde_json::json;

    let api_key = api_key
        .map(|s| s.to_string())
        .or_else(|| std::env::var("OPENAI_API_KEY").ok())
        .ok_or_else(|| {
            ModelRunError::Runtime(anyhow::anyhow!(
                "OpenAI API key required. Set OPENAI_API_KEY or pass api_key parameter."
            ))
        })?;

    let model_name = if model.is_empty() {
        "gpt-4o-mini"
    } else {
        model
    };

    let client = reqwest::blocking::Client::builder()
        .no_proxy()
        .build()
        .map_err(|err| {
            ModelRunError::Runtime(anyhow::anyhow!("failed to build HTTP client: {err}"))
        })?;
    let payload = json!({
        "model": model_name,
        "messages": [
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": text}
        ],
        "response_format": {"type": "json_object"},
        "temperature": 0.1
    });

    let response = client
        .post("https://api.openai.com/v1/chat/completions")
        .header("Authorization", format!("Bearer {}", api_key))
        .json(&payload)
        .send()
        .map_err(|e| ModelRunError::Runtime(anyhow::anyhow!("OpenAI request failed: {}", e)))?
        .error_for_status()
        .map_err(|e| ModelRunError::Runtime(anyhow::anyhow!("OpenAI returned error: {}", e)))?;

    #[derive(serde::Deserialize)]
    struct OpenAIResponse {
        choices: Vec<OpenAIChoice>,
    }
    #[derive(serde::Deserialize)]
    struct OpenAIChoice {
        message: OpenAIMessage,
    }
    #[derive(serde::Deserialize)]
    struct OpenAIMessage {
        content: String,
    }

    let body: OpenAIResponse = response.json().map_err(|e| {
        ModelRunError::Runtime(anyhow::anyhow!("Failed to parse OpenAI response: {}", e))
    })?;

    body.choices
        .into_iter()
        .next()
        .map(|c| c.message.content)
        .ok_or_else(|| ModelRunError::Runtime(anyhow::anyhow!("No response from OpenAI")))
}

fn extract_entities_claude(
    model: &str,
    text: &str,
    system_prompt: &str,
    api_key: Option<&str>,
) -> Result<String, ModelRunError> {
    use serde_json::json;

    let api_key = api_key
        .map(|s| s.to_string())
        .or_else(|| std::env::var("ANTHROPIC_API_KEY").ok())
        .ok_or_else(|| {
            ModelRunError::Runtime(anyhow::anyhow!(
                "Anthropic API key required. Set ANTHROPIC_API_KEY or pass api_key parameter."
            ))
        })?;

    let model_name = if model.is_empty() {
        "claude-3-5-sonnet-20241022"
    } else {
        model
    };

    let client = reqwest::blocking::Client::builder()
        .no_proxy()
        .build()
        .map_err(|err| {
            ModelRunError::Runtime(anyhow::anyhow!("failed to build HTTP client: {err}"))
        })?;
    let payload = json!({
        "model": model_name,
        "max_tokens": 4096,
        "system": format!("{}\n\nRespond with valid JSON only.", system_prompt),
        "messages": [
            {"role": "user", "content": text}
        ]
    });

    let response = client
        .post("https://api.anthropic.com/v1/messages")
        .header("x-api-key", &api_key)
        .header("anthropic-version", "2023-06-01")
        .header("content-type", "application/json")
        .json(&payload)
        .send()
        .map_err(|e| ModelRunError::Runtime(anyhow::anyhow!("Claude request failed: {}", e)))?
        .error_for_status()
        .map_err(|e| ModelRunError::Runtime(anyhow::anyhow!("Claude returned error: {}", e)))?;

    #[derive(serde::Deserialize)]
    struct ClaudeResponse {
        content: Vec<ClaudeContent>,
    }
    #[derive(serde::Deserialize)]
    struct ClaudeContent {
        text: Option<String>,
    }

    let body: ClaudeResponse = response.json().map_err(|e| {
        ModelRunError::Runtime(anyhow::anyhow!("Failed to parse Claude response: {}", e))
    })?;

    body.content
        .into_iter()
        .find_map(|c| c.text)
        .ok_or_else(|| ModelRunError::Runtime(anyhow::anyhow!("No text response from Claude")))
}

fn extract_entities_gemini(
    model: &str,
    text: &str,
    system_prompt: &str,
    api_key: Option<&str>,
) -> Result<String, ModelRunError> {
    use serde_json::json;

    let api_key = api_key
        .map(|s| s.to_string())
        .or_else(|| std::env::var("GEMINI_API_KEY").ok())
        .or_else(|| std::env::var("GOOGLE_API_KEY").ok())
        .ok_or_else(|| {
            ModelRunError::Runtime(anyhow::anyhow!(
                "Gemini API key required. Set GEMINI_API_KEY or pass api_key parameter."
            ))
        })?;

    let model_name = if model.is_empty() {
        "gemini-2.0-flash"
    } else {
        model
    };
    let url = format!(
        "https://generativelanguage.googleapis.com/v1beta/models/{}:generateContent?key={}",
        model_name, api_key
    );

    let client = reqwest::blocking::Client::builder()
        .no_proxy()
        .build()
        .map_err(|err| {
            ModelRunError::Runtime(anyhow::anyhow!("failed to build HTTP client: {err}"))
        })?;
    let payload = json!({
        "contents": [{
            "parts": [{"text": format!("{}\n\nText to analyze:\n{}", system_prompt, text)}]
        }],
        "generationConfig": {
            "temperature": 0.1,
            "responseMimeType": "application/json"
        }
    });

    let response = client
        .post(&url)
        .json(&payload)
        .send()
        .map_err(|e| ModelRunError::Runtime(anyhow::anyhow!("Gemini request failed: {}", e)))?
        .error_for_status()
        .map_err(|e| ModelRunError::Runtime(anyhow::anyhow!("Gemini returned error: {}", e)))?;

    #[derive(serde::Deserialize)]
    struct GeminiResponse {
        candidates: Vec<GeminiCandidate>,
    }
    #[derive(serde::Deserialize)]
    struct GeminiCandidate {
        content: GeminiContent,
    }
    #[derive(serde::Deserialize)]
    struct GeminiContent {
        parts: Vec<GeminiPart>,
    }
    #[derive(serde::Deserialize)]
    struct GeminiPart {
        text: Option<String>,
    }

    let body: GeminiResponse = response.json().map_err(|e| {
        ModelRunError::Runtime(anyhow::anyhow!("Failed to parse Gemini response: {}", e))
    })?;

    body.candidates
        .into_iter()
        .next()
        .and_then(|c| c.content.parts.into_iter().find_map(|p| p.text))
        .ok_or_else(|| ModelRunError::Runtime(anyhow::anyhow!("No text response from Gemini")))
}

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

    static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

    #[test]
    fn normalize_models() {
        assert_eq!(normalize_openai_model(None), "gpt-4o-mini");
        assert_eq!(
            normalize_nvidia_model(Some("meta/llama3-8b-instruct".to_string())),
            "meta/llama3-8b-instruct"
        );
        let _lock = ENV_LOCK.lock().unwrap();
        unsafe {
            std::env::remove_var("NVIDIA_LLM_MODEL");
            std::env::remove_var("NVIDIA_MODEL");
        }
        assert_eq!(normalize_nvidia_model(None), "");
        assert_eq!(normalize_gemini_model(None), "gemini-2.5-flash");
        assert_eq!(normalize_claude_model(None), "claude-sonnet-4-5");
        assert_eq!(normalize_xai_model(None), "grok-4-fast");
        assert_eq!(normalize_groq_model(None), "llama-3.3-70b-versatile");
        assert_eq!(normalize_mistral_model(None), "mistral-large-latest");
    }

    #[test]
    fn parse_entity_json() {
        let json = r#"{"entities": [{"name": "John", "type": "PERSON", "confidence": 0.95}]}"#;
        let entities = parse_entity_response(json).unwrap();
        assert_eq!(entities.len(), 1);
        assert_eq!(entities[0].name, "John");
    }

    #[test]
    fn parse_entity_json_with_markdown() {
        let json = r#"```json
{"entities": [{"name": "Microsoft", "type": "ORG", "confidence": 0.99}]}
```"#;
        let entities = parse_entity_response(json).unwrap();
        assert_eq!(entities.len(), 1);
        assert_eq!(entities[0].name, "Microsoft");
    }

    #[test]
    fn parse_model_spec_test() {
        let (provider, model) = parse_model_spec("openai:gpt-4o");
        assert_eq!(provider, "openai");
        assert_eq!(model, "gpt-4o");

        let (provider, model) = parse_model_spec("gpt-4o-mini");
        assert_eq!(provider, "openai");
        assert_eq!(model, "gpt-4o-mini");
    }

    #[test]
    fn modelkind_parses_ghost_spec() {
        let kind = ModelKind::parse("ghost:/tmp/model.ghostpack").unwrap();
        match kind {
            ModelKind::Ghost { pack_path } => {
                assert_eq!(pack_path, PathBuf::from("/tmp/model.ghostpack"));
            }
            other => panic!("expected ghost, got {other:?}"),
        }
    }

    #[test]
    fn run_model_inference_ghost_returns_unsupported() {
        let hit = SearchHit {
            rank: 0,
            frame_id: 0,
            uri: "mv2://test".to_string(),
            title: Some("Test".to_string()),
            range: (0, 3),
            text: "ctx".to_string(),
            matches: 1,
            chunk_range: None,
            chunk_text: None,
            score: Some(1.0),
            metadata: None,
        };

        let err = run_model_inference(
            "ghost:/tmp/fake.ghostpack",
            "hello?",
            "",
            &[hit],
            None,
            None,
            None,
        )
        .unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("ghost") || msg.contains("Ghost"),
            "error should mention ghost: {msg}"
        );
    }

    #[test]
    fn normalize_question_adds_question_mark() {
        // Should add ? to questions without punctuation
        // Note: abbreviation expansion may also occur (IRR -> IRR (internal rate of return))
        let result = normalize_question("how much is the LP rate");
        assert!(result.ends_with('?'), "should end with ?");

        assert_eq!(
            normalize_question("what is the total revenue"),
            "what is the total revenue?"
        );
        assert_eq!(
            normalize_question("where does John live"),
            "where does John live?"
        );
        assert_eq!(normalize_question("is this correct"), "is this correct?");
        assert_eq!(normalize_question("can you help me"), "can you help me?");
    }

    #[test]
    fn normalize_question_preserves_existing_punctuation() {
        // Should NOT modify queries that already have punctuation
        assert_eq!(normalize_question("how much is X?"), "how much is X?");
        assert_eq!(
            normalize_question("Tell me about the project."),
            "Tell me about the project."
        );
        assert_eq!(normalize_question("Do it now!"), "Do it now!");
    }

    #[test]
    fn normalize_question_ignores_non_questions() {
        // Should NOT add ? to non-question statements
        assert_eq!(
            normalize_question("revenue for Q1 2024"),
            "revenue for Q1 2024"
        );
        assert_eq!(normalize_question("total sales"), "total sales");
        // Should not match partial words
        assert_eq!(
            normalize_question("howitzer specifications"),
            "howitzer specifications"
        );
    }

    #[test]
    fn normalize_question_handles_edge_cases() {
        assert_eq!(normalize_question(""), "");
        assert_eq!(normalize_question("  "), "");
        // Note: typo correction and expansion happen, so result may differ
        let result = normalize_question("  how much  ");
        assert!(result.ends_with('?'), "should end with ?");
    }

    #[test]
    fn fix_typos_corrects_common_errors() {
        assert!(fix_common_typos("teh quick brown fox").contains("the"));
        assert!(fix_common_typos("waht is this").contains("what"));
        assert!(fix_common_typos("totla revenue").contains("total"));
    }

    #[test]
    fn expand_abbreviations_works() {
        // Test that abbreviations are expanded
        let result = expand_abbreviations("what is the irr");
        assert!(result.contains("internal rate of return") || result.contains("irr"));
    }

    #[test]
    fn question_type_detection() {
        assert_eq!(
            detect_question_type("how much is X?"),
            QuestionType::Numeric
        );
        assert_eq!(
            detect_question_type("is this correct?"),
            QuestionType::YesNo
        );
        assert_eq!(detect_question_type("list all items"), QuestionType::List);
        assert_eq!(
            detect_question_type("when was it created?"),
            QuestionType::Temporal
        );
        assert_eq!(
            detect_question_type("why did this happen?"),
            QuestionType::Explanation
        );
        assert_eq!(
            detect_question_type("what is the name?"),
            QuestionType::Factual
        );
    }

    #[test]
    fn postprocess_removes_artifacts() {
        let answer = "Based on the provided context, the value is 42.";
        let processed = postprocess_answer(answer);
        assert!(!processed.starts_with("Based on"));
        assert!(processed.contains("42"));
    }

    #[test]
    fn postprocess_capitalizes() {
        let answer = "the answer is yes";
        let processed = postprocess_answer(answer);
        assert!(processed.starts_with('T'), "should start with capital T");
    }

    #[test]
    fn postprocess_normalizes_whitespace() {
        let answer = "too    many     spaces    here";
        let processed = postprocess_answer(answer);
        assert!(!processed.contains("  "), "should not have double spaces");
    }
}