car-inference 0.14.0

Local model inference for CAR — Candle backend with Qwen3 models
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
//! Protocol abstraction — unified interface for remote model API providers.
//!
//! Each provider (OpenAI, Anthropic, Google) implements `ProtocolHandler`,
//! which owns message formatting, request building, response parsing, and
//! streaming. RemoteBackend becomes a thin dispatcher.

use crate::stream::StreamEvent;
use crate::tasks::generate::{ContentBlock, Message, ResponseFormat, ToolCall};
use crate::InferenceError;
use serde_json::Value;
use std::collections::HashMap;

/// Unified request parameters for any protocol.
#[derive(Debug, Clone)]
pub struct ApiRequest {
    pub model: String,
    pub messages: Vec<Value>,
    pub system: Option<String>,
    pub temperature: f64,
    pub max_tokens: usize,
    pub tools: Option<Vec<Value>>,
    pub tool_choice: Option<String>,
    pub parallel_tool_calls: Option<bool>,
    pub stream: bool,
    pub budget_tokens: usize,
    /// When true, mark system prompt and tool definitions for Anthropic prompt caching.
    /// Enables cache reuse across parent/child agent calls sharing the same prefix.
    pub cache_control: bool,
    /// Optional schema constraint on the response. Each handler maps
    /// this to its provider's native field — see `ResponseFormat`.
    pub response_format: Option<ResponseFormat>,
}

/// Unified response from any protocol.
#[derive(Debug, Clone)]
pub struct ApiResponse {
    pub text: String,
    pub tool_calls: Vec<ToolCall>,
    /// Token usage statistics parsed from the API response.
    pub usage: Option<crate::TokenUsage>,
}

/// Protocol handler trait — each provider implements this.
pub trait ProtocolHandler: Send + Sync {
    /// URL path for the API endpoint (appended to the base URL).
    fn endpoint_path(&self) -> &str;

    /// Build HTTP headers for authentication.
    fn auth_headers(&self, api_key: &str) -> Vec<(String, String)>;

    /// Build the JSON request body from unified ApiRequest.
    fn build_request_body(&self, req: &ApiRequest) -> Value;

    /// Parse a complete (non-streaming) response body into ApiResponse.
    fn parse_response(&self, body: &str) -> Result<ApiResponse, InferenceError>;

    /// Parse a single SSE line into StreamEvents (for streaming responses).
    /// Returns multiple events when a single SSE chunk contains multiple tool calls.
    fn parse_stream_event(&self, event_type: &str, data: &str) -> Vec<StreamEvent>;

    /// Convert conversation Messages to this protocol's message format.
    fn build_messages(
        &self,
        messages: &[Message],
        prompt: &str,
        context: Option<&str>,
        images: Option<&[ContentBlock]>,
    ) -> (Vec<Value>, Option<String>);

    /// Convert tool definitions to this protocol's format.
    fn build_tools(&self, tools: &[Value]) -> Vec<Value>;

    /// Whether this protocol supports streaming.
    fn supports_streaming(&self) -> bool {
        true
    }

    /// Whether this protocol supports extended thinking.
    fn supports_thinking(&self) -> bool {
        false
    }

    /// Whether this protocol accepts video content blocks natively on
    /// the generation endpoint. Defaults to `false`. When a request
    /// carries a video block and the selected protocol returns
    /// `false`, `RemoteBackend::execute_request` rejects the call
    /// with [`InferenceError::UnsupportedMode`] rather than silently
    /// downgrading the video to a text placeholder.
    fn supports_video(&self) -> bool {
        false
    }

    /// Whether this protocol accepts audio content blocks natively.
    /// Same contract as [`supports_video`] — false means the remote
    /// backend pre-check returns `UnsupportedMode` instead of
    /// silently stringifying the audio reference.
    fn supports_audio(&self) -> bool {
        false
    }

    /// Backend identifier used in `UnsupportedMode` error messages.
    /// Handlers override to something recognizable (e.g. "openai",
    /// "anthropic-messages-v1").
    fn protocol_name(&self) -> &'static str {
        "remote"
    }
}

// ---------------------------------------------------------------------------
// OpenAI-compatible handler (also covers OpenAI Responses API for chat)
// ---------------------------------------------------------------------------

pub struct OpenAiHandler;

impl ProtocolHandler for OpenAiHandler {
    fn endpoint_path(&self) -> &str {
        "/v1/chat/completions"
    }

    fn auth_headers(&self, api_key: &str) -> Vec<(String, String)> {
        vec![
            ("Authorization".into(), format!("Bearer {}", api_key)),
            ("Content-Type".into(), "application/json".into()),
        ]
    }

    fn build_request_body(&self, req: &ApiRequest) -> Value {
        let quirks = openai_quirks(&req.model);
        let mut body = serde_json::json!({
            "model": req.model,
            "messages": req.messages,
        });
        if quirks.uses_max_completion_tokens {
            body["max_completion_tokens"] = serde_json::json!(req.max_tokens);
        } else {
            body["max_tokens"] = serde_json::json!(req.max_tokens);
        }
        if req.temperature >= 0.0 && !quirks.rejects_temperature {
            body["temperature"] = serde_json::json!(req.temperature);
        }
        if let Some(ref tools) = req.tools {
            body["tools"] = serde_json::json!(tools);
            body["tool_choice"] = serde_json::json!(req.tool_choice.as_deref().unwrap_or("auto"));
            if let Some(parallel_tool_calls) = req.parallel_tool_calls {
                body["parallel_tool_calls"] = serde_json::json!(parallel_tool_calls);
            }
        }
        // Structured output. OpenAI's strict mode enforces the schema on
        // the response; the looser `json_object` form just demands valid
        // JSON. Schema name defaults to "response" — within the 64-char
        // alphanumeric/-_ constraint that OpenAI imposes on this field.
        match &req.response_format {
            Some(ResponseFormat::JsonSchema {
                schema,
                strict,
                name,
            }) => {
                body["response_format"] = serde_json::json!({
                    "type": "json_schema",
                    "json_schema": {
                        "name": name.as_deref().unwrap_or("response"),
                        "schema": schema,
                        "strict": strict,
                    },
                });
            }
            Some(ResponseFormat::JsonObject) => {
                body["response_format"] = serde_json::json!({ "type": "json_object" });
            }
            None => {}
        }
        if req.stream {
            body["stream"] = serde_json::json!(true);
            // `stream_options.include_usage` makes OpenAI emit a final
            // choiceless chunk with real prompt/completion token counts.
            // Providers that don't recognize the flag ignore it, so
            // always-on for the streaming path is safe and matches
            // pre-refactor behavior in `RemoteBackend::generate_stream`.
            body["stream_options"] = serde_json::json!({ "include_usage": true });
        }
        body
    }

    fn parse_response(&self, body: &str) -> Result<ApiResponse, InferenceError> {
        let parsed: Value = serde_json::from_str(body)
            .map_err(|e| InferenceError::InferenceFailed(format!("parse response: {e}")))?;

        let choice = parsed
            .get("choices")
            .and_then(|c| c.as_array())
            .and_then(|a| a.first())
            .ok_or_else(|| InferenceError::InferenceFailed("empty response".into()))?;

        let message = choice
            .get("message")
            .ok_or_else(|| InferenceError::InferenceFailed("no message in choice".into()))?;

        let text = message
            .get("content")
            .and_then(|c| c.as_str())
            .unwrap_or("")
            .to_string();

        let mut tool_calls = Vec::new();
        if let Some(tcs) = message.get("tool_calls").and_then(|t| t.as_array()) {
            for tc in tcs {
                if let Some(func) = tc.get("function") {
                    let id = tc.get("id").and_then(|i| i.as_str()).map(|s| s.to_string());
                    let name = func
                        .get("name")
                        .and_then(|n| n.as_str())
                        .unwrap_or("")
                        .to_string();
                    let args_str = func
                        .get("arguments")
                        .and_then(|a| a.as_str())
                        .unwrap_or("{}");
                    let arguments: HashMap<String, Value> =
                        serde_json::from_str(args_str).unwrap_or_default();
                    tool_calls.push(ToolCall {
                        id,
                        name,
                        arguments,
                    });
                }
            }
        }

        // Parse usage statistics
        let usage = parsed.get("usage").and_then(|u| {
            Some(crate::TokenUsage {
                prompt_tokens: u.get("prompt_tokens").and_then(|v| v.as_u64()).unwrap_or(0),
                completion_tokens: u
                    .get("completion_tokens")
                    .and_then(|v| v.as_u64())
                    .unwrap_or(0),
                total_tokens: u.get("total_tokens").and_then(|v| v.as_u64()).unwrap_or(0),
                context_window: 0, // filled in by caller from model schema
            })
        });

        Ok(ApiResponse {
            text,
            tool_calls,
            usage,
        })
    }

