Skip to main content

ironflow_engine/executor/
mod.rs

1//! Step executor — reconstructs operations from configs and runs them.
2//!
3//! Each step type (shell, HTTP, agent) has its own executor implementing
4//! the [`StepExecutor`] trait. The [`execute_step_config`] function dispatches
5//! to the appropriate executor based on the [`StepConfig`] variant.
6
7mod agent;
8mod http;
9mod shell;
10
11use std::future::Future;
12use std::sync::Arc;
13
14use rust_decimal::Decimal;
15use serde_json::Value;
16use uuid::Uuid;
17
18use ironflow_core::provider::{AgentProvider, DebugMessage};
19
20use crate::config::StepConfig;
21use crate::error::EngineError;
22
23pub use agent::AgentExecutor;
24pub use http::HttpExecutor;
25pub use shell::ShellExecutor;
26
27/// Result of executing a single step.
28#[derive(Debug, Clone)]
29pub struct StepOutput {
30    /// Serialized output (stdout for shell, body for http, value for agent).
31    pub output: Value,
32    /// Wall-clock duration in milliseconds.
33    pub duration_ms: u64,
34    /// Cost in USD (agent steps only).
35    pub cost_usd: Decimal,
36    /// Input token count (agent steps only).
37    pub input_tokens: Option<u64>,
38    /// Output token count (agent steps only).
39    pub output_tokens: Option<u64>,
40    /// Conversation trace from verbose agent invocations.
41    pub debug_messages: Option<Vec<DebugMessage>>,
42}
43
44/// Result of a single step within a [`parallel`](crate::context::WorkflowContext::parallel) batch.
45#[derive(Debug, Clone)]
46pub struct ParallelStepResult {
47    /// The step name (same as provided to `parallel()`).
48    pub name: String,
49    /// The step execution output.
50    pub output: StepOutput,
51    /// The step ID in the store (for dependency tracking).
52    pub step_id: Uuid,
53}
54
55/// Trait for step executors.
56///
57/// Each step type implements this trait to execute its specific operation
58/// and return a [`StepOutput`].
59pub trait StepExecutor: Send + Sync {
60    /// Execute the step and return structured output.
61    ///
62    /// # Errors
63    ///
64    /// Returns [`EngineError`] if the operation fails.
65    fn execute(
66        &self,
67        provider: &Arc<dyn AgentProvider>,
68    ) -> impl Future<Output = Result<StepOutput, EngineError>> + Send;
69}
70
71/// Execute a [`StepConfig`] and return structured output.
72///
73/// # Errors
74///
75/// Returns [`EngineError::Operation`] if the operation fails.
76///
77/// # Examples
78///
79/// ```no_run
80/// use ironflow_engine::config::{StepConfig, ShellConfig};
81/// use ironflow_engine::executor::execute_step_config;
82/// use ironflow_core::provider::AgentProvider;
83/// use ironflow_core::providers::claude::ClaudeCodeProvider;
84/// use std::sync::Arc;
85///
86/// # async fn example() -> Result<(), ironflow_engine::error::EngineError> {
87/// let provider: Arc<dyn AgentProvider> = Arc::new(ClaudeCodeProvider::new());
88/// let config = StepConfig::Shell(ShellConfig::new("echo hello"));
89/// let output = execute_step_config(&config, &provider).await?;
90/// # Ok(())
91/// # }
92/// ```
93pub async fn execute_step_config(
94    config: &StepConfig,
95    provider: &Arc<dyn AgentProvider>,
96) -> Result<StepOutput, EngineError> {
97    let _kind = match config {
98        StepConfig::Shell(_) => "shell",
99        StepConfig::Http(_) => "http",
100        StepConfig::Agent(_) => "agent",
101        StepConfig::Workflow(_) => "workflow",
102        StepConfig::Approval(_) => "approval",
103    };
104
105    let result = match config {
106        StepConfig::Shell(cfg) => ShellExecutor::new(cfg).execute(provider).await,
107        StepConfig::Http(cfg) => HttpExecutor::new(cfg).execute(provider).await,
108        StepConfig::Agent(cfg) => AgentExecutor::new(cfg).execute(provider).await,
109        StepConfig::Workflow(_) => Err(EngineError::StepConfig(
110            "workflow steps are executed by WorkflowContext, not the executor".to_string(),
111        )),
112        StepConfig::Approval(_) => Err(EngineError::StepConfig(
113            "approval steps are executed by WorkflowContext, not the executor".to_string(),
114        )),
115    };
116
117    #[cfg(feature = "prometheus")]
118    {
119        use ironflow_core::metric_names::{
120            STATUS_ERROR, STATUS_SUCCESS, STEP_DURATION_SECONDS, STEPS_TOTAL,
121        };
122        use metrics::{counter, histogram};
123        let status = if result.is_ok() {
124            STATUS_SUCCESS
125        } else {
126            STATUS_ERROR
127        };
128        counter!(STEPS_TOTAL, "kind" => _kind, "status" => status).increment(1);
129        if let Ok(ref output) = result {
130            histogram!(STEP_DURATION_SECONDS, "kind" => _kind)
131                .record(output.duration_ms as f64 / 1000.0);
132        }
133    }
134
135    result
136}