Repository navigation
Expand file tree
/
Copy pathstream.rs
More file actions
771 lines (718 loc) · 27.2 KB
/
Copy pathstream.rs
File metadata and controls
771 lines (718 loc) · 27.2 KB
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
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//! Streaming half of the neutral IR: incremental response chunks ([`LlmResponseChunk`]),
//! their stream envelope ([`LlmResponseStreamEvent`]), and the streamed response
//! ([`LlmResponse`]) that carries either a live stream or the terminal [`AggLlmResponse`].
use std::collections::BTreeMap;
use std::pin::Pin;
use futures::{Stream, StreamExt};
use http::StatusCode;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use thiserror::Error;
use crate::{
LlmClientError,
format::FormatId,
llm::{AggLlmResponse, ContentBlock, ResponseOutput, Role, StopReason, ToolCall, Usage},
};
/// Status reported for an upstream error delivered inside a streaming body. The
/// upstream already sent a success status line before failing, so there is no real
/// code to propagate; 502 matches how a failed upstream call surfaces elsewhere.
const MID_STREAM_UPSTREAM_STATUS: StatusCode = StatusCode::BAD_GATEWAY;
/// Why a translated event stream stopped early.
#[derive(Debug, Error)]
pub enum LlmStreamError {
/// An upstream sse error
#[error("upstream stream error: {0}")]
Upstream(Value),
/// The stream itself failed
#[error(transparent)]
Client(#[from] LlmClientError),
}
/// A boxed, `Send` stream of response events. Each item may fail independently mid-stream.
pub type LlmResponseStream =
Pin<Box<dyn Stream<Item = Result<LlmResponseStreamEvent, LlmClientError>> + Send>>;
/// Parsed provider event retained for same-format replay.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ProviderStreamEvent {
source: FormatId,
raw: Value,
}
impl ProviderStreamEvent {
/// Source wire format of the retained event.
pub fn source(&self) -> &FormatId {
&self.source
}
/// Parsed provider JSON retained for replay.
pub fn raw(&self) -> &Value {
&self.raw
}
/// Consumes the preservation value into its source format and parsed JSON.
pub fn into_parts(self) -> (FormatId, Value) {
(self.source, self.raw)
}
}
/// One streaming item crossing the host/algorithm boundary.
///
/// `normalized` contains only provider-neutral chunks. `preservation` is opaque to
/// algorithms and is interpreted by `switchyard-translation` for same-format replay.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct LlmResponseStreamEvent {
preservation: Option<ProviderStreamEvent>,
normalized: Vec<LlmResponseChunk>,
}
impl LlmResponseStreamEvent {
/// Creates an event containing only normalized chunks.
pub fn new(normalized: Vec<LlmResponseChunk>) -> Self {
Self {
preservation: None,
normalized,
}
}
/// Creates an event with parsed provider JSON retained for replay.
pub fn preserved(
source: impl Into<FormatId>,
raw: Value,
normalized: Vec<LlmResponseChunk>,
) -> Self {
Self {
preservation: Some(ProviderStreamEvent {
source: source.into(),
raw,
}),
normalized,
}
}
/// Retained provider event, when this event came directly from a provider.
pub fn preservation(&self) -> Option<&ProviderStreamEvent> {
self.preservation.as_ref()
}
/// Provider-neutral chunks carried by this event.
pub fn normalized(&self) -> &[LlmResponseChunk] {
&self.normalized
}
/// Consumes the event into its preservation and normalized content.
pub fn into_parts(self) -> (Option<ProviderStreamEvent>, Vec<LlmResponseChunk>) {
(self.preservation, self.normalized)
}
/// Replaces semantic content and drops raw replay data that no longer describes it.
pub fn replace_normalized(self, normalized: Vec<LlmResponseChunk>) -> Self {
Self::new(normalized)
}
}
impl From<LlmResponseChunk> for LlmResponseStreamEvent {
fn from(chunk: LlmResponseChunk) -> Self {
Self::new(vec![chunk])
}
}
/// A model response: either a live [`Stream`](LlmResponse::Stream) of events or a
/// terminal buffered [`LlmResponse::Agg`] response.
///
/// Not `Clone` — the `Stream` variant owns a single-consumption stream. A buffered
/// backend returns `Agg` directly; a streaming one returns `Stream` and the consumer
/// drives it, folding to an [`AggLlmResponse`] when it needs the whole response.
#[allow(clippy::large_enum_variant)]
pub enum LlmResponse {
/// Live, single-consumption response stream.
Stream(LlmResponseStream),
/// Fully buffered terminal response.
Agg(AggLlmResponse),
}
impl LlmResponse {
/// Borrow the aggregate; `None` while this is still a stream.
pub fn as_agg(&self) -> Option<&AggLlmResponse> {
match self {
LlmResponse::Agg(agg) => Some(agg),
LlmResponse::Stream(_) => None,
}
}
/// Reduce to the buffered aggregate: return an `Agg` unchanged, or drive a `Stream`
/// to completion, folding its normalized chunks into an [`AggLlmResponse`] via
/// [`ResponseAccumulator`]. A stream item error aborts with `Err`, as does an
/// in-band [`LlmResponseChunk::DecodeError`] (as `ResponseTranslation`) or
/// [`LlmResponseChunk::StreamError`] (as `UpstreamHttp`).
pub async fn into_agg(self) -> Result<AggLlmResponse, LlmClientError> {
match self {
LlmResponse::Agg(agg) => Ok(agg),
LlmResponse::Stream(mut stream) => {
let mut accumulator = ResponseAccumulator::new();
while let Some(item) = stream.next().await {
for chunk in item?.normalized {
push_checked_chunk(&mut accumulator, chunk)?;
}
}
Ok(accumulator.finish())
}
}
}
/// Returns the model recorded by a buffered response.
///
/// Returns `None` for a live stream because reading its model-bearing
/// [`LlmResponseChunk::MessageStart`] event would consume the stream.
pub fn selected_model(&self) -> Option<&str> {
match self {
LlmResponse::Agg(agg) => agg.model.as_deref(),
// TODO: How do we get the model name on a stream?
LlmResponse::Stream(_) => None,
}
}
}
/// An encrypted reasoning detail that names its provider item id but carries no payload yet.
fn is_reasoning_id_announcement(detail: &Value) -> bool {
detail.get("type").and_then(Value::as_str) == Some("reasoning.encrypted")
&& detail.get("data").is_none()
}
/// Appends a reasoning detail to an accumulated block. A Responses stream decoder announces a
/// reasoning item's provider id (`{"type": "reasoning.encrypted", "id"}`) before the payload
/// arrives; the payload detail then replaces that announcement so history holds one detail.
fn push_reasoning_detail(details: &mut Vec<Value>, detail: Value) {
if detail.get("type").and_then(Value::as_str) == Some("reasoning.encrypted")
&& let Some(id) = detail.get("id").and_then(Value::as_str)
&& let Some(announcement) = details.iter_mut().find(|existing| {
is_reasoning_id_announcement(existing)
&& existing.get("id").and_then(Value::as_str) == Some(id)
})
{
*announcement = detail;
return;
}
details.push(detail);
}
impl AggLlmResponse {
/// Converts a fully-buffered response into a synthetic chunk stream.
///
/// Useful when a caller had to buffer the response (e.g. to judge it) but the
/// downstream expects `LlmResponse::Stream` — for instance when `stream: true` was
/// requested and the algorithm had to aggregate before it could return.
///
/// This conversion is lossy: only text, reasoning, and tool-call content has
/// a synthetic chunk representation. Refusals, tool results, media, files,
/// unknown blocks, response extensions, and preservation metadata are omitted.
pub fn into_stream(self) -> LlmResponseStream {
let mut chunks: Vec<LlmResponseChunk> = Vec::new();
chunks.push(LlmResponseChunk::MessageStart {
id: self.id,
model: self.model,
});
let mut tool_call_index = 0usize;
for (output_index, output) in self.outputs.into_iter().enumerate() {
for block in output.content {
match block {
ContentBlock::Text { text } => {
chunks.push(LlmResponseChunk::TextDelta {
index: output_index,
text,
});
}
ContentBlock::Reasoning { text, details, .. } => {
if !details.is_empty() {
chunks.push(LlmResponseChunk::ReasoningDetailsDelta {
index: output_index,
details,
text,
});
} else {
chunks.push(LlmResponseChunk::ReasoningDelta {
index: output_index,
text,
});
}
}
ContentBlock::ToolCall(tool) => {
let args = serde_json::to_string(&tool.arguments).unwrap_or_default();
chunks.push(LlmResponseChunk::ToolCallDelta {
index: tool_call_index,
id: Some(tool.id),
name: Some(tool.name),
arguments_delta: Some(args),
});
tool_call_index += 1;
}
// Other variants (Image, ToolResult, Refusal, etc.) don't have
// a streaming chunk representation and don't appear in assistant outputs.
_ => {}
}
}
chunks.push(LlmResponseChunk::MessageStop {
reason: output.stop_reason.and_then(|r| {
serde_json::to_value(r)
.ok()
.and_then(|v| v.as_str().map(String::from))
}),
});
}
chunks.push(LlmResponseChunk::Usage(self.usage));
Box::pin(futures::stream::iter(
chunks.into_iter().map(|chunk| Ok(chunk.into())),
))
}
}
fn push_checked_chunk(
accumulator: &mut ResponseAccumulator,
chunk: LlmResponseChunk,
) -> Result<(), LlmClientError> {
match chunk {
LlmResponseChunk::DecodeError { message } => {
Err(LlmClientError::ResponseTranslation(message))
}
LlmResponseChunk::StreamError { message } => Err(LlmClientError::UpstreamHttp {
status: MID_STREAM_UPSTREAM_STATUS,
body: message,
}),
chunk => {
accumulator.push(chunk);
Ok(())
}
}
}
/// One provider-neutral streaming response chunk.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum LlmResponseChunk {
/// Starts a response message.
MessageStart {
/// Provider response identifier.
id: Option<String>,
/// Model reported by the provider.
model: Option<String>,
},
/// Adds text to one output index.
TextDelta {
/// Provider output index.
index: usize,
/// Text fragment.
text: String,
},
/// Adds reasoning text to one output index.
ReasoningDelta {
/// Provider output index.
index: usize,
/// Reasoning fragment.
text: String,
},
/// Adds structured reasoning details to one output index.
ReasoningDetailsDelta {
/// Provider output index.
index: usize,
/// Reasoning detail objects in provider order.
details: Vec<Value>,
/// Normalized reasoning text represented by or accompanying the details.
text: String,
},
/// Adds or updates a tool call at one index.
ToolCallDelta {
/// Tool-call index within the response.
index: usize,
/// Tool-call identifier, normally supplied by the first delta.
id: Option<String>,
/// Tool name, normally supplied by the first delta.
name: Option<String>,
/// Fragment of the serialized tool arguments.
arguments_delta: Option<String>,
},
/// Reports token usage, normally near the end of the stream.
Usage(Usage),
/// Ends a response message.
MessageStop {
/// Provider stop-reason string before normalization.
reason: Option<String>,
},
/// Reports that an inbound stream event could not be decoded.
DecodeError {
/// Human-readable decoding failure.
message: String,
},
/// Reports an upstream failure delivered inside an otherwise successful stream.
StreamError {
/// Human-readable upstream failure.
message: String,
},
}
/// Folds a sequence of [`LlmResponseChunk`]s into the terminal [`AggLlmResponse`].
///
/// Text and reasoning deltas concatenate; tool-call deltas assemble by index (name,
/// id, and a growing arguments string parsed as JSON at the end); `MessageStart`,
/// `Usage`, and `MessageStop` set the corresponding fields.
/// [`DecodeError`](LlmResponseChunk::DecodeError) and
/// [`StreamError`](LlmResponseChunk::StreamError) chunks are ignored here; use
/// [`LlmResponse::into_agg`] when they must become errors.
///
/// Folding is lossy for multiple outputs: text and reasoning indices are ignored,
/// and [`finish`](Self::finish) produces one assistant output. Prefer
/// [`LlmResponse::into_agg`] when stream errors must be surfaced.
///
/// Drive it by `push`-ing each chunk in order, then call [`finish`](Self::finish).
#[derive(Default)]
pub struct ResponseAccumulator {
id: Option<String>,
model: Option<String>,
text: String,
reasoning: Option<String>,
reasoning_details: Vec<Value>,
tool_calls: BTreeMap<usize, PartialToolCall>,
usage: Usage,
stop_reason: Option<StopReason>,
}
/// A tool call being assembled from streamed [`LlmResponseChunk::ToolCallDelta`]s.
#[derive(Default)]
struct PartialToolCall {
id: Option<String>,
name: Option<String>,
arguments: String,
}
impl ResponseAccumulator {
/// A fresh accumulator with no chunks applied.
pub fn new() -> Self {
Self::default()
}
/// Apply one chunk. Later `MessageStart`/`Usage`/`MessageStop` fields overwrite
/// earlier ones; text, reasoning, and tool-call arguments append.
pub fn push(&mut self, chunk: LlmResponseChunk) {
match chunk {
LlmResponseChunk::MessageStart { id, model } => {
if id.is_some() {
self.id = id;
}
if model.is_some() {
self.model = model;
}
}
LlmResponseChunk::TextDelta { text, .. } => self.text.push_str(&text),
LlmResponseChunk::ReasoningDelta { text, .. } => {
self.reasoning
.get_or_insert_with(String::new)
.push_str(&text);
}
LlmResponseChunk::ReasoningDetailsDelta { details, text, .. } => {
for detail in details {
push_reasoning_detail(&mut self.reasoning_details, detail);
}
if !text.is_empty() {
self.reasoning
.get_or_insert_with(String::new)
.push_str(&text);
}
}
LlmResponseChunk::ToolCallDelta {
index,
id,
name,
arguments_delta,
} => {
let call = self.tool_calls.entry(index).or_default();
if id.is_some() {
call.id = id;
}
if name.is_some() {
call.name = name;
}
if let Some(delta) = arguments_delta {
call.arguments.push_str(&delta);
}
}
LlmResponseChunk::Usage(usage) => self.usage = usage,
LlmResponseChunk::MessageStop { reason } => {
self.stop_reason = Some(stop_reason_from_str(reason.as_deref()));
}
LlmResponseChunk::DecodeError { .. } | LlmResponseChunk::StreamError { .. } => {}
}
}
/// Build the buffered response. Content is ordered reasoning, then text, then
/// tool calls (by ascending delta index) — a single assistant output.
pub fn finish(self) -> AggLlmResponse {
let mut content = Vec::new();
if self.reasoning.is_some() || !self.reasoning_details.is_empty() {
content.push(ContentBlock::Reasoning {
text: self.reasoning.unwrap_or_default(),
signature: None,
// An announcement whose payload never arrived is a stream-level hint only.
details: self
.reasoning_details
.into_iter()
.filter(|detail| !is_reasoning_id_announcement(detail))
.collect(),
});
}
if !self.text.is_empty() {
content.push(ContentBlock::Text { text: self.text });
}
for call in self.tool_calls.into_values() {
content.push(ContentBlock::ToolCall(ToolCall {
id: call.id.unwrap_or_default(),
name: call.name.unwrap_or_default(),
arguments: parse_tool_arguments(&call.arguments),
}));
}
AggLlmResponse {
id: self.id,
model: self.model,
outputs: vec![ResponseOutput {
role: Role::Assistant,
content,
stop_reason: self.stop_reason,
}],
usage: self.usage,
..AggLlmResponse::default()
}
}
}
/// Parse an assembled tool-call arguments string as JSON, falling back to a JSON
/// string when it is not valid JSON and to an empty object when it is empty.
fn parse_tool_arguments(arguments: &str) -> Value {
if arguments.is_empty() {
return Value::Object(serde_json::Map::new());
}
serde_json::from_str(arguments).unwrap_or_else(|_| Value::String(arguments.to_string()))
}
/// Map a provider stop-reason string (as carried by [`LlmResponseChunk::MessageStop`])
/// to a normalized [`StopReason`], covering the common OpenAI and Anthropic spellings.
fn stop_reason_from_str(reason: Option<&str>) -> StopReason {
match reason {
Some("length" | "max_tokens") => StopReason::MaxTokens,
Some("tool_calls" | "function_call" | "tool_use") => StopReason::ToolUse,
Some("content_filter") => StopReason::ContentFilter,
Some("stop" | "end_turn" | "stop_sequence") | None => StopReason::EndTurn,
Some(_) => StopReason::Unknown,
}
}
#[cfg(test)]
mod tests {
use futures::executor::block_on;
use futures::stream;
use super::*;
use serde_json::json;
fn fold(chunks: Vec<LlmResponseChunk>) -> AggLlmResponse {
let mut accumulator = ResponseAccumulator::new();
for chunk in chunks {
accumulator.push(chunk);
}
accumulator.finish()
}
#[test]
fn folds_text_usage_and_stop_reason() {
let agg = fold(vec![
LlmResponseChunk::MessageStart {
id: Some("id1".to_string()),
model: Some("m".to_string()),
},
LlmResponseChunk::TextDelta {
index: 0,
text: "Hel".to_string(),
},
LlmResponseChunk::TextDelta {
index: 0,
text: "lo".to_string(),
},
LlmResponseChunk::Usage(Usage {
output_tokens: Some(2),
..Usage::default()
}),
LlmResponseChunk::MessageStop {
reason: Some("length".to_string()),
},
]);
assert_eq!(agg.id.as_deref(), Some("id1"));
assert_eq!(agg.model.as_deref(), Some("m"));
assert_eq!(agg.usage.output_tokens, Some(2));
assert_eq!(agg.outputs[0].stop_reason, Some(StopReason::MaxTokens));
assert_eq!(
agg.outputs[0].content,
vec![ContentBlock::Text {
text: "Hello".to_string()
}]
);
}
#[test]
fn aggregates_normalized_chunks_inside_stream_event() {
let response = LlmResponse::Stream(Box::pin(stream::iter([Ok(
LlmResponseStreamEvent::preserved(
crate::WireFormat::OpenAiChat,
json!({
"choices": [{"delta": {"content": "hello"}}],
"system_fingerprint": "fp_exact"
}),
vec![LlmResponseChunk::TextDelta {
index: 0,
text: "hello".to_string(),
}],
),
)])));
let aggregate = block_on(response.into_agg()).expect("stream event should aggregate");
assert_eq!(
aggregate.outputs[0].content,
vec![ContentBlock::Text {
text: "hello".to_string()
}]
);
}
#[test]
fn replacing_normalized_content_drops_preservation() {
let event = LlmResponseStreamEvent::preserved(
crate::WireFormat::OpenAiChat,
json!({"choices": [{"delta": {"content": "old"}}]}),
vec![LlmResponseChunk::TextDelta {
index: 0,
text: "old".to_string(),
}],
)
.replace_normalized(vec![LlmResponseChunk::TextDelta {
index: 0,
text: "new".to_string(),
}]);
assert!(event.preservation().is_none());
assert_eq!(
event.normalized(),
&[LlmResponseChunk::TextDelta {
index: 0,
text: "new".to_string(),
}]
);
}
#[test]
fn stream_errors_inside_preserved_events_remain_typed() {
let response = LlmResponse::Stream(Box::pin(stream::iter([Ok(
LlmResponseStreamEvent::preserved(
crate::WireFormat::OpenAiChat,
json!({"error": {"message": "provider failed"}}),
vec![LlmResponseChunk::StreamError {
message: "provider failed".to_string(),
}],
),
)])));
let error = block_on(response.into_agg()).err();
assert!(matches!(
error,
Some(LlmClientError::UpstreamHttp {
status: MID_STREAM_UPSTREAM_STATUS,
..
})
));
}
#[test]
fn assembles_tool_calls_by_index() {
// id/name arrive once, arguments stream across deltas and parse as JSON.
let agg = fold(vec![
LlmResponseChunk::ToolCallDelta {
index: 0,
id: Some("call_1".to_string()),
name: Some("lookup".to_string()),
arguments_delta: Some("{\"q\":".to_string()),
},
LlmResponseChunk::ToolCallDelta {
index: 0,
id: None,
name: None,
arguments_delta: Some("\"rust\"}".to_string()),
},
LlmResponseChunk::MessageStop {
reason: Some("tool_calls".to_string()),
},
]);
assert_eq!(agg.outputs[0].stop_reason, Some(StopReason::ToolUse));
assert_eq!(
agg.outputs[0].content,
vec![ContentBlock::ToolCall(ToolCall {
id: "call_1".to_string(),
name: "lookup".to_string(),
arguments: json!({"q": "rust"}),
})]
);
}
#[test]
fn reasoning_precedes_text_in_content() {
let agg = fold(vec![
LlmResponseChunk::ReasoningDelta {
index: 0,
text: "think".to_string(),
},
LlmResponseChunk::TextDelta {
index: 0,
text: "answer".to_string(),
},
]);
assert_eq!(
agg.outputs[0].content,
vec![
ContentBlock::Reasoning {
text: "think".to_string(),
signature: None,
details: Vec::new(),
},
ContentBlock::Text {
text: "answer".to_string(),
},
]
);
}
#[test]
fn into_stream_round_trips_through_into_agg() {
let original = AggLlmResponse {
id: Some("id1".to_string()),
model: Some("m".to_string()),
outputs: vec![ResponseOutput {
role: Role::Assistant,
content: vec![ContentBlock::Text {
text: "hello".to_string(),
}],
stop_reason: Some(StopReason::EndTurn),
}],
usage: Usage {
output_tokens: Some(3),
..Usage::default()
},
..AggLlmResponse::default()
};
let stream = LlmResponse::Stream(original.clone().into_stream());
let recovered = block_on(stream.into_agg()).expect("into_agg failed");
assert_eq!(recovered.id, original.id);
assert_eq!(recovered.model, original.model);
assert_eq!(recovered.usage.output_tokens, original.usage.output_tokens);
assert_eq!(
recovered.outputs[0].stop_reason,
original.outputs[0].stop_reason
);
assert_eq!(recovered.outputs[0].content, original.outputs[0].content);
}
#[test]
fn into_stream_retains_encrypted_reasoning_and_text() {
let details = vec![json!({
"type": "reasoning.encrypted",
"data": "opaque-encrypted-reasoning"
})];
let original = AggLlmResponse {
outputs: vec![ResponseOutput {
role: Role::Assistant,
content: vec![ContentBlock::Reasoning {
text: "fallback reasoning".to_string(),
signature: None,
details: details.clone(),
}],
stop_reason: Some(StopReason::EndTurn),
}],
..AggLlmResponse::default()
};
let recovered = block_on(LlmResponse::Stream(original.into_stream()).into_agg())
.expect("into_agg failed");
let ContentBlock::Reasoning {
text,
details: recovered_details,
..
} = &recovered.outputs[0].content[0]
else {
panic!("expected reasoning block");
};
assert_eq!(text, "fallback reasoning");
assert_eq!(recovered_details, &details);
}
#[test]
fn into_agg_preserves_stream_item_error() {
let response = LlmResponse::Stream(Box::pin(stream::once(async {
Err(LlmClientError::Timeout {
source: Box::new(std::io::Error::other("timed out")),
})
})));
let Err(error) = block_on(response.into_agg()) else {
panic!("expected stream aggregation to fail");
};
assert!(matches!(error, LlmClientError::Timeout { .. }));
}
}