Skip to main content

gadk_engine/workflow/nodes/
join.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//! `JoinNode`: converges multiple branches.
17
18use std::any::Any;
19use std::sync::Arc;
20
21use futures::StreamExt;
22use gadk_agent::{AgentError, Context, EventStream};
23use gadk_session::Event;
24use serde_json::{Map, Value};
25
26use crate::error::WorkflowError;
27use crate::workflow::route::go_quote;
28use crate::workflow::{BaseNode, Node, NodeConfig};
29
30/// Converges multiple branches, waiting for every predecessor.
31pub struct JoinNode {
32    pub(crate) base: BaseNode,
33}
34
35impl JoinNode {
36    /// A join node without an input schema.
37    pub fn new(name: &str) -> Arc<JoinNode> {
38        Arc::new(JoinNode {
39            base: BaseNode::new(name, "", NodeConfig::default()),
40        })
41    }
42
43    /// A JoinNode whose input is validated against `schema`.
44    pub fn with_schema(name: &str, schema: Value) -> Result<Arc<JoinNode>, WorkflowError> {
45        let base = BaseNode::with_schemas(name, "", Some(schema), None, NodeConfig::default())?;
46        Ok(Arc::new(JoinNode { base }))
47    }
48}
49
50impl Node for JoinNode {
51    fn name(&self) -> &str {
52        self.base.name()
53    }
54    fn description(&self) -> &str {
55        self.base.description()
56    }
57    fn config(&self) -> &NodeConfig {
58        self.base.config()
59    }
60    fn input_schema(&self) -> Option<&Value> {
61        self.base.input_schema()
62    }
63    fn output_schema(&self) -> Option<&Value> {
64        self.base.output_schema()
65    }
66    /// Validates each predecessor's output individually against the input schema; a non-map input
67    /// falls back to the default validation.
68    fn validate_input(&self, input: Value) -> Result<Value, WorkflowError> {
69        if self.base.input_schema().is_none() {
70            return Ok(input);
71        }
72        if let Value::Object(m) = &input {
73            let mut out = Map::new();
74            for (pred, v) in m {
75                let validated = self.base.validate_input(v.clone()).map_err(|e| {
76                    WorkflowError::new(e.kind(), format!("predecessor {}: {e}", go_quote(pred)))
77                        .with_source(e.into())
78                })?;
79                out.insert(pred.clone(), validated);
80            }
81            return Ok(Value::Object(out));
82        }
83        self.base.validate_input(input)
84    }
85    fn validate_output(&self, v: Value) -> Result<Value, WorkflowError> {
86        self.base.validate_output(v)
87    }
88    /// Yields the scheduler-aggregated predecessor map as its output.
89    fn run(&self, ctx: Context, input: Value) -> EventStream {
90        let mut ev = Event::new(ctx.platform(), ctx.invocation_id());
91        ev.output = (!input.is_null()).then_some(input);
92        futures::stream::once(async move { Ok::<Event, AgentError>(ev) }).boxed()
93    }
94    fn as_any(&self) -> &dyn Any {
95        self
96    }
97}