Skip to main content

adk_graph/
stream.rs

1//! Streaming types for graph execution
2
3use crate::state::State;
4use serde::Serialize;
5use serde_json::Value;
6use std::collections::HashMap;
7
8/// Stream mode options
9#[derive(Clone, Copy, Debug, Default)]
10pub enum StreamMode {
11    /// Full state after each super-step
12    #[default]
13    Values,
14    /// Only state changes
15    Updates,
16    /// LLM tokens and messages
17    Messages,
18    /// Custom events from nodes
19    Custom,
20    /// Debug information
21    Debug,
22}
23
24/// Events emitted during streaming
25#[derive(Clone, Debug, Serialize)]
26#[serde(tag = "type", rename_all = "snake_case")]
27pub enum StreamEvent {
28    /// State snapshot
29    State { state: State, step: usize },
30
31    /// State updates from a node
32    Updates { node: String, updates: HashMap<String, Value> },
33
34    /// Message/token from LLM
35    Message { node: String, content: String, is_final: bool },
36
37    /// Custom event from node
38    Custom { node: String, event_type: String, data: Value },
39
40    /// Debug event
41    Debug { event_type: String, data: Value },
42
43    /// Node started execution
44    NodeStart { node: String, step: usize },
45
46    /// Node completed execution
47    NodeEnd { node: String, step: usize, duration_ms: u64 },
48
49    /// Super-step completed
50    StepComplete { step: usize, nodes_executed: Vec<String> },
51
52    /// Execution was interrupted
53    Interrupted {
54        /// The node that paused, or the gated node.
55        node: String,
56        /// What the pause says.
57        message: String,
58        /// The pause's id, for a dynamic interrupt: answer it under this key
59        /// ([`Interrupt::id`](crate::interrupt::Interrupt::id)).
60        #[serde(skip_serializing_if = "Option::is_none")]
61        interrupt_id: Option<String>,
62    },
63
64    /// A node asked to pause, reported by `Node::execute_stream`.
65    ///
66    /// The streamed path yields events rather than a `NodeOutput`, so a node's own
67    /// interrupt request needs a way through. The executor turns this into the
68    /// pause and does not forward it, so a caller sees only `Interrupted`.
69    NodeInterrupt {
70        /// The node asking to pause.
71        node: String,
72        /// What the pause says.
73        message: String,
74        /// The data it attached.
75        data: Option<serde_json::Value>,
76        /// The JSON Schema its answer must satisfy.
77        #[serde(skip_serializing_if = "Option::is_none")]
78        response_schema: Option<serde_json::Value>,
79    },
80
81    /// Execution resumed from a checkpoint
82    Resumed { step: usize, pending_nodes: Vec<String> },
83
84    /// Graph execution completed
85    Done { state: State, total_steps: usize },
86
87    /// Error occurred
88    Error { message: String, node: Option<String> },
89
90    /// Emitted when dynamic route dispatch occurs.
91    RouteDispatched { source: String, targets: Vec<String> },
92}
93
94impl StreamEvent {
95    /// Create a state event
96    pub fn state(state: State, step: usize) -> Self {
97        Self::State { state, step }
98    }
99
100    /// Create an updates event
101    pub fn updates(node: &str, updates: HashMap<String, Value>) -> Self {
102        Self::Updates { node: node.to_string(), updates }
103    }
104
105    /// Create a message event
106    pub fn message(node: &str, content: &str, is_final: bool) -> Self {
107        Self::Message { node: node.to_string(), content: content.to_string(), is_final }
108    }
109
110    /// Create a custom event
111    pub fn custom(node: &str, event_type: &str, data: Value) -> Self {
112        Self::Custom { node: node.to_string(), event_type: event_type.to_string(), data }
113    }
114
115    /// Create a debug event
116    pub fn debug(event_type: &str, data: Value) -> Self {
117        Self::Debug { event_type: event_type.to_string(), data }
118    }
119
120    /// Create a node start event
121    pub fn node_start(node: &str, step: usize) -> Self {
122        Self::NodeStart { node: node.to_string(), step }
123    }
124
125    /// Create a node end event
126    pub fn node_end(node: &str, step: usize, duration_ms: u64) -> Self {
127        Self::NodeEnd { node: node.to_string(), step, duration_ms }
128    }
129
130    /// Create a step complete event
131    pub fn step_complete(step: usize, nodes_executed: Vec<String>) -> Self {
132        Self::StepComplete { step, nodes_executed }
133    }
134
135    /// Create an interrupted event
136    pub fn interrupted(node: &str, message: &str) -> Self {
137        Self::Interrupted {
138            node: node.to_string(),
139            message: message.to_string(),
140            interrupt_id: None,
141        }
142    }
143
144    /// Create a resumed event
145    pub fn resumed(step: usize, pending_nodes: Vec<String>) -> Self {
146        Self::Resumed { step, pending_nodes }
147    }
148
149    /// Create a done event
150    pub fn done(state: State, total_steps: usize) -> Self {
151        Self::Done { state, total_steps }
152    }
153
154    /// Create an error event
155    pub fn error(message: &str, node: Option<&str>) -> Self {
156        Self::Error { message: message.to_string(), node: node.map(|s| s.to_string()) }
157    }
158
159    /// Create an event reporting that a node asked to pause.
160    pub fn node_interrupt(node: &str, message: &str, data: Option<serde_json::Value>) -> Self {
161        Self::NodeInterrupt {
162            node: node.to_string(),
163            message: message.to_string(),
164            data,
165            response_schema: None,
166        }
167    }
168
169    /// Create a route dispatched event
170    pub fn route_dispatched(source: &str, targets: Vec<String>) -> Self {
171        Self::RouteDispatched { source: source.to_string(), targets }
172    }
173}