|
16 | 16 |
|
17 | 17 | package com.google.adk.utils;
|
18 | 18 |
|
19 |
| -import com.google.adk.tools.BuiltInCodeExecutionTool; |
| 19 | +import static com.google.common.base.Strings.isNullOrEmpty; |
| 20 | + |
| 21 | +import com.google.adk.agents.BaseAgent; |
| 22 | +import com.google.adk.agents.LlmAgent; |
| 23 | +import com.google.adk.agents.LoopAgent; |
| 24 | +import com.google.adk.agents.ParallelAgent; |
| 25 | +import com.google.adk.agents.SequentialAgent; |
| 26 | +import com.google.adk.tools.AgentTool; |
| 27 | +import com.google.adk.tools.BaseTool; |
20 | 28 | import com.google.adk.tools.GoogleSearchTool;
|
| 29 | +import com.google.adk.tools.LoadArtifactsTool; |
21 | 30 | import java.util.Map;
|
22 | 31 | import java.util.Optional;
|
23 | 32 | import java.util.concurrent.ConcurrentHashMap;
|
| 33 | +import javax.annotation.Nonnull; |
24 | 34 | import org.slf4j.Logger;
|
25 | 35 | import org.slf4j.LoggerFactory;
|
26 | 36 |
|
@@ -74,18 +84,44 @@ public class ComponentRegistry {
|
74 | 84 |
|
75 | 85 | private final Map<String, Object> registry = new ConcurrentHashMap<>();
|
76 | 86 |
|
77 |
| - public ComponentRegistry() { |
| 87 | + protected ComponentRegistry() { |
78 | 88 | initializePreWiredEntries();
|
79 | 89 | }
|
80 | 90 |
|
81 | 91 | /** Initializes the registry with base pre-wired ADK instances. */
|
82 | 92 | private void initializePreWiredEntries() {
|
83 |
| - registry.put("google_search", new GoogleSearchTool()); |
84 |
| - registry.put("code_execution", new BuiltInCodeExecutionTool()); |
| 93 | + registerAdkAgentClass(LlmAgent.class); |
| 94 | + registerAdkAgentClass(LoopAgent.class); |
| 95 | + registerAdkAgentClass(ParallelAgent.class); |
| 96 | + registerAdkAgentClass(SequentialAgent.class); |
| 97 | + |
| 98 | + registerAdkToolInstance("google_search", new GoogleSearchTool()); |
| 99 | + registerAdkToolInstance("load_artifacts", new LoadArtifactsTool()); |
| 100 | + |
| 101 | + registerAdkToolClass(AgentTool.class); |
| 102 | + // TODO: add all python tools that also exist in Java. |
85 | 103 |
|
86 | 104 | logger.debug("Initialized base pre-wired entries in ComponentRegistry");
|
87 | 105 | }
|
88 | 106 |
|
| 107 | + private void registerAdkAgentClass(Class<? extends BaseAgent> agentClass) { |
| 108 | + registry.put(agentClass.getName(), agentClass); |
| 109 | + // For python compatibility, also register the name used in ADK Python. |
| 110 | + registry.put("google.adk.agents." + agentClass.getSimpleName(), agentClass); |
| 111 | + } |
| 112 | + |
| 113 | + private void registerAdkToolInstance(String name, @Nonnull Object toolInstance) { |
| 114 | + registry.put(name, toolInstance); |
| 115 | + // For python compatibility, also register the name used in ADK Python. |
| 116 | + registry.put("google.adk.tools." + name, toolInstance); |
| 117 | + } |
| 118 | + |
| 119 | + private void registerAdkToolClass(@Nonnull Class<?> toolClass) { |
| 120 | + registry.put(toolClass.getName(), toolClass); |
| 121 | + // For python compatibility, also register the name used in ADK Python. |
| 122 | + registry.put("google.adk.tools." + toolClass.getSimpleName(), toolClass); |
| 123 | + } |
| 124 | + |
89 | 125 | /**
|
90 | 126 | * Registers an object with the given name. This can override pre-wired entries.
|
91 | 127 | *
|
@@ -182,4 +218,109 @@ public static synchronized void setInstance(ComponentRegistry newInstance) {
|
182 | 218 | instance = newInstance;
|
183 | 219 | logger.info("ComponentRegistry singleton instance updated");
|
184 | 220 | }
|
| 221 | + |
| 222 | + /** |
| 223 | + * Resolves the agent class based on the agent class name from the configuration. |
| 224 | + * |
| 225 | + * @param agentClassName the name of the agent class from the config |
| 226 | + * @return the corresponding agent class |
| 227 | + * @throws IllegalArgumentException if the agent class is not supported |
| 228 | + */ |
| 229 | + @SuppressWarnings({"unchecked", "rawtypes"}) // For type casting. |
| 230 | + public static Class<? extends BaseAgent> resolveAgentClass(String agentClassName) { |
| 231 | + // If no agent_class is specified, it will default to LlmAgent. |
| 232 | + if (isNullOrEmpty(agentClassName)) { |
| 233 | + return LlmAgent.class; |
| 234 | + } |
| 235 | + |
| 236 | + ComponentRegistry registry = getInstance(); |
| 237 | + |
| 238 | + if (agentClassName.contains(".")) { |
| 239 | + // If agentClassName contains '.', use it directly |
| 240 | + Optional<Class> agentClass = registry.get(agentClassName, Class.class); |
| 241 | + if (agentClass.isPresent() && BaseAgent.class.isAssignableFrom(agentClass.get())) { |
| 242 | + return (Class<? extends BaseAgent>) agentClass.get(); |
| 243 | + } |
| 244 | + } else { |
| 245 | + // First try the simple name |
| 246 | + Optional<Class> agentClass = registry.get(agentClassName, Class.class); |
| 247 | + if (agentClass.isPresent() && BaseAgent.class.isAssignableFrom(agentClass.get())) { |
| 248 | + return (Class<? extends BaseAgent>) agentClass.get(); |
| 249 | + } |
| 250 | + |
| 251 | + // If not found, try with com.google.adk.agents prefix |
| 252 | + agentClass = registry.get("com.google.adk.agents." + agentClassName, Class.class); |
| 253 | + if (agentClass.isPresent() && BaseAgent.class.isAssignableFrom(agentClass.get())) { |
| 254 | + return (Class<? extends BaseAgent>) agentClass.get(); |
| 255 | + } |
| 256 | + } |
| 257 | + |
| 258 | + throw new IllegalArgumentException( |
| 259 | + "agentClass '" + agentClassName + "' is not in registry or not a subclass of BaseAgent."); |
| 260 | + } |
| 261 | + |
| 262 | + /** |
| 263 | + * Resolves the tool instance based on the tool name from the configuration. |
| 264 | + * |
| 265 | + * @param name the name of the tool from the config |
| 266 | + * @return an Optional containing the tool instance if found, empty otherwise |
| 267 | + */ |
| 268 | + public static Optional<BaseTool> resolveToolInstance(String name) { |
| 269 | + if (isNullOrEmpty(name)) { |
| 270 | + return Optional.empty(); |
| 271 | + } |
| 272 | + |
| 273 | + ComponentRegistry registry = getInstance(); |
| 274 | + |
| 275 | + if (name.contains(".")) { |
| 276 | + // If name contains '.', use it directly |
| 277 | + return registry.get(name, BaseTool.class); |
| 278 | + } else { |
| 279 | + // First try the simple name |
| 280 | + Optional<BaseTool> toolInstance = registry.get(name, BaseTool.class); |
| 281 | + if (toolInstance.isPresent()) { |
| 282 | + return toolInstance; |
| 283 | + } |
| 284 | + |
| 285 | + // If not found, try with google.adk.tools prefix |
| 286 | + return registry.get("google.adk.tools." + name, BaseTool.class); |
| 287 | + } |
| 288 | + } |
| 289 | + |
| 290 | + /** |
| 291 | + * Resolves the tool class based on the tool class name from the configuration. |
| 292 | + * |
| 293 | + * @param toolClassName the name of the tool class from the config |
| 294 | + * @return an Optional containing the tool class if found, empty otherwise |
| 295 | + */ |
| 296 | + @SuppressWarnings({"unchecked", "rawtypes"}) // For type casting. |
| 297 | + public static Optional<Class<? extends BaseTool>> resolveToolClass(String toolClassName) { |
| 298 | + if (isNullOrEmpty(toolClassName)) { |
| 299 | + return Optional.empty(); |
| 300 | + } |
| 301 | + |
| 302 | + ComponentRegistry registry = getInstance(); |
| 303 | + |
| 304 | + if (toolClassName.contains(".")) { |
| 305 | + // If toolClassName contains '.', use it directly |
| 306 | + Optional<Class> toolClass = registry.get(toolClassName, Class.class); |
| 307 | + if (toolClass.isPresent() && BaseTool.class.isAssignableFrom(toolClass.get())) { |
| 308 | + return Optional.of((Class<? extends BaseTool>) toolClass.get()); |
| 309 | + } |
| 310 | + } else { |
| 311 | + // First try the simple name |
| 312 | + Optional<Class> toolClass = registry.get(toolClassName, Class.class); |
| 313 | + if (toolClass.isPresent() && BaseTool.class.isAssignableFrom(toolClass.get())) { |
| 314 | + return Optional.of((Class<? extends BaseTool>) toolClass.get()); |
| 315 | + } |
| 316 | + |
| 317 | + // If not found, try with google.adk.tools prefix |
| 318 | + toolClass = registry.get("google.adk.tools." + toolClassName, Class.class); |
| 319 | + if (toolClass.isPresent() && BaseTool.class.isAssignableFrom(toolClass.get())) { |
| 320 | + return Optional.of((Class<? extends BaseTool>) toolClass.get()); |
| 321 | + } |
| 322 | + } |
| 323 | + |
| 324 | + return Optional.empty(); |
| 325 | + } |
185 | 326 | }
|
0 commit comments