Files
DevFlow/crates/df-workflow/src/node.rs
绝尘 98393b4908 新增: 初始化 DevFlow 项目仓库
Tauri 2 + Vue 3 + Vite 6 桌面应用,Rust workspace 含 13 个 crate
(df-ai / df-storage / df-workflow / df-core / df-execute 等)。
核心能力:AI 聊天 agentic 循环(工具调用+人工审批)、工作流引擎、
任务/想法/项目/阶段管理、可追溯性,及配套前端组件。
2026-06-12 01:31:05 +08:00

84 lines
2.1 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_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<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,
/// 输出元数据
pub metadata: std::collections::HashMap<String, String>,
}
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<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;
}