ToolRegistry API Reference
The ToolRegistry
class provides a mechanism for managing a collection of Tool
instances. It allows for registering, retrieving, and executing tools.
While AgentForge
has its own internal tool registration, ToolRegistry
can be used independently if you need to manage sets of tools programmatically for more advanced scenarios.
ToolRegistry
Class
Constructor
constructor(initialTools: Tool[] = [])
Creates a new ToolRegistry
instance.
initialTools: Tool[]
(optional): An array ofTool
instances to register immediately upon creation. Defaults to an empty array.
Methods
register(tool: Tool): ToolRegistry
Registers a Tool
with the registry. Throws an error if a tool with the same name is already registered.
tool: Tool
: TheTool
instance to register.- Returns: The
ToolRegistry
instance for method chaining.
get(name: string): Tool
Retrieves a registered Tool
by its name. Throws an error if no tool with the given name is found.
name: string
: The name of the tool to retrieve.- Returns: The
Tool
instance.
has(name: string): boolean
Checks if a tool with the given name is registered.
name: string
: The name of the tool to check.- Returns:
true
if the tool is registered,false
otherwise.
unregister(name: string): boolean
Removes a tool from the registry by its name.
name: string
: The name of the tool to unregister.- Returns:
true
if the tool was successfully unregistered,false
if no tool with that name was found.
getAll(): Tool[]
Retrieves all tools currently registered in this registry.
- Returns: An array of
Tool
instances.
getAllConfigs(): ToolConfig[]
Retrieves the configuration (as ToolConfig
objects) for all registered tools. (See Tool.getConfig()
on the Tool
page).
- Returns: An array of
ToolConfig
objects.
getAllConfigChatCompletion(): ChatCompletionTool[]
Retrieves the chat completion-specific configurations (as ChatCompletionTool
objects from token.js
) for all registered tools. (See Tool.getChatCompletionConfig()
on the Tool
page).
- Returns: An array of
ChatCompletionTool
objects.
async execute(name: string, params: Record<string, any>): Promise<any>
Executes a registered Tool
by its name with the provided parameters. This is a convenience method that calls get(name)
and then tool.execute(params)
.
name: string
: The name of the tool to execute.params: Record<string, any>
: The parameters to pass to the tool'sexecute
method.- Returns: A
Promise
that resolves to the result of the tool's execution.
merge(other: ToolRegistry): ToolRegistry
Creates a new ToolRegistry
containing all tools from the current registry and another ToolRegistry
. If a tool name exists in both, the tool from the current registry takes precedence (it is not overwritten from the other
registry if already present).
other: ToolRegistry
: AnotherToolRegistry
instance to merge with.- Returns: A new
ToolRegistry
instance containing the merged set of tools.
// Example Usage (Conceptual)
import { ToolRegistry, Tool } from "agent-forge"; // Assuming Tool is the base class
// Assume MyCustomTool1 and MyCustomTool2 are classes extending Tool
// class MyCustomTool1 extends Tool { /* ... implementation ... */ }
// class MyCustomTool2 extends Tool { /* ... implementation ... */ }
// const tool1 = new MyCustomTool1(/* ...constructor args... */);
// const tool2 = new MyCustomTool2(/* ...constructor args... */);
// const registry = new ToolRegistry();
// registry.register(tool1);
// registry.register(tool2);
// if (registry.has(tool1.name)) {
// const retrievedTool = registry.get(tool1.name);
// // console.log(`Retrieved tool: ${retrievedTool.name}`);
// }
// const allTools = registry.getAll();
// console.log(`Total tools registered: ${allTools.length}`);
// async function runRegisteredTool() {
// try {
// // Assuming tool1 (MyCustomTool1) takes a parameter { input: "some value" }
// const result = await registry.execute(tool1.name, { input: "some value for tool1" });
// console.log(`Result from ${tool1.name}:`, result);
// } catch (error) {
// console.error("Error executing tool from registry:", error);
// }
// }
// runRegisteredTool();
// const registry2 = new ToolRegistry([new MyCustomTool2(/* slightly different config or another instance */)]);
// const mergedRegistry = registry.merge(registry2);
// console.log(`Total tools in merged registry: ${mergedRegistry.getAll().length}`);