aimux-core 0.5.0

Core abstractions for aimux: LanguageModel trait, message types, and user-facing API
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
//! User-facing API: `generate_text` and `stream_text`.
//!
//! These are the primary entry points for users. They:
//! 1. Convert user prompt (`ModelPrompt`) to `LanguageModelPrompt` (provider-facing).
//! 2. Build `CallOptions` from user-facing options.
//! 3. Call `LanguageModel::do_generate` / `do_stream`.
//! 4. Extract / wrap the result for the user.

use std::collections::HashMap;
use std::pin::Pin;
use std::time::Instant;

use futures::{Stream, StreamExt};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tracing::Instrument;
use ts_rs::TS;

use crate::error::AiMuxError;
use crate::language_model::LanguageModel;
use crate::language_model_message::convert_to_language_model_prompt;
use crate::message::{ModelMessage, ModelPrompt};
use crate::options::{CallOptions, ResponseFormat, ToolChoice};
use crate::parse_tool_call::{RawToolCall, ToolCallRepair, parse_tool_call};
use crate::result::{
    FilePart, GenerateContent, GenerateResult, ReasoningPart, SourcePart, StreamResult,
    StreamTextResultAggregated,
};
use crate::stream_part::StreamPart;
use crate::tool::Tool;
use crate::types::{FinishReason, ReasoningEffort, Usage, Warning};
use crate::{AbortSignal, retry, timeout};

