aither-openai 0.6.0

OpenAI provider integration for aither
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
use crate::PROVIDER_NAME;
use crate::{
    DEEPSEEK_BASE_URL, DEFAULT_AUDIO_FORMAT, DEFAULT_AUDIO_MODEL, DEFAULT_AUDIO_VOICE,
    DEFAULT_BASE_URL, DEFAULT_EMBEDDING_DIM, DEFAULT_EMBEDDING_MODEL, DEFAULT_IMAGE_MODEL,
    DEFAULT_MODEL, DEFAULT_MODERATION_MODEL, DEFAULT_TRANSCRIPTION_MODEL, OPENROUTER_BASE_URL,
    error::OpenAIError,
    request::{
        ChatCompletionRequest, ChatMessagePayload, ParameterSnapshot, ResponsesInputItem,
        ResponsesRequest, ResponsesTool, ToolPayload, convert_responses_tools, convert_tools,
        responses_tool_choice, to_chat_messages, to_responses_input,
    },
    response::{
        ChatCompletionChunk, ChatCompletionUsage, ResponsesOutputItem, ResponsesStreamEvent,
        ResponsesUsage, should_skip_event,
    },
};
use aither_core::{
    LanguageModel,
    llm::{
        Event, LLMRequest, ReasoningState, ToolCall, Usage,
        model::{Ability, Profile as ModelProfile, ToolChoice},
        oneshot,
    },
};
use futures_core::Stream;
use futures_lite::StreamExt;
use std::{collections::HashMap, future::Future, sync::Arc, time::Duration};
use zenwave::{Client, client, header};

/// Configuration for request retry behavior.
#[derive(Debug, Clone)]
pub struct RetryConfig {
    /// Maximum number of retry attempts (0 = no retries).
    pub max_retries: u32,
    /// Initial delay before first retry.
    pub initial_delay: Duration,
    /// Maximum delay between retries.
    pub max_delay: Duration,
    /// Multiplier for exponential backoff.
    pub backoff_multiplier: f64,
}

impl Default for RetryConfig {
    fn default() -> Self {
        Self {
            max_retries: 3,
            initial_delay: Duration::from_millis(500),
            max_delay: Duration::from_secs(30),
            backoff_multiplier: 2.0,
        }
    }
}

impl RetryConfig {
    /// Create a config with no retries.
    #[must_use]
    pub fn none() -> Self {
        Self {
            max_retries: 0,
            ..Default::default()
        }
    }

    /// Calculate delay for a given attempt number (0-indexed).
    #[allow(
        clippy::cast_possible_truncation,
        clippy::cast_possible_wrap,
        clippy::cast_precision_loss,
        clippy::cast_sign_loss
    )]
    fn delay_for_attempt(&self, attempt: u32) -> Duration {
        let delay_ms =
            self.initial_delay.as_millis() as f64 * self.backoff_multiplier.powi(attempt as i32);
        let delay = Duration::from_millis(delay_ms as u64);
        delay.min(self.max_delay)
    }
}

fn is_retryable_json_error(error: &serde_json::Error) -> bool {
    matches!(error.classify(), serde_json::error::Category::Eof)
}

/// Check if an error is retryable.
fn is_retryable_error(err: &OpenAIError) -> bool {
    match err {
        OpenAIError::Http(_)
        | OpenAIError::Body(_)
        | OpenAIError::Stream(_)
        | OpenAIError::RateLimit { .. }
        | OpenAIError::ServerError { .. }
        | OpenAIError::Timeout => true,
        // API and decode errors are generally not retryable.
        OpenAIError::Api(_) | OpenAIError::Decode(_) => false,
        // EOF while parsing JSON is typically a truncated/empty upstream body.
        OpenAIError::Json(error) => is_retryable_json_error(error),
    }
}

/// Get retry delay for an error, respecting Retry-After header for rate limits.
fn get_retry_delay(err: &OpenAIError, attempt: u32, config: &RetryConfig) -> Duration {
    if let OpenAIError::RateLimit {
        retry_after: Some(delay),
        ..
    } = err
    {
        // Respect Retry-After header, but cap at max_delay
        return (*delay).min(config.max_delay);
    }
    config.delay_for_attempt(attempt)
}

/// Sleep for the given duration (runtime-agnostic).
async fn sleep(duration: Duration) {
    #[cfg(not(target_arch = "wasm32"))]
    {
        async_io::Timer::after(duration).await;
    }
    #[cfg(target_arch = "wasm32")]
    {
        let _ = duration;
    }
}

fn map_zenwave_error(error: zenwave::Error) -> OpenAIError {
    if matches!(error, zenwave::Error::Timeout) {
        OpenAIError::Timeout
    } else {
        OpenAIError::Http(error)
    }
}

async fn request_with_timeout<T>(
    timeout: Duration,
    fut: impl Future<Output = Result<T, zenwave::Error>>,
) -> Result<T, OpenAIError> {
    #[cfg(not(target_arch = "wasm32"))]
    {
        futures_lite::future::or(
            async move { fut.await.map_err(map_zenwave_error) },
            async move {
                async_io::Timer::after(timeout).await;
                Err(OpenAIError::Timeout)
            },
        )
        .await
    }
    #[cfg(target_arch = "wasm32")]
    {
        // The browser timer is not `Send`, and `LanguageModel` requires the
        // futures built on top of this one to be. Run the timer on the JS event
        // loop where it belongs and wait on a channel, which is `Send`.
        let millis = timeout.as_millis().min(u128::from(u32::MAX)) as u32;
        let (expired_tx, expired_rx) = async_channel::bounded::<()>(1);
        wasm_bindgen_futures::spawn_local(async move {
            gloo_timers::future::TimeoutFuture::new(millis).await;
            let _ = expired_tx.send(()).await;
        });

        futures_lite::future::or(
            async move { fut.await.map_err(map_zenwave_error) },
            async move {
                // A closed channel means the timer task was dropped, which can
                // only happen once the whole request is being torn down.
                let _ = expired_rx.recv().await;
                Err(OpenAIError::Timeout)
            },
        )
        .await
    }
}

/// Result of attempting to establish an SSE stream.
type SseStreamResult = Result<zenwave::sse::SseStream, OpenAIError>;

/// Attempt to make an SSE request with retry logic.
///
/// Returns a streaming SSE connection on success, or the last error on failure.
async fn sse_request_with_retry<F, Fut>(cfg: &Config, make_request: F) -> SseStreamResult
where
    F: Fn() -> Fut,
    Fut: std::future::Future<Output = SseStreamResult>,
{
    let retry_config = &cfg.retry;
    let mut attempt = 0;

    loop {
        match make_request().await {
            Ok(stream) => return Ok(stream),
            Err(err) => {
                // Check if we should retry
                if attempt < retry_config.max_retries && is_retryable_error(&err) {
                    let delay = get_retry_delay(&err, attempt, retry_config);
                    tracing::warn!(
                        attempt = attempt + 1,
                        max_retries = retry_config.max_retries,
                        delay_ms = delay.as_millis(),
                        error = %err,
                        "Request failed, retrying"
                    );
                    sleep(delay).await;
                    attempt += 1;
                } else {
                    return Err(err);
                }
            }
        }
    }
}

/// `OpenAI` model backed by the Responses API by default, with legacy
/// `chat.completions` support for compatibility.
#[derive(Clone, Debug)]
pub struct OpenAI {
    inner: Arc<Config>,
}