    fn parse_stream_event(&self, _event_type: &str, data: &str) -> Vec<StreamEvent> {
        crate::stream::parse_openai_sse_line(&format!("data: {}", data))
    }

    fn build_messages(
        &self,
        messages: &[Message],
        prompt: &str,
        context: Option<&str>,
        images: Option<&[ContentBlock]>,
    ) -> (Vec<Value>, Option<String>) {
        if !messages.is_empty() {
            let mut result = Vec::new();
            if let Some(ctx) = context {
                result.push(serde_json::json!({"role": "system", "content": ctx}));
            }
            for msg in messages {
                match msg {
                    Message::System { content } => {
                        result.push(serde_json::json!({"role": "system", "content": content}));
                    }
                    Message::User { content } => {
                        result.push(serde_json::json!({"role": "user", "content": content}));
                    }
                    Message::UserMultimodal { content } => {
                        let blocks: Vec<Value> = content
                            .iter()
                            .map(|block| match block {
                                ContentBlock::Text { text } => {
                                    serde_json::json!({"type": "text", "text": text})
                                }
                                ContentBlock::ImageBase64 { data, media_type } => {
                                    serde_json::json!({
                                        "type": "image_url",
                                        "image_url": {
                                            "url": format!("data:{};base64,{}", media_type, data),
                                        }
                                    })
                                }
                                ContentBlock::ImageUrl { url, detail } => {
                                    serde_json::json!({
                                        "type": "image_url",
                                        "image_url": {
                                            "url": url,
                                            "detail": detail,
                                        }
                                    })
                                }
                                // Video / audio on OpenAI-compatible:
                                // the API has no native input for
                                // these, and `RemoteBackend` pre-
                                // rejects them via
                                // `supports_video()` / `supports_audio()`.
                                // The prior implementation produced a
                                // text placeholder here (`[video: …]`)
                                // which silently corrupted behavior
                                // for any caller that bypassed the
                                // pre-check. Panic loudly instead;
                                // tests assert `UnsupportedMode`
                                // instead of exercising this arm.
                                ContentBlock::VideoPath { .. }
                                | ContentBlock::VideoUrl { .. }
                                | ContentBlock::VideoBase64 { .. }
                                | ContentBlock::AudioPath { .. }
                                | ContentBlock::AudioUrl { .. }
                                | ContentBlock::AudioBase64 { .. } => {
                                    unreachable!(
                                        "video/audio ContentBlock reached OpenAI \
                                         build_messages — should have been rejected \
                                         by RemoteBackend::execute_request"
                                    )
                                }
                            })
                            .collect();
                        result.push(serde_json::json!({"role": "user", "content": blocks}));
                    }
                    Message::Assistant {
                        content,
                        tool_calls,
                    } => {
                        if tool_calls.is_empty() {
                            result
                                .push(serde_json::json!({"role": "assistant", "content": content}));
                        } else {
                            let tc: Vec<Value> = tool_calls.iter().enumerate().map(|(i, tc)| {
                                let id = tc.id.clone().unwrap_or_else(|| format!("call_{}", i));
                                serde_json::json!({
                                    "id": id,
                                    "type": "function",
                                    "function": {
                                        "name": tc.name,
                                        "arguments": serde_json::to_string(&tc.arguments).unwrap_or_default(),
                                    }
                                })
                            }).collect();
                            let mut msg =
                                serde_json::json!({"role": "assistant", "tool_calls": tc});
                            if !content.is_empty() {
                                msg["content"] = serde_json::json!(content);
                            }
                            result.push(msg);
                        }
                    }
                    Message::ToolResult {
                        tool_use_id,
                        content,
                    } => {
                        result.push(serde_json::json!({
                            "role": "tool",
                            "tool_call_id": tool_use_id,
                            "content": content,
                        }));
                    }
                    // OpenAI Chat Completions has no native concept
                    // of provider output items; they're a Responses
                    // API construct. Drop them silently — the items
                    // remain in the caller's stored history and the
                    // chat-completions request still has the matching
                    // assistant tool_use blocks for context.
                    Message::ProviderOutputItems { .. } => continue,
                }
            }
            (result, None) // system already in messages
        } else {
            let mut msgs = Vec::new();
            if let Some(ctx) = context {
                msgs.push(serde_json::json!({"role": "system", "content": ctx}));
            }
            if let Some(images) = images.filter(|images| !images.is_empty()) {
                let mut blocks = vec![serde_json::json!({"type": "text", "text": prompt})];
                for image in images {
                    let block = match image {
                        ContentBlock::Text { text } => {
                            serde_json::json!({"type": "text", "text": text})
                        }
                        ContentBlock::ImageBase64 { data, media_type } => {
                            serde_json::json!({
                                "type": "image_url",
                                "image_url": {
                                    "url": format!("data:{};base64,{}", media_type, data),
                                }
                            })
                        }
                        ContentBlock::ImageUrl { url, detail } => {
                            serde_json::json!({
                                "type": "image_url",
                                "image_url": {
                                    "url": url,
                                    "detail": detail,
                                }
                            })
                        }
                        ContentBlock::VideoPath { .. }
                        | ContentBlock::VideoUrl { .. }
                        | ContentBlock::VideoBase64 { .. }
                        | ContentBlock::AudioPath { .. }
                        | ContentBlock::AudioUrl { .. }
                        | ContentBlock::AudioBase64 { .. } => {
                            unreachable!(
                                "video/audio ContentBlock reached OpenAI build_messages \
                                 — should have been rejected by RemoteBackend::execute_request"
                            )
                        }
                    };
                    blocks.push(block);
                }
                msgs.push(serde_json::json!({"role": "user", "content": blocks}));
            } else {
                msgs.push(serde_json::json!({"role": "user", "content": prompt}));
            }
            (msgs, None)
        }
    }

    fn build_tools(&self, tools: &[Value]) -> Vec<Value> {
        tools
            .iter()
            .map(|t| {
                if t.get("type").is_some() {
                    t.clone()
                } else {
                    serde_json::json!({"type": "function", "function": t})
                }
            })
            .collect()
    }

    fn protocol_name(&self) -> &'static str {
        "openai"
    }
}

/// Per-model OpenAI API quirks. Centralized so the next quirk to land
/// (reasoning_effort, verbosity, ...) gets one match in one place
/// instead of a new boolean and a new `starts_with` chain at every
/// call site.
struct OpenAiQuirks {
    /// Newer models reject `max_tokens` and require `max_completion_tokens`.
    uses_max_completion_tokens: bool,
    /// o-series models reject any non-default `temperature` value.
    rejects_temperature: bool,
}

fn openai_quirks(model: &str) -> OpenAiQuirks {
    let m = model.to_lowercase();
    let is_o_series = m.starts_with("o1") || m.starts_with("o3") || m.starts_with("o4");
    OpenAiQuirks {
        uses_max_completion_tokens: is_o_series
            || m.starts_with("gpt-5")
            || m.starts_with("gpt-4.1"),
        rejects_temperature: is_o_series,
    }
}

// ---------------------------------------------------------------------------
// Anthropic handler
// ---------------------------------------------------------------------------

pub struct AnthropicHandler;

impl ProtocolHandler for AnthropicHandler {
    fn endpoint_path(&self) -> &str {
        "/v1/messages"
    }

