1616
1717package com .google .adk .agents ;
1818
19+ import static com .google .common .base .Strings .nullToEmpty ;
20+
1921import com .fasterxml .jackson .databind .DeserializationFeature ;
2022import com .fasterxml .jackson .databind .MapperFeature ;
2123import com .fasterxml .jackson .databind .ObjectMapper ;
3133import java .nio .file .Paths ;
3234import java .util .ArrayList ;
3335import java .util .List ;
36+ import java .util .function .Consumer ;
37+ import javax .annotation .Nullable ;
3438import org .slf4j .Logger ;
3539import 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