Class: Tools::Registry
- Inherits:
-
Object
- Object
- Tools::Registry
- Defined in:
- lib/tools/registry.rb
Overview
Manages tool registration, schema export, and dispatch. Tools are registered by class and looked up by name at execution time. An optional context hash is passed to each tool’s constructor, allowing shared dependencies (e.g. a ShellSession) to reach tools that need them.
Instance Attribute Summary collapse
-
#tools ⇒ Hash{String => Class}
readonly
Registered tool classes keyed by name.
Instance Method Summary collapse
-
#any? ⇒ Boolean
Whether any tools are registered.
-
#execute(name, input) ⇒ String, Hash
Instantiate and execute a tool by name.
-
#initialize(context: {}) ⇒ Registry
constructor
A new instance of Registry.
-
#register(tool_class) ⇒ void
Register a tool class.
-
#registered?(name) ⇒ Boolean
Whether a tool with the given name is registered.
-
#schemas ⇒ Array<Hash>
Schema array for the Anthropic tools API parameter.
Constructor Details
#initialize(context: {}) ⇒ Registry
Returns a new instance of Registry.
20 21 22 23 |
# File 'lib/tools/registry.rb', line 20 def initialize(context: {}) @tools = {} @context = context end |
Instance Attribute Details
#tools ⇒ Hash{String => Class} (readonly)
Returns registered tool classes keyed by name.
17 18 19 |
# File 'lib/tools/registry.rb', line 17 def tools @tools end |
Instance Method Details
#any? ⇒ Boolean
Returns whether any tools are registered.
56 57 58 |
# File 'lib/tools/registry.rb', line 56 def any? @tools.any? end |
#execute(name, input) ⇒ String, Hash
Instantiate and execute a tool by name. The registry’s context is forwarded to the tool constructor as keyword arguments.
44 45 46 47 |
# File 'lib/tools/registry.rb', line 44 def execute(name, input) tool_class = @tools.fetch(name) { raise UnknownToolError, "Unknown tool: #{name}" } tool_class.new(**@context).execute(input) end |
#register(tool_class) ⇒ void
This method returns an undefined value.
Register a tool class. The class must respond to .tool_name.
28 29 30 |
# File 'lib/tools/registry.rb', line 28 def register(tool_class) @tools[tool_class.tool_name] = tool_class end |
#registered?(name) ⇒ Boolean
Returns whether a tool with the given name is registered.
51 52 53 |
# File 'lib/tools/registry.rb', line 51 def registered?(name) @tools.key?(name) end |
#schemas ⇒ Array<Hash>
Returns schema array for the Anthropic tools API parameter.
33 34 35 |
# File 'lib/tools/registry.rb', line 33 def schemas @tools.values.map(&:schema) end |