turnframe-provider 0.1.0

Provider-neutral model interfaces, capability routing, fallback policy and conformance suite for Turnframe
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
//! Incremental output and its deterministic reassembly (spec §18.5, §20.8).
//!
//! [`ModelStream`] is what [`ModelProvider::stream`](crate::provider::ModelProvider::stream)
//! returns: a pinned, boxed, `Send` stream of [`StreamEvent`]s. It exists so the
//! UI can show prose as it arrives — and only prose. Invariant I16 and spec
//! §18.5 are blunt about the rest: an operational claim is never streamed
//! before commit, and interactions and receipts are emitted as whole typed
//! events, not as text the user watches assemble.
//!
//! # Reassembly is a contract, not a convenience
//!
//! [`reconstruct`] rebuilds the same [`ModelResponse`] the non-streaming call
//! would have produced. The conformance suite asserts the two are equal for the
//! same fixture (spec §20.8), which is what makes it safe for the runtime to
//! stream one purpose and not another without maintaining two parsers.
//!
//! Reassembly is deterministic and total:
//!
//! * text deltas are concatenated in arrival order into **one**
//!   [`ContentPart::Text`], placed before the tool calls;
//! * tool-call argument fragments are concatenated **per call id**, in arrival
//!   order, and the result must parse as JSON;
//! * tool calls appear in the order their [`StreamEvent::ToolCallStart`]
//!   arrived;
//! * a fragment for an unannounced call, a duplicated announcement, a second
//!   finish event or a stream that ends without one is a
//!   [`Malformed`](crate::error::ProviderErrorKind::Malformed) failure — never
//!   a best-effort partial response (I18);
//! * a [`StreamEvent::ResponseId`] sets the provider's response identifier and
//!   a [`StreamEvent::Warning`] adds a warning, so the streamed path reports
//!   the same identifier and the same dropped features the whole path does
//!   instead of the caller having to seed them out of band.
//!
//! ```
//! # use turnframe_provider::prelude::*;
//! # use turnframe_provider::stream::StreamAccumulator;
//! # async fn demo() -> Result<(), ProviderError> {
//! let events = vec![
//!     StreamEvent::text("Ho preparato "),
//!     StreamEvent::text("la modifica."),
//!     StreamEvent::Finish { reason: FinishReason::Stop },
//! ];
//! let stream = ModelStream::from_events(events);
//! let seed = StreamAccumulator::new(RequestId::nil(), "openai", "gpt-4o");
//! let response = turnframe_provider::stream::reconstruct(stream, seed).await?;
//! assert_eq!(response.text(), "Ho preparato la modifica.");
//! # Ok(())
//! # }
//! ```

use std::collections::HashMap;
use std::fmt;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration;

use futures::stream::{Stream, StreamExt};
use serde::{Deserialize, Serialize};

use crate::error::ProviderError;
use crate::ids::{CallId, ModelKey, ProviderKey, RequestId};
use crate::request::{ContentPart, ToolCall};
use crate::response::{FinishReason, ModelResponse, ResponseWarning, TokenUsage};

/// One increment of a streamed answer.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[non_exhaustive]
pub enum StreamEvent {
    /// More prose.
    TextDelta {
        /// The fragment. Concatenated verbatim; adapters do not trim.
        text: String,
    },
    /// A tool call begins. Must precede any fragment for the same id.
    ToolCallStart {
        /// Provider-assigned id.
        id: CallId,
        /// Tool name.
        name: String,
    },
    /// More argument bytes for a call already started.
    ToolCallDelta {
        /// Which call.
        id: CallId,
        /// A fragment of the JSON arguments. Rarely valid JSON on its own.
        arguments_fragment: String,
    },
    /// A tool call's arguments are complete.
    ToolCallEnd {
        /// Which call.
        id: CallId,
    },
    /// Reported token usage. Providers send it once, at the end; a later
    /// report replaces an earlier one.
    Usage {
        /// The counts.
        usage: TokenUsage,
    },
    /// The provider's own response identifier, as soon as the wire reveals it.
    ///
    /// Most vendors put it on the first frame — an OpenAI chunk's `id`, an
    /// Anthropic `message_start`, a Gemini `responseId` — so the caller no
    /// longer has to seed it out of band to have it on the rebuilt response.
    /// A later identifier replaces an earlier one.
    ResponseId {
        /// The identifier. For a support ticket; never a body, never a header.
        id: String,
    },
    /// Something the adapter wants the caller to know without failing the call.
    ///
    /// The streamed path reports exactly what the whole path reports. Without
    /// this event an adapter that had to drop a stop sequence or a cache hint
    /// could say so in [`ModelResponse::warnings`](crate::response::ModelResponse::warnings)
    /// when called whole, and had nowhere to say it when called streamed —
    /// which made the same call honest one way and silent the other.
    Warning {
        /// What was given up.
        warning: ResponseWarning,
    },
    /// Generation stopped. Exactly one per stream, and it must arrive.
    Finish {
        /// Why.
        reason: FinishReason,
    },
}