/// Selects which `OpenAI` API surface to use.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ApiKind {
    /// The recommended Responses API.
    Responses,
    /// Legacy Chat Completions API (deprecated by `OpenAI`).
    ChatCompletions,
}

impl OpenAI {
    /// Create a new client using the provided API key and default model.
    pub fn new(api_key: impl Into<String>) -> Self {
        Self::builder(api_key).build()
    }

    /// Create a client configured for [`Deepseek`](https://api-docs.deepseek.com)'s OpenAI-compatible endpoint.
    pub fn deepseek(api_key: impl Into<String>) -> Self {
        Self::builder(api_key).base_url(DEEPSEEK_BASE_URL).build()
    }

    /// Create a client configured for [`OpenRouter`](https://openrouter.ai)'s OpenAI-compatible endpoint.
    pub fn openrouter(api_key: impl Into<String>) -> Self {
        Self::builder(api_key).base_url(OPENROUTER_BASE_URL).build()
    }

    /// Start building an [`OpenAI`] client with custom configuration.
    #[must_use]
    pub fn builder(api_key: impl Into<String>) -> Builder {
        Builder::new(api_key)
    }

    /// Override the default chat model in-place.
    #[must_use]
    pub fn with_model(mut self, model: impl Into<String>) -> Self {
        Arc::make_mut(&mut self.inner).chat_model = sanitize_model(model);
        self
    }

    /// Override the REST base URL (useful for OpenAI-compatible endpoints).
    #[must_use]
    pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
        Arc::make_mut(&mut self.inner).base_url = base_url.into();
        self
    }

    /// Select which `OpenAI` API to call.
    #[must_use]
    pub fn with_api(mut self, api: ApiKind) -> Self {
        Arc::make_mut(&mut self.inner).api_kind = api;
        self
    }

    /// Use the recommended Responses API.
    #[must_use]
    pub fn with_responses_api(self) -> Self {
        self.with_api(ApiKind::Responses)
    }

    /// Use the legacy Chat Completions API (deprecated by `OpenAI`).
    #[must_use]
    pub fn with_chat_completions_api(self) -> Self {
        self.with_api(ApiKind::ChatCompletions)
    }

    /// Send deprecated `max_tokens` alongside `max_completion_tokens` for compatibility.
    ///
    /// `OpenAI` deprecates `max_tokens` and it is incompatible with reasoning models.
    #[must_use]
    pub fn with_legacy_max_tokens(mut self, enabled: bool) -> Self {
        Arc::make_mut(&mut self.inner).legacy_max_tokens = enabled;
        self
    }

    /// Override the embeddings model identifier.
    #[must_use]
    pub fn with_embedding_model(mut self, model: impl Into<String>) -> Self {
        let model = sanitize_model(model);
        let cfg = Arc::make_mut(&mut self.inner);
        cfg.embedding_model = model;
        if let Some(dim) = infer_embedding_dim(&cfg.embedding_model) {
            cfg.embedding_dimensions = dim;
        }
        self
    }

    /// Override the embedding dimension (defaults depend on model).
    #[must_use]
    pub fn with_embedding_dimensions(mut self, dimensions: usize) -> Self {
        Arc::make_mut(&mut self.inner).embedding_dimensions = dimensions;
        self
    }

    /// Override the image generation model identifier.
    #[must_use]
    pub fn with_image_model(mut self, model: impl Into<String>) -> Self {
        Arc::make_mut(&mut self.inner).image_model = sanitize_model(model);
        self
    }

    /// Override the default text-to-speech model identifier.
    #[must_use]
    pub fn with_audio_model(mut self, model: impl Into<String>) -> Self {
        Arc::make_mut(&mut self.inner).audio_model = sanitize_model(model);
        self
    }

    /// Set the default text-to-speech voice (e.g., `alloy`).
    #[must_use]
    pub fn with_audio_voice(mut self, voice: impl Into<String>) -> Self {
        Arc::make_mut(&mut self.inner).audio_voice = voice.into();
        self
    }

    /// Set the preferred audio format (`mp3`, `wav`, `flac`).
    #[must_use]
    pub fn with_audio_format(mut self, format: impl Into<String>) -> Self {
        Arc::make_mut(&mut self.inner).audio_format = format.into();
        self
    }

    /// Override the transcription model identifier.
    #[must_use]
    pub fn with_transcription_model(mut self, model: impl Into<String>) -> Self {
        Arc::make_mut(&mut self.inner).transcription_model = sanitize_model(model);
        self
    }

    /// Override the moderation model identifier.
    #[must_use]
    pub fn with_moderation_model(mut self, model: impl Into<String>) -> Self {
        Arc::make_mut(&mut self.inner).moderation_model = sanitize_model(model);
        self
    }

    pub(crate) fn config(&self) -> Arc<Config> {
        self.inner.clone()
    }
}

impl LanguageModel for OpenAI {
    type Error = OpenAIError;

    #[allow(clippy::too_many_lines)]
    fn respond(
        &self,
        request: LLMRequest,
    ) -> impl Stream<Item = Result<Event, Self::Error>> + Send {
        let cfg = self.inner.clone();
        let (messages, parameters, tool_defs) = request.into_parts();
        let tool_defs = filter_tool_definitions(tool_defs, &parameters.tool_choice);
        let mut snapshot = ParameterSnapshot::from(&parameters);
        snapshot.legacy_max_tokens = cfg.legacy_max_tokens;
        let has_attachments = messages.iter().any(|msg| !msg.attachments().is_empty());
        let has_audio = messages.iter().any(|message| {
            message
                .attachments()
                .iter()
                .any(|attachment| attachment.media_type().as_ref().starts_with("audio/"))
        });
        let effective_api_kind = if cfg.api_kind == ApiKind::Responses && has_audio {
            ApiKind::ChatCompletions
        } else {
            cfg.api_kind
        };

        async_stream::stream! {
            if parameters.cache.claude.is_some() || parameters.cache.gemini.is_some() {
                yield Err(OpenAIError::Api(
                    "OpenAI provider only accepts cache.openai settings".to_string(),
                ));
                return;
            }

            if effective_api_kind == ApiKind::ChatCompletions
                && messages.iter().flat_map(aither_core::llm::Message::attachments).any(|attachment| {
                    let media_type = attachment.media_type().as_ref();
                    !media_type.starts_with("image/") && !media_type.starts_with("audio/")
                })
            {
                yield Err(OpenAIError::Api(
                    "Chat Completions accepts only image and audio attachments".to_string(),
                ));
                return;
            }

            if effective_api_kind == ApiKind::Responses
                && messages.iter().flat_map(aither_core::llm::Message::attachments).any(|attachment| {
                    attachment.media_type().as_ref().starts_with("video/")
                })
            {
                yield Err(OpenAIError::Api(
                    "OpenAI Responses API does not support video attachments".to_string(),
                ));
                return;
            }

            if effective_api_kind == ApiKind::ChatCompletions
                && (!snapshot.openai_tools.is_empty() || snapshot.websearch || snapshot.code_execution)
            {
                yield Err(OpenAIError::Api(
                    "OpenAI native hosted tools require the Responses API".to_string(),
                ));
                return;
            }

            let messages = if has_attachments && effective_api_kind == ApiKind::Responses {
                #[cfg(not(target_arch = "wasm32"))]
                {
                    match crate::attachments::resolve_messages(&cfg, messages).await {
                        Ok(resolved) => resolved,
                        Err(err) => {
                            yield Err(err);
                            return;
                        }
                    }
                }
                #[cfg(target_arch = "wasm32")]
                {
                    yield Err(OpenAIError::Api(
                        "file:// attachments are not supported on wasm32 targets".to_string(),
                    ));
                    return;
                }
            } else {
                messages
            };

            match effective_api_kind {
                ApiKind::ChatCompletions => {
                    let payload_messages = match to_chat_messages(&messages).await {
                        Ok(payload) => payload,
                        Err(error) => {
                            yield Err(error);
                            return;
                        }
                    };
                    let openai_tools = if tool_defs.is_empty() {
                        None
                    } else {
                        Some(convert_tools(tool_defs))
                    };

                    let mut events = chat_completions_stream(
                        cfg,
                        payload_messages,
                        snapshot,
                        openai_tools,
                    );

                    while let Some(event) = events.next().await {
                        yield event;
                    }
                }
                ApiKind::Responses => {
                    let responses_input = match to_responses_input(&messages) {
                        Ok(input) => input,
                        Err(err) => {
                            yield Err(err);
                            return;
                        }
                    };

                    let mut events = responses_stream(
                        cfg,
                        responses_input,
                        snapshot,
                        tool_defs,
                    );

                    while let Some(event) = events.next().await {
                        yield event;
                    }
                }
            }
        }
    }

