`Class ComponentRegistry`
java.lang.Object
com.google.adk.utils.ComponentRegistry
- Direct Known Subclasses:
CustomDemoRegistry
一个用于按名称存储和检索 ADK 实例的注册表。
这个类提供了一个包含常见 ADK 组件的基础注册表(registry),旨在供希望添加自己自定义内容的用户进行扩展。该注册表完全支持多线程操作,并且可以存储任何类型的对象。
线程安全性(Thread Safety):
- 由于使用了底层的 `ConcurrentHashMap`,所有实例方法都是线程安全的。
- 通过使用 `volatile` 关键字,单例实例的访问是线程安全的(即多个线程可以同时访问该实例而不会导致数据竞争或不一致的问题)。
- `setInstance()` 方法是同步的,以确保单例对象的替换过程是原子性的(即整个替换操作不会被其他线程打断)。
预先设置好的基本条目包括:
- “google_search” – GoogleSearchTool 实例
- “code_execution” – BuiltInCodeExecutionTool 的实例
- “exit_loop” – ExitLoopTool 实例
- "url_context" – UrlContextTool 实例
- “google_maps_grounding” – GoogleMapsTool 实例
示例用法:
// Use the singleton instance
ComponentRegistry registry = ComponentRegistry.getInstance();
Optional<GoogleSearchTool> searchTool = registry.get("google_search", GoogleSearchTool.class);
// Extend ComponentRegistry to add custom pre-wired entries
public class MyComponentRegistry extends ComponentRegistry {
public MyComponentRegistry() {
super(); // Initialize base pre-wired entries
register("my_custom_tool", new MyCustomTool());
register("my_agent", new MyCustomAgent());
}
}
// Replace the singleton with custom registry when server starts
ComponentRegistry.setInstance(new MyComponentRegistry());
-
构造函数概述
Constructors -
方法概述
Modifier and TypeMethodDescription根据名称检索对象,不进行类型检查。<T> Optional<T> 根据名称检索一个对象,并尝试将其转换为指定的类型。static ComponentRegistry返回 `ComponentRegistry` 的全局单例实例。getToolNamesWithPrefix(String prefix) void使用给定的名称注册一个对象。static Optional<Callbacks.AfterAgentCallback> static Optional<Callbacks.AfterModelCallback> static Optional<Callbacks.AfterToolCallback> resolveAgentClass(String agentClassName) 根据配置文件中提供的代理类名称,加载相应的代理类。resolveAgentInstance(String name) 从注册表中检索相应的代理实例。static Optional<Callbacks.BeforeToolCallback> resolveToolClass(String toolClassName) 根据配置文件中提供的工具类名称,确定相应的工具类。resolveToolInstance(String name) 根据配置文件中的工具名称来查找相应的工具实例。static Optional<Class<? extends BaseToolset>> resolveToolsetClass(String toolsetClassName) 根据名称从注册表中查找相应的工具集类,或者尝试加载该类。static Optional<BaseToolset> resolveToolsetInstance(String name) 根据名称从注册表中检索相应的工具集实例。static voidsetInstance(ComponentRegistry newInstance) 使用一个新的 `ComponentRegistry` 更新全局单例实例。
-
构造函数详细信息
-
ComponentRegistry
protected ComponentRegistry()
-
-
方法详细信息(Method Details)
-
register
使用给定的名称注册一个对象。这可以覆盖预先设置好的条目(即系统默认的值)。由于使用了底层的 `ConcurrentHashMap`,因此该方法是线程安全的。
- Parameters:
name- the name to associate with the objectvalue- the object to register (can be an instance, class, function, etc.)- Throws:
IllegalArgumentException- if name is null or empty, or if value is null
-
get
根据名称检索一个对象,并尝试将其转换为指定的类型。- Type Parameters:
T- the type parameter- Parameters:
name- the name of the object to retrievetype- the expected type of the object- Returns:
- an Optional containing the object if found and castable to the specified type, or an empty Optional otherwise
-
get
-
getInstance
返回 `ComponentRegistry` 的全局单例实例。- Returns:
- the singleton ComponentRegistry instance
-
setInstance
使用新的 `ComponentRegistry` 更新全局单例实例。这在服务器启动时非常有用,因为它可以将默认的注册表替换为自定义的注册表。此方法是线程安全的(即多个线程可以同时调用该方法而不会导致数据竞争或不一致),并且能够确保所有线程都能原子地(即同步地)看到已更新的实例状态。
- Parameters:
newInstance- the new ComponentRegistry instance to use as the singleton- Throws:
IllegalArgumentException- if newInstance is null
-
resolveAgentInstance
从注册表中检索相应的代理实例。此方法根据给定的键在 `ComponentRegistry` 中查找相应的代理(agent)。在初始化过程中,该注册表应该已经被预先填充了所有可用的代理信息。
该密钥可以是用于注册代理的任何字符串,例如:
- 类名:`com.example.LifeAgent`
- 一个静态字段引用:`"com.example.LifeAgent.INSTANCE"`
- 一个简单的名称:“life_agent”
- 任何自定义键:`"sub_agents_config.life_agent.agent"`
- Parameters:
name- the registry key to look up- Returns:
- an Optional containing the BaseAgent if found, or empty if not found
-
resolveAgentClass
根据配置文件中提供的代理类名称,加载相应的代理类。- Parameters:
agentClassName- the name of the agent class from the config- Returns:
- the corresponding agent class
- Throws:
IllegalArgumentException- if the agent class is not supported
-
resolveToolsetInstance
根据名称从注册表中检索相应的工具集实例。- Parameters:
name- The name of the toolset instance to resolve.- Returns:
- An Optional containing the toolset instance if found, empty otherwise.
-
resolveToolInstance
-
resolveToolClass
-
resolveToolsetClass
根据名称从注册表中查找相应的工具集类,或者尝试加载该类。此方法遵循与……相同的模式。
resolveToolClass如果不是因为使用了 BaseToolset,那么实现过程会首先检查注册表(registry),如果类名中包含点(表示这是一个完整的类名),则会尝试直接加载该类。- Parameters:
toolsetClassName- the name of the toolset class from the config- Returns:
- an Optional containing the toolset class if found, empty otherwise
-
getToolNamesWithPrefix
-
resolveBeforeAgentCallback
-
resolveAfterAgentCallback
-
resolveBeforeModelCallback
-
resolveAfterModelCallback
-
resolveBeforeToolCallback
-
resolveAfterToolCallback
-