impl StreamEvent {
    /// A text delta.
    #[must_use]
    pub fn text(text: impl Into<String>) -> Self {
        Self::TextDelta { text: text.into() }
    }

    /// A tool-call announcement.
    #[must_use]
    pub fn tool_call_start(id: impl Into<CallId>, name: impl Into<String>) -> Self {
        Self::ToolCallStart {
            id: id.into(),
            name: name.into(),
        }
    }

    /// An argument fragment.
    #[must_use]
    pub fn tool_call_delta(id: impl Into<CallId>, fragment: impl Into<String>) -> Self {
        Self::ToolCallDelta {
            id: id.into(),
            arguments_fragment: fragment.into(),
        }
    }

    /// A tool-call terminator.
    #[must_use]
    pub fn tool_call_end(id: impl Into<CallId>) -> Self {
        Self::ToolCallEnd { id: id.into() }
    }

    /// The provider's response identifier.
    #[must_use]
    pub fn response_id(id: impl Into<String>) -> Self {
        Self::ResponseId { id: id.into() }
    }

    /// A warning the adapter wants carried into the rebuilt response.
    #[must_use]
    pub const fn warning(warning: ResponseWarning) -> Self {
        Self::Warning { warning }
    }

    /// Stable snake-case label.
    #[must_use]
    pub const fn kind(&self) -> &'static str {
        match self {
            Self::TextDelta { .. } => "text_delta",
            Self::ToolCallStart { .. } => "tool_call_start",
            Self::ToolCallDelta { .. } => "tool_call_delta",
            Self::ToolCallEnd { .. } => "tool_call_end",
            Self::Usage { .. } => "usage",
            Self::ResponseId { .. } => "response_id",
            Self::Warning { .. } => "warning",
            Self::Finish { .. } => "finish",
        }
    }

    /// Returns `true` for the events a UI may render directly (spec §18.5:
    /// only model-authored prose is streamed to the user).
    #[must_use]
    pub const fn is_user_visible(&self) -> bool {
        matches!(self, Self::TextDelta { .. })
    }
}

/// The item type a [`ModelStream`] yields.
pub type StreamItem = Result<StreamEvent, ProviderError>;

/// A stream of [`StreamEvent`]s from one model call.
///
/// Dropping it cancels the call: an adapter must not leave a task running
/// behind a dropped stream, and the conformance suite checks that dropping is
/// clean.
pub struct ModelStream {
    inner: Pin<Box<dyn Stream<Item = StreamItem> + Send>>,
}

impl ModelStream {
    /// Wraps any `Send` stream of events.
    #[must_use]
    pub fn new(stream: impl Stream<Item = StreamItem> + Send + 'static) -> Self {
        Self {
            inner: Box::pin(stream),
        }
    }

    /// A stream that yields a fixed sequence of events. For fixtures and tests.
    #[must_use]
    pub fn from_events(events: Vec<StreamEvent>) -> Self {
        Self::new(futures::stream::iter(events.into_iter().map(Ok)))
    }

    /// A stream that yields the given items, failures included.
    #[must_use]
    pub fn from_items(items: Vec<StreamItem>) -> Self {
        Self::new(futures::stream::iter(items))
    }

    /// A stream that fails immediately.
    #[must_use]
    pub fn failed(error: ProviderError) -> Self {
        Self::from_items(vec![Err(error)])
    }

    /// Collects every item, for tests and fixtures.
    pub async fn collect_items(self) -> Vec<StreamItem> {
        self.inner.collect().await
    }
}

impl fmt::Debug for ModelStream {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("ModelStream(..)")
    }
}

impl Stream for ModelStream {
    type Item = StreamItem;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        self.inner.as_mut().poll_next(cx)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.inner.size_hint()
    }
}

