Class: Tools::Registry
- Inherits:
-
Object
- Object
- Tools::Registry
- Defined in:
- lib/tools/registry.rb
Overview
Instance Attribute Summary collapse
-
#tools ⇒ Hash{String => Class, Object}
readonly
Registered tools keyed by name.
Instance Method Summary collapse
-
#any? ⇒ Boolean
Whether any tools are registered.
-
#execute(name, input) ⇒ String, Hash
Execute a tool by name.
-
#initialize(context: {}) ⇒ Registry
constructor
A new instance of Registry.
-
#register(tool) ⇒ void
Register a tool class or instance.
-
#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.
21 22 23 24 |
# File 'lib/tools/registry.rb', line 21 def initialize(context: {}) @tools = {} @context = context end |
Instance Attribute Details
#tools ⇒ Hash{String => Class, Object} (readonly)
Returns registered tools keyed by name.
18 19 20 |
# File 'lib/tools/registry.rb', line 18 def tools @tools end |
Instance Method Details
#any? ⇒ Boolean
Returns whether any tools are registered.
63 64 65 |
# File 'lib/tools/registry.rb', line 63 def any? @tools.any? end |
#execute(name, input) ⇒ String, Hash
Execute a tool by name. Classes are instantiated with the registry’s context; instances are called directly.
50 51 52 53 54 |
# File 'lib/tools/registry.rb', line 50 def execute(name, input) tool = @tools.fetch(name) { raise UnknownToolError, "Unknown tool: #{name}" } instance = tool.is_a?(Class) ? tool.new(**@context) : tool instance.execute(input) end |
#register(tool) ⇒ void
This method returns an undefined value.
Register a tool class or instance. Must respond to tool_name and schema.
29 30 31 |
# File 'lib/tools/registry.rb', line 29 def register(tool) @tools[tool.tool_name] = tool end |
#registered?(name) ⇒ Boolean
Returns whether a tool with the given name is registered.
58 59 60 |
# File 'lib/tools/registry.rb', line 58 def registered?(name) @tools.key?(name) end |
#schemas ⇒ Array<Hash>
Returns schema array for the Anthropic tools API parameter. Each schema includes an optional ‘timeout` parameter (seconds) injected by the registry. The agent can override the default per call for long-running operations.
37 38 39 40 |
# File 'lib/tools/registry.rb', line 37 def schemas default = Anima::Settings.tool_timeout @tools.values.map { |tool| inject_timeout(tool.schema, default) } end |