Skip to main content

sz_rust_workflow/
lib.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2024-2026 SZ-Rust Team
3//
4#![forbid(unsafe_code)]
5#![allow(missing_docs)]
6#![doc = "SZ-Rust 工作流引擎 — 状态机/审批流/插件节点编排"]
7//!
8//! ## 核心组件
9//!
10//! - [`config::WorkflowConfig`] — 引擎配置
11//! - [`error::WorkflowError`] / [`error::WorkflowErrorCode`] — 错误类型(26 错误码)
12//! - [`definition`] — 流程定义模型与解析校验
13//! - [`engine`] — 状态机/审批流/实例/插件节点引擎
14//! - [`guard`] — 守卫条件求值
15//! - [`scheduling`] — 审批策略/候选人解析/任务动作/容错策略
16//! - [`instance`] — 实例/任务/历史领域模型
17//! - [`repository`] — 持久化 Repository trait + InMemory 实现
18//! - [`observability`] — 事件总线/指标/审计
19//! - [`integration`] — 插件卸载联动/敏感字段脱敏
20//! - [`api`] — 设计器 HTTP API
21
22pub mod api;
23pub mod config;
24pub mod definition;
25pub mod deps;
26pub mod engine;
27pub mod error;
28pub mod guard;
29pub mod instance;
30pub mod integration;
31pub mod observability;
32pub mod repository;
33pub mod scheduling;
34
35pub use config::WorkflowConfig;
36pub use definition::{
37    ApprovalStrategyType, CandidateStrategy, DefinitionFormat, DefinitionParser,
38    DefinitionValidator, FaultStrategy, FlowDefinition, IssueSeverity, NodeConfig, NodeType,
39    StateMachineDefinition, Transition, ValidationIssue,
40};
41pub use deps::{WorkflowDeps, WorkflowDepsBuilder};
42pub use engine::WorkflowEngine;
43pub use error::{WorkflowError, WorkflowErrorCode, WorkflowResult};
44pub use instance::{
45    FlowInstance, HistoryEntry, InstanceStatus, PageRequest, PageResult, Task, TaskAction,
46    TaskStatus,
47};
48
49// ============================================================================
50// Addon 接线:WorkflowState + register_routes
51// ============================================================================
52
53use axum::response::Json;
54use serde_json::json;
55use sz_rust_core::router::RouterBuilder;
56
57/// workflow addon 状态
58#[derive(Clone)]
59pub struct WorkflowState {
60    pub version: &'static str,
61}
62
63impl Default for WorkflowState {
64    fn default() -> Self {
65        Self {
66            version: env!("CARGO_PKG_VERSION"),
67        }
68    }
69}
70
71fn create_engine() -> WorkflowEngine {
72    let config = WorkflowConfig::default();
73    let deps = WorkflowDeps::default_for_test();
74    WorkflowEngine::new(config, deps)
75}
76
77/// 注册 workflow addon 路由到 sz300 RouterBuilder
78pub fn register_routes<S>(builder: RouterBuilder<S>, state: WorkflowState) -> RouterBuilder<S>
79where
80    S: Clone + Send + Sync + 'static,
81{
82    let builder = builder.get("/api/workflow/health", {
83        let v = state.version;
84        move || async move {
85            let _engine = create_engine();
86            Json(json!({
87                "code": 1,
88                "msg": "success",
89                "data": {
90                    "plugin": "workflow",
91                    "status": "active",
92                    "engine": "WorkflowEngine",
93                    "version": v
94                }
95            }))
96        }
97    });
98
99    let builder = builder.get("/api/workflow/definitions", {
100        move || async move {
101            Json(json!({
102                "code": 1,
103                "msg": "success",
104                "data": {
105                    "definitions": [],
106                    "total": 0
107                }
108            }))
109        }
110    });
111
112    let builder = builder.get("/api/workflow/instances", {
113        move || async move {
114            let engine = create_engine();
115            let page = PageRequest::default();
116            let pending_tasks = engine
117                .query_tasks("", page)
118                .await
119                .map(|r| r.total)
120                .unwrap_or(0);
121            Json(json!({
122                "code": 1,
123                "msg": "success",
124                "data": {
125                    "instances": [],
126                    "total": 0,
127                    "pending_tasks": pending_tasks
128                }
129            }))
130        }
131    });
132
133    builder
134}
135
136pub mod capability;
137pub use capability::WorkflowPlugin;