Skip to main content

gadk_engine/workflow/
edge_builder.rs

1// Copyright 2026 Chris Olson
2// Portions derived from ADK-Go (https://github.com/google/adk-go), Copyright 2026 Google LLC.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! `EdgeBuilder`, `chain` and `concat`.
17
18use std::collections::BTreeMap;
19use std::sync::Arc;
20
21use crate::workflow::{Edge, Node, Route, StringRoute};
22
23/// Accumulates edges fluently.
24#[derive(Clone, Default)]
25pub struct EdgeBuilder {
26    edges: Vec<Edge>,
27}
28
29impl EdgeBuilder {
30    /// An empty builder.
31    pub fn new() -> Self {
32        Self::default()
33    }
34
35    /// Adds an unconditional edge between two nodes.
36    pub fn add(&mut self, from: &Arc<dyn Node>, to: &Arc<dyn Node>) -> &mut Self {
37        self.edges.push(Edge {
38            from: Arc::clone(from),
39            to: Arc::clone(to),
40            route: None,
41        });
42        self
43    }
44
45    /// Adds an edge with a route condition between two nodes.
46    pub fn add_route(
47        &mut self,
48        from: &Arc<dyn Node>,
49        to: &Arc<dyn Node>,
50        route: Arc<dyn Route>,
51    ) -> &mut Self {
52        self.edges.push(Edge {
53            from: Arc::clone(from),
54            to: Arc::clone(to),
55            route: Some(route),
56        });
57        self
58    }
59
60    /// Adds edges from one source to each target.
61    pub fn add_fan_out(&mut self, from: &Arc<dyn Node>, to: &[Arc<dyn Node>]) -> &mut Self {
62        for t in to {
63            self.add(from, t);
64        }
65        self
66    }
67
68    /// Adds edges from each source to one target.
69    pub fn add_fan_in(&mut self, to: &Arc<dyn Node>, from: &[Arc<dyn Node>]) -> &mut Self {
70        for f in from {
71            self.add(f, to);
72        }
73        self
74    }
75
76    /// Adds one `StringRoute` edge per entry, in byte-wise lexicographic order of the route key
77    /// (so "10" precedes "2"). That order is observable: it drives the order successors are
78    /// started in. A later duplicate key replaces an earlier one (map semantics).
79    pub fn add_routes(
80        &mut self,
81        from: &Arc<dyn Node>,
82        routes: impl IntoIterator<Item = (String, Arc<dyn Node>)>,
83    ) -> &mut Self {
84        let sorted: BTreeMap<String, Arc<dyn Node>> = routes.into_iter().collect();
85        for (key, to) in sorted {
86            self.add_route(from, &to, Arc::new(StringRoute(key)));
87        }
88        self
89    }
90
91    /// The accumulated edges.
92    pub fn build(&self) -> Vec<Edge> {
93        self.edges.clone()
94    }
95}
96
97/// Edges forming a chain of nodes; fewer than two nodes → no edges.
98pub fn chain(nodes: &[Arc<dyn Node>]) -> Vec<Edge> {
99    nodes
100        .windows(2)
101        .map(|w| Edge {
102            from: Arc::clone(&w[0]),
103            to: Arc::clone(&w[1]),
104            route: None,
105        })
106        .collect()
107}
108
109/// Combines edge lists into a single list.
110pub fn concat(parts: impl IntoIterator<Item = Vec<Edge>>) -> Vec<Edge> {
111    parts.into_iter().flatten().collect()
112}