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.
-
#truncation_threshold(name) ⇒ Integer?
Returns the truncation threshold for a tool, or
nilif the tool opts out of truncation (e.g. read_file tool has its own pagination).
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.
80 81 82 |
# File 'lib/tools/registry.rb', line 80 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.
54 55 56 57 58 |
# File 'lib/tools/registry.rb', line 54 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.
75 76 77 |
# File 'lib/tools/registry.rb', line 75 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. Tools with session-dependent schemas (e.g. Think with budget-based maxLength, Bash with CWD in description) are instantiated with context to generate their schema:
41 42 43 44 |
# File 'lib/tools/registry.rb', line 41 def schemas default = Anima::Settings.tool_timeout @tools.values.map { |tool| inject_timeout(resolve_schema(tool), default) } end |
#truncation_threshold(name) ⇒ Integer?
Returns the truncation threshold for a tool, or nil if the tool opts out of truncation (e.g. read_file tool has its own pagination). MCP tools and other duck-typed instances use the default threshold.
66 67 68 69 70 71 |
# File 'lib/tools/registry.rb', line 66 def truncation_threshold(name) tool = @tools[name] return tool.truncation_threshold if tool&.respond_to?(:truncation_threshold) Anima::Settings.max_tool_response_chars end |