    fn auth_headers(&self, api_key: &str) -> Vec<(String, String)> {
        vec![
            ("x-api-key".into(), api_key.to_string()),
            ("anthropic-version".into(), "2023-06-01".into()),
            ("anthropic-beta".into(), "prompt-caching-2024-07-31".into()),
            ("Content-Type".into(), "application/json".into()),
        ]
    }

    fn build_request_body(&self, req: &ApiRequest) -> Value {
        let mut body = serde_json::json!({
            "model": req.model,
            "max_tokens": req.max_tokens,
            "messages": req.messages,
        });

        if req.budget_tokens > 0 {
            body["thinking"] = serde_json::json!({
                "type": "enabled",
                "budget_tokens": req.budget_tokens,
            });
            // Anthropic requires temperature unset when thinking is enabled
        } else if req.temperature >= 0.0 {
            body["temperature"] = serde_json::json!(req.temperature);
        }

        if let Some(ref system) = req.system {
            if req.cache_control {
                // Use structured system prompt with cache_control for prompt caching
                body["system"] = serde_json::json!([{
                    "type": "text",
                    "text": system,
                    "cache_control": {"type": "ephemeral"}
                }]);
            } else {
                body["system"] = Value::String(system.clone());
            }
        }

        if let Some(ref tools) = req.tools {
            body["tools"] = Value::Array(tools.clone());
            if req.cache_control && !tools.is_empty() {
                // Mark last tool definition for cache breakpoint
                if let Some(arr) = body["tools"].as_array_mut() {
                    if let Some(last) = arr.last_mut() {
                        if let Some(obj) = last.as_object_mut() {
                            obj.insert(
                                "cache_control".to_string(),
                                serde_json::json!({"type": "ephemeral"}),
                            );
                        }
                    }
                }
            }
            body["tool_choice"] = serde_json::json!({"type": "auto"});
        }

        // Anthropic has no native response_format field as of early 2026.
        // Schema-validated output is achieved via tool_use coercion
        // (define a tool whose `input_schema` is your schema, set
        // `tool_choice: required`). Surfacing that here would conflict
        // with caller-supplied tools, so we log and ignore — the caller
        // can keep using the tool-coercion idiom that's worked all along.
        if req.response_format.is_some() {
            tracing::warn!(
                "response_format set on Anthropic request — Anthropic has no native \
                 JSON-schema field; the request will run unconstrained. Use tool_use \
                 with tool_choice=required to enforce a schema on Claude.",
            );
        }

        if req.stream {
            body["stream"] = serde_json::json!(true);
        }

        body
    }

    fn parse_response(&self, body: &str) -> Result<ApiResponse, InferenceError> {
        let parsed: Value = serde_json::from_str(body)
            .map_err(|e| InferenceError::InferenceFailed(format!("parse response: {e}")))?;

        let mut text = String::new();
        let mut tool_calls = Vec::new();
        let mut thinking_text = String::new();

        if let Some(content) = parsed.get("content").and_then(|c| c.as_array()) {
            for block in content {
                match block.get("type").and_then(|t| t.as_str()) {
                    Some("text") => {
                        if let Some(t) = block.get("text").and_then(|t| t.as_str()) {
                            text.push_str(t);
                        }
                    }
                    Some("thinking") => {
                        // Extended thinking block — capture as fallback if no text blocks exist
                        if let Some(t) = block.get("thinking").and_then(|t| t.as_str()) {
                            tracing::debug!(thinking_len = t.len(), "extended thinking block");
                            thinking_text.push_str(t);
                        }
                    }
                    Some("tool_use") => {
                        if let (Some(name), Some(input)) = (
                            block.get("name").and_then(|n| n.as_str()),
                            block.get("input"),
                        ) {
                            let id = block
                                .get("id")
                                .and_then(|i| i.as_str())
                                .map(|s| s.to_string());
                            let arguments: HashMap<String, Value> =
                                serde_json::from_value(input.clone()).unwrap_or_default();
                            tool_calls.push(ToolCall {
                                id,
                                name: name.to_string(),
                                arguments,
                            });
                        }
                    }
                    _ => {}
                }
            }
        }

        // Fallback: if the model produced only thinking blocks with no text output,
        // use the thinking content. This prevents empty responses when extended
        // thinking is auto-enabled and the model reasons entirely in thinking blocks.
        if text.is_empty() && !thinking_text.is_empty() && tool_calls.is_empty() {
            tracing::warn!(
                thinking_len = thinking_text.len(),
                "response had only thinking blocks, using thinking content as text fallback"
            );
            text = thinking_text;
        }

        // Parse usage statistics
        let usage = parsed.get("usage").and_then(|u| {
            Some(crate::TokenUsage {
                prompt_tokens: u.get("input_tokens").and_then(|v| v.as_u64()).unwrap_or(0),
                completion_tokens: u.get("output_tokens").and_then(|v| v.as_u64()).unwrap_or(0),
                total_tokens: u.get("input_tokens").and_then(|v| v.as_u64()).unwrap_or(0)
                    + u.get("output_tokens").and_then(|v| v.as_u64()).unwrap_or(0),
                context_window: 0, // filled in by caller from model schema
            })
        });

        Ok(ApiResponse {
            text,
            tool_calls,
            usage,
        })
    }

    fn parse_stream_event(&self, event_type: &str, data: &str) -> Vec<StreamEvent> {
        crate::stream::parse_anthropic_sse_line(event_type, data)
    }

