Skip to content

Commit 5137604

Browse files
damianmomotgooglecopybara-github
authored andcommitted
feat: add the experimental JoinNode workflow node
PiperOrigin-RevId: 998666473
1 parent 91a1838 commit 5137604

2 files changed

Lines changed: 221 additions & 0 deletions

File tree

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* 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+
17+
package com.google.adk.workflow;
18+
19+
import static com.google.common.base.Preconditions.checkNotNull;
20+
import static com.google.common.base.Preconditions.checkState;
21+
22+
import com.google.adk.agents.Context;
23+
import com.google.adk.annotations.Experimental;
24+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
25+
import com.google.genai.types.Schema;
26+
import io.reactivex.rxjava3.core.Flowable;
27+
import org.jspecify.annotations.Nullable;
28+
29+
/**
30+
* A node that waits for every predecessor to complete, then outputs their outputs keyed by
31+
* predecessor name. Route each predecessor into the join every time it completes: one that routes
32+
* elsewhere still counts as complete, and if it completes last, the join never runs.
33+
*/
34+
@Experimental
35+
public final class JoinNode extends BaseNode {
36+
37+
private JoinNode(Builder builder) {
38+
super(
39+
builder.name,
40+
builder.description,
41+
/* rerunOnResume= */ false,
42+
/* waitForOutput= */ false,
43+
NodeConfig.builder().build(),
44+
builder.inputSchema,
45+
builder.outputSchema,
46+
/* stateSchema= */ null);
47+
}
48+
49+
public static Builder builder() {
50+
return new Builder();
51+
}
52+
53+
@Override
54+
public boolean requiresAllPredecessors() {
55+
return true;
56+
}
57+
58+
/** Outputs its input unchanged, or nothing when the input is null. */
59+
@Override
60+
public Flowable<?> runNode(Context context, @Nullable Object nodeInput) {
61+
return nodeInput == null ? Flowable.empty() : Flowable.just(nodeInput);
62+
}
63+
64+
/** Builder for {@link JoinNode}. Only the name is required. */
65+
public static final class Builder {
66+
private @Nullable String name;
67+
private String description = "";
68+
private @Nullable Schema inputSchema;
69+
private @Nullable Schema outputSchema;
70+
71+
private Builder() {}
72+
73+
@CanIgnoreReturnValue
74+
public Builder name(String name) {
75+
this.name = checkNotNull(name);
76+
return this;
77+
}
78+
79+
@CanIgnoreReturnValue
80+
public Builder description(String description) {
81+
this.description = checkNotNull(description);
82+
return this;
83+
}
84+
85+
/** Sets the schema that each predecessor's output must match on its own, not the joined map. */
86+
@CanIgnoreReturnValue
87+
public Builder inputSchema(@Nullable Schema inputSchema) {
88+
this.inputSchema = inputSchema;
89+
return this;
90+
}
91+
92+
@CanIgnoreReturnValue
93+
public Builder outputSchema(@Nullable Schema outputSchema) {
94+
this.outputSchema = outputSchema;
95+
return this;
96+
}
97+
98+
public JoinNode build() {
99+
checkState(name != null, "JoinNode.Builder requires name to be set.");
100+
return new JoinNode(this);
101+
}
102+
}
103+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* 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+
17+
package com.google.adk.workflow;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static org.junit.Assert.assertThrows;
21+
22+
import com.google.adk.agents.CallbackContext;
23+
import com.google.adk.agents.Context;
24+
import com.google.adk.testing.TestUtils;
25+
import com.google.common.collect.ImmutableMap;
26+
import com.google.genai.types.Schema;
27+
import java.util.List;
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
import org.junit.runners.JUnit4;
31+
32+
@RunWith(JUnit4.class)
33+
public final class JoinNodeTest {
34+
35+
@Test
36+
public void build_withOnlyAName_leavesTheRestAtDefaults() {
37+
JoinNode join = JoinNode.builder().name("gather").build();
38+
39+
assertThat(join.name()).isEqualTo("gather");
40+
assertThat(join.description()).isEmpty();
41+
assertThat(join.inputSchema()).isEmpty();
42+
assertThat(join.outputSchema()).isEmpty();
43+
assertThat(join.rerunOnResume()).isFalse();
44+
assertThat(join.waitForOutput()).isFalse();
45+
assertThat(join.config()).isEqualTo(NodeConfig.builder().build());
46+
assertThat(join.stateSchema()).isEmpty();
47+
}
48+
49+
@Test
50+
public void build_setsEveryProperty() {
51+
Schema inputSchema = Schema.builder().description("input").build();
52+
Schema outputSchema = Schema.builder().description("output").build();
53+
54+
JoinNode join =
55+
JoinNode.builder()
56+
.name("gather")
57+
.description("Joins the research notes.")
58+
.inputSchema(inputSchema)
59+
.outputSchema(outputSchema)
60+
.build();
61+
62+
assertThat(join.description()).isEqualTo("Joins the research notes.");
63+
assertThat(join.inputSchema()).hasValue(inputSchema);
64+
assertThat(join.outputSchema()).hasValue(outputSchema);
65+
}
66+
67+
@Test
68+
public void build_rejectsAMissingName() {
69+
JoinNode.Builder builder = JoinNode.builder();
70+
71+
IllegalStateException e = assertThrows(IllegalStateException.class, builder::build);
72+
73+
assertThat(e).hasMessageThat().contains("name");
74+
}
75+
76+
@Test
77+
public void name_rejectsNull() {
78+
JoinNode.Builder builder = JoinNode.builder();
79+
80+
assertThrows(NullPointerException.class, () -> builder.name(null));
81+
}
82+
83+
@Test
84+
public void description_rejectsNull() {
85+
JoinNode.Builder builder = JoinNode.builder();
86+
87+
assertThrows(NullPointerException.class, () -> builder.description(null));
88+
}
89+
90+
@Test
91+
public void requiresAllPredecessors_isTrue() {
92+
assertThat(JoinNode.builder().name("gather").build().requiresAllPredecessors()).isTrue();
93+
}
94+
95+
@Test
96+
public void runNode_outputsTheJoinedOutputsUnchanged() {
97+
JoinNode join = JoinNode.builder().name("gather").build();
98+
ImmutableMap<String, Object> joined = ImmutableMap.of("a", "A", "b", "B");
99+
100+
List<?> outputs = join.runNode(context(), joined).toList().blockingGet();
101+
102+
assertThat(outputs).containsExactly(joined);
103+
}
104+
105+
@Test
106+
public void runNode_withoutInput_outputsNothing() {
107+
JoinNode join = JoinNode.builder().name("gather").build();
108+
109+
List<?> outputs = join.runNode(context(), /* nodeInput= */ null).toList().blockingGet();
110+
111+
assertThat(outputs).isEmpty();
112+
}
113+
114+
private static Context context() {
115+
return new CallbackContext(
116+
TestUtils.createInvocationContext(TestUtils.createRootAgent()), /* eventActions= */ null);
117+
}
118+
}

0 commit comments

Comments
 (0)