    fn complete(&self, prefix: &str) -> impl Stream<Item = Result<Event, Self::Error>> + Send {
        self.respond(oneshot(
            "Continue the user provided text without additional commentary.",
            prefix,
        ))
    }

    fn profile(&self) -> impl Future<Output = ModelProfile> + Send {
        let cfg = self.inner.clone();
        async move {
            // Try to fetch context window from API, fallback to models database
            let context_length = match fetch_model_context_length(&cfg).await {
                Ok(len) => len,
                Err(e) => {
                    tracing::debug!("API did not return context_length: {e}");
                    aither_models::lookup(&cfg.chat_model)
                        .and_then(aither_models::ModelEntry::max_input_tokens)
                        .unwrap_or_else(|| {
                            // Custom/self-hosted models are legitimately absent
                            // from both the provider API and the registry, so
                            // assume a conservative context window rather than
                            // failing the whole conversation.
                            tracing::warn!(
                                model = %cfg.chat_model,
                                assumed_context_length = UNKNOWN_MODEL_CONTEXT_LENGTH,
                                "model missing context metadata from provider and aither-models"
                            );
                            UNKNOWN_MODEL_CONTEXT_LENGTH
                        })
                }
            };

            let mut profile = ModelProfile::new(
                cfg.chat_model.clone(),
                "OpenAI",
                cfg.chat_model.clone(),
                "OpenAI GPT family model",
                context_length,
            )
            .with_ability(Ability::ToolUse);
            for ability in &cfg.native_abilities {
                if !profile.abilities.contains(ability) {
                    profile.abilities.push(*ability);
                }
            }
            profile
        }
    }
}

/// Fetch context window size from the models API.
/// Context window assumed for models with no metadata from either the
/// provider API or the aither-models registry (custom/self-hosted models).
/// Conservative enough to be safe for compaction budgeting on any modern
/// OpenAI-compatible backend.
const UNKNOWN_MODEL_CONTEXT_LENGTH: u32 = 32_768;

async fn fetch_model_context_length(cfg: &Config) -> Result<u32, OpenAIError> {
    use crate::response::ModelsListResponse;

    let url = format!("{}/models", cfg.base_url.trim_end_matches('/'));
    let mut backend = client();
    let mut builder = backend
        .get(&url)
        .map_err(OpenAIError::Http)?
        .header(
            header::AUTHORIZATION.as_str(),
            format!("Bearer {}", cfg.api_key),
        )
        .map_err(OpenAIError::Http)?;
    for (name, value) in &cfg.extra_headers {
        builder = builder
            .header(name.clone(), value.clone())
            .map_err(OpenAIError::Http)?;
    }
    let response: ModelsListResponse =
        request_with_timeout(cfg.request_timeout, builder.json()).await?;

    // Find matching model
    let mut model_found = false;
    for model in response.data {
        if model.id == cfg.chat_model {
            model_found = true;
            if let Some(ctx) = model.context_length.or(model.max_tokens) {
                tracing::debug!(
                    "Fetched context_length={} for model '{}'",
                    ctx,
                    cfg.chat_model
                );
                return Ok(ctx);
            }
        }
    }

    // More specific error message
    if model_found {
        Err(OpenAIError::Api(format!(
            "Model '{}' found but missing context_length field (proxy may not support it)",
            cfg.chat_model
        )))
    } else {
        Err(OpenAIError::Api(format!(
            "Model '{}' not found in /models response",
            cfg.chat_model
        )))
    }
}

fn chat_completions_stream(
    cfg: Arc<Config>,
    payload_messages: Vec<ChatMessagePayload>,
    snapshot: ParameterSnapshot,
    openai_tools: Option<Vec<ToolPayload>>,
) -> impl Stream<Item = Result<Event, OpenAIError>> + Send + Unpin {
    Box::pin(chat_completions_stream_inner(
        cfg,
        payload_messages,
        snapshot,
        openai_tools,
    ))
}

/// Accumulated tool call state for streaming.
#[derive(Debug, Default)]
struct ToolCallAccumulator {
    id: Option<String>,
    name: Option<String>,
    arguments: String,
}

/// Make a chat completions SSE request (single attempt).
async fn chat_completions_request(
    cfg: &Config,
    request: &ChatCompletionRequest,
) -> SseStreamResult {
    let endpoint = cfg.request_url("/chat/completions");
    let mut backend = client();

    let build_result = backend
        .post(endpoint)
        .and_then(|b| b.header(header::AUTHORIZATION.as_str(), cfg.request_auth()))
        .and_then(|b| b.header(header::USER_AGENT.as_str(), "aither-openai/0.1"))
        .and_then(|b| b.header(header::ACCEPT.as_str(), "text/event-stream"));

    let mut builder = build_result.map_err(OpenAIError::Http)?;

    if let Some(org) = &cfg.organization {
        builder = builder
            .header("OpenAI-Organization", org.clone())
            .map_err(OpenAIError::Http)?;
    }
    for (name, value) in &cfg.extra_headers {
        builder = builder
            .header(name.clone(), value.clone())
            .map_err(OpenAIError::Http)?;
    }

    request_with_timeout(
        cfg.request_timeout,
        builder.json_body(request).map_err(OpenAIError::Http)?.sse(),
    )
    .await
}