/// A tool call being assembled.
#[derive(Debug, Clone)]
struct PendingCall {
    order: usize,
    name: String,
    fragments: String,
    ended: bool,
}

/// Rebuilds a [`ModelResponse`] from [`StreamEvent`]s.
///
/// Seed it with the identity of the call, feed it every event with
/// [`push`](Self::push), then call [`finish`](Self::finish). [`reconstruct`]
/// does exactly that over a whole [`ModelStream`].
#[derive(Debug, Clone)]
pub struct StreamAccumulator {
    request_id: RequestId,
    provider: ProviderKey,
    model: ModelKey,
    raw_id: Option<String>,
    latency: Duration,
    text: String,
    calls: HashMap<CallId, PendingCall>,
    usage: TokenUsage,
    finish: Option<FinishReason>,
    warnings: Vec<ResponseWarning>,
}

impl StreamAccumulator {
    /// A fresh accumulator for one call.
    #[must_use]
    pub fn new(
        request_id: RequestId,
        provider: impl Into<ProviderKey>,
        model: impl Into<ModelKey>,
    ) -> Self {
        Self {
            request_id,
            provider: provider.into(),
            model: model.into(),
            raw_id: None,
            latency: Duration::ZERO,
            text: String::new(),
            calls: HashMap::new(),
            usage: TokenUsage::none(),
            finish: None,
            warnings: Vec::new(),
        }
    }

    /// Records the provider's response identifier.
    ///
    /// Only needed when the identifier is known before the stream opens — from
    /// a response header, say. An adapter that reads it off the wire emits a
    /// [`StreamEvent::ResponseId`] instead, and the last one wins.
    #[must_use]
    pub fn with_raw_id(mut self, raw_id: impl Into<String>) -> Self {
        self.raw_id = Some(raw_id.into());
        self
    }

    /// Records the measured latency of the whole stream.
    #[must_use]
    pub const fn with_latency(mut self, latency: Duration) -> Self {
        self.latency = latency;
        self
    }

    /// Adds a warning the adapter already knows about.
    ///
    /// For what the request conversion gave up before the call. A warning the
    /// stream itself reveals travels as a [`StreamEvent::Warning`], and a
    /// warning that arrives both ways is recorded once.
    #[must_use]
    pub fn with_warning(mut self, warning: ResponseWarning) -> Self {
        self.warnings.push(warning);
        self
    }

    /// Absorbs one event.
    ///
    /// # Errors
    ///
    /// Returns [`ProviderErrorKind::Malformed`](crate::error::ProviderErrorKind::Malformed)
    /// for a fragment or terminator that names a call which was never started,
    /// a call started twice, a fragment after the call ended, or a second
    /// finish event.
    pub fn push(&mut self, event: StreamEvent) -> Result<(), ProviderError> {
        match event {
            StreamEvent::TextDelta { text } => self.text.push_str(&text),
            StreamEvent::ToolCallStart { id, name } => {
                let order = self.calls.len();
                if self.calls.contains_key(&id) {
                    return Err(self.malformed("tool_call_started_twice"));
                }
                self.calls.insert(
                    id,
                    PendingCall {
                        order,
                        name,
                        fragments: String::new(),
                        ended: false,
                    },
                );
            }
            StreamEvent::ToolCallDelta {
                id,
                arguments_fragment,
            } => {
                let Some(call) = self.calls.get_mut(&id) else {
                    return Err(self.malformed("tool_call_delta_without_start"));
                };
                if call.ended {
                    return Err(self.malformed("tool_call_delta_after_end"));
                }
                call.fragments.push_str(&arguments_fragment);
            }
            StreamEvent::ToolCallEnd { id } => {
                let Some(call) = self.calls.get_mut(&id) else {
                    return Err(self.malformed("tool_call_end_without_start"));
                };
                call.ended = true;
            }
            StreamEvent::Usage { usage } => self.usage = usage,
            StreamEvent::ResponseId { id } => self.raw_id = Some(id),
            StreamEvent::Warning { warning } => {
                if !self.warnings.contains(&warning) {
                    self.warnings.push(warning);
                }
            }
            StreamEvent::Finish { reason } => {
                if self.finish.is_some() {
                    return Err(self.malformed("duplicate_finish"));
                }
                self.finish = Some(reason);
            }
        }
        Ok(())
    }