    fn build_messages(
        &self,
        messages: &[Message],
        prompt: &str,
        context: Option<&str>,
        images: Option<&[ContentBlock]>,
    ) -> (Vec<Value>, Option<String>) {
        // Anthropic's `/v1/messages` takes `system` as a top-level
        // field — pushing `{"role": "system", ...}` into the messages
        // array returns a 400. Fold any `Message::System` into the
        // same slot that `context` uses; when both are present, the
        // caller-supplied context wins (it's the deliberate system
        // prompt built by the runtime) and we append the incoming
        // system message on a new line so nothing is silently dropped.
        let mut system = context.map(|c| c.to_string());
        if !messages.is_empty() {
            for msg in messages {
                if let Message::System { content } = msg {
                    system = Some(match system {
                        Some(existing) if !existing.is_empty() => {
                            format!("{existing}\n\n{content}")
                        }
                        _ => content.clone(),
                    });
                }
            }
        }

        if !messages.is_empty() {
            let mut result = Vec::new();
            for msg in messages {
                match msg {
                    // Already folded into the system field above — skip.
                    Message::System { .. } => continue,
                    Message::User { content } => {
                        result.push(serde_json::json!({"role": "user", "content": content}));
                    }
                    Message::UserMultimodal { content } => {
                        let blocks: Vec<Value> = content
                            .iter()
                            .map(|block| {
                                match block {
                                    ContentBlock::Text { text } => {
                                        serde_json::json!({"type": "text", "text": text})
                                    }
                                    ContentBlock::ImageBase64 { data, media_type } => {
                                        serde_json::json!({
                                            "type": "image",
                                            "source": {
                                                "type": "base64",
                                                "media_type": media_type,
                                                "data": data,
                                            }
                                        })
                                    }
                                    ContentBlock::ImageUrl { url, .. } => {
                                        // Anthropic uses source.url format
                                        serde_json::json!({
                                            "type": "image",
                                            "source": {
                                                "type": "url",
                                                "url": url,
                                            }
                                        })
                                    }
                                    // Anthropic's /v1/messages has no
                                    // native video or audio path.
                                    // RemoteBackend pre-rejects these;
                                    // panic here so direct callers
                                    // can't accidentally ship a silent
                                    // `[video: …]` placeholder.
                                    ContentBlock::VideoPath { .. }
                                    | ContentBlock::VideoUrl { .. }
                                    | ContentBlock::VideoBase64 { .. }
                                    | ContentBlock::AudioPath { .. }
                                    | ContentBlock::AudioUrl { .. }
                                    | ContentBlock::AudioBase64 { .. } => {
                                        unreachable!(
                                            "video/audio ContentBlock reached Anthropic \
                                             build_messages — should have been rejected \
                                             by RemoteBackend::execute_request"
                                        )
                                    }
                                }
                            })
                            .collect();
                        result.push(serde_json::json!({"role": "user", "content": blocks}));
                    }
                    Message::Assistant {
                        content,
                        tool_calls,
                    } => {
                        let mut blocks: Vec<Value> = Vec::new();
                        if !content.is_empty() {
                            blocks.push(serde_json::json!({"type": "text", "text": content}));
                        }
                        for (i, tc) in tool_calls.iter().enumerate() {
                            let id = tc.id.clone().unwrap_or_else(|| format!("toolu_{}", i));
                            blocks.push(serde_json::json!({
                                "type": "tool_use",
                                "id": id,
                                "name": tc.name,
                                "input": tc.arguments,
                            }));
                        }
                        if blocks.is_empty() {
                            blocks.push(serde_json::json!({"type": "text", "text": ""}));
                        }
                        result.push(serde_json::json!({"role": "assistant", "content": blocks}));
                    }
                    Message::ToolResult {
                        tool_use_id,
                        content,
                    } => {
                        result.push(serde_json::json!({
                            "role": "user",
                            "content": [{
                                "type": "tool_result",
                                "tool_use_id": tool_use_id,
                                "content": content,
                            }]
                        }));
                    }
                    // Provider output items are an OpenAI Responses
                    // API construct; Anthropic's `/v1/messages` has
                    // no equivalent. Drop them — the caller's stored
                    // history still has the matching tool_use blocks.
                    Message::ProviderOutputItems { .. } => continue,
                }
            }
            (result, system)
        } else {
            let content = if let Some(images) = images.filter(|images| !images.is_empty()) {
                let mut blocks = vec![serde_json::json!({"type": "text", "text": prompt})];
                for image in images {
                    let block = match image {
                        ContentBlock::Text { text } => {
                            serde_json::json!({"type": "text", "text": text})
                        }
                        ContentBlock::ImageBase64 { data, media_type } => {
                            serde_json::json!({
                                "type": "image",
                                "source": {
                                    "type": "base64",
                                    "media_type": media_type,
                                    "data": data,
                                }
                            })
                        }
                        ContentBlock::ImageUrl { url, .. } => {
                            serde_json::json!({
                                "type": "image",
                                "source": {
                                    "type": "url",
                                    "url": url,
                                }
                            })
                        }
                        ContentBlock::VideoPath { .. }
                        | ContentBlock::VideoUrl { .. }
                        | ContentBlock::VideoBase64 { .. }
                        | ContentBlock::AudioPath { .. }
                        | ContentBlock::AudioUrl { .. }
                        | ContentBlock::AudioBase64 { .. } => {
                            unreachable!(
                                "video/audio ContentBlock reached Anthropic build_messages \
                                 — should have been rejected by RemoteBackend::execute_request"
                            )
                        }
                    };
                    blocks.push(block);
                }
                Value::Array(blocks)
            } else {
                Value::String(prompt.to_string())
            };
            let msgs = vec![serde_json::json!({"role": "user", "content": content})];
            (msgs, system)
        }
    }

    fn build_tools(&self, tools: &[Value]) -> Vec<Value> {
        tools.iter().filter_map(|t| {
            let func = t.get("function").unwrap_or(t);
            Some(serde_json::json!({
                "name": func.get("name")?,
                "description": func.get("description").and_then(|d| d.as_str()).unwrap_or(""),
                "input_schema": func.get("parameters").cloned().unwrap_or(serde_json::json!({"type": "object"})),
            }))
        }).collect()
    }

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

    fn protocol_name(&self) -> &'static str {
        "anthropic"
    }
}

// ---------------------------------------------------------------------------
// Google (Gemini) handler
// ---------------------------------------------------------------------------

pub struct GoogleHandler;

impl ProtocolHandler for GoogleHandler {
    fn endpoint_path(&self) -> &str {
        ""
    } // URL is fully custom

    fn auth_headers(&self, _api_key: &str) -> Vec<(String, String)> {
        // Google uses query param auth, not headers
        vec![("Content-Type".into(), "application/json".into())]
    }

    fn build_request_body(&self, req: &ApiRequest) -> Value {
        let mut body = serde_json::json!({
            "contents": req.messages,
        });

        if let Some(ref system) = req.system {
            body["systemInstruction"] = serde_json::json!({
                "parts": [{"text": system}],
            });
        }

        let mut generation_config = serde_json::json!({
            "maxOutputTokens": req.max_tokens,
        });
        if req.temperature >= 0.0 {
            generation_config["temperature"] = serde_json::json!(req.temperature);
        }
        // Gemini exposes structured output via generationConfig.responseMimeType
        // and (optionally) responseSchema. The schema dialect is a subset
        // of JSON Schema — basic types/enums/required/object-properties.
        // We pass schemas through as-is and let the upstream validator
        // surface dialect mismatches; this keeps the field portable
        // across Gemini model versions.
        match &req.response_format {
            Some(ResponseFormat::JsonSchema { schema, .. }) => {
                generation_config["responseMimeType"] = serde_json::json!("application/json");
                generation_config["responseSchema"] = schema.clone();
            }
            Some(ResponseFormat::JsonObject) => {
                generation_config["responseMimeType"] = serde_json::json!("application/json");
            }
            None => {}
        }
        body["generationConfig"] = generation_config;

        if let Some(ref tools) = req.tools {
            body["tools"] = serde_json::json!([{
                "functionDeclarations": tools,
            }]);
            body["toolConfig"] = serde_json::json!({
                "functionCallingConfig": {
                    "mode": "AUTO",
                }
            });
        }

        body
    }

    fn parse_response(&self, body: &str) -> Result<ApiResponse, InferenceError> {
        let parsed: Value = serde_json::from_str(body)
            .map_err(|e| InferenceError::InferenceFailed(format!("parse response: {e}")))?;

        let parts = parsed
            .get("candidates")
            .and_then(|c| c.as_array())
            .and_then(|a| a.first())
            .and_then(|c| c.get("content"))
            .and_then(|c| c.get("parts"))
            .and_then(|p| p.as_array())
            .cloned()
            .unwrap_or_default();

        let mut text_chunks = Vec::new();
        let mut tool_calls = Vec::new();
        for part in parts {
            if let Some(text) = part.get("text").and_then(|t| t.as_str()) {
                text_chunks.push(text.to_string());
            }
            if let Some(function_call) = part
                .get("functionCall")
                .or_else(|| part.get("function_call"))
            {
                let name = function_call
                    .get("name")
                    .and_then(|n| n.as_str())
                    .unwrap_or_default()
                    .to_string();
                let arguments = function_call
                    .get("args")
                    .or_else(|| function_call.get("arguments"))
                    .and_then(|args| args.as_object())
                    .map(|map| {
                        map.iter()
                            .map(|(k, v)| (k.clone(), v.clone()))
                            .collect::<HashMap<_, _>>()
                    })
                    .unwrap_or_default();
                if !name.is_empty() {
                    tool_calls.push(ToolCall {
                        id: None,
                        name,
                        arguments,
                    });
                }
            }
        }

        let usage = parsed.get("usageMetadata").map(|usage| crate::TokenUsage {
            prompt_tokens: usage
                .get("promptTokenCount")
                .and_then(|v| v.as_u64())
                .unwrap_or(0),
            completion_tokens: usage
                .get("candidatesTokenCount")
                .and_then(|v| v.as_u64())
                .unwrap_or(0),
            total_tokens: usage
                .get("totalTokenCount")
                .and_then(|v| v.as_u64())
                .unwrap_or(0),
            context_window: 0,
        });

        Ok(ApiResponse {
            text: text_chunks.join("\n"),
            tool_calls,
            usage,
        })
    }

    fn parse_stream_event(&self, _event_type: &str, _data: &str) -> Vec<StreamEvent> {
        Vec::new() // Google streaming not implemented
    }

