Class: SwarmSDK::Tools::Registry
- Inherits:
-
Object
- Object
- SwarmSDK::Tools::Registry
- Defined in:
- lib/swarm_sdk/tools/registry.rb
Overview
Registry for built-in SwarmSDK tools
Maps tool names (symbols) to their RubyLLM::Tool classes. Provides validation, lookup, and factory functionality for tool registration.
Tool Creation Pattern
Tools register themselves with their creation requirements via the tool_factory method.
This eliminates the need for a giant case statement in ToolConfigurator.
Tools fall into three categories:
- No params: Simple tools with no initialization requirements (Think, Clock)
- Directory only: Tools needing working directory (Bash, Grep, Glob)
- Agent context: Tools needing agent tracking (Read, Write, Edit, MultiEdit)
- Scratchpad: Tools needing scratchpad storage instance
Note: Plugin-provided tools (e.g., memory tools) are NOT in this registry. They are registered via SwarmSDK::PluginRegistry instead.
Constant Summary collapse
- BUILTIN_TOOLS =
All available built-in tools
Maps tool names to their classes. The class must respond to
creation_requirementsto specify what parameters are needed for instantiation. { Read: SwarmSDK::Tools::Read, Write: SwarmSDK::Tools::Write, Edit: SwarmSDK::Tools::Edit, MultiEdit: SwarmSDK::Tools::MultiEdit, Bash: SwarmSDK::Tools::Bash, Grep: SwarmSDK::Tools::Grep, Glob: SwarmSDK::Tools::Glob, TodoWrite: SwarmSDK::Tools::TodoWrite, ScratchpadWrite: :scratchpad, # Requires scratchpad storage instance ScratchpadRead: :scratchpad, # Requires scratchpad storage instance ScratchpadList: :scratchpad, # Requires scratchpad storage instance Think: SwarmSDK::Tools::Think, WebFetch: SwarmSDK::Tools::WebFetch, Clock: SwarmSDK::Tools::Clock, }.freeze
Class Method Summary collapse
-
.available_names ⇒ Array<Symbol>
Get all available built-in tool names.
-
.create(name, context = {}) ⇒ RubyLLM::Tool
Create a tool instance using the Factory Pattern.
-
.exists?(name) ⇒ Boolean
Check if a tool exists.
-
.get(name) ⇒ Class, ...
Get tool class by name.
-
.get_many(names) ⇒ Array<Class>
Get multiple tool classes by names.
-
.validate(names) ⇒ Array<Symbol>
Validate tool names.
Class Method Details
.available_names ⇒ Array<Symbol>
Get all available built-in tool names
Note: Does NOT include plugin-provided tools. To get all available tools including plugins, combine with SwarmSDK::PluginRegistry.tools.
143 144 145 |
# File 'lib/swarm_sdk/tools/registry.rb', line 143 def available_names BUILTIN_TOOLS.keys end |
.create(name, context = {}) ⇒ RubyLLM::Tool
Create a tool instance using the Factory Pattern
Uses the tool's creation_requirements class method to determine
what parameters to pass to the constructor.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/swarm_sdk/tools/registry.rb', line 83 def create(name, context = {}) name_sym = name.to_sym tool_entry = BUILTIN_TOOLS[name_sym] raise ConfigurationError, "Unknown tool: #{name}" unless tool_entry # Handle scratchpad tools specially (they use factory methods) if tool_entry == :scratchpad return create_scratchpad_tool(name_sym, context[:scratchpad_storage]) end # Get the tool class and its requirements tool_class = tool_entry # Check if tool defines creation requirements if tool_class.respond_to?(:creation_requirements) requirements = tool_class.creation_requirements params = extract_params(requirements, context, name) tool_class.new(**params) else # No requirements - simple instantiation tool_class.new end end |
.exists?(name) ⇒ Boolean
Check if a tool exists
Note: Only checks built-in tools. Plugin-provided tools are checked via SwarmSDK::PluginRegistry.plugin_tool?() instead.
132 133 134 135 |
# File 'lib/swarm_sdk/tools/registry.rb', line 132 def exists?(name) name_sym = name.to_sym BUILTIN_TOOLS.key?(name_sym) end |
.get(name) ⇒ Class, ...
Get tool class by name
Note: Plugin-provided tools are NOT returned by this method. They are managed by SwarmSDK::PluginRegistry instead.
66 67 68 69 |
# File 'lib/swarm_sdk/tools/registry.rb', line 66 def get(name) name_sym = name.to_sym BUILTIN_TOOLS[name_sym] end |
.get_many(names) ⇒ Array<Class>
Get multiple tool classes by names
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/swarm_sdk/tools/registry.rb', line 113 def get_many(names) names.map do |name| tool_class = get(name) unless tool_class raise ConfigurationError, "Unknown tool: #{name}. Available tools: #{available_names.join(", ")}" end tool_class end end |
.validate(names) ⇒ Array<Symbol>
Validate tool names
151 152 153 |
# File 'lib/swarm_sdk/tools/registry.rb', line 151 def validate(names) names.reject { |name| exists?(name) } end |