/// Matches the AI SDK's `isOutputChunk`: only chunks containing model output
/// start or reset the first/chunk output timers.
fn is_output_chunk(part: &StreamPart) -> bool {
    match part {
        StreamPart::TextDelta { delta, .. }
        | StreamPart::ReasoningDelta { delta, .. }
        | StreamPart::ToolInputDelta { delta, .. } => !delta.is_empty(),
        StreamPart::ToolCall { .. } | StreamPart::File { .. } => true,
        _ => false,
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// User-facing options
// ─────────────────────────────────────────────────────────────────────────────

/// User-facing options for `generate_text` and `stream_text`.
///
/// Unlike `CallOptions` (provider-facing), this does not include `prompt`
/// (passed separately) and defaults are more ergonomic.
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[ts(export)]
pub struct GenerateTextOptions {
    pub max_output_tokens: Option<u32>,
    pub temperature: Option<f64>,
    pub stop_sequences: Option<Vec<String>>,
    pub top_p: Option<f64>,
    pub top_k: Option<f64>,
    pub presence_penalty: Option<f64>,
    pub frequency_penalty: Option<f64>,
    pub response_format: Option<ResponseFormat>,
    #[ts(type = "number | null")]
    pub seed: Option<u64>,
    pub tools: Option<Vec<Tool>>,
    pub tool_choice: Option<ToolChoice>,
    pub headers: Option<HashMap<String, String>>,
    pub provider_options: Option<HashMap<String, Value>>,
    /// Top-level reasoning effort.
    pub reasoning: Option<ReasoningEffort>,
    /// System instructions prepended to the prompt.
    pub instructions: Option<String>,
    /// Per-call request body overrides (deep-merged). See RFC-0017.
    pub body_overrides: Option<Value>,
    /// Per-call retry count override. `None` = provider default, `Some(0)` = disable.
    pub max_retries: Option<u32>,
    /// Per-call timeout configuration (total / first-chunk / chunk idle).
    pub timeout: Option<crate::options::TimeoutConfiguration>,
    /// Session identifier, for grouping consecutive calls into a session
    /// (observability, see RFC-0024). Orthogonal to RFC-0019 session-affinity
    /// headers. When `None` and the optional session inferer is enabled, one
    /// may be inferred from prompt-prefix continuation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub session_id: Option<String>,
    /// Abort signal for cancelling the call.
    ///
    /// Runtime handle — never crosses the JSON boundary. Rust callers set it
    /// directly; the Node binding bridges a JS `AbortSignal` natively.
    #[serde(skip)]
    #[ts(skip)]
    pub abort_signal: Option<AbortSignal>,
    /// Emit raw provider stream chunks as `StreamPart::Raw` (debugging aid).
    pub include_raw_chunks: Option<bool>,
    /// Optional one-shot repair callback for unknown, malformed, or
    /// schema-invalid tool calls.
    #[serde(skip)]
    #[ts(skip)]
    pub repair_tool_call: Option<ToolCallRepair>,
}

impl GenerateTextOptions {
    /// Build the provider-facing `CallOptions` from user-facing options.
    #[must_use]
    pub fn into_call_options(
        self,
        prompt: crate::language_model_message::LanguageModelPrompt,
    ) -> CallOptions {
        CallOptions {
            prompt,
            max_output_tokens: self.max_output_tokens,
            temperature: self.temperature,
            stop_sequences: self.stop_sequences,
            top_p: self.top_p,
            top_k: self.top_k,
            presence_penalty: self.presence_penalty,
            frequency_penalty: self.frequency_penalty,
            response_format: self.response_format,
            seed: self.seed,
            tools: self.tools,
            tool_choice: self.tool_choice.unwrap_or_default(),
            headers: self.headers,
            provider_options: self.provider_options,
            reasoning: self.reasoning,
            body_overrides: self.body_overrides,
            max_retries: self.max_retries,
            timeout: self.timeout,
            session_id: self.session_id,
            abort_signal: self.abort_signal,
            call_id: None,
            recording_context: None,
            include_raw_chunks: self.include_raw_chunks,
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// User-facing result types
// ─────────────────────────────────────────────────────────────────────────────

/// Result of `generate_text` (user-facing).
#[derive(Debug, Serialize, Deserialize, TS)]
#[ts(export)]
pub struct GenerateTextResult {
    /// The generated text (concatenated from all text content parts).
    pub text: String,
    /// Tool calls requested by the model.
    pub tool_calls: Vec<crate::tool::ToolCall>,
    /// Why generation stopped.
    pub finish_reason: FinishReason,
    /// Token usage.
    pub usage: Usage,
    /// Warnings from the provider.
    pub warnings: Vec<Warning>,
    /// Raw provider result (for advanced use).
    pub raw: GenerateResult,
    // ── M7: top-level aggregation (extracted from `raw.content`) ──
    /// Reasoning / thinking segments from the model.
    #[serde(default)]
    pub reasoning: Vec<ReasoningPart>,
    /// Concatenated reasoning text (convenience for `reasoning.iter().map(text).join("")`).
    #[serde(default)]
    pub reasoning_text: String,
    /// Sources / citations (search-preview models).
    #[serde(default)]
    pub sources: Vec<SourcePart>,
    /// Files generated by the model.
    #[serde(default)]
    pub files: Vec<FilePart>,
    /// Assistant messages ready to append to the prompt for the next turn
    /// (solves the multi-turn "manually build assistant message" footgun).
    #[serde(default)]
    pub response_messages: Vec<ModelMessage>,
    /// The raw provider-specific finish reason string (e.g. "stop",
    /// "end_turn", "safety"). Useful when `finish_reason.unified` is `Other`.
    #[serde(default)]
    pub raw_finish_reason: Option<String>,
    /// Provider-specific metadata (e.g. Anthropic cache info). Mirrored from
    /// `raw.provider_metadata` for top-level convenience.
    #[serde(default)]
    pub provider_metadata: Option<Value>,
    /// Response metadata (id, timestamp, model_id). Mirrored from
    /// `raw.response` for top-level convenience.
    #[serde(default)]
    pub response: crate::types::ResponseMetadata,
    /// Total token usage across all steps. In single-step mode (aimux's
    /// default), `total_usage` equals `usage`. Provided for AI SDK parity.
    #[serde(default)]
    pub total_usage: Usage,
}

/// Result of `generate_object` (user-facing, M12). The parsed JSON object plus
/// convenience fields from the underlying `generate_text` call.
#[derive(Debug, Serialize, Deserialize, TS)]
#[ts(export)]
pub struct GenerateObjectResult {
    /// The parsed JSON object returned by the model.
    pub object: Value,
    /// Why generation stopped.
    pub finish_reason: FinishReason,
    /// Raw provider-specific finish reason string.
    #[serde(default)]
    pub raw_finish_reason: Option<String>,
    /// Token usage.
    pub usage: Usage,
    /// Warnings from the provider.
    pub warnings: Vec<Warning>,
    /// Concatenated reasoning text (if the model produced reasoning/thinking).
    #[serde(default)]
    pub reasoning: Option<String>,
    /// Provider-specific metadata (e.g. Anthropic cache info).
    #[serde(default)]
    pub provider_metadata: Option<Value>,
    /// Response metadata (id, timestamp, model_id).
    #[serde(default)]
    pub response: crate::types::ResponseMetadata,
    /// The full `generate_text` result (for advanced use).
    pub raw: GenerateTextResult,
}

/// Result of `stream_text` (user-facing).
pub struct StreamTextResult {
    /// The stream of `StreamPart` items.
    pub stream: Pin<Box<dyn Stream<Item = Result<StreamPart, AiMuxError>> + Send>>,
    /// The request body that was sent (for debugging / cache probing, RFC-0015).
    pub request_body: Option<serde_json::Value>,
    /// Response headers.
    pub response_headers: Option<HashMap<String, String>>,
}

impl std::fmt::Debug for StreamTextResult {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("StreamTextResult")
            .field("stream", &"<stream>")
            .field("request_body", &self.request_body)
            .field("response_headers", &self.response_headers)
            .finish()
    }
}

impl StreamTextResult {
    /// Consume the stream and collect all text deltas into a single `String`.
    ///
    /// # Errors
    ///
    /// Returns the stream's error part or transport error, propagated from the
    /// underlying model stream.
    pub async fn text(self) -> Result<String, AiMuxError> {
        let mut result = String::new();
        let mut saw_output = false;
        let mut saw_finish = false;
        let mut provider_error: Option<AiMuxError> = None;
        let mut stream = self.stream;
        while let Some(part) = stream.next().await {
            let part = match part {
                Ok(part) => part,
                // A recoverable frame error is data on this stream (core keeps
                // the stream alive past it); fold it into the same first-error
                // slot as a provider error event and keep consuming so the
                // trailing Finish still lands (usage, recording).
                Err(error) if error.is_recoverable_stream_error() => {
                    if provider_error.is_none() {
                        provider_error = Some(error);
                    }
                    continue;
                }
                Err(error) => return Err(error),
            };
            if is_output_chunk(&part) {
                saw_output = true;
            }
            match part {
                StreamPart::TextDelta { delta, .. } => result.push_str(&delta),
                StreamPart::Finish { .. } => {
                    saw_finish = true;
                    break;
                }
                // Keep consuming after a provider error event: a trailing
                // Finish still carries usage for the recording layer. The
                // error is returned once the stream reaches its end.
                StreamPart::Error { error } if provider_error.is_none() => {
                    provider_error = Some(error);
                }
                _ => {}
            }
        }
        if let Some(error) = provider_error {
            return Err(error);
        }
        // AI SDK semantics: an incomplete stream with no output at all is an
        // error; an incomplete stream with partial output retains the partial
        // result.
        if !saw_finish && !saw_output {
            return Err(AiMuxError::InvalidResponseData(
                "No output generated. The model stream ended without a finish chunk.".into(),
            ));
        }
        Ok(result)
    }

    /// Consume the full stream and return an aggregated result (M11).
    ///
    /// Unlike [`text`](Self::text) (which returns only the concatenated text),
    /// this collects reasoning, tool calls, sources, files, usage, finish
    /// reason, and builds `response_messages` for the next turn.
    ///
    /// # Errors
    ///
    /// Returns the stream's error part or transport error, propagated from the
    /// underlying model stream.
    pub async fn consume(self) -> Result<StreamTextResultAggregated, AiMuxError> {
        let mut text = String::new();
        let mut tool_calls: Vec<crate::tool::ToolCall> = Vec::new();
        let mut sources: Vec<SourcePart> = Vec::new();
        let mut files: Vec<FilePart> = Vec::new();
        let mut warnings: Vec<Warning> = Vec::new();
        let mut finish_reason = FinishReason {
            unified: crate::types::FinishReasonUnified::Stop,
            raw: None,
        };
        let mut raw_finish_reason: Option<String> = None;
        let mut usage = Usage::default();
        let mut finish_provider_metadata: Option<Value> = None;
        let mut response: Option<crate::types::ResponseMetadata> = None;
        let mut rm = crate::response_messages::ResponseMessageBuilder::new();

        let mut saw_output = false;
        let mut saw_finish = false;
        let mut provider_error: Option<AiMuxError> = None;
        let mut stream = self.stream;
        while let Some(part) = stream.next().await {
            let part = match part {
                Ok(part) => part,
                // A recoverable frame error is data on this stream (core keeps
                // the stream alive past it); fold it into the same first-error
                // slot as a provider error event and keep consuming so the
                // trailing Finish still lands (usage, recording).
                Err(error) if error.is_recoverable_stream_error() => {
                    if provider_error.is_none() {
                        provider_error = Some(error);
                    }
                    continue;
                }
                Err(error) => return Err(error),
            };
            if is_output_chunk(&part) {
                saw_output = true;
            }
            match part {
                StreamPart::TextStart {
                    provider_metadata, ..
                } => rm.text_start(provider_metadata),
                StreamPart::TextDelta {
                    delta,
                    provider_metadata,
                    ..
                } => {
                    text.push_str(&delta);
                    rm.text_delta(&delta, provider_metadata);
                }
                StreamPart::TextEnd {
                    provider_metadata, ..
                } => rm.text_end(provider_metadata),
                StreamPart::ReasoningStart {
                    provider_metadata, ..
                } => rm.reasoning_start(provider_metadata),
                StreamPart::ReasoningDelta {
                    delta,
                    provider_metadata,
                    ..
                } => rm.reasoning_delta(&delta, provider_metadata),
                StreamPart::ReasoningEnd {
                    provider_metadata, ..
                } => rm.reasoning_end(provider_metadata),
                StreamPart::ToolCall {
                    tool_call_id,
                    tool_name,
                    input,
                    provider_executed,
                    dynamic,
                    thought_signature,
                    provider_metadata,
                    invalid,
                    error,
                    ..
                } => {
                    let call = crate::tool::ToolCall {
                        tool_call_id,
                        tool_name,
                        input,
                        provider_executed,
                        dynamic,
                        thought_signature,
                        provider_metadata,
                        invalid,
                        error,
                    };
                    rm.tool_call(&call);
                    tool_calls.push(call);
                }
                StreamPart::ToolResult {
                    tool_call_id,
                    tool_name,
                    result,
                    is_error,
                    preliminary,
                    dynamic,
                    provider_metadata,
                } => rm.tool_result(
                    tool_call_id,
                    tool_name,
                    result,
                    is_error,
                    preliminary,
                    dynamic,
                    provider_metadata,
                ),
                StreamPart::Source {
                    id,
                    source_type,
                    url,
                    title,
                    ..
                } => {
                    sources.push(SourcePart {
                        id,
                        source_type,
                        url,
                        title,
                    });
                }
                StreamPart::File {
                    data, media_type, ..
                } => {
                    files.push(FilePart { data, media_type });
                }
                StreamPart::StreamStart { warnings: w, .. } => {
                    warnings = w;
                }
                StreamPart::Finish {
                    finish_reason: fr,
                    usage: u,
                    provider_metadata: pm,
                } => {
                    raw_finish_reason = fr.raw.clone();
                    finish_reason = fr;
                    usage = u.clone();
                    finish_provider_metadata = pm;
                    saw_finish = true;
                    break;
                }
                // Keep consuming after a provider error event: a trailing
                // Finish still carries usage for the recording layer. The
                // error is returned once the stream reaches its end.
                StreamPart::Error { error } if provider_error.is_none() => {
                    provider_error = Some(error);
                }
                StreamPart::ResponseMetadata {
                    id,
                    timestamp,
                    model_id,
                } => {
                    response = Some(crate::types::ResponseMetadata {
                        id,
                        timestamp,
                        model_id,
                    });
                }
                _ => {}
            }
        }

        if let Some(error) = provider_error {
            return Err(error);
        }
        if !saw_finish {
            // AI SDK semantics: an incomplete stream with no output at all is
            // an error; an incomplete stream with partial output retains the
            // partial result but must not claim a normal stop.
            if !saw_output {
                return Err(AiMuxError::InvalidResponseData(
                    "No output generated. The model stream ended without a finish chunk.".into(),
                ));
            }
            finish_reason = FinishReason {
                unified: crate::types::FinishReasonUnified::Other,
                raw: None,
            };
        }

        let crate::response_messages::ResponseMessages {
            messages: response_messages,
            reasoning,
        } = rm.finish();

        let reasoning_text = reasoning
            .iter()
            .map(|r| r.text.as_str())
            .collect::<Vec<_>>()
            .join("");

        Ok(StreamTextResultAggregated {
            text,
            reasoning,
            reasoning_text,
            tool_calls,
            sources,
            files,
            finish_reason,
            raw_finish_reason,
            total_usage: usage.clone(),
            usage,
            warnings,
            provider_metadata: finish_provider_metadata,
            response,
            response_messages,
        })
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// User-facing functions
// ─────────────────────────────────────────────────────────────────────────────

/// Generate text (non-streaming).
///
/// # Example
///
/// ```no_run
/// use aimux_core::prelude::*;
///
/// # async fn example(model: &dyn LanguageModel) -> Result<(), AiMuxError> {
/// let result = generate_text(
///     model,
///     "What is Rust?",
///     GenerateTextOptions::default(),
/// ).await?;
///
/// println!("{}", result.text);
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// Returns the error produced by the model's `do_generate` call (provider API,
/// network, or abort).
pub async fn generate_text(
    model: &dyn LanguageModel,
    prompt: impl Into<ModelPrompt>,
    options: GenerateTextOptions,
) -> Result<GenerateTextResult, AiMuxError> {
    // 1. Convert user prompt to provider-facing prompt.
    let repair_tool_call = options.repair_tool_call.clone();
    let tools = options.tools.clone();
    let operation_instructions = options.instructions.clone();
    let (messages, instructions) = split_prompt(prompt.into(), operation_instructions.as_deref());
    let lm_prompt = convert_to_language_model_prompt(&messages, instructions);

    // 2. Build CallOptions.
    let mut call_options = options.into_call_options(lm_prompt);
    let operation_timeout =
        timeout::OperationTimeout::new(call_options.timeout.unwrap_or_default())?;
    let abort_signal = call_options.abort_signal.clone();
    let retries = retry::prepare_retries(
        call_options.max_retries,
        model.retry_config(),
        abort_signal.clone(),
    );

    // 2a. RFC-0023: 关闭时零成本(M2 评审)——仅在录制开启时生成 call_id
    //     并绑定 recorder 快照;传输封闭由层 A 收尾声明(P1 无层 B,barrier
    //     前提;P2 移交层 B)。
    let context = crate::recording::recorder().map(|recorder| {
        let call_id = crate::recording::new_call_id();
        call_options.call_id = Some(call_id.clone());
        let ctx = crate::recording::RecordingContext::new(call_id.clone(), recorder);
        call_options.recording_context = Some(ctx.clone());
        ctx.recorder.record_input(
            &ctx.call_id,
            &call_options,
            model.provider(),
            model.model_id(),
        );
        ctx.recorder
            .record_provider(&ctx.call_id, &model.config_snapshot());
        // 层 A 早发 closed(defense-in-depth):无 HTTP 的调用(mock/local)也
        // 能完成 barrier;真实 HTTP 的骨架 finalized=false 仍会挡写,由层 B
        // 结束再发 closed 完成。双发幂等。
        ctx.recorder.record_transport_closed(&ctx.call_id);
        ctx
    });
    let call_id = context.as_ref().map(|c| c.call_id.clone());
    let recorder = context.as_ref().map(|c| c.recorder.clone());

    // 2b. Session grouping (RFC-0024): explicit session_id first, opt-in
    //     prompt-prefix inference as fallback. Recorded before the call so
    //     failures are still part of the session. No-op unless a
    //     SessionStore is registered (init_session_store); the resolution
    //     and store lookups are cheap (once-locked env check + two RwLock
    //     reads) when no store / inferer is registered.
    if let (Some((session_id, source)), Some(store)) = (
        crate::session::resolve_session_id(
            call_options.session_id.as_deref(),
            &call_options.prompt,
        ),
        crate::session::session_store(),
    ) {
        let call = store.append(&session_id, call_options.call_id.as_deref(), source);
        // RFC-0024 P3: 录制带上会话归组信息(仅录制开启时有效;
        // recorder/call_id 同时存在才可能被写入)。
        if let (Some(recorder), Some(call_id)) = (&recorder, &call_id) {
            recorder.record_session(call_id, &session_id, call.step);
        }
    }

    // 3. Call provider, inside the "generate" span (RFC-0014 §4.1 span tree).
    //    The http layer's `http_request` span nests under this one.
    let started = Instant::now();
    let span = tracing::info_span!(
        target: "aimux_core::generate",
        "generate",
        provider = %model.provider(),
        model = %model.model_id(),
        modality = "text",
    );
    let operation = retries.retry(|| {
        if let Some(context) = &context {
            let _ = context.start_attempt();
        }
        do_generate_with_logging(model, &call_options, span.clone(), started)
    });
    let result = match timeout::run(operation, abort_signal.as_ref(), operation_timeout).await {
        Ok(r) => r,
        Err(e) => {
            if let (Some(rec), Some(call_id)) = (&recorder, &call_id) {
                rec.record_outcome(call_id, &crate::recording::OutcomeRecord::from_error(&e));
            }
            return Err(e);
        }
    };
    // 4. Extract text, tool calls, reasoning, sources, files from content.
    let mut text = String::new();
    let mut tool_calls = Vec::new();
    let mut sources = Vec::new();
    let mut files = Vec::new();
    // Build the assistant response message content parts in parallel.
    let mut rm = crate::response_messages::ResponseMessageBuilder::new();
    for content in &result.content {
        match content {
            GenerateContent::Text {
                text: t,
                provider_metadata,
            } => {
                text.push_str(t);
                rm.text(t, provider_metadata.as_ref());
            }
            GenerateContent::ToolCall {
                tool_call_id,
                tool_name,
                input,
                provider_executed,
                dynamic,
                thought_signature,
                provider_metadata,
                ..
            } => {
                let parsed = parse_tool_call(
                    RawToolCall {
                        tool_call_id: tool_call_id.clone(),
                        tool_name: tool_name.clone(),
                        input: input.clone(),
                        provider_executed: *provider_executed,
                        dynamic: *dynamic,
                        thought_signature: thought_signature.clone(),
                        provider_metadata: provider_metadata.clone(),
                    },
                    tools.as_deref(),
                    repair_tool_call.as_ref(),
                    &messages,
                    operation_instructions.as_deref(),
                )
                .await;
                rm.tool_call(&parsed);
                tool_calls.push(parsed);
            }
            GenerateContent::Reasoning {
                text: rtext,
                provider_metadata,
            } => {
                rm.reasoning(rtext, provider_metadata.as_ref());
            }
            GenerateContent::Source {
                id,
                source_type,
                url,
                title,
                ..
            } => {
                sources.push(SourcePart {
                    id: id.clone(),
                    source_type: source_type.clone(),
                    url: url.clone(),
                    title: title.clone(),
                });
            }
            GenerateContent::File {
                data, media_type, ..
            } => {
                files.push(FilePart {
                    data: data.clone(),
                    media_type: media_type.clone(),
                });
            }
            // Provider-executed results stay in the assistant message so the
            // provider can replay its own server-tool transcript next turn.
            GenerateContent::ToolResult {
                tool_call_id,
                tool_name,
                result,
                is_error,
                preliminary,
                dynamic,
                provider_metadata,
            } => {
                rm.tool_result(
                    tool_call_id.clone(),
                    tool_name.clone(),
                    result.clone(),
                    *is_error,
                    *preliminary,
                    *dynamic,
                    provider_metadata.clone(),
                );
            }
        }
    }

    let crate::response_messages::ResponseMessages {
        messages: response_messages,
        reasoning,
    } = rm.finish();
    let reasoning_text = reasoning
        .iter()
        .map(|r| r.text.as_str())
        .collect::<Vec<_>>()
        .join("");

    // Parsing and optional repair are part of the user operation. Record a
    // successful outcome only after that phase has completed within the same
    // deadline/cancellation scope as the provider call.
    if let (Some(rec), Some(call_id)) = (&recorder, &call_id) {
        rec.record_outcome(
            call_id,
            &crate::recording::OutcomeRecord::from_generate_result(&result),
        );
    }

    // Extract fields before moving `result` into `raw`.
    let raw_finish_reason = result.finish_reason.raw.clone();
    let provider_metadata = result.provider_metadata.clone();
    let response = result.response.clone();
    let usage = result.usage.clone();

    Ok(GenerateTextResult {
        text,
        tool_calls,
        finish_reason: result.finish_reason.clone(),
        usage: usage.clone(),
        warnings: result.warnings.clone(),
        raw: result,
        reasoning,
        reasoning_text,
        sources,
        files,
        response_messages,
        raw_finish_reason,
        provider_metadata,
        response,
        total_usage: usage,
    })
}

/// Generate a structured JSON object (M12). Uses `generate_text` with
/// `response_format: Json`, then parses the model output into a JSON value.
///
/// The schema is passed via `options.response_format = ResponseFormat::Json {
/// schema: Some(schema), .. }`. JSON repair (`fix_json`) is applied before
/// parsing to handle truncated or slightly malformed model output. No schema
/// validation is performed (weak validation — relies on the model following
/// the schema; use retries for robustness).
///
/// # Example
/// ```
/// # use aimux_core::prelude::*;
/// # use aimux_core::options::ResponseFormat;
/// # async fn example(model: &dyn aimux_core::LanguageModel) -> Result<(), aimux_core::error::AiMuxError> {
/// let result = generate_object(
///     model,
///     "Extract: John is 25 years old.",
///     GenerateTextOptions {
///         response_format: Some(ResponseFormat::Json {
///             schema: Some(serde_json::json!({
///                 "type": "object",
///                 "properties": { "name": { "type": "string" }, "age": { "type": "number" } },
///                 "required": ["name", "age"]
///             })),
///             name: None,
///             description: None,
///         }),
///         ..Default::default()
///     },
/// ).await?;
/// assert_eq!(result.object["name"], "John");
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// Propagates `generate_text` failures; additionally returns `JsonParse` when
/// the (repaired) model output is not valid JSON.
pub async fn generate_object(
    model: &dyn LanguageModel,
    prompt: impl Into<ModelPrompt>,
    options: GenerateTextOptions,
) -> Result<GenerateObjectResult, AiMuxError> {
    let text_result = generate_text(model, prompt, options).await?;
    let repaired = crate::json_repair::fix_json(&text_result.text);
    let object: Value = serde_json::from_str(&repaired).map_err(|e| {
        AiMuxError::JsonParse(format!(
            "generateObject: model output is not valid JSON after repair: {e}"
        ))
    })?;
    let raw_finish_reason = text_result.raw_finish_reason.clone();
    let finish_reason = text_result.finish_reason.clone();
    let usage = text_result.usage.clone();
    let warnings = text_result.warnings.clone();
    let reasoning = if text_result.reasoning_text.is_empty() {
        None
    } else {
        Some(text_result.reasoning_text.clone())
    };
    let provider_metadata = text_result.raw.provider_metadata.clone();
    let response = text_result.raw.response.clone();
    Ok(GenerateObjectResult {
        object,
        finish_reason,
        raw_finish_reason,
        usage,
        warnings,
        reasoning,
        provider_metadata,
        response,
        raw: text_result,
    })
}

/// Extract the reasoning signature from provider metadata, checking all known
/// provider keys (Anthropic, Bedrock).
/// Stream text from the model.
///
/// # Example
///
/// ```no_run
/// use aimux_core::prelude::*;
/// use futures::StreamExt;
///
/// # async fn example(model: &dyn LanguageModel) -> Result<(), AiMuxError> {
/// let result = stream_text(
///     model,
///     "Write a haiku about Rust.",
///     GenerateTextOptions::default(),
/// ).await?;
///
/// let mut stream = result.stream;
/// while let Some(part) = stream.next().await {
///     if let StreamPart::TextDelta { delta, .. } = part? {
///         print!("{}", delta);
///     }
/// }
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// Returns setup failures from the model's `do_stream` call. Provider error
/// events surface as `Ok(StreamPart::Error { .. })`; transport failures,
/// caller cancellation, and Core timeouts surface as `Err(AiMuxError)` stream
/// items.
pub async fn stream_text(
    model: &dyn LanguageModel,
    prompt: impl Into<ModelPrompt>,
    options: GenerateTextOptions,
) -> Result<StreamTextResult, AiMuxError> {
    // 1. Convert user prompt to provider-facing prompt.
    let repair_tool_call = options.repair_tool_call.clone();
    let tools = options.tools.clone();
    let operation_instructions = options.instructions.clone();
    let (messages, instructions) = split_prompt(prompt.into(), operation_instructions.as_deref());
    let lm_prompt = convert_to_language_model_prompt(&messages, instructions);

    // 2. Build CallOptions.
    let mut call_options = options.into_call_options(lm_prompt);
    let stream_timeout = call_options.timeout.unwrap_or_default();
    let operation_timeout = timeout::OperationTimeout::new(stream_timeout)?;
    // Armed at operation start: providers await their first SSE event inside
    // do_stream, so the first-chunk budget must already be counting there —
    // a 200-then-silence server is otherwise unbounded when only
    // first_chunk_ms is configured.
    let first_chunk_deadline = stream_timeout
        .first_chunk_ms
        .map(|duration_ms| timeout::TimeoutDeadline::from_now("First chunk", duration_ms))
        .transpose()?;
    let abort_signal = call_options.abort_signal.clone();
    let retries = retry::prepare_retries(
        call_options.max_retries,
        model.retry_config(),
        abort_signal.clone(),
    );

    // 2a. RFC-0023: 关闭时零成本(M2 评审)——仅在录制开启时生成 call_id
    //     并绑定 recorder 快照;传输封闭由层 A 收尾声明(P1 无层 B,barrier
    //     前提;P2 移交层 B)。
    let context = crate::recording::recorder().map(|recorder| {
        let call_id = crate::recording::new_call_id();
        call_options.call_id = Some(call_id.clone());
        let ctx = crate::recording::RecordingContext::new(call_id.clone(), recorder);
        call_options.recording_context = Some(ctx.clone());
        ctx.recorder.record_input(
            &ctx.call_id,
            &call_options,
            model.provider(),
            model.model_id(),
        );
        ctx.recorder
            .record_provider(&ctx.call_id, &model.config_snapshot());
        // 层 A 早发 closed(defense-in-depth):无 HTTP 的调用(mock/local)也
        // 能完成 barrier;真实 HTTP 的骨架 finalized=false 仍会挡写,由层 B
        // 结束再发 closed 完成。双发幂等。
        ctx.recorder.record_transport_closed(&ctx.call_id);
        ctx
    });
    let call_id = context.as_ref().map(|c| c.call_id.clone());
    let recorder = context.as_ref().map(|c| c.recorder.clone());

    // 2b. Session grouping (RFC-0024): explicit session_id first, opt-in
    //     prompt-prefix inference as fallback. Recorded before the call so
    //     failures are still part of the session. No-op unless a
    //     SessionStore is registered (init_session_store); the resolution
    //     and store lookups are cheap (once-locked env check + two RwLock
    //     reads) when no store / inferer is registered.
    if let (Some((session_id, source)), Some(store)) = (
        crate::session::resolve_session_id(
            call_options.session_id.as_deref(),
            &call_options.prompt,
        ),
        crate::session::session_store(),
    ) {
        let call = store.append(&session_id, call_options.call_id.as_deref(), source);
        // RFC-0024 P3: 录制带上会话归组信息(仅录制开启时有效;
        // recorder/call_id 同时存在才可能被写入)。
        if let (Some(recorder), Some(call_id)) = (&recorder, &call_id) {
            recorder.record_session(call_id, &session_id, call.step);
        }
    }

    // 3. Call provider, inside the "generate" span (RFC-0014 §4.1 span tree).
    let span = tracing::info_span!(
        target: "aimux_core::generate",
        "generate",
        provider = %model.provider(),
        model = %model.model_id(),
        modality = "text",
    );
    let operation = retries.retry(|| {
        if let Some(context) = &context {
            let _ = context.start_attempt();
        }
        // Call-level retry boundary: once do_stream returns a stream, retry
        // no longer recreates the provider request; the returned stream is
        // consumed by the stream-step timeout logic below.
        model.do_stream(&call_options).instrument(span.clone())
    });
    let setup_deadline = timeout::min_deadline(operation_timeout.deadline(), first_chunk_deadline);
    let result: StreamResult =
        match timeout::run_until(operation, abort_signal.as_ref(), setup_deadline).await {
            Ok(r) => r,
            Err(e) => {
                if let (Some(rec), Some(call_id)) = (&recorder, &call_id) {
                    rec.record_outcome(call_id, &crate::recording::OutcomeRecord::from_error(&e));
                }
                return Err(e);
            }
        };
    // 解构避免部分 move(result 各字段去向不同)。
    let StreamResult {
        stream,
        request_body,
        response_headers,
    } = result;

    // The consumption phase keeps observing the same deadlines: the
    // first-chunk budget armed at operation start continues until the first
    // output chunk.
    let operation_deadline = operation_timeout.deadline();
    // A pump task drives the provider stream, so the deadlines below measure
    // when provider output *arrives*: a deadline observed only inside
    // `poll_next` keeps elapsing while nobody polls, charging the provider
    // for the consumer's latency. The channel is unbounded so the pump never
    // blocks on the consumer (which would put that latency back into the
    // timers); the AI SDK's `streamText` consumes eagerly for the same
    // reason, and buffering is bounded by the response itself.
    //
    // Spawned even with no abort signal or deadlines armed: the pump is also
    // the stream's terminal fuse — `Finish` and non-recoverable errors must
    // end the stream under every configuration, or a provider that keeps the
    // connection open after `Finish` hangs a consumer reading to end-of-stream.
    let stream: Pin<Box<dyn Stream<Item = Result<StreamPart, AiMuxError>> + Send>> = {
        let mut stream = stream;
        let chunk_ms = stream_timeout.chunk_ms;
        let pump_abort = abort_signal.clone();
        let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
        let pump = tokio::spawn(async move {
            let mut chunk_deadline = first_chunk_deadline;

            loop {
                let next = tokio::select! {
                    biased;
                    // Dropping the provider stream cancels the in-flight HTTP
                    // exchange; the consumer side reports the abort error.
                    () = timeout::wait_for_abort(pump_abort.as_ref()) => break,
                    () = timeout::wait_for_deadline(operation_deadline) => {
                        let _ = tx.send(Err(operation_deadline
                            .expect("operation deadline future only resolves for a deadline")
                            .error()));
                        break;
                    }
                    () = timeout::wait_for_deadline(chunk_deadline) => {
                        let _ = tx.send(Err(chunk_deadline
                            .expect("chunk deadline future only resolves for a deadline")
                            .error()));
                        break;
                    }
                    item = stream.next() => item,
                };

                let Some(item) = next else { break };
                let terminal = is_terminal_item(&item);
                if let Ok(part) = &item
                    && is_output_chunk(part)
                {
                    chunk_deadline = match chunk_ms
                        .map(|duration_ms| timeout::TimeoutDeadline::from_now("Chunk", duration_ms))
                        .transpose()
                    {
                        Ok(deadline) => deadline,
                        Err(error) => {
                            let _ = tx.send(Err(error));
                            break;
                        }
                    };
                }

                // A closed receiver means the consumer dropped the stream.
                if tx.send(item).is_err() || terminal {
                    break;
                }
            }
        });
        let pump = AbortOnDrop(pump);
        Box::pin(async_stream::stream! {
            let _pump = pump;
            loop {
                let next = tokio::select! {
                    biased;
                    () = timeout::wait_for_abort(abort_signal.as_ref()) => {
                        yield Err(AiMuxError::from_abort_signal(
                            abort_signal.as_ref().expect("abort future only resolves for a signal"),
                        ));
                        break;
                    }
                    item = rx.recv() => item,
                };
                let Some(item) = next else { break };
                // Mirror the pump's fuse here so the stream ends right after
                // the terminal item, ahead of a later abort or the channel
                // close racing it.
                let terminal = is_terminal_item(&item);
                yield item;
                if terminal {
                    break;
                }
            }
        })
    };
    let mut stream = stream;
    let stream: Pin<Box<dyn Stream<Item = Result<StreamPart, AiMuxError>> + Send>> =
        Box::pin(async_stream::stream! {
            while let Some(item) = stream.next().await {
                match item {
                    Ok(StreamPart::ToolCall {
                        tool_call_id,
                        tool_name,
                        input,
                        provider_executed,
                        dynamic,
                        thought_signature,
                        provider_metadata,
                        ..
                    }) => {
                        let parsed = parse_tool_call(
                            RawToolCall {
                                tool_call_id,
                                tool_name,
                                input: crate::parse_tool_call::raw_tool_input(&input),
                                provider_executed,
                                dynamic,
                                thought_signature,
                                provider_metadata,
                            },
                            tools.as_deref(),
                            repair_tool_call.as_ref(),
                            &messages,
                            operation_instructions.as_deref(),
                        ).await;
                        yield Ok(StreamPart::ToolCall {
                            tool_call_id: parsed.tool_call_id,
                            tool_name: parsed.tool_name,
                            input: parsed.input,
                            provider_executed: parsed.provider_executed,
                            dynamic: parsed.dynamic,
                            thought_signature: parsed.thought_signature,
                            invalid: parsed.invalid,
                            error: parsed.error,
                            provider_metadata: parsed.provider_metadata,
                        });
                    }
                    item => yield item,
                }
            }
        });
    // 录制开启时才包装(终结时写 outcome + 传输封闭);关闭时零成本透传。
    let stream = crate::recording::RecordingOutcomeStream::new(
        stream,
        recorder.clone(),
        call_id.unwrap_or_default(),
    );

    // 4. Return wrapped stream to user (request_body/response_headers kept for
    //    debugging / cache probing, RFC-0015).
    Ok(StreamTextResult {
        stream: Box::pin(stream),
        request_body,
        response_headers,
    })
}

/// A malformed individual frame does not prove that the transport is dead:
/// SSE framing lets a later event still be valid. `Finish` and transport/Core
/// errors are terminal so the stream is not polled past a dead source.
fn is_terminal_item(item: &Result<StreamPart, AiMuxError>) -> bool {
    matches!(item, Ok(StreamPart::Finish { .. }))
        || matches!(item, Err(error) if !error.is_recoverable_stream_error())
}

/// Aborts the stream pump task when the returned stream is dropped, so an
/// abandoned stream leaves no task driving the provider connection.
struct AbortOnDrop(tokio::task::JoinHandle<()>);

impl Drop for AbortOnDrop {
    fn drop(&mut self) {
        self.0.abort();
    }
}
/// Run `do_generate` inside the RFC-0014 `generate` span and emit the
/// `generate_end` event. A plain async fn (rather than an inline async block)
/// so the `?` error type is pinned by the declared return type.
async fn do_generate_with_logging(
    model: &dyn LanguageModel,
    call_options: &CallOptions,
    span: tracing::Span,
    started: Instant,
) -> Result<GenerateResult, AiMuxError> {
    let r = async { model.do_generate(call_options).await }
        .instrument(span)
        .await?;
    tracing::info!(
        target: "aimux_core::generate",
        ok = true,
        duration_ms = started.elapsed().as_millis() as u64,
        finish_reason = ?r.finish_reason.unified,
        "generate_end"
    );
    Ok(r)
}

/// Split a `ModelPrompt` into messages + optional instructions.
///
/// If the prompt is a plain string, it becomes a single user message.
/// If the prompt is messages, they are used as-is.
/// Instructions are passed through separately (they get prepended by
/// `convert_to_language_model_prompt`).
pub(crate) fn split_prompt(
    prompt: ModelPrompt,
    instructions: Option<&str>,
) -> (Vec<ModelMessage>, Option<&str>) {
    match prompt {
        ModelPrompt::Text(text) => (vec![ModelMessage::user(text)], instructions),
        ModelPrompt::Messages(msgs) => (msgs, instructions),
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// OpenAI Chat Completions output (RFC-0026)
// ─────────────────────────────────────────────────────────────────────────────

use crate::openai_output::{
    ChatCompletion, ChatCompletionFunction, ChatCompletionStream, ChatCompletionToolCall,
    OpenAiStreamOptions, parsed_tool_call_arguments, to_chat_completion, to_chat_completion_stream,
};

/// Generate text and return the result as an OpenAI Chat Completion.
///
/// This is the OpenAI-compatible equivalent of [`generate_text`]. Internally
/// it calls `generate_text`, then converts the [`GenerateResult`] into a
/// [`ChatCompletion`] via [`to_chat_completion`].
/// Tool calls reflect Core parsing and any configured repair; provider
/// response metadata, usage, and non-tool content are preserved from `raw`.
///
/// Works with **any** provider (OpenAI, Anthropic, Google, …) — the output is
/// always standard OpenAI Chat Completions JSON.
///
/// # Example
///
/// ```no_run
/// use aimux_core::prelude::*;
///
/// # async fn example(model: &dyn LanguageModel) -> Result<(), AiMuxError> {
/// let completion = generate_text_as_openai(
///     model,
///     "What is Rust?",
///     GenerateTextOptions::default(),
/// ).await?;
///
/// println!("{}", completion.choices[0].message.content.as_deref().unwrap());
/// println!("{}", completion.usage.prompt_tokens);
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// Propagates any error from the underlying `generate_text` call.
pub async fn generate_text_as_openai(
    model: &dyn LanguageModel,
    prompt: impl Into<ModelPrompt>,
    options: GenerateTextOptions,
) -> Result<ChatCompletion, AiMuxError> {
    let result = generate_text(model, prompt, options).await?;
    Ok(generate_text_result_to_chat_completion(
        &result,
        model.model_id(),
    ))
}

/// Convert a [`GenerateTextResult`] into an OpenAI [`ChatCompletion`].
///
/// The pure half of [`generate_text_as_openai`]: tool calls come from
/// `result.tool_calls` (so they reflect Core parsing and any repair already
/// applied to the result), everything else from `result.raw` via
/// [`to_chat_completion`]. Hosts that repair a result themselves (RFC-0035)
/// convert it with this afterwards.
///
/// `model_id` is the fallback for `ChatCompletion.model` when the provider
/// response carries no model id.
#[must_use]
pub fn generate_text_result_to_chat_completion(
    result: &GenerateTextResult,
    model_id: &str,
) -> ChatCompletion {
    let mut completion = to_chat_completion(&result.raw, model_id);
    if let Some(choice) = completion.choices.first_mut() {
        choice.message.tool_calls = if result.tool_calls.is_empty() {
            None
        } else {
            Some(
                result
                    .tool_calls
                    .iter()
                    .map(|tool_call| ChatCompletionToolCall {
                        id: tool_call.tool_call_id.clone(),
                        tool_type: "function".to_string(),
                        function: ChatCompletionFunction {
                            name: tool_call.tool_name.clone(),
                            arguments: parsed_tool_call_arguments(
                                &tool_call.input,
                                tool_call.invalid,
                                tool_call.error.as_ref(),
                            ),
                        },
                    })
                    .collect(),
            )
        };
    }
    completion
}

/// Stream text and return the result as a stream of OpenAI Chat Completion chunks.
///
/// This is the OpenAI-compatible equivalent of [`stream_text`]. Internally
/// it calls `stream_text`, then converts the `StreamPart` stream into a
/// `ChatCompletionChunk` stream via [`to_chat_completion_stream`].
///
/// Works with **any** provider (OpenAI, Anthropic, Google, …) — the output is
/// always standard OpenAI Chat Completions streaming chunks.
///
/// Tool-call argument deltas are the provider's text, forwarded verbatim as it
/// arrives, so this stream does **not** reflect tool-call repair — aligned
/// with the AI SDK, which likewise never withholds input deltas. Use
/// [`generate_text_as_openai`] or the native `stream_text` when repaired calls
/// are required.
///
/// # Example
///
/// ```no_run
/// use aimux_core::prelude::*;
/// use futures::StreamExt;
///
/// # async fn example(model: &dyn LanguageModel) -> Result<(), AiMuxError> {
/// let result = stream_text_as_openai(
///     model,
///     "Write a haiku about Rust.",
///     GenerateTextOptions::default(),
///     OpenAiStreamOptions::default(),
/// ).await?;
///
/// let mut stream = result.stream;
/// while let Some(chunk) = stream.next().await {
///     let chunk = chunk?;
///     if let Some(delta) = chunk.choices.first()
///         && let Some(content) = &delta.delta.content {
///         print!("{}", content);
///     }
/// }
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// Propagates any error from the underlying `stream_text` call.
pub async fn stream_text_as_openai(
    model: &dyn LanguageModel,
    prompt: impl Into<ModelPrompt>,
    options: GenerateTextOptions,
    stream_options: OpenAiStreamOptions,
) -> Result<ChatCompletionStream, AiMuxError> {
    let result = stream_text(model, prompt, options).await?;
    Ok(to_chat_completion_stream(
        result.stream,
        model.model_id(),
        stream_options,
    ))
}

#[cfg(test)]
mod operation_retry_tests {
    use std::sync::atomic::{AtomicUsize, Ordering};

    use async_trait::async_trait;

    use super::*;
    use crate::{ApiCallError, LanguageModel, prelude::StreamPart};

    enum StreamBehavior {
        Normal,
        FinishThenPending,
        ProviderErrorThenFinish,
        ParseErrorThenEvent,
        TransportErrorThenPending,
        NeverReturns,
        RetryableFirstError,
        NonRetryableFirstError,
    }

    struct StreamModel {
        behavior: StreamBehavior,
        calls: AtomicUsize,
    }

    impl StreamModel {
        fn new(behavior: StreamBehavior) -> Self {
            Self {
                behavior,
                calls: AtomicUsize::new(0),
            }
        }

        fn api_error(status: u16, retryable: bool) -> AiMuxError {
            AiMuxError::ApiCall(Box::new(ApiCallError {
                status_code: Some(status),
                response_headers: Some(HashMap::from([("retry-after-ms".into(), "0".into())])),
                is_retryable: retryable,
                ..ApiCallError::new(
                    "stream setup failed",
                    "https://example.test/stream",
                    serde_json::json!({}),
                )
            }))
        }

        fn single_event_stream() -> StreamResult {
            StreamResult {
                stream: Box::pin(futures::stream::iter([Ok(StreamPart::TextDelta {
                    id: "text-1".into(),
                    delta: "hello".into(),
                    provider_metadata: None,
                })])),
                request_body: None,
                response_headers: None,
            }
        }

        fn finish_stream() -> StreamResult {
            let finish = StreamPart::Finish {
                finish_reason: crate::types::FinishReason {
                    unified: crate::types::FinishReasonUnified::Stop,
                    raw: Some("stop".into()),
                },
                usage: Usage::default(),
                provider_metadata: None,
            };
            StreamResult {
                stream: Box::pin(
                    futures::stream::iter([Ok(finish)]).chain(futures::stream::pending()),
                ),
                request_body: None,
                response_headers: None,
            }
        }

        fn provider_error_then_finish_stream() -> StreamResult {
            let finish = StreamPart::Finish {
                finish_reason: crate::types::FinishReason {
                    unified: crate::types::FinishReasonUnified::Error,
                    raw: None,
                },
                usage: Usage::default(),
                provider_metadata: None,
            };
            StreamResult {
                stream: Box::pin(
                    futures::stream::iter([
                        Ok(StreamPart::Error {
                            error: AiMuxError::Other("provider error".into()),
                        }),
                        Ok(finish),
                    ])
                    .chain(futures::stream::pending()),
                ),
                request_body: None,
                response_headers: None,
            }
        }

        fn parse_error_then_event_stream() -> StreamResult {
            StreamResult {
                stream: Box::pin(futures::stream::iter([
                    Err(AiMuxError::JsonParse("malformed SSE data".into())),
                    Ok(StreamPart::TextDelta {
                        id: "text-1".into(),
                        delta: "after-error".into(),
                        provider_metadata: None,
                    }),
                ])),
                request_body: None,
                response_headers: None,
            }
        }
    }

    #[async_trait]
    impl LanguageModel for StreamModel {
        fn provider(&self) -> &str {
            "test"
        }

        fn model_id(&self) -> &str {
            "stream-model"
        }

        async fn do_generate(&self, _options: &CallOptions) -> Result<GenerateResult, AiMuxError> {
            Err(AiMuxError::Other("unused".into()))
        }

        async fn do_stream(&self, _options: &CallOptions) -> Result<StreamResult, AiMuxError> {
            let attempt = self.calls.fetch_add(1, Ordering::SeqCst);
            match self.behavior {
                StreamBehavior::Normal => Ok(Self::single_event_stream()),
                StreamBehavior::FinishThenPending => Ok(Self::finish_stream()),
                StreamBehavior::ProviderErrorThenFinish => {
                    Ok(Self::provider_error_then_finish_stream())
                }
                StreamBehavior::ParseErrorThenEvent => Ok(Self::parse_error_then_event_stream()),
                StreamBehavior::TransportErrorThenPending => Ok(StreamResult {
                    stream: Box::pin(
                        futures::stream::iter([Err(AiMuxError::ApiCall(Box::new(ApiCallError {
                            is_retryable: true,
                            ..ApiCallError::new(
                                "connection reset",
                                "https://example.test/stream",
                                serde_json::json!({}),
                            )
                        })))])
                        .chain(futures::stream::pending()),
                    ),
                    request_body: None,
                    response_headers: None,
                }),
                StreamBehavior::NeverReturns => std::future::pending().await,
                StreamBehavior::RetryableFirstError if attempt == 0 => {
                    Err(Self::api_error(429, true))
                }
                StreamBehavior::RetryableFirstError => Ok(Self::single_event_stream()),
                StreamBehavior::NonRetryableFirstError => Err(Self::api_error(400, false)),
            }
        }
    }

    fn retry_options() -> GenerateTextOptions {
        GenerateTextOptions {
            max_retries: Some(2),
            ..Default::default()
        }
    }

    #[tokio::test]
    async fn stream_setup_preserves_the_peeked_first_event() {
        let model = StreamModel::new(StreamBehavior::Normal);
        let mut result = stream_text(&model, "hello", retry_options()).await.unwrap();
        let first = result.stream.next().await.unwrap().unwrap();
        assert!(matches!(first, StreamPart::TextDelta { delta, .. } if delta == "hello"));
        assert!(result.stream.next().await.is_none());
    }

    #[tokio::test]
    async fn late_consumer_still_receives_a_first_chunk_delivered_on_time() {
        // The provider yields immediately; the first-chunk budget measures
        // that arrival, not when the caller gets around to polling. Sleeping
        // past the budget before the first poll must not turn delivered
        // output into a timeout.
        let model = StreamModel::new(StreamBehavior::Normal);
        let options = GenerateTextOptions {
            max_retries: Some(0),
            timeout: Some(crate::options::TimeoutConfiguration {
                first_chunk_ms: Some(1),
                ..Default::default()
            }),
            ..Default::default()
        };
        let mut result = stream_text(&model, "hello", options).await.unwrap();

        tokio::time::sleep(std::time::Duration::from_millis(20)).await;

        assert!(matches!(
            result.stream.next().await.unwrap().unwrap(),
            StreamPart::TextDelta { delta, .. } if delta == "hello"
        ));
        assert!(result.stream.next().await.is_none());
    }

    #[tokio::test]
    async fn caller_abort_still_cancels_the_returned_stream() {
        let model = StreamModel::new(StreamBehavior::Normal);
        let abort_signal = AbortSignal::new();
        let options = GenerateTextOptions {
            max_retries: Some(0),
            abort_signal: Some(abort_signal.clone()),
            ..Default::default()
        };
        let mut result = stream_text(&model, "hello", options).await.unwrap();

        abort_signal.abort();

        assert!(matches!(
            result.stream.next().await.unwrap(),
            Err(AiMuxError::Aborted(message)) if message == "request aborted"
        ));
    }

    #[tokio::test]
    async fn terminal_stream_part_stops_stream_consumption() {
        let model = StreamModel::new(StreamBehavior::FinishThenPending);
        let options = GenerateTextOptions {
            max_retries: Some(0),
            timeout: Some(crate::options::TimeoutConfiguration {
                total_ms: Some(50),
                ..Default::default()
            }),
            ..Default::default()
        };
        let mut result = stream_text(&model, "hello", options).await.unwrap();

        assert!(matches!(
            result.stream.next().await.unwrap().unwrap(),
            StreamPart::Finish { .. }
        ));
        tokio::time::sleep(std::time::Duration::from_millis(60)).await;
        assert!(result.stream.next().await.is_none());
    }

    #[tokio::test]
    async fn provider_error_part_does_not_hide_the_final_finish() {
        let model = StreamModel::new(StreamBehavior::ProviderErrorThenFinish);
        let abort_signal = AbortSignal::new();
        let options = GenerateTextOptions {
            max_retries: Some(0),
            abort_signal: Some(abort_signal.clone()),
            ..Default::default()
        };
        let mut result = stream_text(&model, "hello", options).await.unwrap();

        assert!(matches!(
            result.stream.next().await.unwrap().unwrap(),
            StreamPart::Error { .. }
        ));
        assert!(matches!(
            result.stream.next().await.unwrap().unwrap(),
            StreamPart::Finish { finish_reason, .. }
                if finish_reason.unified == crate::types::FinishReasonUnified::Error
        ));
        abort_signal.abort();
        assert!(result.stream.next().await.is_none());
    }

    #[tokio::test]
    async fn timeout_wrapper_continues_after_a_malformed_stream_frame() {
        let model = StreamModel::new(StreamBehavior::ParseErrorThenEvent);
        let options = GenerateTextOptions {
            max_retries: Some(0),
            timeout: Some(crate::options::TimeoutConfiguration {
                total_ms: Some(1_000),
                ..Default::default()
            }),
            ..Default::default()
        };
        let mut result = stream_text(&model, "hello", options).await.unwrap();

        assert!(matches!(
            result.stream.next().await.unwrap(),
            Err(AiMuxError::JsonParse(_))
        ));
        assert!(matches!(
            result.stream.next().await.unwrap().unwrap(),
            StreamPart::TextDelta { delta, .. } if delta == "after-error"
        ));
    }

    #[tokio::test]
    async fn abort_wrapper_continues_after_a_malformed_stream_frame() {
        let model = StreamModel::new(StreamBehavior::ParseErrorThenEvent);
        let options = GenerateTextOptions {
            max_retries: Some(0),
            abort_signal: Some(AbortSignal::new()),
            ..Default::default()
        };
        let mut result = stream_text(&model, "hello", options).await.unwrap();

        assert!(matches!(
            result.stream.next().await.unwrap(),
            Err(AiMuxError::JsonParse(_))
        ));
        assert!(matches!(
            result.stream.next().await.unwrap().unwrap(),
            StreamPart::TextDelta { delta, .. } if delta == "after-error"
        ));
    }

    #[tokio::test]
    async fn first_chunk_timeout_bounds_the_stream_setup_phase() {
        // Providers await their first SSE event inside do_stream; a
        // 200-then-silence server must be bounded by first_chunk_ms alone.
        let model = StreamModel::new(StreamBehavior::NeverReturns);
        let options = GenerateTextOptions {
            max_retries: Some(0),
            timeout: Some(crate::options::TimeoutConfiguration {
                first_chunk_ms: Some(5),
                ..Default::default()
            }),
            ..Default::default()
        };
        let error = stream_text(&model, "hello", options).await.unwrap_err();
        assert!(
            matches!(error, AiMuxError::Timeout(ref message) if message == "First chunk timeout of 5ms exceeded"),
            "got {error:?}"
        );
    }

    #[tokio::test]
    async fn transport_error_ends_the_wrapped_stream() {
        let model = StreamModel::new(StreamBehavior::TransportErrorThenPending);
        let abort_signal = AbortSignal::new();
        let options = GenerateTextOptions {
            max_retries: Some(0),
            abort_signal: Some(abort_signal.clone()),
            ..Default::default()
        };
        let mut result = stream_text(&model, "hello", options).await.unwrap();

        assert!(matches!(
            result.stream.next().await.unwrap(),
            Err(AiMuxError::ApiCall(_))
        ));
        // Without treating Err as terminal this next() would hang on the
        // pending tail of the dead source stream.
        assert!(result.stream.next().await.is_none());
    }

    #[tokio::test]
    async fn retryable_first_stream_error_retries_before_returning_stream() {
        let model = StreamModel::new(StreamBehavior::RetryableFirstError);
        let mut result = stream_text(&model, "hello", retry_options()).await.unwrap();
        assert_eq!(model.calls.load(Ordering::SeqCst), 2);
        assert!(matches!(
            result.stream.next().await.unwrap().unwrap(),
            StreamPart::TextDelta { delta, .. } if delta == "hello"
        ));
    }

    #[tokio::test]
    async fn non_retryable_first_stream_error_is_returned_unchanged() {
        let model = StreamModel::new(StreamBehavior::NonRetryableFirstError);
        let error = stream_text(&model, "hello", retry_options())
            .await
            .unwrap_err();
        assert!(
            matches!(error, AiMuxError::ApiCall(ref detail) if detail.status_code == Some(400))
        );
        assert_eq!(model.calls.load(Ordering::SeqCst), 1);
    }
}

#[cfg(test)]
mod stream_aggregation_tests {
    use super::*;
    use crate::types::FinishReasonUnified;

    fn result_from(parts: Vec<Result<StreamPart, AiMuxError>>) -> StreamTextResult {
        StreamTextResult {
            stream: Box::pin(futures::stream::iter(parts)),
            request_body: None,
            response_headers: None,
        }
    }

    fn delta(text: &str) -> StreamPart {
        StreamPart::TextDelta {
            id: "text-1".into(),
            delta: text.into(),
            provider_metadata: None,
        }
    }

    fn finish() -> StreamPart {
        StreamPart::Finish {
            finish_reason: FinishReason {
                unified: FinishReasonUnified::Stop,
                raw: Some("stop".into()),
            },
            usage: Usage::default(),
            provider_metadata: None,
        }
    }

    #[tokio::test]
    async fn truncated_stream_with_partial_output_does_not_claim_a_normal_stop() {
        let result = result_from(vec![Ok(delta("par"))]).consume().await.unwrap();
        assert_eq!(result.text, "par");
        assert_eq!(result.finish_reason.unified, FinishReasonUnified::Other);

        // text() retains the partial result (AI SDK semantics).
        let text = result_from(vec![Ok(delta("par"))]).text().await.unwrap();
        assert_eq!(text, "par");
    }

    #[tokio::test]
    async fn empty_incomplete_stream_is_an_error() {
        let error = result_from(vec![]).consume().await.unwrap_err();
        assert!(
            matches!(&error, AiMuxError::InvalidResponseData(message) if message.contains("without a finish chunk"))
        );
        let error = result_from(vec![]).text().await.unwrap_err();
        assert!(matches!(error, AiMuxError::InvalidResponseData(_)));
    }

    #[tokio::test]
    async fn stream_with_finish_still_reports_the_provider_finish_reason() {
        let result = result_from(vec![Ok(delta("hi")), Ok(finish())])
            .consume()
            .await
            .unwrap();
        assert_eq!(result.finish_reason.unified, FinishReasonUnified::Stop);
    }

    #[tokio::test]
    async fn provider_error_still_wins_after_draining_a_trailing_finish() {
        let parts = vec![
            Ok(delta("partial")),
            Ok(StreamPart::Error {
                error: AiMuxError::Other("provider error".into()),
            }),
            Ok(finish()),
        ];
        let error = result_from(parts).consume().await.unwrap_err();
        assert!(matches!(error, AiMuxError::Other(message) if message == "provider error"));
    }
}