use a3s_flow::{
FlowEngine, FlowError, FlowRuntime, RuntimeCommand,
StepInvocation, WorkflowInvocation, WorkflowSpec,
};
use async_trait::async_trait;
use serde_json::json;
use std::sync::Arc;
struct GreetingRuntime;
#[async_trait]
impl FlowRuntime for GreetingRuntime {
async fn run_workflow(
&self,
invocation: WorkflowInvocation,
) -> a3s_flow::Result<RuntimeCommand> {
let ctx = invocation.context();
if let Some(output) = ctx.step_output("greet") {
return Ok(ctx.complete(output.clone()));
}
Ok(ctx.schedule_step(
"greet",
"greet_user",
json!({ "name": ctx.input()["name"] }),
))
}
async fn run_step(
&self,
invocation: StepInvocation,
) -> a3s_flow::Result<serde_json::Value> {
match invocation.step_name.as_str() {
"greet_user" => Ok(json!({
"message": format!(
"hello {}",
invocation.input["name"]
.as_str()
.unwrap_or("unknown"),
),
})),
name => Err(FlowError::Runtime(
format!("unknown step: {name}"),
)),
}
}
}
#[tokio::main]
async fn main() -> a3s_flow::Result<()> {
let engine = FlowEngine::in_memory(Arc::new(GreetingRuntime));
let spec = WorkflowSpec::rust_embedded(
"demo.greeting",
"0.1.0",
"demo",
"main",
);
let run_id = engine
.start_with_id(
"greeting-ada",
spec,
json!({ "name": "Ada" }),
)
.await?;
let snapshot = engine.snapshot(&run_id).await?;
println!("status={:?}", snapshot.status);
println!("output={:?}", snapshot.output);
Ok(())
}