Skip to main content

workflow/
workflow.rs

1use agent_line::{Agent, Ctx, Outcome, Runner, StepResult, Workflow};
2
3#[derive(Clone)]
4struct State {
5    n: i32,
6}
7
8struct AddOne;
9impl Agent<State> for AddOne {
10    fn name(&self) -> &'static str {
11        "add_one"
12    }
13    fn run(&mut self, state: State, _ctx: &mut Ctx) -> StepResult<State> {
14        Ok((State { n: state.n + 1 }, Outcome::Continue))
15    }
16}
17
18struct StopAtThree;
19impl Agent<State> for StopAtThree {
20    fn name(&self) -> &'static str {
21        "stop"
22    }
23    fn run(&mut self, state: State, _ctx: &mut Ctx) -> StepResult<State> {
24        if state.n >= 3 {
25            Ok((state, Outcome::Done))
26        } else {
27            Ok((state, Outcome::Next("add_one")))
28        }
29    }
30}
31
32fn main() {
33    let mut ctx = Ctx::new();
34    let wf = Workflow::builder("demo")
35        .register(AddOne)
36        .register(StopAtThree)
37        .start_at("add_one")
38        .then("stop")
39        .build()
40        .unwrap();
41
42    let final_state = Runner::new(wf).run(State { n: 0 }, &mut ctx).unwrap();
43    println!("final n={}", final_state.n);
44}