    fn build_messages(
        &self,
        messages: &[Message],
        prompt: &str,
        context: Option<&str>,
        images: Option<&[ContentBlock]>,
    ) -> (Vec<Value>, Option<String>) {
        // Fold any Message::System entries into Gemini's
        // `systemInstruction` slot (returned via the second tuple
        // element). Multiple System messages are joined — the caller-
        // supplied `context` wins when both are set.
        let mut system_instruction: Option<String> = context.map(|c| c.to_string());
        if !messages.is_empty() {
            for msg in messages {
                if let Message::System { content } = msg {
                    system_instruction = Some(match system_instruction {
                        Some(existing) if !existing.is_empty() => {
                            format!("{existing}\n\n{content}")
                        }
                        _ => content.clone(),
                    });
                }
            }
        }

        if !messages.is_empty() {
            let contents = messages
                .iter()
                // System messages are already folded into
                // system_instruction — drop them from contents[].
                // Provider output items are an OpenAI Responses
                // API construct with no Gemini equivalent — also
                // drop, the caller's stored history still has the
                // matching tool_use blocks.
                .filter(|msg| {
                    !matches!(
                        msg,
                        Message::System { .. } | Message::ProviderOutputItems { .. }
                    )
                })
                .map(|msg| match msg {
                    // Unreachable after the filter, but the compiler
                    // still needs the arms for exhaustiveness.
                    Message::System { .. } => unreachable!("System filtered above"),
                    Message::ProviderOutputItems { .. } => {
                        unreachable!("ProviderOutputItems filtered above")
                    }
                    Message::User { content } => serde_json::json!({
                        "role": "user",
                        "parts": [{"text": content}],
                    }),
                    Message::UserMultimodal { content } => serde_json::json!({
                        "role": "user",
                        "parts": content.iter().map(google_part_from_block).collect::<Vec<_>>(),
                    }),
                    Message::Assistant {
                        content,
                        tool_calls,
                    } => {
                        let mut parts = Vec::new();
                        if !content.is_empty() {
                            parts.push(serde_json::json!({"text": content}));
                        }
                        for tool_call in tool_calls {
                            parts.push(serde_json::json!({
                                "functionCall": {
                                    "name": tool_call.name,
                                    "args": tool_call.arguments,
                                }
                            }));
                        }
                        serde_json::json!({
                            "role": "model",
                            "parts": parts,
                        })
                    }
                    Message::ToolResult {
                        tool_use_id,
                        content,
                    } => serde_json::json!({
                        "role": "tool",
                        "parts": [{
                            "functionResponse": {
                                "name": tool_use_id,
                                "response": {"content": content},
                            }
                        }],
                    }),
                })
                .collect();
            (contents, system_instruction)
        } else {
            let mut parts = vec![serde_json::json!({"text": prompt})];
            if let Some(images) = images.filter(|images| !images.is_empty()) {
                parts.extend(images.iter().map(google_part_from_block));
            }
            (
                vec![serde_json::json!({
                    "role": "user",
                    "parts": parts,
                })],
                system_instruction,
            )
        }
    }

    fn build_tools(&self, tools: &[Value]) -> Vec<Value> {
        tools
            .iter()
            .filter_map(|t| {
                let func = t.get("function").unwrap_or(t);
                Some(serde_json::json!({
                    "name": func.get("name")?,
                    "description": func.get("description").and_then(|d| d.as_str()).unwrap_or(""),
                    "parameters": func.get("parameters").cloned().unwrap_or(serde_json::json!({"type": "object"})),
                }))
            })
            .collect()
    }

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

    fn supports_video(&self) -> bool {
        // Google Gemini's generateContent API accepts video parts via
        // fileData (URL) or inlineData (base64) — the only mainstream
        // multimodal provider that does, as of this writeup.
        true
    }

    fn supports_audio(&self) -> bool {
        // Gemini accepts audio natively via fileData/inlineData on
        // the same contents[] array as text/image/video.
        true
    }

    fn protocol_name(&self) -> &'static str {
        "google-gemini"
    }
}

fn google_part_from_block(block: &ContentBlock) -> Value {
    match block {
        ContentBlock::Text { text } => serde_json::json!({"text": text}),
        ContentBlock::ImageBase64 { data, media_type } => serde_json::json!({
            "inlineData": {
                "mimeType": media_type,
                "data": data,
            }
        }),
        ContentBlock::ImageUrl { url, .. } => serde_json::json!({
            "fileData": {
                "mimeType": infer_mime_type_from_url(url),
                "fileUri": url,
            }
        }),
        // Google's Gemini API does accept video fileData/inlineData, so
        // emit the native representation when we have a concrete source.
        // Base64 inline video gets the raw `data` pass-through, keyed to
        // the caller's media_type.
        ContentBlock::VideoPath { path, .. } => serde_json::json!({
            "fileData": {
                "mimeType": "video/mp4",
                "fileUri": format!("file://{path}"),
            }
        }),
        ContentBlock::VideoUrl { url, .. } => serde_json::json!({
            "fileData": {
                "mimeType": "video/mp4",
                "fileUri": url,
            }
        }),
        ContentBlock::VideoBase64 {
            data, media_type, ..
        } => serde_json::json!({
            "inlineData": {
                "mimeType": media_type,
                "data": data,
            }
        }),
        // Gemini accepts audio as fileData or inlineData, same shape
        // as video. Default mime type is wav; callers who supply an
        // explicit media_type via AudioBase64 get exact pass-through.
        ContentBlock::AudioPath { path, .. } => serde_json::json!({
            "fileData": {
                "mimeType": "audio/wav",
                "fileUri": format!("file://{path}"),
            }
        }),
        ContentBlock::AudioUrl { url, .. } => serde_json::json!({
            "fileData": {
                "mimeType": "audio/wav",
                "fileUri": url,
            }
        }),
        ContentBlock::AudioBase64 {
            data, media_type, ..
        } => serde_json::json!({
            "inlineData": {
                "mimeType": media_type,
                "data": data,
            }
        }),
    }
}

fn infer_mime_type_from_url(url: &str) -> &'static str {
    let lower = url.to_ascii_lowercase();
    if lower.ends_with(".png") {
        "image/png"
    } else if lower.ends_with(".webp") {
        "image/webp"
    } else if lower.ends_with(".heic") {
        "image/heic"
    } else if lower.ends_with(".heif") {
        "image/heif"
    } else {
        "image/jpeg"
    }
}

// ---------------------------------------------------------------------------
// Factory
// ---------------------------------------------------------------------------

/// Get the appropriate protocol handler for an API protocol.
pub fn handler_for(protocol: crate::schema::ApiProtocol) -> Box<dyn ProtocolHandler> {
    match protocol {
        crate::schema::ApiProtocol::OpenAiCompat | crate::schema::ApiProtocol::OpenAiResponses => {
            Box::new(OpenAiHandler)
        }
        crate::schema::ApiProtocol::Anthropic => Box::new(AnthropicHandler),
        crate::schema::ApiProtocol::Google => Box::new(GoogleHandler),
        crate::schema::ApiProtocol::AzureOpenAi => Box::new(AzureOpenAiHandler),
    }
}

// ---------------------------------------------------------------------------
// Azure OpenAI handler — same request format as OpenAI but different auth/URL
// ---------------------------------------------------------------------------

pub struct AzureOpenAiHandler;

impl ProtocolHandler for AzureOpenAiHandler {
    fn endpoint_path(&self) -> &str {
        // Azure uses deployment-based URLs; the actual path is built in remote.rs
        "/openai/deployments"
    }

    fn auth_headers(&self, api_key: &str) -> Vec<(String, String)> {
        vec![
            ("api-key".into(), api_key.to_string()),
            ("Content-Type".into(), "application/json".into()),
        ]
    }

    fn build_request_body(&self, req: &ApiRequest) -> Value {
        // Same as OpenAI format
        OpenAiHandler.build_request_body(req)
    }

    fn parse_response(&self, body: &str) -> Result<ApiResponse, crate::InferenceError> {
        OpenAiHandler.parse_response(body)
    }

    fn parse_stream_event(&self, event_type: &str, data: &str) -> Vec<crate::stream::StreamEvent> {
        OpenAiHandler.parse_stream_event(event_type, data)
    }

    fn build_messages(
        &self,
        messages: &[Message],
        prompt: &str,
        context: Option<&str>,
        images: Option<&[ContentBlock]>,
    ) -> (Vec<Value>, Option<String>) {
        OpenAiHandler.build_messages(messages, prompt, context, images)
    }

