Skip to main content

butterflow_models/
workflow.rs

1use chrono::{DateTime, Utc};
2use schemars::JsonSchema;
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5use std::path::PathBuf;
6use uuid::Uuid;
7
8use crate::node::Node;
9use crate::state::StateSchema;
10use crate::template::Template;
11use ts_rs::TS;
12
13/// Represents a workflow definition
14#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, TS)]
15pub struct Workflow {
16    /// Version of the workflow format
17    pub version: String,
18
19    /// State schema definition
20    #[serde(default)]
21    #[ts(optional=nullable)]
22    pub state: Option<WorkflowState>,
23
24    // Why using as="Option<Vec<Template>>" -> https://github.com/Aleph-Alpha/ts-rs/issues/175
25    /// Templates for reusable components
26    #[serde(default)]
27    #[ts(optional, as = "Option<Vec<Template>>")]
28    pub templates: Vec<Template>,
29
30    /// Nodes in the workflow
31    pub nodes: Vec<Node>,
32}
33
34/// Represents the state schema for a workflow
35#[derive(Debug, Clone, Serialize, Deserialize, Default, JsonSchema, TS)]
36pub struct WorkflowState {
37    /// Schema definitions
38    #[serde(default)]
39    pub schema: Vec<StateSchema>,
40}
41
42/// Represents a workflow run
43#[derive(Debug, Clone, Serialize, Deserialize, TS)]
44pub struct WorkflowRun {
45    /// Unique identifier for the workflow run
46    pub id: Uuid,
47
48    /// The workflow definition
49    pub workflow: Workflow,
50
51    /// Current status of the workflow run
52    pub status: WorkflowStatus,
53
54    /// Parameters passed to the workflow
55    pub params: HashMap<String, String>,
56
57    /// Tasks created for this workflow run
58    pub tasks: Vec<Uuid>,
59
60    /// Start time of the workflow run
61    pub started_at: DateTime<Utc>,
62
63    /// End time of the workflow run (if completed or failed)
64    #[serde(default)]
65    #[ts(optional=nullable)]
66    pub ended_at: Option<DateTime<Utc>>,
67
68    // This is not persisted, only used during runtime
69    /// The absolute path to the root directory of the workflow bundle
70    #[ts(skip)]
71    #[serde(skip)]
72    pub bundle_path: Option<PathBuf>,
73}
74
75/// Status of a workflow run
76#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
77pub enum WorkflowStatus {
78    /// Workflow is pending execution
79    Pending,
80
81    /// Workflow is currently running
82    Running,
83
84    /// Workflow has completed successfully
85    Completed,
86
87    /// Workflow has failed
88    Failed,
89
90    /// Workflow is paused waiting for manual triggers
91    AwaitingTrigger,
92
93    /// Workflow has been canceled
94    Canceled,
95}