|
17 | 17 | package com.google.adk.agents; |
18 | 18 |
|
19 | 19 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 20 | +import static com.google.common.base.Strings.nullToEmpty; |
20 | 21 |
|
| 22 | +import com.google.adk.agents.ConfigAgentUtils.ConfigurationException; |
21 | 23 | import com.google.adk.events.Event; |
| 24 | +import com.google.adk.utils.ComponentRegistry; |
22 | 25 | import com.google.common.collect.ImmutableList; |
23 | 26 | import com.google.errorprone.annotations.CanIgnoreReturnValue; |
24 | 27 | import io.reactivex.rxjava3.core.Flowable; |
25 | 28 | import java.util.ArrayList; |
26 | 29 | import java.util.List; |
| 30 | +import java.util.function.Consumer; |
| 31 | +import javax.annotation.Nullable; |
| 32 | +import org.slf4j.Logger; |
| 33 | +import org.slf4j.LoggerFactory; |
27 | 34 |
|
28 | 35 | /** |
29 | 36 | * A shell agent that runs its sub-agents in parallel in isolated manner. |
|
34 | 41 | */ |
35 | 42 | public class ParallelAgent extends BaseAgent { |
36 | 43 |
|
| 44 | + private static final Logger logger = LoggerFactory.getLogger(ParallelAgent.class); |
| 45 | + |
37 | 46 | /** |
38 | 47 | * Constructor for ParallelAgent. |
39 | 48 | * |
@@ -120,6 +129,82 @@ public static Builder builder() { |
120 | 129 | return new Builder(); |
121 | 130 | } |
122 | 131 |
|
| 132 | + /** |
| 133 | + * Creates a ParallelAgent from configuration. |
| 134 | + * |
| 135 | + * @param config the agent configuration |
| 136 | + * @param configAbsPath The absolute path to the agent config file. |
| 137 | + * @return the configured ParallelAgent |
| 138 | + * @throws ConfigurationException if the configuration is invalid |
| 139 | + */ |
| 140 | + public static ParallelAgent fromConfig(ParallelAgentConfig config, String configAbsPath) |
| 141 | + throws ConfigurationException { |
| 142 | + logger.debug("Creating ParallelAgent from config: {}", config.name()); |
| 143 | + |
| 144 | + // Validate required fields |
| 145 | + if (config.name() == null || config.name().trim().isEmpty()) { |
| 146 | + throw new ConfigurationException("Agent name is required"); |
| 147 | + } |
| 148 | + |
| 149 | + // Create builder with required fields |
| 150 | + Builder builder = |
| 151 | + ParallelAgent.builder().name(config.name()).description(nullToEmpty(config.description())); |
| 152 | + |
| 153 | + // Resolve and add subagents using the utility class |
| 154 | + if (config.subAgents() != null && !config.subAgents().isEmpty()) { |
| 155 | + ImmutableList<BaseAgent> subAgents = |
| 156 | + ConfigAgentUtils.resolveSubAgents(config.subAgents(), configAbsPath); |
| 157 | + builder.subAgents(subAgents); |
| 158 | + } |
| 159 | + |
| 160 | + // Resolve callbacks if configured |
| 161 | + setCallbacksFromConfig(config, builder); |
| 162 | + |
| 163 | + // Build and return the agent |
| 164 | + ParallelAgent agent = builder.build(); |
| 165 | + logger.info( |
| 166 | + "Successfully created ParallelAgent: {} with {} subagents", |
| 167 | + agent.name(), |
| 168 | + agent.subAgents() != null ? agent.subAgents().size() : 0); |
| 169 | + |
| 170 | + return agent; |
| 171 | + } |
| 172 | + |
| 173 | + private static void setCallbacksFromConfig(ParallelAgentConfig config, Builder builder) |
| 174 | + throws ConfigurationException { |
| 175 | + setCallbackFromConfig( |
| 176 | + config.beforeAgentCallbacks(), |
| 177 | + Callbacks.BeforeAgentCallbackBase.class, |
| 178 | + "before_agent_callback", |
| 179 | + builder::beforeAgentCallback); |
| 180 | + setCallbackFromConfig( |
| 181 | + config.afterAgentCallbacks(), |
| 182 | + Callbacks.AfterAgentCallbackBase.class, |
| 183 | + "after_agent_callback", |
| 184 | + builder::afterAgentCallback); |
| 185 | + } |
| 186 | + |
| 187 | + private static <T> void setCallbackFromConfig( |
| 188 | + @Nullable List<ParallelAgentConfig.CallbackRef> refs, |
| 189 | + Class<T> callbackBaseClass, |
| 190 | + String callbackTypeName, |
| 191 | + Consumer<ImmutableList<T>> builderSetter) |
| 192 | + throws ConfigurationException { |
| 193 | + if (refs != null) { |
| 194 | + ImmutableList.Builder<T> list = ImmutableList.builder(); |
| 195 | + for (ParallelAgentConfig.CallbackRef ref : refs) { |
| 196 | + list.add( |
| 197 | + ComponentRegistry.getInstance() |
| 198 | + .get(ref.name(), callbackBaseClass) |
| 199 | + .orElseThrow( |
| 200 | + () -> |
| 201 | + new ConfigurationException( |
| 202 | + "Invalid " + callbackTypeName + ": " + ref.name()))); |
| 203 | + } |
| 204 | + builderSetter.accept(list.build()); |
| 205 | + } |
| 206 | + } |
| 207 | + |
123 | 208 | /** |
124 | 209 | * Sets the branch for the current agent in the invocation context. |
125 | 210 | * |
|
0 commit comments