#[allow(clippy::too_many_lines)]
fn chat_completions_stream_inner(
    cfg: Arc<Config>,
    payload_messages: Vec<ChatMessagePayload>,
    snapshot: ParameterSnapshot,
    openai_tools: Option<Vec<ToolPayload>>,
) -> impl Stream<Item = Result<Event, OpenAIError>> + Send {
    let include_reasoning = snapshot.include_reasoning;

    async_stream::stream! {
        let request = ChatCompletionRequest::new(
            cfg.chat_model.clone(),
            payload_messages,
            &snapshot,
            openai_tools,
            true,
        );

        tracing::debug!(request = %serde_json::to_string_pretty(&request).unwrap_or_default(), "Sending chat completion request");

        // Make request with retry
        let sse_stream = match sse_request_with_retry(&cfg, || chat_completions_request(&cfg, &request)).await {
            Ok(stream) => stream,
            Err(e) => {
                yield Err(e);
                return;
            }
        };
        futures_lite::pin!(sse_stream);

        // Accumulate tool calls by index - streaming sends id/name first, then arguments incrementally
        let mut tool_calls: std::collections::HashMap<usize, ToolCallAccumulator> =
            std::collections::HashMap::new();
        let mut usage: Option<Usage> = None;
        while let Some(event) = sse_stream.next().await {
            match event {
                Ok(e) => {
                    if should_skip_event(&e) {
                        continue;
                    }
                    let data = e.text_data();
                    if data == "[DONE]" {
                        continue;
                    }
                    tracing::debug!(sse_event = %data, "Received SSE event");

                    // Check for API error response
                    if let Ok(error_obj) = serde_json::from_str::<serde_json::Value>(data)
                        && let Some(error) = error_obj.get("error") {
                            let msg = error.get("message")
                                .and_then(|m| m.as_str())
                                .unwrap_or("Unknown API error");
                            yield Err(OpenAIError::Api(msg.to_string()));
                            return;
                        }

                    match serde_json::from_str::<ChatCompletionChunk>(data) {
                        Ok(chunk) => {
                            if let Some(chunk_usage) = &chunk.usage {
                                usage = Some(usage_from_chat_completion(chunk_usage));
                            }
                            // Emit text events
                            for choice in &chunk.choices {
                                // Check for malformed function call
                                if let Some(ref reason) = choice.finish_reason
                                    && reason.contains("malformed_function_call") {
                                        yield Err(OpenAIError::Api("malformed function call".to_string()));
                                        return;
                                    }

                                if let Some(content) = &choice.delta.content
                                    && !content.is_empty() {
                                        yield Ok(Event::Text(content.clone()));
                                    }
                                // Emit reasoning if enabled
                                if include_reasoning
                                    && let Some(reasoning) = &choice.delta.reasoning_content
                                        && !reasoning.is_empty() {
                                            yield Ok(Event::Reasoning(reasoning.clone()));
                                        }
                                // Accumulate tool calls and emit deltas
                                if let Some(calls) = &choice.delta.tool_calls {
                                    for call in calls {
                                        let index = call.index.unwrap_or(0);
                                        let acc = tool_calls.entry(index).or_default();
                                        if let Some(id) = &call.id {
                                            acc.id = Some(id.clone());
                                        }
                                        if let Some(function) = &call.function {
                                            if let Some(name) = &function.name {
                                                acc.name = Some(name.clone());
                                            }
                                            if let Some(args) = &function.arguments {
                                                acc.arguments.push_str(args);
                                            }
                                        }
                                        // Emit delta for UI progress
                                        if let (Some(id), Some(name)) = (&acc.id, &acc.name) {
                                            yield Ok(Event::ToolCallDelta {
                                                id: id.clone(),
                                                name: name.clone(),
                                                arguments_fragment: acc.arguments.clone(),
                                            });
                                        }
                                    }
                                }
                            }
                        }
                        Err(e) => {
                            yield Err(OpenAIError::from(e));
                            return;
                        }
                    }
                }
                Err(e) => {
                    yield Err(OpenAIError::from(e));
                    return;
                }
            }
        }

        // Emit accumulated tool calls at the end
        let mut sorted_calls: Vec<_> = tool_calls.into_iter().collect();
        sorted_calls.sort_by_key(|(index, _)| *index);
        for (_, acc) in sorted_calls {
            if let (Some(id), Some(name)) = (acc.id, acc.name) {
                let arguments = if acc.arguments.is_empty() {
                    serde_json::Value::Object(serde_json::Map::default())
                } else {
                    serde_json::from_str(&acc.arguments).unwrap_or_else(|_| {
                        serde_json::Value::Object(serde_json::Map::default())
                    })
                };
                yield Ok(Event::ToolCall(ToolCall {
                    id,
                    name,
                    arguments,
                    reasoning_state: None,
                }));
            }
        }
        if let Some(final_usage) = usage {
            yield Ok(Event::Usage(final_usage));
        }
    }
}

fn responses_stream(
    cfg: Arc<Config>,
    input: Vec<ResponsesInputItem>,
    snapshot: ParameterSnapshot,
    tool_defs: Vec<aither_core::llm::tool::ToolDefinition>,
) -> impl Stream<Item = Result<Event, OpenAIError>> + Send + Unpin {
    Box::pin(responses_stream_inner(cfg, input, snapshot, tool_defs))
}

/// Accumulated function call state for Responses API streaming.
#[derive(Debug, Default)]
struct FunctionCallAccumulator {
    item_id: Option<String>,
    call_id: Option<String>,
    name: Option<String>,
    arguments: String,
    emitted: bool,
}

fn normalize_response_call_id(call_id: Option<String>, fallback: &str) -> String {
    match call_id {
        Some(call_id) if !call_id.trim().is_empty() => call_id,
        _ => fallback.to_string(),
    }
}

fn non_empty_response_id(value: Option<String>) -> Option<String> {
    value.filter(|value| !value.trim().is_empty())
}

fn fallback_response_item_id(output_index: usize) -> String {
    format!("response_output_{output_index}")
}

fn parse_tool_call_arguments(arguments: &str) -> serde_json::Value {
    if arguments.is_empty() {
        serde_json::Value::Object(serde_json::Map::default())
    } else {
        serde_json::from_str(arguments)
            .unwrap_or_else(|_| serde_json::Value::Object(serde_json::Map::default()))
    }
}

fn usage_from_chat_completion(raw: &ChatCompletionUsage) -> Usage {
    Usage {
        prompt_tokens: raw.prompt_tokens,
        completion_tokens: raw.completion_tokens,
        total_tokens: raw.total_tokens,
        reasoning_tokens: raw
            .completion_tokens_details
            .as_ref()
            .and_then(|details| details.reasoning_tokens),
        cache_read_tokens: raw
            .prompt_tokens_details
            .as_ref()
            .and_then(|details| details.cached_tokens),
        cache_write_tokens: None,
        cost_usd: None,
        stop_reason: None,
    }
}

fn usage_from_responses(raw: &ResponsesUsage) -> Usage {
    Usage {
        prompt_tokens: raw.input_tokens,
        completion_tokens: raw.output_tokens,
        total_tokens: raw.total_tokens,
        reasoning_tokens: raw
            .output_token_details
            .as_ref()
            .and_then(|details| details.reasoning_tokens),
        cache_read_tokens: raw
            .input_token_details
            .as_ref()
            .and_then(|details| details.cached_tokens),
        cache_write_tokens: None,
        cost_usd: None,
        stop_reason: None,
    }
}

fn mark_and_emit_done_function_call(
    function_calls: &mut HashMap<usize, FunctionCallAccumulator>,
    output_index: usize,
    item_id: Option<String>,
    response_id: String,
    call_id: Option<String>,
    name: String,
    arguments: &str,
) -> ToolCall {
    let fallback_id = non_empty_response_id(item_id.clone())
        .or_else(|| non_empty_response_id(Some(response_id.clone())))
        .unwrap_or_else(|| fallback_response_item_id(output_index));
    let resolved_call_id = normalize_response_call_id(call_id, &fallback_id);

    let acc = function_calls.entry(output_index).or_default();
    acc.item_id =
        non_empty_response_id(item_id).or_else(|| non_empty_response_id(Some(response_id)));
    acc.call_id = Some(resolved_call_id.clone());
    acc.name = Some(name.clone());
    acc.arguments.clear();
    acc.arguments.push_str(arguments);
    acc.emitted = true;

    ToolCall {
        id: resolved_call_id,
        name,
        arguments: parse_tool_call_arguments(arguments),
        reasoning_state: None,
    }
}

