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>
Returns tool schemas for the Anthropic API.
-
#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.
85 86 87 |
# File 'lib/tools/registry.rb', line 85 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.
59 60 61 62 63 |
# File 'lib/tools/registry.rb', line 59 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.
80 81 82 |
# File 'lib/tools/registry.rb', line 80 def registered?(name) @tools.key?(name) end |
#schemas ⇒ Array<Hash>
Returns tool schemas for the Anthropic API. The last schema is annotated with cache_control so the API caches the entire tools prefix (tools are evaluated first in cache prefix order).
44 45 46 47 48 49 |
# File 'lib/tools/registry.rb', line 44 def schemas default = Anima::Settings.tool_timeout result = @tools.values.map { |tool| inject_timeout(resolve_schema(tool), default) } result.last[:cache_control] = {type: "ephemeral"} result 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.
71 72 73 74 75 76 |
# File 'lib/tools/registry.rb', line 71 def truncation_threshold(name) tool = @tools[name] return tool.truncation_threshold if tool&.respond_to?(:truncation_threshold) Anima::Settings.max_tool_response_chars end |