Skip to content

Commit dbd26d9

Browse files
authored
Merge branch 'main' into kawo
2 parents cccbd40 + c2c4e46 commit dbd26d9

File tree

16 files changed

+415
-316
lines changed

16 files changed

+415
-316
lines changed

core/src/main/java/com/google/adk/agents/BaseAgent.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.adk.events.Event;
2525
import com.google.adk.plugins.PluginManager;
2626
import com.google.common.collect.ImmutableList;
27+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2728
import com.google.errorprone.annotations.DoNotCall;
2829
import com.google.genai.types.Content;
2930
import io.opentelemetry.api.trace.Span;
@@ -389,4 +390,73 @@ public static BaseAgent fromConfig(BaseAgentConfig config, String configAbsPath)
389390
throw new UnsupportedOperationException(
390391
"BaseAgent is abstract. Override fromConfig in concrete subclasses.");
391392
}
393+
394+
/**
395+
* Base Builder for all agents.
396+
*
397+
* @param <B> The concrete builder type.
398+
*/
399+
public abstract static class Builder<B extends Builder<B>> {
400+
protected String name;
401+
protected String description;
402+
protected ImmutableList<BaseAgent> subAgents;
403+
protected ImmutableList<BeforeAgentCallback> beforeAgentCallback;
404+
protected ImmutableList<AfterAgentCallback> afterAgentCallback;
405+
406+
/** This is a safe cast to the concrete builder type. */
407+
@SuppressWarnings("unchecked")
408+
protected B self() {
409+
return (B) this;
410+
}
411+
412+
@CanIgnoreReturnValue
413+
public B name(String name) {
414+
this.name = name;
415+
return self();
416+
}
417+
418+
@CanIgnoreReturnValue
419+
public B description(String description) {
420+
this.description = description;
421+
return self();
422+
}
423+
424+
@CanIgnoreReturnValue
425+
public B subAgents(List<? extends BaseAgent> subAgents) {
426+
this.subAgents = ImmutableList.copyOf(subAgents);
427+
return self();
428+
}
429+
430+
@CanIgnoreReturnValue
431+
public B subAgents(BaseAgent... subAgents) {
432+
this.subAgents = ImmutableList.copyOf(subAgents);
433+
return self();
434+
}
435+
436+
@CanIgnoreReturnValue
437+
public B beforeAgentCallback(BeforeAgentCallback beforeAgentCallback) {
438+
this.beforeAgentCallback = ImmutableList.of(beforeAgentCallback);
439+
return self();
440+
}
441+
442+
@CanIgnoreReturnValue
443+
public B beforeAgentCallback(List<Callbacks.BeforeAgentCallbackBase> beforeAgentCallback) {
444+
this.beforeAgentCallback = CallbackUtil.getBeforeAgentCallbacks(beforeAgentCallback);
445+
return self();
446+
}
447+
448+
@CanIgnoreReturnValue
449+
public B afterAgentCallback(AfterAgentCallback afterAgentCallback) {
450+
this.afterAgentCallback = ImmutableList.of(afterAgentCallback);
451+
return self();
452+
}
453+
454+
@CanIgnoreReturnValue
455+
public B afterAgentCallback(List<Callbacks.AfterAgentCallbackBase> afterAgentCallback) {
456+
this.afterAgentCallback = CallbackUtil.getAfterAgentCallbacks(afterAgentCallback);
457+
return self();
458+
}
459+
460+
public abstract BaseAgent build();
461+
}
392462
}