fn drain_pending_function_calls(
    function_calls: HashMap<usize, FunctionCallAccumulator>,
) -> Vec<ToolCall> {
    function_calls
        .into_iter()
        .filter(|(_, acc)| !acc.emitted)
        .filter_map(|(output_index, acc)| {
            let name = acc.name?;
            let fallback_id = acc
                .item_id
                .clone()
                .unwrap_or_else(|| fallback_response_item_id(output_index));
            let call_id = normalize_response_call_id(acc.call_id, &fallback_id);
            Some(ToolCall {
                id: call_id,
                name,
                arguments: parse_tool_call_arguments(&acc.arguments),
                reasoning_state: None,
            })
        })
        .collect()
}

#[allow(clippy::items_after_statements)]
fn validate_serialized_responses_request(request: &ResponsesRequest) -> Result<(), OpenAIError> {
    let request_value = serde_json::to_value(request).map_err(OpenAIError::Json)?;
    let Some(items) = request_value
        .get("input")
        .and_then(serde_json::Value::as_array)
    else {
        return Ok(());
    };

    fn find_blank_call_id(value: &serde_json::Value, path: &str) -> Option<(String, String)> {
        match value {
            serde_json::Value::Object(map) => {
                for (key, child) in map {
                    let child_path = format!("{path}.{key}");
                    if key == "call_id"
                        && let Some(call_id) = child.as_str()
                        && call_id.trim().is_empty()
                    {
                        return Some((child_path, call_id.to_string()));
                    }
                    if let Some(found) = find_blank_call_id(child, &child_path) {
                        return Some(found);
                    }
                }
                None
            }
            serde_json::Value::Array(items) => {
                for (index, child) in items.iter().enumerate() {
                    let child_path = format!("{path}[{index}]");
                    if let Some(found) = find_blank_call_id(child, &child_path) {
                        return Some(found);
                    }
                }
                None
            }
            serde_json::Value::Null
            | serde_json::Value::Bool(_)
            | serde_json::Value::Number(_)
            | serde_json::Value::String(_) => None,
        }
    }

    for (index, item) in items.iter().enumerate() {
        if let Some((path, call_id)) = find_blank_call_id(item, &format!("input[{index}]")) {
            let snippet = serde_json::to_string(item).map_err(OpenAIError::Json)?;
            return Err(OpenAIError::Api(format!(
                "responses request serialized blank call_id at {path}: {call_id:?}; item={snippet}"
            )));
        }
    }
    Ok(())
}

/// Make a responses API SSE request (single attempt).
async fn responses_request(cfg: &Config, request: &ResponsesRequest) -> SseStreamResult {
    validate_serialized_responses_request(request)?;
    let serialized_request = serde_json::to_string(request).map_err(OpenAIError::Json)?;
    let endpoint = cfg.request_url("/responses");
    let mut backend = client();

    let build_result = backend
        .post(endpoint)
        .and_then(|b| b.header(header::AUTHORIZATION.as_str(), cfg.request_auth()))
        .and_then(|b| b.header(header::USER_AGENT.as_str(), "aither-openai/0.1"))
        .and_then(|b| b.header(header::ACCEPT.as_str(), "text/event-stream"));

    let mut builder = build_result.map_err(OpenAIError::Http)?;

    if let Some(org) = &cfg.organization {
        builder = builder
            .header("OpenAI-Organization", org.clone())
            .map_err(OpenAIError::Http)?;
    }
    for (name, value) in &cfg.extra_headers {
        builder = builder
            .header(name.clone(), value.clone())
            .map_err(OpenAIError::Http)?;
    }

    match request_with_timeout(
        cfg.request_timeout,
        builder.json_body(request).map_err(OpenAIError::Http)?.sse(),
    )
    .await
    {
        Ok(stream) => Ok(stream),
        Err(error) => {
            if error.to_string().contains("call_id") {
                return Err(OpenAIError::Api(format!(
                    "{error}\nSerialized responses request: {serialized_request}"
                )));
            }
            Err(error)
        }
    }
}