    /// Builds the response.
    ///
    /// # Errors
    ///
    /// Returns [`ProviderErrorKind::Malformed`](crate::error::ProviderErrorKind::Malformed)
    /// when no finish event arrived — a truncated stream is not a short answer
    /// — or when a call's concatenated fragments are not valid JSON.
    pub fn finish(self) -> Result<ModelResponse, ProviderError> {
        let Some(finish) = self.finish else {
            return Err(self.malformed("stream_ended_without_finish"));
        };

        let mut content = Vec::with_capacity(self.calls.len() + 1);
        if !self.text.is_empty() {
            content.push(ContentPart::text(self.text.clone()));
        }

        let mut ordered: Vec<(CallId, PendingCall)> = self.calls.clone().into_iter().collect();
        ordered.sort_by_key(|(_, call)| call.order);
        for (id, call) in ordered {
            let raw = call.fragments.trim();
            let arguments: serde_json::Value = if raw.is_empty() {
                serde_json::Value::Object(serde_json::Map::new())
            } else {
                serde_json::from_str(raw)
                    .map_err(|_| self.malformed("tool_call_arguments_not_json"))?
            };
            content.push(ContentPart::ToolCall(ToolCall::new(
                id, call.name, arguments,
            )));
        }

        let mut warnings = self.warnings.clone();
        warnings.push(ResponseWarning::Reconstructed);
        if self.usage.is_unreported() {
            warnings.push(ResponseWarning::UsageUnreported);
        }

        Ok(ModelResponse {
            request_id: self.request_id,
            provider: self.provider,
            model: self.model,
            content,
            finish,
            usage: self.usage,
            raw_id: self.raw_id,
            latency: self.latency,
            warnings,
        })
    }

    /// Builds a malformed failure already labelled with the call's identity.
    fn malformed(&self, code: &str) -> ProviderError {
        ProviderError::malformed(code).with_model(&crate::ids::ModelRef {
            provider: self.provider.clone(),
            model: self.model.clone(),
        })
    }
}

