-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconditional_branch.rs
More file actions
87 lines (82 loc) · 2.63 KB
/
Copy pathconditional_branch.rs
File metadata and controls
87 lines (82 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//! Conditional branch: an IF node routes to exactly one of two tool_call branches.
//! Run: cargo run --example conditional_branch --features mock
#[cfg(feature = "mock")]
#[tokio::main(flavor = "current_thread")]
async fn main() {
use serde_json::{Value, json};
use tinyflows::caps::mock::mock_capabilities;
use tinyflows::compiler::compile;
use tinyflows::engine::run;
use tinyflows::model::{Edge, Node, NodeKind, WorkflowGraph};
fn node(id: &str, kind: NodeKind, config: Value) -> Node {
Node {
id: id.into(),
kind,
type_version: 1,
name: id.into(),
config,
ports: vec![],
position: None,
}
}
fn edge(from: &str, port: &str, to: &str) -> Edge {
Edge {
from_node: from.into(),
from_port: port.into(),
to_node: to.into(),
to_port: "main".into(),
}
}
// trigger -> condition("check") -> true:welcome / false:upsell
let graph = WorkflowGraph {
nodes: vec![
node("trigger", NodeKind::Trigger, Value::Null),
node(
"check",
NodeKind::Condition,
json!({ "field": "is_member" }),
),
node(
"welcome",
NodeKind::ToolCall,
json!({ "slug": "slack.welcome" }),
),
node(
"upsell",
NodeKind::ToolCall,
json!({ "slug": "crm.upsell" }),
),
],
edges: vec![
edge("trigger", "main", "check"),
edge("check", "true", "welcome"),
edge("check", "false", "upsell"),
],
..Default::default()
};
let compiled = compile(&graph).expect("compile");
let outcome = run(
&compiled,
json!({ "is_member": true }),
&mock_capabilities(),
)
.await
.expect("run");
let welcome_ran = !outcome.output["nodes"]["welcome"].is_null();
let upsell_ran = !outcome.output["nodes"]["upsell"].is_null();
println!("input: {{ \"is_member\": true }}");
println!("welcome branch ran: {welcome_ran}");
println!("upsell branch ran: {upsell_ran}");
assert!(
welcome_ran && !upsell_ran,
"only the welcome branch should run"
);
let tool = &outcome.output["nodes"]["welcome"]["items"][0]["json"]["tool"];
println!("=> took the TRUE branch; welcome called tool {tool}");
}
#[cfg(not(feature = "mock"))]
fn main() {
eprintln!(
"Run with the `mock` feature: cargo run --example conditional_branch --features mock"
);
}