#[allow(clippy::too_many_lines, clippy::collapsible_match)]
fn responses_stream_inner(
    cfg: Arc<Config>,
    input: Vec<ResponsesInputItem>,
    snapshot: ParameterSnapshot,
    tool_defs: Vec<aither_core::llm::tool::ToolDefinition>,
) -> impl Stream<Item = Result<Event, OpenAIError>> + Send {
    let include_reasoning = snapshot.include_reasoning;

    async_stream::stream! {
        let mut response_tools = convert_responses_tools(tool_defs);
        let allow_builtins = matches!(snapshot.tool_choice, ToolChoice::Auto | ToolChoice::Required);
        if allow_builtins {
            let native_tools = snapshot.openai_tools.clone();
            if snapshot.websearch || native_tools.web_search.is_some() {
                let tool = native_tools.web_search.unwrap_or_default();
                response_tools.push(ResponsesTool::from(tool));
            }
            for tool in native_tools.file_search {
                response_tools.push(ResponsesTool::from(tool));
            }
            if snapshot.code_execution || native_tools.code_interpreter.is_some() {
                let tool = native_tools.code_interpreter.unwrap_or_default();
                response_tools.push(ResponsesTool::from(tool));
            }
            if let Some(tool) = native_tools.image_generation {
                response_tools.push(ResponsesTool::from(tool));
            }
            for tool in native_tools.mcp {
                response_tools.push(ResponsesTool::from(tool));
            }
            if let Some(tool) = native_tools.computer_use {
                response_tools.push(ResponsesTool::from(tool));
            }
        }
        let response_tools = if response_tools.is_empty() {
            None
        } else {
            Some(response_tools)
        };
        let has_tools = response_tools.is_some();
        let tool_choice = responses_tool_choice(&snapshot, has_tools);

        let mut request = ResponsesRequest::new(
            cfg.chat_model.clone(),
            input,
            &snapshot,
            response_tools,
            tool_choice,
            true, // stream: true
        );
        request.store = cfg.store;
        let serialized_request = serde_json::to_string(&request).unwrap_or_default();

        tracing::debug!(request = %serde_json::to_string_pretty(&request).unwrap_or_default(), "Sending responses request");

        // Make request with retry
        let sse_stream = match sse_request_with_retry(&cfg, || responses_request(&cfg, &request)).await {
            Ok(stream) => stream,
            Err(e) => {
                yield Err(e);
                return;
            }
        };
        futures_lite::pin!(sse_stream);

        // Accumulate function calls by output_index because provider item identifiers can be
        // absent or empty on some streamed events.
        let mut function_calls: HashMap<usize, FunctionCallAccumulator> = HashMap::new();
        let mut usage: Option<Usage> = None;
        let mut usage_emitted = false;

        while let Some(event) = sse_stream.next().await {
            match event {
                Ok(e) => {
                    if should_skip_event(&e) {
                        continue;
                    }
                    let data = e.text_data();
                    if data == "[DONE]" {
                        continue;
                    }
                    tracing::trace!(sse_event = %data, "Received Responses API SSE event");

                    // Check for API error response
                    if let Ok(error_obj) = serde_json::from_str::<serde_json::Value>(data)
                        && let Some(error) = error_obj.get("error") {
                            let msg = error.get("message")
                                .and_then(|m| m.as_str())
                                .unwrap_or("Unknown API error");
                            if msg.contains("call_id") {
                                yield Err(OpenAIError::Api(format!(
                                    "{msg}\nSerialized responses request: {serialized_request}"
                                )));
                            } else {
                                yield Err(OpenAIError::Api(msg.to_string()));
                            }
                            return;
                        }

                    match serde_json::from_str::<ResponsesStreamEvent>(data) {
                        Ok(stream_event) => {
                            match stream_event {
                                ResponsesStreamEvent::ResponseCreated { response } => {
                                    if let Some(meta) = response.and_then(|resp| resp.usage) {
                                        usage = Some(usage_from_responses(&meta));
                                    }
                                }
                                ResponsesStreamEvent::OutputTextDelta { delta, .. } => {
                                    if !delta.is_empty() {
                                        yield Ok(Event::Text(delta));
                                    }
                                }
                                ResponsesStreamEvent::ReasoningTextDelta { delta, .. } |
                                ResponsesStreamEvent::ReasoningSummaryTextDelta { delta, .. } => {
                                    if include_reasoning && !delta.is_empty() {
                                        yield Ok(Event::Reasoning(delta));
                                    }
                                }
                                ResponsesStreamEvent::OutputItemAdded { item, item_id, output_index } => {
                                    // When a function_call item is added, capture id and name
                                    if let ResponsesOutputItem::FunctionCall { id, call_id, name, .. } = item {
                                        let fallback_id = non_empty_response_id(item_id.clone())
                                            .or_else(|| non_empty_response_id(Some(id.clone())))
                                            .unwrap_or_else(|| fallback_response_item_id(output_index));
                                        let acc = function_calls.entry(output_index).or_default();
                                        acc.item_id = non_empty_response_id(item_id)
                                            .or_else(|| non_empty_response_id(Some(id)));
                                        acc.call_id = Some(normalize_response_call_id(call_id, &fallback_id));
                                        acc.name = Some(name.clone());
                                        // Emit initial delta with tool name
                                        if let Some(ref call_id) = acc.call_id {
                                            yield Ok(Event::ToolCallDelta {
                                                id: call_id.clone(),
                                                name,
                                                arguments_fragment: String::new(),
                                            });
                                        }
                                    }
                                }
                                ResponsesStreamEvent::FunctionCallArgumentsDelta { delta, output_index, .. } => {
                                    let acc = function_calls.entry(output_index).or_default();
                                    acc.arguments.push_str(&delta);
                                    // Emit delta for UI progress
                                    if let (Some(call_id), Some(name)) = (&acc.call_id, &acc.name) {
                                        yield Ok(Event::ToolCallDelta {
                                            id: call_id.clone(),
                                            name: name.clone(),
                                            arguments_fragment: acc.arguments.clone(),
                                        });
                                    }
                                }
                                ResponsesStreamEvent::FunctionCallArgumentsDone { arguments, output_index, .. } => {
                                    let acc = function_calls.entry(output_index).or_default();
                                    acc.arguments = arguments;
                                }
                                ResponsesStreamEvent::OutputItemDone { item, item_id, output_index } => {
                                    // Emit function call when item is done
                                    if let ResponsesOutputItem::FunctionCall { id, call_id, name, arguments } = item {
                                        let tool_call = mark_and_emit_done_function_call(
                                            &mut function_calls,
                                            output_index,
                                            item_id,
                                            id,
                                            call_id,
                                            name,
                                            &arguments,
                                        );
                                        yield Ok(Event::ToolCall(tool_call));
                                    } else if let ResponsesOutputItem::Reasoning {
                                        id, encrypted_content, ..
                                    } = item
                                    {
                                        // Only encrypted reasoning is replayable;
                                        // a summary alone cannot be verified.
                                        if let Some(encrypted) = encrypted_content {
                                            let payload = serde_json::json!({
                                                "type": "reasoning",
                                                "id": id,
                                                "encrypted_content": encrypted,
                                            });
                                            yield Ok(Event::ReasoningState(ReasoningState::new(
                                                PROVIDER_NAME,
                                                payload.to_string(),
                                            )));
                                        }
                                    }
                                }
                                ResponsesStreamEvent::ResponseCompleted { response } => {
                                    if let Some(meta) = response.usage {
                                        let final_usage = usage_from_responses(&meta);
                                        usage = Some(final_usage.clone());
                                        yield Ok(Event::Usage(final_usage));
                                        usage_emitted = true;
                                    }
                                }
                                ResponsesStreamEvent::ResponseFailed { error } => {
                                    let msg = error
                                        .and_then(|e| e.message)
                                        .unwrap_or_else(|| "Response failed".to_string());
                                    if msg.contains("call_id") {
                                        yield Err(OpenAIError::Api(format!(
                                            "{msg}\nSerialized responses request: {serialized_request}"
                                        )));
                                    } else {
                                        yield Err(OpenAIError::Api(msg));
                                    }
                                    return;
                                }
                                ResponsesStreamEvent::Error { message, .. } => {
                                    let msg = message.unwrap_or_else(|| "Unknown error".to_string());
                                    if msg.contains("call_id") {
                                        yield Err(OpenAIError::Api(format!(
                                            "{msg}\nSerialized responses request: {serialized_request}"
                                        )));
                                    } else {
                                        yield Err(OpenAIError::Api(msg));
                                    }
                                    return;
                                }
                                // Ignore other events
                                _ => {}
                            }
                        }
                        Err(e) => {
                            tracing::warn!(error = %e, data = %data, "Failed to parse Responses API event");
                        }
                    }
                }
                Err(e) => {
                    yield Err(OpenAIError::from(e));
                    return;
                }
            }
        }

        // Emit any remaining accumulated function calls (fallback if OutputItemDone wasn't received)
        for tool_call in drain_pending_function_calls(function_calls) {
            yield Ok(Event::ToolCall(tool_call));
        }
        if !usage_emitted && let Some(final_usage) = usage {
            yield Ok(Event::Usage(final_usage));
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::response::{
        ChatCompletionTokensDetails, ChatCompletionUsage, ChatPromptTokensDetails,
        ResponsesInputTokenDetails, ResponsesOutputTokenDetails, ResponsesUsage,
    };
    use std::time::Duration;

    #[test]
    fn done_function_call_is_not_emitted_twice() {
        let mut function_calls = HashMap::new();
        let emitted = mark_and_emit_done_function_call(
            &mut function_calls,
            1,
            Some("item_1".to_string()),
            "item_1".to_string(),
            Some("call_1".to_string()),
            "search".to_string(),
            r#"{"q":"rust"}"#,
        );
        assert_eq!(emitted.id, "call_1");
        assert_eq!(emitted.name, "search");
        assert_eq!(drain_pending_function_calls(function_calls).len(), 0);
    }

    #[test]
    fn pending_function_call_is_emitted_once_via_fallback() {
        let mut function_calls = HashMap::new();
        function_calls.insert(
            2,
            FunctionCallAccumulator {
                item_id: Some("item_2".to_string()),
                call_id: Some("call_2".to_string()),
                name: Some("lookup".to_string()),
                arguments: r#"{"id":42}"#.to_string(),
                emitted: false,
            },
        );
        let pending = drain_pending_function_calls(function_calls);
        assert_eq!(pending.len(), 1);
        assert_eq!(pending[0].id, "call_2");
        assert_eq!(pending[0].name, "lookup");
    }

    #[test]
    fn empty_done_call_id_falls_back_to_item_id() {
        let mut function_calls = HashMap::new();
        let emitted = mark_and_emit_done_function_call(
            &mut function_calls,
            3,
            Some("item_3".to_string()),
            "item_3".to_string(),
            Some(String::new()),
            "search".to_string(),
            r#"{"q":"terminal first"}"#,
        );
        assert_eq!(emitted.id, "item_3");
    }

    #[test]
    fn empty_pending_call_id_falls_back_to_item_id() {
        let mut function_calls = HashMap::new();
        function_calls.insert(
            4,
            FunctionCallAccumulator {
                item_id: Some("item_4".to_string()),
                call_id: Some("   ".to_string()),
                name: Some("lookup".to_string()),
                arguments: r#"{"id":7}"#.to_string(),
                emitted: false,
            },
        );
        let pending = drain_pending_function_calls(function_calls);
        assert_eq!(pending.len(), 1);
        assert_eq!(pending[0].id, "item_4");
        assert_eq!(pending[0].name, "lookup");
    }

    #[test]
    fn empty_stream_identifiers_fall_back_to_output_index() {
        let mut function_calls = HashMap::new();
        function_calls.insert(
            12,
            FunctionCallAccumulator {
                item_id: None,
                call_id: None,
                name: None,
                arguments: r#"{"q":"may"}"#.to_string(),
                emitted: false,
            },
        );
        let emitted = mark_and_emit_done_function_call(
            &mut function_calls,
            12,
            None,
            String::new(),
            Some(String::new()),
            "search".to_string(),
            r#"{"q":"may"}"#,
        );
        assert_eq!(emitted.id, "response_output_12");
    }

    #[test]
    fn serialized_responses_request_rejects_empty_call_id() {
        let snapshot = ParameterSnapshot::from(&aither_core::llm::model::Parameters::default());
        let request = ResponsesRequest::new(
            "gpt-5.4".to_string(),
            vec![ResponsesInputItem::FunctionCall {
                kind: "function_call",
                call_id: String::new(),
                name: "search".to_string(),
                arguments: "{}".to_string(),
            }],
            &snapshot,
            None,
            responses_tool_choice(&snapshot, false),
            false,
        );

        let error = validate_serialized_responses_request(&request)
            .expect_err("empty serialized call_id must fail");
        assert!(
            error.to_string().contains("input[0]"),
            "unexpected error: {error}"
        );
    }

    #[test]
    fn serialized_responses_request_rejects_whitespace_call_id() {
        let snapshot = ParameterSnapshot::from(&aither_core::llm::model::Parameters::default());
        let request = ResponsesRequest::new(
            "gpt-5.4".to_string(),
            vec![ResponsesInputItem::FunctionCallOutput {
                kind: "function_call_output",
                call_id: "   ".to_string(),
                output: "{}".to_string(),
            }],
            &snapshot,
            None,
            responses_tool_choice(&snapshot, false),
            false,
        );

        let error = validate_serialized_responses_request(&request)
            .expect_err("whitespace serialized call_id must fail");
        assert!(
            error.to_string().contains("blank call_id"),
            "unexpected error: {error}"
        );
    }

    #[tokio::test]
    async fn configured_timeout_is_enforced_for_sse_requests() {
        let timeout = request_with_timeout(
            Duration::from_millis(20),
            futures_lite::future::pending::<Result<(), zenwave::Error>>(),
        )
        .await;
        assert!(matches!(timeout, Err(OpenAIError::Timeout)));
    }

    #[test]
    fn chat_usage_mapping_keeps_reasoning_and_cache_tokens() {
        let usage = ChatCompletionUsage {
            prompt_tokens: Some(11),
            completion_tokens: Some(7),
            total_tokens: Some(18),
            prompt_tokens_details: Some(ChatPromptTokensDetails {
                cached_tokens: Some(3),
            }),
            completion_tokens_details: Some(ChatCompletionTokensDetails {
                reasoning_tokens: Some(2),
            }),
        };
        let mapped = usage_from_chat_completion(&usage);
        assert_eq!(mapped.prompt_tokens, Some(11));
        assert_eq!(mapped.completion_tokens, Some(7));
        assert_eq!(mapped.total_tokens, Some(18));
        assert_eq!(mapped.reasoning_tokens, Some(2));
        assert_eq!(mapped.cache_read_tokens, Some(3));
    }

    #[test]
    fn responses_usage_mapping_keeps_reasoning_and_cache_tokens() {
        let usage = ResponsesUsage {
            input_tokens: Some(19),
            output_tokens: Some(5),
            total_tokens: Some(24),
            input_token_details: Some(ResponsesInputTokenDetails {
                cached_tokens: Some(4),
            }),
            output_token_details: Some(ResponsesOutputTokenDetails {
                reasoning_tokens: Some(1),
            }),
        };
        let mapped = usage_from_responses(&usage);
        assert_eq!(mapped.prompt_tokens, Some(19));
        assert_eq!(mapped.completion_tokens, Some(5));
        assert_eq!(mapped.total_tokens, Some(24));
        assert_eq!(mapped.reasoning_tokens, Some(1));
        assert_eq!(mapped.cache_read_tokens, Some(4));
    }
}

fn filter_tool_definitions(
    defs: Vec<aither_core::llm::tool::ToolDefinition>,
    choice: &ToolChoice,
) -> Vec<aither_core::llm::tool::ToolDefinition> {
    match choice {
        ToolChoice::None => Vec::new(),
        ToolChoice::Exact(name) => defs
            .into_iter()
            .filter(|tool| tool.name() == name)
            .collect(),
        ToolChoice::Auto | ToolChoice::Required => defs,
    }
}

/// Builder for [`OpenAI`] clients.
#[derive(Debug)]
pub struct Builder {
    api_key: String,
    base_url: String,
    api_kind: ApiKind,
    chat_model: String,
    embedding_model: String,
    embedding_dimensions: usize,
    image_model: String,
    audio_model: String,
    audio_voice: String,
    audio_format: String,
    transcription_model: String,
    moderation_model: String,
    legacy_max_tokens: bool,
    organization: Option<String>,
    native_abilities: Vec<Ability>,
    retry: RetryConfig,
    request_timeout: Duration,
    extra_headers: Vec<(String, String)>,
    store: Option<bool>,
}

/// Default request timeout (5 minutes - generous for long completions).
const DEFAULT_REQUEST_TIMEOUT: Duration = Duration::from_secs(300);

impl Builder {
    fn new(api_key: impl Into<String>) -> Self {
        Self {
            api_key: api_key.into(),
            base_url: DEFAULT_BASE_URL.to_string(),
            api_kind: ApiKind::Responses,
            chat_model: DEFAULT_MODEL.to_string(),
            embedding_model: DEFAULT_EMBEDDING_MODEL.to_string(),
            embedding_dimensions: DEFAULT_EMBEDDING_DIM,
            image_model: DEFAULT_IMAGE_MODEL.to_string(),
            audio_model: DEFAULT_AUDIO_MODEL.to_string(),
            audio_voice: DEFAULT_AUDIO_VOICE.to_string(),
            audio_format: DEFAULT_AUDIO_FORMAT.to_string(),
            transcription_model: DEFAULT_TRANSCRIPTION_MODEL.to_string(),
            moderation_model: DEFAULT_MODERATION_MODEL.to_string(),
            legacy_max_tokens: false,
            organization: None,
            native_abilities: Vec::new(),
            retry: RetryConfig::default(),
            request_timeout: DEFAULT_REQUEST_TIMEOUT,
            extra_headers: Vec::new(),
            store: None,
        }
    }

    /// Set a custom API base URL.
    #[must_use]
    pub fn base_url(mut self, url: impl Into<String>) -> Self {
        self.base_url = url.into();
        self
    }

    /// Use the recommended Responses API.
    #[must_use]
    pub const fn use_responses_api(mut self) -> Self {
        self.api_kind = ApiKind::Responses;
        self
    }

    /// Use the legacy Chat Completions API (deprecated by `OpenAI`).
    #[must_use]
    pub const fn use_chat_completions_api(mut self) -> Self {
        self.api_kind = ApiKind::ChatCompletions;
        self
    }

    /// Send deprecated `max_tokens` alongside `max_completion_tokens` for compatibility.
    ///
    /// `OpenAI` deprecates `max_tokens` and it is incompatible with reasoning models.
    #[must_use]
    pub const fn legacy_max_tokens(mut self, enabled: bool) -> Self {
        self.legacy_max_tokens = enabled;
        self
    }

    /// Select a model identifier (e.g., `gpt-4o-mini`, `o1-mini`).
    #[must_use]
    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.chat_model = sanitize_model(model);
        self
    }

    /// Select the embeddings model identifier.
    #[must_use]
    pub fn embedding_model(mut self, model: impl Into<String>) -> Self {
        let model = sanitize_model(model);
        if let Some(dim) = infer_embedding_dim(&model) {
            self.embedding_dimensions = dim;
        }
        self.embedding_model = model;
        self
    }

    /// Override the embedding vector dimension.
    #[must_use]
    pub const fn embedding_dimensions(mut self, dimensions: usize) -> Self {
        self.embedding_dimensions = dimensions;
        self
    }

    /// Select the image model identifier.
    #[must_use]
    pub fn image_model(mut self, model: impl Into<String>) -> Self {
        self.image_model = sanitize_model(model);
        self
    }

    /// Select the default audio generation model.
    #[must_use]
    pub fn audio_model(mut self, model: impl Into<String>) -> Self {
        self.audio_model = sanitize_model(model);
        self
    }

    /// Override the default TTS voice.
    #[must_use]
    pub fn audio_voice(mut self, voice: impl Into<String>) -> Self {
        self.audio_voice = voice.into();
        self
    }

    /// Override the synthesized audio format.
    #[must_use]
    pub fn audio_format(mut self, format: impl Into<String>) -> Self {
        self.audio_format = format.into();
        self
    }

    /// Select the transcription model identifier.
    #[must_use]
    pub fn transcription_model(mut self, model: impl Into<String>) -> Self {
        self.transcription_model = sanitize_model(model);
        self
    }

    /// Select the moderation model identifier.
    #[must_use]
    pub fn moderation_model(mut self, model: impl Into<String>) -> Self {
        self.moderation_model = sanitize_model(model);
        self
    }

    /// Attach an `OpenAI` organization header.
    #[must_use]
    pub fn organization(mut self, organization: impl Into<String>) -> Self {
        self.organization = Some(organization.into());
        self
    }

    /// Attach an extra header sent with every request.
    ///
    /// Used by endpoints that authenticate or route on custom headers (for
    /// example the `ChatGPT` Codex backend's `ChatGPT-Account-Id`). Headers set
    /// here are sent alongside the built-in `Authorization` header.
    #[must_use]
    pub fn extra_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.extra_headers.push((name.into(), value.into()));
        self
    }

    /// Set the Responses API `store` field.
    ///
    /// `store: false` disables server-side response persistence. Some
    /// backends (the `ChatGPT` Codex endpoint) require it to be present and
    /// false; the default `None` omits the field entirely.
    #[must_use]
    pub const fn store(mut self, store: bool) -> Self {
        self.store = Some(store);
        self
    }

    /// Declare extra native capabilities (e.g., web search, PDF understanding) supported by the upstream model.
    #[must_use]
    pub fn native_capabilities(mut self, abilities: impl IntoIterator<Item = Ability>) -> Self {
        for ability in abilities {
            if !self.native_abilities.contains(&ability) {
                self.native_abilities.push(ability);
            }
        }
        self
    }

    /// Mark this model as having built-in web search support.
    #[must_use]
    pub fn enable_native_web_search(self) -> Self {
        self.native_capabilities([Ability::WebSearch])
    }

    /// Mark this model as having native PDF/document understanding.
    #[must_use]
    pub fn enable_native_pdf(self) -> Self {
        self.native_capabilities([Ability::Pdf])
    }

    /// Configure retry behavior for failed requests.
    ///
    /// By default, requests are retried up to 3 times with exponential backoff.
    /// Retries happen on network errors and certain HTTP status codes (429, 500, 502, 503, 504).
    #[must_use]
    pub const fn retry(mut self, config: RetryConfig) -> Self {
        self.retry = config;
        self
    }

    /// Set maximum number of retry attempts.
    #[must_use]
    pub const fn max_retries(mut self, max_retries: u32) -> Self {
        self.retry.max_retries = max_retries;
        self
    }

    /// Disable retries entirely.
    #[must_use]
    pub fn no_retry(mut self) -> Self {
        self.retry = RetryConfig::none();
        self
    }

    /// Set the request timeout.
    ///
    /// Default is 5 minutes, which is generous for long completions.
    /// The timeout applies to the entire request, including connection and response streaming.
    #[must_use]
    pub const fn timeout(mut self, timeout: Duration) -> Self {
        self.request_timeout = timeout;
        self
    }

    /// Consume the builder and create an [`OpenAI`] client.
    #[must_use]
    pub fn build(self) -> OpenAI {
        OpenAI {
            inner: Arc::new(Config {
                api_key: self.api_key,
                base_url: self.base_url,
                api_kind: self.api_kind,
                chat_model: self.chat_model,
                embedding_model: self.embedding_model,
                embedding_dimensions: self.embedding_dimensions,
                image_model: self.image_model,
                audio_model: self.audio_model,
                audio_voice: self.audio_voice,
                audio_format: self.audio_format,
                transcription_model: self.transcription_model,
                moderation_model: self.moderation_model,
                legacy_max_tokens: self.legacy_max_tokens,
                organization: self.organization,
                native_abilities: self.native_abilities,
                retry: self.retry,
                request_timeout: self.request_timeout,
                extra_headers: self.extra_headers,
                store: self.store,
            }),
        }
    }
}

