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
//! Joining the text fragments a stream delivered back into whole blocks.
//!
//! Callers see text as it arrives, one [`StreamEvent`](crate::StreamEvent) per fragment. What
//! goes into conversation history is the other shape: a turn the model produced in one breath
//! is one block. Writing the fragments straight to history would replay a dozen text blocks to
//! the server on the next request in place of the one it sent.
//!
//! Only *adjacent* text is joined. A text block on either side of a tool call is a separate
//! block, because the tool call sits between them in the turn.
use crateContentBlock;
/// Joins runs of adjacent [`ContentBlock::Text`], leaving every other block untouched.
///
/// Takes a slice because the common caller still needs its fragments afterwards: the client
/// hands them to the caller one at a time while writing the joined turn to history.
///
/// # Examples
///
/// ```
/// use open_agent::{ContentBlock, TextBlock, coalesce_text_blocks};
///
/// let fragments = vec![
/// ContentBlock::Text(TextBlock::new("Hel")),
/// ContentBlock::Text(TextBlock::new("lo")),
/// ];
///
/// let joined = coalesce_text_blocks(&fragments);
///
/// assert_eq!(joined.len(), 1);
/// match &joined[0] {
/// ContentBlock::Text(text) => assert_eq!(text.text, "Hello"),
/// _ => unreachable!(),
/// }
/// ```