    fn build_tools(&self, tools: &[Value]) -> Vec<Value> {
        OpenAiHandler.build_tools(tools)
    }

    fn protocol_name(&self) -> &'static str {
        "azure-openai"
    }
}

// ---------------------------------------------------------------------------
// Google URL builder (special case — uses query param auth)
// ---------------------------------------------------------------------------

/// Build the full Google API URL with model and key.
pub fn google_url(endpoint: &str, model: &str, api_key: &str) -> String {
    let base = endpoint.trim_end_matches('/');
    format!(
        "{}/v1beta/models/{}:generateContent?key={}",
        base, model, api_key
    )
}

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

    #[test]
    fn openai_single_turn_messages() {
        let handler = OpenAiHandler;
        let (msgs, system) = handler.build_messages(&[], "Hello", Some("Be helpful"), None);
        assert_eq!(msgs.len(), 2);
        assert_eq!(msgs[0]["role"], "system");
        assert_eq!(msgs[1]["content"], "Hello");
        assert!(system.is_none()); // system is in messages
    }

    #[test]
    fn openai_multi_turn_messages() {
        let handler = OpenAiHandler;
        let messages = vec![
            Message::User {
                content: "Hi".into(),
            },
            Message::Assistant {
                content: "Hello!".into(),
                tool_calls: vec![],
            },
            Message::User {
                content: "Search for X".into(),
            },
        ];
        let (msgs, _) = handler.build_messages(&messages, "", None, None);
        assert_eq!(msgs.len(), 3);
        assert_eq!(msgs[2]["content"], "Search for X");
    }

    #[test]
    fn openai_tool_call_messages() {
        let handler = OpenAiHandler;
        let tc = ToolCall {
            id: None,
            name: "search".into(),
            arguments: [("q".into(), Value::String("rust".into()))].into(),
        };
        let messages = vec![
            Message::User {
                content: "Search".into(),
            },
            Message::Assistant {
                content: String::new(),
                tool_calls: vec![tc],
            },
            Message::ToolResult {
                tool_use_id: "call_0".into(),
                content: "found it".into(),
            },
        ];
        let (msgs, _) = handler.build_messages(&messages, "", None, None);
        assert_eq!(msgs.len(), 3);
        assert!(msgs[1].get("tool_calls").is_some());
        assert_eq!(msgs[2]["role"], "tool");
    }

    #[test]
    fn anthropic_system_separate() {
        let handler = AnthropicHandler;
        let (msgs, system) = handler.build_messages(&[], "Hello", Some("Be helpful"), None);
        assert_eq!(msgs.len(), 1); // only user message
        assert_eq!(system, Some("Be helpful".into()));
    }

    #[test]
    fn anthropic_tool_use_format() {
        let handler = AnthropicHandler;
        let tc = ToolCall {
            id: None,
            name: "search".into(),
            arguments: [("q".into(), Value::String("test".into()))].into(),
        };
        let messages = vec![
            Message::User {
                content: "Search".into(),
            },
            Message::Assistant {
                content: String::new(),
                tool_calls: vec![tc],
            },
            Message::ToolResult {
                tool_use_id: "toolu_0".into(),
                content: "result".into(),
            },
        ];
        let (msgs, _) = handler.build_messages(&messages, "", None, None);
        assert_eq!(msgs.len(), 3);
        let assistant_content = msgs[1].get("content").unwrap().as_array().unwrap();
        assert_eq!(assistant_content[0]["type"], "tool_use");
        let user_content = msgs[2].get("content").unwrap().as_array().unwrap();
        assert_eq!(user_content[0]["type"], "tool_result");
    }

    #[test]
    fn anthropic_thinking_in_request() {
        let handler = AnthropicHandler;
        let req = ApiRequest {
            model: "claude".into(),
            messages: vec![serde_json::json!({"role": "user", "content": "plan"})],
            system: None,
            temperature: 0.7,
            max_tokens: 4096,
            tools: None,
            tool_choice: None,
            parallel_tool_calls: None,
            stream: false,
            budget_tokens: 8000,
            cache_control: false,
            response_format: None,
        };
        let body = handler.build_request_body(&req);
        assert!(body.get("thinking").is_some());
        assert_eq!(body["thinking"]["budget_tokens"], 8000);
        assert!(body.get("temperature").is_none()); // removed when thinking enabled
    }

    fn empty_request(model: &str) -> ApiRequest {
        ApiRequest {
            model: model.into(),
            messages: vec![serde_json::json!({"role": "user", "content": "hi"})],
            system: None,
            temperature: 0.7,
            max_tokens: 256,
            tools: None,
            tool_choice: None,
            parallel_tool_calls: None,
            stream: false,
            budget_tokens: 0,
            cache_control: false,
            response_format: None,
        }
    }

    #[test]
    fn openai_streaming_request_carries_stream_options_include_usage() {
        // Streaming refactor (#125): build_request_body is now the
        // single source of truth for request shape. When stream=true
        // it must add `stream_options.include_usage = true` so the
        // streaming tail-chunk carries real prompt/completion token
        // counts. Without this, RemoteBackend::generate_stream loses
        // the usage signal that #79's TokenUsage parser depends on.
        let handler = OpenAiHandler;
        let mut req = empty_request("gpt-5");
        req.stream = true;
        let body = handler.build_request_body(&req);
        assert_eq!(body["stream"], serde_json::json!(true));
        assert_eq!(
            body["stream_options"]["include_usage"],
            serde_json::json!(true),
            "streaming bodies must include `stream_options.include_usage` so usage flows back"
        );
    }

    #[test]
    fn openai_non_streaming_request_omits_stream_options() {
        // Inverse — non-streaming requests must not carry stream
        // fields at all (some compatible providers reject the
        // unknown `stream_options` outside a streaming call).
        let handler = OpenAiHandler;
        let req = empty_request("gpt-5");
        let body = handler.build_request_body(&req);
        assert!(body.get("stream").is_none());
        assert!(body.get("stream_options").is_none());
    }

    #[test]
    fn anthropic_streaming_request_marks_stream_true() {
        // Sanity check that Anthropic's body also gets stream=true
        // through the same build_request_body abstraction. Anthropic
        // doesn't have a `stream_options` field — it emits usage in
        // its `message_start` and `message_delta` SSE frames already.
        let handler = AnthropicHandler;
        let mut req = empty_request("claude-opus-4-7");
        req.stream = true;
        let body = handler.build_request_body(&req);
        assert_eq!(body["stream"], serde_json::json!(true));
        assert!(
            body.get("stream_options").is_none(),
            "Anthropic has no stream_options field; usage flows via SSE frames"
        );
    }

    #[test]
    fn openai_emits_strict_json_schema_response_format() {
        let mut req = empty_request("gpt-5");
        req.response_format = Some(ResponseFormat::JsonSchema {
            schema: serde_json::json!({
                "type": "object",
                "properties": {"answer": {"type": "string"}},
                "required": ["answer"]
            }),
            strict: true,
            name: Some("answer_schema".into()),
        });
        let body = OpenAiHandler.build_request_body(&req);
        let rf = body.get("response_format").expect("response_format set");
        assert_eq!(rf["type"], "json_schema");
        assert_eq!(rf["json_schema"]["name"], "answer_schema");
        assert_eq!(rf["json_schema"]["strict"], true);
        assert_eq!(rf["json_schema"]["schema"]["required"][0], "answer");
    }

    #[test]
    fn openai_emits_json_object_when_no_schema() {
        let mut req = empty_request("gpt-4o");
        req.response_format = Some(ResponseFormat::JsonObject);
        let body = OpenAiHandler.build_request_body(&req);
        assert_eq!(body["response_format"]["type"], "json_object");
        // No json_schema sub-object on the looser variant.
        assert!(body["response_format"].get("json_schema").is_none());
    }

    #[test]
    fn openai_omits_response_format_when_none() {
        let req = empty_request("gpt-4o");
        let body = OpenAiHandler.build_request_body(&req);
        assert!(body.get("response_format").is_none());
    }

    #[test]
    fn google_emits_response_mime_and_schema() {
        let mut req = empty_request("gemini-2.5-pro");
        req.response_format = Some(ResponseFormat::JsonSchema {
            schema: serde_json::json!({"type": "object"}),
            strict: false,
            name: None,
        });
        let body = GoogleHandler.build_request_body(&req);
        let cfg = body.get("generationConfig").expect("generationConfig");
        assert_eq!(cfg["responseMimeType"], "application/json");
        assert_eq!(cfg["responseSchema"]["type"], "object");
    }

    #[test]
    fn google_json_object_skips_schema() {
        let mut req = empty_request("gemini-2.5-pro");
        req.response_format = Some(ResponseFormat::JsonObject);
        let body = GoogleHandler.build_request_body(&req);
        let cfg = body.get("generationConfig").expect("generationConfig");
        assert_eq!(cfg["responseMimeType"], "application/json");
        assert!(cfg.get("responseSchema").is_none());
    }

    #[test]
    fn anthropic_does_not_emit_response_format_field() {
        // No native response_format field on Anthropic — request must
        // fly without it (handler logs a warning, documented in the
        // ResponseFormat doc comment).
        let mut req = empty_request("claude-opus-4-7");
        req.response_format = Some(ResponseFormat::JsonSchema {
            schema: serde_json::json!({"type": "object"}),
            strict: true,
            name: None,
        });
        let body = AnthropicHandler.build_request_body(&req);
        assert!(body.get("response_format").is_none());
        assert!(body.get("responseSchema").is_none());
    }

    #[test]
    fn openai_tools_wrapped() {
        let handler = OpenAiHandler;
        let tools = vec![serde_json::json!({"name": "search", "parameters": {}})];
        let built = handler.build_tools(&tools);
        assert_eq!(built[0]["type"], "function");
        assert!(built[0].get("function").is_some());
    }

    #[test]
    fn openai_request_preserves_required_tool_choice_and_parallel_tool_calls() {
        let handler = OpenAiHandler;
        let req = ApiRequest {
            model: "gpt-5.4-mini".into(),
            messages: vec![serde_json::json!({"role": "user", "content": "extract"})],
            system: None,
            temperature: 0.0,
            max_tokens: 1024,
            tools: Some(vec![serde_json::json!({
                "type": "function",
                "function": {
                    "name": "extract_action_items",
                    "parameters": {"type": "object", "additionalProperties": false}
                }
            })]),
            tool_choice: Some("required".into()),
            parallel_tool_calls: Some(false),
            stream: false,
            budget_tokens: 0,
            cache_control: false,
            response_format: None,
        };

        let body = handler.build_request_body(&req);

        assert_eq!(body["tool_choice"], "required");
        assert_eq!(body["parallel_tool_calls"], false);
        assert_eq!(body["tools"][0]["function"]["name"], "extract_action_items");
    }

    #[test]
    fn anthropic_tools_format() {
        let handler = AnthropicHandler;
        let tools = vec![
            serde_json::json!({"function": {"name": "search", "description": "Search", "parameters": {"type": "object"}}}),
        ];
        let built = handler.build_tools(&tools);
        assert_eq!(built[0]["name"], "search");
        assert!(built[0].get("input_schema").is_some());
    }

    #[test]
    fn google_no_streaming() {
        let handler = GoogleHandler;
        assert!(!handler.supports_streaming());
    }

    #[test]
    fn anthropic_supports_thinking() {
        let handler = AnthropicHandler;
        assert!(handler.supports_thinking());
    }

    #[test]
    fn handler_factory() {
        use crate::schema::ApiProtocol;
        let h = handler_for(ApiProtocol::Anthropic);
        assert!(h.supports_thinking());

        let h = handler_for(ApiProtocol::OpenAiCompat);
        assert!(!h.supports_thinking());
    }

    #[test]
    fn openai_parse_text_response() {
        let handler = OpenAiHandler;
        let body = r#"{"choices":[{"message":{"content":"Hello world"}}]}"#;
        let resp = handler.parse_response(body).unwrap();
        assert_eq!(resp.text, "Hello world");
        assert!(resp.tool_calls.is_empty());
    }

    #[test]
    fn openai_parse_usage() {
        let handler = OpenAiHandler;
        let body = r#"{"choices":[{"message":{"content":"Hi"}}],"usage":{"prompt_tokens":10,"completion_tokens":5,"total_tokens":15}}"#;
        let resp = handler.parse_response(body).unwrap();
        let usage = resp.usage.unwrap();
        assert_eq!(usage.prompt_tokens, 10);
        assert_eq!(usage.completion_tokens, 5);
        assert_eq!(usage.total_tokens, 15);
    }

    #[test]
    fn openai_parse_multiple_tool_calls() {
        let handler = OpenAiHandler;
        let body = r#"{"choices":[{"message":{"content":"","tool_calls":[{"function":{"name":"read_file","arguments":"{\"path\":\"a.rs\"}"}},{"function":{"name":"read_file","arguments":"{\"path\":\"b.rs\"}"}}]}}]}"#;
        let resp = handler.parse_response(body).unwrap();
        assert_eq!(resp.tool_calls.len(), 2);
        assert_eq!(resp.tool_calls[0].name, "read_file");
        assert_eq!(resp.tool_calls[1].name, "read_file");
    }

    #[test]
    fn anthropic_parse_tool_response() {
        let handler = AnthropicHandler;
        let body = r#"{"content":[{"type":"text","text":"Let me search"},{"type":"tool_use","name":"search","id":"t1","input":{"q":"rust"}}]}"#;
        let resp = handler.parse_response(body).unwrap();
        assert_eq!(resp.text, "Let me search");
        assert_eq!(resp.tool_calls.len(), 1);
        assert_eq!(resp.tool_calls[0].name, "search");
    }

    #[test]
    fn anthropic_parse_usage() {
        let handler = AnthropicHandler;
        let body = r#"{"content":[{"type":"text","text":"Hi"}],"usage":{"input_tokens":12,"output_tokens":3}}"#;
        let resp = handler.parse_response(body).unwrap();
        let usage = resp.usage.unwrap();
        assert_eq!(usage.prompt_tokens, 12);
        assert_eq!(usage.completion_tokens, 3);
        assert_eq!(usage.total_tokens, 15);
    }

    #[test]
    fn anthropic_parse_multiple_tool_calls() {
        let handler = AnthropicHandler;
        let body = r#"{"content":[{"type":"text","text":"I'll read both files"},{"type":"tool_use","name":"read","id":"t1","input":{"path":"a.rs"}},{"type":"tool_use","name":"read","id":"t2","input":{"path":"b.rs"}}]}"#;
        let resp = handler.parse_response(body).unwrap();
        assert_eq!(resp.text, "I'll read both files");
        assert_eq!(resp.tool_calls.len(), 2);
        assert_eq!(resp.tool_calls[0].name, "read");
        assert_eq!(resp.tool_calls[1].name, "read");
    }

    #[test]
    fn anthropic_cache_control_system_prompt() {
        let handler = AnthropicHandler;
        let req = ApiRequest {
            model: "claude".into(),
            messages: vec![serde_json::json!({"role": "user", "content": "hello"})],
            system: Some("You are helpful.".into()),
            temperature: 0.7,
            max_tokens: 1024,
            tools: None,
            tool_choice: None,
            parallel_tool_calls: None,
            stream: false,
            budget_tokens: 0,
            cache_control: true,
            response_format: None,
        };
        let body = handler.build_request_body(&req);
        // System should be an array with cache_control block
        let system = body.get("system").unwrap();
        assert!(system.is_array());
        let blocks = system.as_array().unwrap();
        assert_eq!(blocks.len(), 1);
        assert_eq!(blocks[0]["type"], "text");
        assert_eq!(blocks[0]["text"], "You are helpful.");
        assert_eq!(blocks[0]["cache_control"]["type"], "ephemeral");
    }

    #[test]
    fn anthropic_cache_control_disabled() {
        let handler = AnthropicHandler;
        let req = ApiRequest {
            model: "claude".into(),
            messages: vec![serde_json::json!({"role": "user", "content": "hello"})],
            system: Some("You are helpful.".into()),
            temperature: 0.7,
            max_tokens: 1024,
            tools: None,
            tool_choice: None,
            parallel_tool_calls: None,
            stream: false,
            budget_tokens: 0,
            cache_control: false,
            response_format: None,
        };
        let body = handler.build_request_body(&req);
        // System should be a plain string when cache_control is false
        assert!(body.get("system").unwrap().is_string());
    }

    #[test]
    fn anthropic_cache_control_tools() {
        let handler = AnthropicHandler;
        let tools = vec![
            serde_json::json!({"name": "search", "description": "Search", "input_schema": {"type": "object"}}),
            serde_json::json!({"name": "read", "description": "Read file", "input_schema": {"type": "object"}}),
        ];
        let req = ApiRequest {
            model: "claude".into(),
            messages: vec![serde_json::json!({"role": "user", "content": "hello"})],
            system: None,
            temperature: 0.7,
            max_tokens: 1024,
            tools: Some(tools),
            tool_choice: None,
            parallel_tool_calls: None,
            stream: false,
            budget_tokens: 0,
            cache_control: true,
            response_format: None,
        };
        let body = handler.build_request_body(&req);
        let tools_arr = body["tools"].as_array().unwrap();
        // Only the last tool should have cache_control
        assert!(tools_arr[0].get("cache_control").is_none());
        assert_eq!(tools_arr[1]["cache_control"]["type"], "ephemeral");
    }

    #[test]
    fn anthropic_beta_header_included() {
        let handler = AnthropicHandler;
        let headers = handler.auth_headers("test-key");
        let beta = headers.iter().find(|(k, _)| k == "anthropic-beta");
        assert!(beta.is_some());
        assert_eq!(beta.unwrap().1, "prompt-caching-2024-07-31");
    }

    #[test]
    fn google_parse_response() {
        let handler = GoogleHandler;
        let body = r#"{"candidates":[{"content":{"parts":[{"text":"Hello from Gemini"}]}}]}"#;
        let resp = handler.parse_response(body).unwrap();
        assert_eq!(resp.text, "Hello from Gemini");
    }

    #[test]
    fn google_tools_format() {
        let handler = GoogleHandler;
        let tools = vec![serde_json::json!({
            "function": {
                "name": "search",
                "description": "Search docs",
                "parameters": {"type": "object"}
            }
        })];
        let built = handler.build_tools(&tools);
        assert_eq!(built[0]["name"], "search");
        assert!(built[0].get("parameters").is_some());
    }

    #[test]
    fn google_builds_multimodal_messages() {
        let handler = GoogleHandler;
        let messages = vec![Message::UserMultimodal {
            content: vec![
                ContentBlock::Text {
                    text: "Describe this image.".to_string(),
                },
                ContentBlock::ImageUrl {
                    url: "https://example.com/cat.jpg".to_string(),
                    detail: "auto".to_string(),
                },
            ],
        }];
        let (msgs, system) = handler.build_messages(&messages, "", Some("Be concise"), None);
        assert_eq!(msgs.len(), 1);
        assert_eq!(msgs[0]["role"], "user");
        let parts = msgs[0]["parts"].as_array().unwrap();
        assert_eq!(parts[0]["text"], "Describe this image.");
        assert!(parts[1].get("fileData").is_some());
        assert_eq!(system, Some("Be concise".to_string()));
    }

    #[test]
    fn google_request_body_includes_tools_and_system() {
        let handler = GoogleHandler;
        let req = ApiRequest {
            model: "gemini-2.5-flash".into(),
            messages: vec![serde_json::json!({
                "role": "user",
                "parts": [{"text": "Find the file and summarize it."}],
            })],
            system: Some("Use tools when needed.".into()),
            temperature: 0.2,
            max_tokens: 512,
            tools: Some(vec![serde_json::json!({
                "name": "search",
                "description": "Search files",
                "parameters": {"type": "object"}
            })]),
            tool_choice: None,
            parallel_tool_calls: None,
            stream: false,
            budget_tokens: 0,
            cache_control: false,
            response_format: None,
        };
        let body = handler.build_request_body(&req);
        assert!(body.get("systemInstruction").is_some());
        assert!(body.get("tools").is_some());
        assert_eq!(body["toolConfig"]["functionCallingConfig"]["mode"], "AUTO");
        assert_eq!(body["generationConfig"]["maxOutputTokens"], 512);
    }

    #[test]
    fn google_parse_multiple_tool_calls_and_usage() {
        let handler = GoogleHandler;
        let body = r#"{
            "candidates":[{"content":{"parts":[
                {"text":"Let me do that."},
                {"functionCall":{"name":"search","args":{"q":"rust"}}},
                {"functionCall":{"name":"read_file","args":{"path":"src/lib.rs"}}}
            ]}}],
            "usageMetadata":{"promptTokenCount":10,"candidatesTokenCount":4,"totalTokenCount":14}
        }"#;
        let resp = handler.parse_response(body).unwrap();
        assert_eq!(resp.text, "Let me do that.");
        assert_eq!(resp.tool_calls.len(), 2);
        assert_eq!(resp.tool_calls[0].name, "search");
        assert_eq!(resp.tool_calls[1].name, "read_file");
        let usage = resp.usage.unwrap();
        assert_eq!(usage.prompt_tokens, 10);
        assert_eq!(usage.completion_tokens, 4);
        assert_eq!(usage.total_tokens, 14);
    }

    #[test]
    fn openai_vision_message() {
        let handler = OpenAiHandler;
        let messages = vec![Message::UserMultimodal {
            content: vec![
                ContentBlock::Text {
                    text: "What is in this image?".to_string(),
                },
                ContentBlock::ImageUrl {
                    url: "https://example.com/cat.jpg".to_string(),
                    detail: "auto".to_string(),
                },
            ],
        }];
        let (msgs, _) = handler.build_messages(&messages, "", None, None);
        assert_eq!(msgs.len(), 1);
        let content = msgs[0]["content"].as_array().unwrap();
        assert_eq!(content.len(), 2);
        assert_eq!(content[0]["type"], "text");
        assert_eq!(content[1]["type"], "image_url");
    }

    #[test]
    fn anthropic_vision_message() {
        let handler = AnthropicHandler;
        let messages = vec![Message::UserMultimodal {
            content: vec![
                ContentBlock::Text {
                    text: "Describe this.".to_string(),
                },
                ContentBlock::ImageBase64 {
                    data: "iVBOR...".to_string(),
                    media_type: "image/png".to_string(),
                },
            ],
        }];
        let (msgs, _) = handler.build_messages(&messages, "", None, None);
        assert_eq!(msgs.len(), 1);
        let content = msgs[0]["content"].as_array().unwrap();
        assert_eq!(content[0]["type"], "text");
        assert_eq!(content[1]["type"], "image");
        assert_eq!(content[1]["source"]["type"], "base64");
    }

    #[test]
    fn openai_single_turn_images() {
        let handler = OpenAiHandler;
        let images = vec![ContentBlock::ImageUrl {
            url: "https://example.com/cat.jpg".to_string(),
            detail: "high".to_string(),
        }];
        let (msgs, _) = handler.build_messages(&[], "Describe this image", None, Some(&images));
        let content = msgs[0]["content"].as_array().unwrap();
        assert_eq!(content.len(), 2);
        assert_eq!(content[0]["type"], "text");
        assert_eq!(content[1]["type"], "image_url");
    }

    #[test]
    fn anthropic_single_turn_images() {
        let handler = AnthropicHandler;
        let images = vec![ContentBlock::ImageBase64 {
            data: "iVBOR...".to_string(),
            media_type: "image/png".to_string(),
        }];
        let (msgs, _) = handler.build_messages(&[], "Describe this image", None, Some(&images));
        let content = msgs[0]["content"].as_array().unwrap();
        assert_eq!(content.len(), 2);
        assert_eq!(content[0]["type"], "text");
        assert_eq!(content[1]["type"], "image");
        assert_eq!(content[1]["source"]["type"], "base64");
    }
}