#[derive(Debug, Clone)]
pub struct Config {
    pub(crate) api_key: String,
    pub(crate) base_url: String,
    pub(crate) api_kind: ApiKind,
    pub(crate) chat_model: String,
    pub(crate) embedding_model: String,
    pub(crate) embedding_dimensions: usize,
    pub(crate) image_model: String,
    pub(crate) audio_model: String,
    pub(crate) audio_voice: String,
    pub(crate) audio_format: String,
    pub(crate) transcription_model: String,
    pub(crate) moderation_model: String,
    pub(crate) legacy_max_tokens: bool,
    pub(crate) organization: Option<String>,
    pub(crate) native_abilities: Vec<Ability>,
    pub(crate) retry: RetryConfig,
    pub(crate) request_timeout: Duration,
    pub(crate) extra_headers: Vec<(String, String)>,
    pub(crate) store: Option<bool>,
}

impl Config {
    pub(crate) fn request_url(&self, path: &str) -> String {
        format!(
            "{}/{}",
            self.base_url.trim_end_matches('/'),
            path.trim_start_matches('/')
        )
    }

    pub(crate) fn request_auth(&self) -> String {
        format!("Bearer {}", self.api_key)
    }
}

fn sanitize_model(model: impl Into<String>) -> String {
    model.into().trim().to_string()
}

fn infer_embedding_dim(model: &str) -> Option<usize> {
    match model {
        "text-embedding-3-large" => Some(3072),
        "text-embedding-3-small" | "text-embedding-ada-002" => Some(1536),
        _ => None,
    }
}