core/src/main/java/com/google/adk/agents/BaseAgentConfig.java

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,29 @@ public class BaseAgentConfig {
2929
private String agentClass;
3030
private List<AgentRefConfig> subAgents;
3131

32+
// Callback configuration (names resolved via ComponentRegistry)
33+
private List<CallbackRef> beforeAgentCallbacks;
34+
private List<CallbackRef> afterAgentCallbacks;
35+
36+
/** Reference to a callback stored in the ComponentRegistry. */
37+
public static class CallbackRef {
38+
private String name;
39+
40+
public CallbackRef() {}
41+
42+
public CallbackRef(String name) {
43+
this.name = name;
44+
}
45+
46+
public String name() {
47+
return name;
48+
}
49+
50+
public void setName(String name) {
51+
this.name = name;
52+
}
53+
}
54+
3255
/**
3356
* Configuration for referencing other agents (subagents). Supports both config-based references
3457
* (YAML files) and programmatic references (via code registry).
@@ -67,6 +90,10 @@ public void setCode(String code) {
6790

6891
public BaseAgentConfig() {}
6992

93+
public BaseAgentConfig(String agentClass) {
94+
this.agentClass = agentClass;
95+
}
96+
7097
/**
7198
* Constructor with basic fields.
7299
*
@@ -96,19 +123,35 @@ public void setDescription(String description) {
96123
this.description = description;
97124
}
98125

99-
public String agentClass() {
100-
return agentClass;
101-
}
102-
103126
public void setAgentClass(String agentClass) {
104127
this.agentClass = agentClass;
105128
}
106129

130+
public String agentClass() {
131+
return agentClass;
132+
}
133+
107134
public List<AgentRefConfig> subAgents() {
108135
return subAgents;
109136
}
110137

111138
public void setSubAgents(List<AgentRefConfig> subAgents) {
112139
this.subAgents = subAgents;
113140
}
141+
142+
public List<CallbackRef> beforeAgentCallbacks() {
143+
return beforeAgentCallbacks;
144+
}
145+
146+
public void setBeforeAgentCallbacks(List<CallbackRef> beforeAgentCallbacks) {
147+
this.beforeAgentCallbacks = beforeAgentCallbacks;
148+
}
149+
150+
public List<CallbackRef> afterAgentCallbacks() {
151+
return afterAgentCallbacks;
152+
}
153+
154+
public void setAfterAgentCallbacks(List<CallbackRef> afterAgentCallbacks) {
155+
this.afterAgentCallbacks = afterAgentCallbacks;
156+
}
114157
}

core/src/main/java/com/google/adk/agents/ConfigAgentUtils.java

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.adk.agents;
1818

19+
import static com.google.common.base.Strings.nullToEmpty;
20+
1921
import com.fasterxml.jackson.databind.DeserializationFeature;
2022
import com.fasterxml.jackson.databind.MapperFeature;
2123
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -31,6 +33,8 @@
3133
import java.nio.file.Paths;
3234
import java.util.ArrayList;
3335
import java.util.List;
36+
import java.util.function.Consumer;
37+
import javax.annotation.Nullable;
3438
import org.slf4j.Logger;
3539
import org.slf4j.LoggerFactory;
3640

@@ -45,6 +49,86 @@ public final class ConfigAgentUtils {
4549

4650
private ConfigAgentUtils() {}
4751

52+
/**
53+
* Configures the common properties of an agent builder from the configuration.
54+
*
55+
* @param builder The agent builder.
56+
* @param config The agent configuration.
57+
* @param configAbsPath The absolute path to the config file (for resolving relative paths).
58+
* @throws ConfigurationException if the configuration is invalid.
59+
*/
60+
public static void resolveAndSetCommonAgentFields(
61+
BaseAgent.Builder<?> builder, BaseAgentConfig config, String configAbsPath)
62+
throws ConfigurationException {
63+
if (config.name() == null || config.name().trim().isEmpty()) {
64+
throw new ConfigurationException("Agent name is required");
65+
}
66+
builder.name(config.name());
67+
builder.description(nullToEmpty(config.description()));
68+
69+
if (config.subAgents() != null && !config.subAgents().isEmpty()) {
70+
builder.subAgents(resolveSubAgents(config.subAgents(), configAbsPath));
71+
}
72+
73+
setBaseAgentCallbacks(config, builder::beforeAgentCallback, builder::afterAgentCallback);
74+
}
75+
76+
/**
77+
* Resolves and sets callbacks from configuration.
78+
*
79+
* @param refs The list of callback references from config.
80+
* @param callbackBaseClass The base class of the callback.
81+
* @param callbackTypeName The name of the callback type for error messages.
82+
* @param builderSetter The setter method on the builder to apply the resolved callbacks.
83+
* @param <T> The type of the callback.
84+
* @throws ConfigurationException if a callback cannot be resolved.
85+
*/
86+
public static <T> void resolveAndSetCallback(
87+
@Nullable List<BaseAgentConfig.CallbackRef> refs,
88+
Class<T> callbackBaseClass,
89+
String callbackTypeName,
90+
Consumer<ImmutableList<T>> builderSetter)
91+
throws ConfigurationException {
92+
if (refs != null) {
93+
ImmutableList.Builder<T> list = ImmutableList.builder();
94+
for (BaseAgentConfig.CallbackRef ref : refs) {
95+
list.add(
96+
ComponentRegistry.getInstance()
97+
.get(ref.name(), callbackBaseClass)
98+
.orElseThrow(
99+
() ->
100+
new ConfigurationException(
101+
"Invalid " + callbackTypeName + ": " + ref.name())));
102+
}
103+
builderSetter.accept(list.build());
104+
}
105+
}
106+
107+
/**
108+
* Sets the common agent callbacks (before/after agent) from the config to the builder setters.
109+
*
110+
* @param config The agent configuration.
111+
* @param beforeSetter The setter for before-agent callbacks.
112+
* @param afterSetter The setter for after-agent callbacks.
113+
* @throws ConfigurationException if a callback cannot be resolved.
114+
*/
115+
public static void setBaseAgentCallbacks(
116+
BaseAgentConfig config,
117+
Consumer<ImmutableList<Callbacks.BeforeAgentCallbackBase>> beforeSetter,
118+
Consumer<ImmutableList<Callbacks.AfterAgentCallbackBase>> afterSetter)
119+
throws ConfigurationException {
120+
resolveAndSetCallback(
121+
config.beforeAgentCallbacks(),
122+
Callbacks.BeforeAgentCallbackBase.class,
123+
"before_agent_callback",
124+
beforeSetter);
125+
resolveAndSetCallback(
126+
config.afterAgentCallbacks(),
127+
Callbacks.AfterAgentCallbackBase.class,
128+
"after_agent_callback",
129+
afterSetter);
130+
}
131+
48132
/**
49133
* Load agent from a YAML config file path.
50134
*
@@ -53,7 +137,6 @@ private ConfigAgentUtils() {}
53137
* @throws ConfigurationException if loading fails
54138
*/
55139
public static BaseAgent fromConfig(String configPath) throws ConfigurationException {
56-
57140
File configFile = new File(configPath);
58141
if (!configFile.exists()) {
59142
logger.error("Config file not found: {}", configPath);
@@ -72,6 +155,7 @@ public static BaseAgent fromConfig(String configPath) throws ConfigurationExcept
72155
Class<? extends BaseAgentConfig> configClass = getConfigClassForAgent(agentClass);
73156
BaseAgentConfig config = loadConfigAsType(absolutePath, configClass);
74157
logger.info("agentClass value = '{}'", config.agentClass());
158+
logger.info("configClass value = '{}'", configClass.getName());
75159

76160
// Use reflection to call the fromConfig method with the correct types
77161
java.lang.reflect.Method fromConfigMethod =
@@ -215,6 +299,14 @@ private static Class<? extends BaseAgentConfig> getConfigClassForAgent(
215299
return LlmAgentConfig.class;
216300
}
217301

302+
if (agentClass == SequentialAgent.class) {
303+
return SequentialAgentConfig.class;
304+
}
305+
306+
if (agentClass == ParallelAgent.class) {
307+
return ParallelAgentConfig.class;
308+
}
309+
218310
// TODO: Add more agent class to config class mappings as needed
219311
// Example:
220312
// if (agentClass == CustomAgent.class) {

0 commit comments

Comments
 (0)