Tauri 2 + Vue 3 + Vite 6 桌面应用,Rust workspace 含 13 个 crate (df-ai / df-storage / df-workflow / df-core / df-execute 等)。 核心能力:AI 聊天 agentic 循环(工具调用+人工审批)、工作流引擎、 任务/想法/项目/阶段管理、可追溯性,及配套前端组件。
84 lines
2.1 KiB
Rust
84 lines
2.1 KiB
Rust
//! 节点抽象: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>;
|
||
|
||
/// 节点参数 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;
|
||
}
|