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
//! Partial response accumulator for stream recovery.
/// Partial response accumulator.
///
/// Collects streamed text and thinking deltas so they can be recovered
/// if the stream terminates unexpectedly.
#[derive(Debug, Default)]
pub struct PartialResponse {
/// Accumulated response text.
text: String,
/// Accumulated thinking / reasoning text.
thinking: String,
/// Whether any thinking content has been received.
has_thinking: bool,
}
impl PartialResponse {
/// Create a new empty partial response.
pub fn new() -> Self {
Self::default()
}
/// Append a text delta to the accumulated response.
pub fn push_text(&mut self, delta: &str) {
self.text.push_str(delta);
}
/// Append a thinking delta to the accumulated reasoning.
pub fn push_thinking(&mut self, delta: &str) {
self.has_thinking = true;
self.thinking.push_str(delta);
}
/// Take ownership of the accumulated text, leaving an empty string behind.
pub fn take_text(&mut self) -> String {
std::mem::take(&mut self.text)
}
/// Access the accumulated response text.
pub fn text(&self) -> &str {
&self.text
}
/// Access the accumulated thinking / reasoning text.
pub fn thinking(&self) -> &str {
&self.thinking
}
/// Returns `true` if any thinking content has been received.
pub fn has_thinking(&self) -> bool {
self.has_thinking
}
/// Returns `true` if no text or thinking has been accumulated.
pub fn is_empty(&self) -> bool {
self.text.is_empty() && self.thinking.is_empty()
}
/// Clear all accumulated content.
pub fn clear(&mut self) {
self.text.clear();
self.thinking.clear();
self.has_thinking = false;
}
}