//! 节点抽象:Node trait、NodeContext、NodeOutput use async_trait::async_trait; use serde::{Deserialize, Serialize}; use df_core::types::NodeId; use super::eventbus::EventBus; use super::state::StateMachine; /// 节点输入/输出上下文 #[derive(Debug, Clone)] pub struct NodeContext { /// 节点 ID pub node_id: NodeId, /// 上游节点输出(key 为上游节点 ID) pub inputs: std::collections::HashMap, /// 节点配置参数 pub config: serde_json::Value, /// 工作流执行 ID pub execution_id: String, /// 事件总线 pub event_bus: EventBus, /// 节点状态机(用于检查取消状态) pub node_status: StateMachine, } /// 节点执行输出 #[derive(Debug, Clone, Serialize, Deserialize)] pub struct NodeOutput { /// 输出数据 pub data: serde_json::Value, /// 输出元数据 pub metadata: std::collections::HashMap, } impl NodeOutput { /// 创建空输出 pub fn empty() -> Self { Self { data: serde_json::Value::Null, metadata: std::collections::HashMap::new(), } } /// 从 JSON 值创建输出 pub fn from_value(data: serde_json::Value) -> Self { Self { data, metadata: std::collections::HashMap::new(), } } } /// 节点执行结果 pub type NodeResult = anyhow::Result; /// 节点参数 Schema(JSON Schema 格式) #[derive(Debug, Clone, Serialize, Deserialize)] pub struct NodeSchema { /// 参数 JSON Schema pub params: serde_json::Value, /// 输出 JSON Schema pub output: serde_json::Value, } /// 节点 trait — 所有工作流节点必须实现 #[async_trait] pub trait Node: Send + Sync { /// 执行节点逻辑 async fn execute(&self, ctx: NodeContext) -> NodeResult; /// 返回节点的参数 Schema fn schema(&self) -> NodeSchema; /// 该节点是否为阻塞节点(阻塞工作流,等待外部输入) fn is_blocking(&self) -> bool { false } /// 节点类型名称 fn node_type(&self) -> &str; }