use crate::state::State;
use serde::Serialize;
use serde_json::Value;
use std::collections::HashMap;
#[derive(Clone, Copy, Debug, Default)]
pub enum StreamMode {
#[default]
Values,
Updates,
Messages,
Custom,
Debug,
}
#[derive(Clone, Debug, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum StreamEvent {
State { state: State, step: usize },
Updates { node: String, updates: HashMap<String, Value> },
Message { node: String, content: String, is_final: bool },
Custom { node: String, event_type: String, data: Value },
Debug { event_type: String, data: Value },
NodeStart { node: String, step: usize },
NodeEnd { node: String, step: usize, duration_ms: u64 },
StepComplete { step: usize, nodes_executed: Vec<String> },
Interrupted {
node: String,
message: String,
#[serde(skip_serializing_if = "Option::is_none")]
interrupt_id: Option<String>,
},
NodeInterrupt {
node: String,
message: String,
data: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
response_schema: Option<serde_json::Value>,
},
Resumed { step: usize, pending_nodes: Vec<String> },
Done { state: State, total_steps: usize },
Error { message: String, node: Option<String> },
RouteDispatched { source: String, targets: Vec<String> },
}
impl StreamEvent {
pub fn state(state: State, step: usize) -> Self {
Self::State { state, step }
}
pub fn updates(node: &str, updates: HashMap<String, Value>) -> Self {
Self::Updates { node: node.to_string(), updates }
}
pub fn message(node: &str, content: &str, is_final: bool) -> Self {
Self::Message { node: node.to_string(), content: content.to_string(), is_final }
}
pub fn custom(node: &str, event_type: &str, data: Value) -> Self {
Self::Custom { node: node.to_string(), event_type: event_type.to_string(), data }
}
pub fn debug(event_type: &str, data: Value) -> Self {
Self::Debug { event_type: event_type.to_string(), data }
}
pub fn node_start(node: &str, step: usize) -> Self {
Self::NodeStart { node: node.to_string(), step }
}
pub fn node_end(node: &str, step: usize, duration_ms: u64) -> Self {
Self::NodeEnd { node: node.to_string(), step, duration_ms }
}
pub fn step_complete(step: usize, nodes_executed: Vec<String>) -> Self {
Self::StepComplete { step, nodes_executed }
}
pub fn interrupted(node: &str, message: &str) -> Self {
Self::Interrupted {
node: node.to_string(),
message: message.to_string(),
interrupt_id: None,
}
}
pub fn resumed(step: usize, pending_nodes: Vec<String>) -> Self {
Self::Resumed { step, pending_nodes }
}
pub fn done(state: State, total_steps: usize) -> Self {
Self::Done { state, total_steps }
}
pub fn error(message: &str, node: Option<&str>) -> Self {
Self::Error { message: message.to_string(), node: node.map(|s| s.to_string()) }
}
pub fn node_interrupt(node: &str, message: &str, data: Option<serde_json::Value>) -> Self {
Self::NodeInterrupt {
node: node.to_string(),
message: message.to_string(),
data,
response_schema: None,
}
}
pub fn route_dispatched(source: &str, targets: Vec<String>) -> Self {
Self::RouteDispatched { source: source.to_string(), targets }
}
}