Skip to main content

daimon_core/
stream.rs

1//! Streaming event types for progressive model responses.
2
3use std::pin::Pin;
4
5use futures::Stream;
6
7use crate::error::Result;
8
9/// An event emitted during a streaming model response.
10#[derive(Debug, Clone)]
11pub enum StreamEvent {
12    /// A chunk of generated text.
13    TextDelta(String),
14    /// A tool call is starting (name and ID known, arguments pending).
15    ToolCallStart { id: String, name: String },
16    /// A chunk of tool call arguments (JSON fragment).
17    ToolCallDelta { id: String, arguments_delta: String },
18    /// A tool call's arguments are complete and the tool will be executed.
19    ToolCallEnd { id: String },
20    /// A tool has produced its result.
21    ToolResult {
22        id: String,
23        content: String,
24        is_error: bool,
25    },
26    /// Token usage and cost for the current iteration.
27    ///
28    /// Emitted after each model invocation completes (once per ReAct
29    /// iteration). During streaming, token counts are estimated from
30    /// character length (~4 chars/token).
31    Usage {
32        iteration: usize,
33        input_tokens: u32,
34        output_tokens: u32,
35        estimated_cost: f64,
36    },
37    /// An error occurred during streaming (non-fatal; the stream may continue).
38    Error(String),
39    /// The stream is complete.
40    Done,
41}
42
43/// A boxed, pinned stream of [`StreamEvent`] results.
44pub type ResponseStream = Pin<Box<dyn Stream<Item = Result<StreamEvent>> + Send>>;