Files
DevFlow/crates/df-workflow/src/node.rs

78 lines
1.9 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! 节点抽象Node trait、NodeContext、NodeOutput
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use df_types::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<String, NodeOutput>,
/// 节点配置参数
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,
}
impl NodeOutput {
/// 创建空输出
pub fn empty() -> Self {
Self {
data: serde_json::Value::Null,
}
}
/// 从 JSON 值创建输出
pub fn from_value(data: serde_json::Value) -> Self {
Self { data }
}
}
/// 节点执行结果
pub type NodeResult = anyhow::Result<NodeOutput>;
/// 节点参数 SchemaJSON 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;
}