feat(flow): 新增流式执行模式与SSE支持
新增流式执行模式,通过SSE实时推送节点执行事件与日志 重构HTTP执行器与中间件,提取通用HTTP客户端组件 优化前端测试面板,支持流式模式切换与实时日志展示 更新依赖版本并修复密码哈希的随机数生成器问题 修复前端节点类型映射问题,确保Code节点表单可用
This commit is contained in:
@ -4,11 +4,16 @@ use serde::Deserialize;
|
||||
use tracing::{info, error};
|
||||
use crate::middlewares::jwt::AuthUser;
|
||||
|
||||
// 新增:引入通用 SSE 组件
|
||||
use crate::middlewares::sse;
|
||||
|
||||
pub fn router() -> Router<Db> {
|
||||
Router::new()
|
||||
.route("/flows", post(create).get(list))
|
||||
.route("/flows/{id}", get(get_one).put(update).delete(remove))
|
||||
.route("/flows/{id}/run", post(run))
|
||||
// 新增:流式运行(SSE)端点
|
||||
.route("/flows/{id}/run/stream", post(run_stream))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@ -82,4 +87,23 @@ async fn run(State(db): State<Db>, user: AuthUser, Path(id): Path<String>, Json(
|
||||
Err(AppError::InternalMsg(full))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 新增:SSE 流式运行端点,请求体沿用 RunReq(只包含 input)
|
||||
async fn run_stream(State(db): State<Db>, user: AuthUser, Path(id): Path<String>, Json(req): Json<RunReq>) -> Result<axum::response::sse::Sse<impl futures::Stream<Item = Result<axum::response::sse::Event, std::convert::Infallible>>>, AppError> {
|
||||
// 建立 mpsc 通道用于接收引擎的流式事件
|
||||
let (tx, rx) = tokio::sync::mpsc::channel::<crate::flow::context::StreamEvent>(16);
|
||||
|
||||
// 启动后台任务运行流程,将事件通过 tx 发送
|
||||
let db_clone = db.clone();
|
||||
let id_clone = id.clone();
|
||||
let input = req.input.clone();
|
||||
let user_info = Some((user.uid, user.username));
|
||||
tokio::spawn(async move {
|
||||
// 复用 flow_service::run 内部大部分逻辑,但通过 DriveOptions 注入 event_tx
|
||||
let _ = flow_service::run_with_stream(db_clone, &id_clone, flow_service::RunReq { input }, user_info, tx).await;
|
||||
});
|
||||
|
||||
// 由通用组件把 Receiver 包装为 SSE 响应
|
||||
Ok(sse::from_mpsc(rx))
|
||||
}
|
||||
Reference in New Issue
Block a user