Skip to main content

rune_chain_core/
stream.rs

1use serde::{Deserialize, Serialize};
2
3/// A single chunk delivered by a streaming LLM response.
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct StreamData {
6    /// The partial text content of this chunk.
7    pub content: String,
8    /// `true` for the final chunk; after this the stream ends.
9    pub is_finished: bool,
10}
11
12impl StreamData {
13    /// Create a non-terminal chunk carrying the given text fragment.
14    ///
15    /// # Example
16    ///
17    /// ```rust
18    /// use rune_chain_core::StreamData;
19    ///
20    /// let chunk = StreamData::chunk("Hello");
21    /// assert_eq!(chunk.content, "Hello");
22    /// assert!(!chunk.is_finished);
23    /// ```
24    pub fn chunk(content: impl Into<String>) -> Self {
25        Self {
26            content: content.into(),
27            is_finished: false,
28        }
29    }
30
31    /// Create the terminal chunk signalling the end of the stream.
32    ///
33    /// # Example
34    ///
35    /// ```rust
36    /// use rune_chain_core::StreamData;
37    ///
38    /// let done = StreamData::done();
39    /// assert!(done.is_finished);
40    /// assert!(done.content.is_empty());
41    /// ```
42    pub fn done() -> Self {
43        Self {
44            content: String::new(),
45            is_finished: true,
46        }
47    }
48}