/// Drains `stream` into the response it describes.
///
/// The `seed` carries the identity and timing the events do not: request id,
/// provider and model keys, and the measured latency. The provider's response
/// identifier and any warning may come either from the seed or from the stream
/// itself, through [`StreamEvent::ResponseId`] and [`StreamEvent::Warning`].
///
/// # Errors
///
/// Propagates the first [`ProviderError`] the stream yields, and returns the
/// reassembly failures documented on [`StreamAccumulator::push`] and
/// [`StreamAccumulator::finish`].
pub async fn reconstruct(
    mut stream: ModelStream,
    mut seed: StreamAccumulator,
) -> Result<ModelResponse, ProviderError> {
    while let Some(item) = stream.next().await {
        seed.push(item?)?;
    }
    seed.finish()
}

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

    fn seed() -> StreamAccumulator {
        StreamAccumulator::new(RequestId::nil(), "openai", "gpt-4o")
    }

    async fn rebuild(events: Vec<StreamEvent>) -> Result<ModelResponse, ProviderError> {
        reconstruct(ModelStream::from_events(events), seed()).await
    }

    #[tokio::test]
    async fn text_deltas_concatenate_in_order() {
        let response = rebuild(vec![
            StreamEvent::text("Ho "),
            StreamEvent::text("preparato "),
            StreamEvent::text("la modifica."),
            StreamEvent::Usage {
                usage: TokenUsage::new(10, 4),
            },
            StreamEvent::Finish {
                reason: FinishReason::Stop,
            },
        ])
        .await
        .unwrap();
        assert_eq!(response.text(), "Ho preparato la modifica.");
        assert_eq!(response.content.len(), 1, "one text part, not three");
        assert_eq!(response.usage, TokenUsage::new(10, 4));
        assert_eq!(response.finish, FinishReason::Stop);
        assert!(response.warnings.contains(&ResponseWarning::Reconstructed));
    }

    #[tokio::test]
    async fn tool_call_fragments_concatenate_per_id() {
        let response = rebuild(vec![
            StreamEvent::tool_call_start("call_a", "plan"),
            StreamEvent::tool_call_start("call_b", "plan"),
            StreamEvent::tool_call_delta("call_a", "{\"n\":"),
            StreamEvent::tool_call_delta("call_b", "{\"n\":"),
            StreamEvent::tool_call_delta("call_a", "1}"),
            StreamEvent::tool_call_delta("call_b", "2}"),
            StreamEvent::tool_call_end("call_a"),
            StreamEvent::tool_call_end("call_b"),
            StreamEvent::Finish {
                reason: FinishReason::ToolCalls,
            },
        ])
        .await
        .unwrap();
        let calls = response.tool_calls();
        assert_eq!(calls.len(), 2);
        assert_eq!(calls[0].id.as_str(), "call_a");
        assert_eq!(calls[0].arguments, json!({"n": 1}));
        assert_eq!(calls[1].id.as_str(), "call_b");
        assert_eq!(calls[1].arguments, json!({"n": 2}));
    }

    #[tokio::test]
    async fn text_comes_before_tool_calls() {
        let response = rebuild(vec![
            StreamEvent::tool_call_start("c", "plan"),
            StreamEvent::text("preambolo"),
            StreamEvent::tool_call_delta("c", "{}"),
            StreamEvent::tool_call_end("c"),
            StreamEvent::Finish {
                reason: FinishReason::ToolCalls,
            },
        ])
        .await
        .unwrap();
        assert_eq!(response.content.len(), 2);
        assert_eq!(response.content[0].kind(), "text");
        assert_eq!(response.content[1].kind(), "tool_call");
    }

    #[tokio::test]
    async fn a_call_with_no_fragments_gets_an_empty_object() {
        let response = rebuild(vec![
            StreamEvent::tool_call_start("c", "ping"),
            StreamEvent::tool_call_end("c"),
            StreamEvent::Finish {
                reason: FinishReason::ToolCalls,
            },
        ])
        .await
        .unwrap();
        assert_eq!(response.tool_calls()[0].arguments, json!({}));
    }

    #[tokio::test]
    async fn reassembly_failures_are_malformed_and_never_partial() {
        let cases = [
            (
                vec![
                    StreamEvent::tool_call_delta("ghost", "{}"),
                    StreamEvent::Finish {
                        reason: FinishReason::ToolCalls,
                    },
                ],
                "tool_call_delta_without_start",
            ),
            (
                vec![
                    StreamEvent::tool_call_end("ghost"),
                    StreamEvent::Finish {
                        reason: FinishReason::ToolCalls,
                    },
                ],
                "tool_call_end_without_start",
            ),
            (
                vec![
                    StreamEvent::tool_call_start("c", "plan"),
                    StreamEvent::tool_call_start("c", "plan"),
                    StreamEvent::Finish {
                        reason: FinishReason::ToolCalls,
                    },
                ],
                "tool_call_started_twice",
            ),
            (
                vec![
                    StreamEvent::tool_call_start("c", "plan"),
                    StreamEvent::tool_call_end("c"),
                    StreamEvent::tool_call_delta("c", "{}"),
                    StreamEvent::Finish {
                        reason: FinishReason::ToolCalls,
                    },
                ],
                "tool_call_delta_after_end",
            ),
            (
                vec![
                    StreamEvent::Finish {
                        reason: FinishReason::Stop,
                    },
                    StreamEvent::Finish {
                        reason: FinishReason::Stop,
                    },
                ],
                "duplicate_finish",
            ),
            (
                vec![StreamEvent::text("ciao")],
                "stream_ended_without_finish",
            ),
            (
                vec![
                    StreamEvent::tool_call_start("c", "plan"),
                    StreamEvent::tool_call_delta("c", "{\"n\":"),
                    StreamEvent::Finish {
                        reason: FinishReason::ToolCalls,
                    },
                ],
                "tool_call_arguments_not_json",
            ),
        ];
        for (events, expected) in cases {
            let error = rebuild(events).await.unwrap_err();
            assert_eq!(
                error.code().map(|code| code.as_str().to_owned()),
                Some(expected.to_owned()),
                "{error}"
            );
            assert!(matches!(
                error.kind(),
                crate::error::ProviderErrorKind::Malformed
            ));
            assert_eq!(error.provider().map(ProviderKey::as_str), Some("openai"));
        }
    }

    #[tokio::test]
    async fn an_error_item_stops_reassembly_immediately() {
        let stream = ModelStream::from_items(vec![
            Ok(StreamEvent::text("half")),
            Err(ProviderError::transport("connection_reset")),
            Ok(StreamEvent::Finish {
                reason: FinishReason::Stop,
            }),
        ]);
        let error = reconstruct(stream, seed()).await.unwrap_err();
        assert!(matches!(
            error.kind(),
            crate::error::ProviderErrorKind::Transport
        ));
    }

    #[tokio::test]
    async fn a_failed_stream_surfaces_its_error() {
        let stream = ModelStream::failed(ProviderError::rate_limited(None));
        let error = reconstruct(stream, seed()).await.unwrap_err();
        assert!(matches!(
            error.kind(),
            crate::error::ProviderErrorKind::RateLimited { .. }
        ));
        assert_eq!(
            format!("{:?}", ModelStream::from_events(vec![])),
            "ModelStream(..)"
        );
    }

    #[tokio::test]
    async fn the_seed_supplies_identity_and_timing() {
        let seed = StreamAccumulator::new(RequestId::nil(), "anthropic", "claude")
            .with_raw_id("msg_01")
            .with_latency(Duration::from_millis(250))
            .with_warning(ResponseWarning::SynthesizedCallIds);
        let response = reconstruct(
            ModelStream::from_events(vec![
                StreamEvent::text("x"),
                StreamEvent::Finish {
                    reason: FinishReason::Stop,
                },
            ]),
            seed,
        )
        .await
        .unwrap();
        assert_eq!(response.provider.as_str(), "anthropic");
        assert_eq!(response.raw_id.as_deref(), Some("msg_01"));
        assert_eq!(response.latency, Duration::from_millis(250));
        assert!(
            response
                .warnings
                .contains(&ResponseWarning::SynthesizedCallIds)
        );
        assert!(
            response
                .warnings
                .contains(&ResponseWarning::UsageUnreported)
        );
    }

    #[tokio::test]
    async fn the_stream_can_carry_the_identifier_and_the_warning_itself() {
        // The gap this closes: an adapter that had to drop a feature said so on
        // the whole path and had nowhere to say it on the streamed one, and the
        // response identifier had to be seeded before the first frame arrived.
        let response = rebuild(vec![
            StreamEvent::response_id("chatcmpl-1"),
            StreamEvent::warning(ResponseWarning::FeatureDropped {
                feature: "stop_sequences".to_owned(),
            }),
            StreamEvent::text("ok"),
            StreamEvent::response_id("chatcmpl-final"),
            StreamEvent::Finish {
                reason: FinishReason::Stop,
            },
        ])
        .await
        .unwrap();
        assert_eq!(
            response.raw_id.as_deref(),
            Some("chatcmpl-final"),
            "a later identifier replaces an earlier one"
        );
        assert!(
            response
                .warnings
                .contains(&ResponseWarning::FeatureDropped {
                    feature: "stop_sequences".to_owned(),
                })
        );
        assert!(response.warnings.contains(&ResponseWarning::Reconstructed));
    }

    #[tokio::test]
    async fn a_warning_the_seed_and_the_stream_both_carry_is_recorded_once() {
        let dropped = ResponseWarning::FeatureDropped {
            feature: "cache_hint".to_owned(),
        };
        let seed = StreamAccumulator::new(RequestId::nil(), "openai", "gpt-4o")
            .with_warning(dropped.clone());
        let response = reconstruct(
            ModelStream::from_events(vec![
                StreamEvent::warning(dropped.clone()),
                StreamEvent::Finish {
                    reason: FinishReason::Stop,
                },
            ]),
            seed,
        )
        .await
        .unwrap();
        assert_eq!(
            response
                .warnings
                .iter()
                .filter(|warning| **warning == dropped)
                .count(),
            1
        );
    }

    #[test]
    fn events_round_trip_and_only_text_is_user_visible() {
        let events = [
            StreamEvent::text("a"),
            StreamEvent::tool_call_start("c", "plan"),
            StreamEvent::tool_call_delta("c", "{}"),
            StreamEvent::tool_call_end("c"),
            StreamEvent::Usage {
                usage: TokenUsage::new(1, 1),
            },
            StreamEvent::response_id("resp_1"),
            StreamEvent::warning(ResponseWarning::UsageUnreported),
            StreamEvent::Finish {
                reason: FinishReason::Stop,
            },
        ];
        let mut kinds: Vec<&str> = events.iter().map(StreamEvent::kind).collect();
        kinds.sort_unstable();
        kinds.dedup();
        assert_eq!(kinds.len(), 8);
        for event in &events {
            let json = serde_json::to_string(event).unwrap();
            let back: StreamEvent = serde_json::from_str(&json).unwrap();
            assert_eq!(&back, event);
        }
        assert!(events[0].is_user_visible());
        assert!(!events[1].is_user_visible());
    }
}