mini_langchain/tools/
stream.rs1use crate::llm::tokens::TokenUsage;
2use serde_json::Value;
3use std::io::{self, Write};
4
5#[derive(Debug, Clone)]
6pub struct StreamData {
7 pub value: Value,
8 pub tokens: Option<TokenUsage>,
9 pub content: String,
10}
11
12
13impl StreamData {
14 pub fn new<S: Into<String>>(value: Value, tokens: Option<TokenUsage>, content: S) -> Self {
15 Self {
16 value,
17 tokens,
18 content: content.into(),
19 }
20 }
21
22 pub fn to_stdout(&self) -> io::Result<()> {
23 let stdout = io::stdout();
24 let mut handle = stdout.lock();
25 write!(handle, "{}", self.content)?;
26 handle.flush()
27 }
28}