bamboo_engine/runtime/stream/
handler.rs1use std::time::Duration;
2
3use tokio::sync::mpsc;
4use tokio_util::sync::CancellationToken;
5
6use bamboo_agent_core::tools::ToolCall;
7use bamboo_agent_core::{AgentError, AgentEvent};
8use bamboo_llm::LLMStream;
9
10mod chunk_handling;
11mod consume;
12mod stream_state;
13
14const DEFAULT_STREAM_IDLE_TIMEOUT: Duration = Duration::from_secs(120);
29
30pub struct StreamHandlingOutput {
31 pub response_id: Option<String>,
32 pub content: String,
33 pub reasoning_content: String,
34 pub reasoning_signature: Option<String>,
38 pub token_count: usize,
39 pub tool_calls: Vec<ToolCall>,
40 pub output_tokens: u64,
41 pub thinking_tokens: u64,
42 pub cache_creation_input_tokens: u64,
43 pub cache_read_input_tokens: u64,
44 pub input_tokens: u64,
45}
46
47pub async fn consume_llm_stream(
48 stream: LLMStream,
49 event_tx: &mpsc::Sender<AgentEvent>,
50 cancel_token: &CancellationToken,
51 session_id: &str,
52) -> Result<StreamHandlingOutput, AgentError> {
53 consume::consume_llm_stream_internal(
54 stream,
55 Some(event_tx),
56 cancel_token,
57 session_id,
58 DEFAULT_STREAM_IDLE_TIMEOUT,
59 )
60 .await
61}
62
63pub async fn consume_llm_stream_silent(
64 stream: LLMStream,
65 cancel_token: &CancellationToken,
66 session_id: &str,
67) -> Result<StreamHandlingOutput, AgentError> {
68 consume::consume_llm_stream_internal(
69 stream,
70 None,
71 cancel_token,
72 session_id,
73 DEFAULT_STREAM_IDLE_TIMEOUT,
74 )
75 .await
76}
77
78#[cfg(test)]
79mod tests;