mod agent;
mod code;
mod end;
mod http_request;
mod if_else;
mod start;
use std::sync::Arc;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use crate::{
Result,
common::Vars,
runtime::Context,
workflow::node::{NodeExecutionStatus, NodeId},
};
pub use agent::AgentAction;
pub use code::CodeAction;
pub use end::EndAction;
pub use http_request::HttpRequestAction;
pub use if_else::IfElseAction;
pub use start::StartAction;
#[derive(Serialize, Deserialize, Debug, Clone, Copy, Default, PartialEq, Eq, strum::AsRefStr, strum::EnumString)]
#[serde(rename_all = "snake_case")]
#[strum(serialize_all = "snake_case")]
pub enum ActionType {
#[default]
None,
Agent,
Code,
End,
HttpRequest,
IfElse,
Start,
}
#[async_trait]
#[typetag::serde(tag = "type")]
pub trait Action: Send + Sync {
fn create(params: serde_json::Value) -> Result<Self>
where
Self: Sized;
fn schema() -> serde_json::Value
where
Self: Sized;
fn action_type(&self) -> ActionType;
async fn run(
&self,
ctx: Arc<Context>,
nid: NodeId,
) -> Result<ActionOutput>;
}
#[derive(Debug, Clone)]
pub struct ActionOutput {
pub status: NodeExecutionStatus,
pub outputs: Vars,
pub error: Option<String>,
pub exception: Option<String>,
}
impl ActionOutput {
pub fn success(outputs: Vars) -> Self {
Self {
status: NodeExecutionStatus::Succeeded,
outputs,
error: None,
exception: None,
}
}
pub fn failed(error: String) -> Self {
Self {
status: NodeExecutionStatus::Failed,
outputs: Vars::new(),
error: Some(error),
exception: None,
}
}
pub fn exception(message: String) -> Self {
Self {
status: NodeExecutionStatus::Exception,
outputs: Vars::new(),
error: None,
exception: Some(message),
}
}
pub fn stopped() -> Self {
Self {
status: NodeExecutionStatus::Stopped,
outputs: Vars::new(),
error: None,
exception: None,
}
}
}