Class: SwarmSDK::Tools::Registry

Inherits:
Object
  • Object
show all
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:

  1. No params: Simple tools with no initialization requirements (Think, Clock)
  2. Directory only: Tools needing working directory (Bash, Grep, Glob)
  3. Agent context: Tools needing agent tracking (Read, Write, Edit, MultiEdit)
  4. 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.

Examples:

Adding a new tool with creation requirements

# In the tool class:
class MyTool < RubyLLM::Tool
  def self.creation_requirements
    [:agent_name, :directory]
  end
end

# In registry:
BUILTIN_TOOLS = {
  MyTool: SwarmSDK::Tools::MyTool,
}

Constant Summary collapse

BUILTIN_TOOLS =

All available built-in tools

Maps tool names to their classes. The class must respond to creation_requirements to 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

Class Method Details

.available_namesArray<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.

Returns:

  • (Array<Symbol>)


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.

Parameters:

  • name (Symbol, String)

    Tool name

  • context (Hash) (defaults to: {})

    Available context for tool creation

Options Hash (context):

  • :agent_name (Symbol)

    Agent identifier

  • :directory (String)

    Agent's working directory

  • :scratchpad_storage (Object)

    Scratchpad storage instance

Returns:

  • (RubyLLM::Tool)

    Instantiated tool

Raises:



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.

Parameters:

  • name (Symbol, String)

    Tool name

Returns:

  • (Boolean)


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.

Parameters:

  • name (Symbol, String)

    Tool name

Returns:

  • (Class, Symbol, nil)

    Tool class, :scratchpad marker, or nil if not found



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

Parameters:

  • names (Array<Symbol, String>)

    Tool names

Returns:

  • (Array<Class>)

    Array of tool classes

Raises:



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

Parameters:

  • names (Array<Symbol, String>)

    Tool names to validate

Returns:

  • (Array<Symbol>)

    Invalid tool names



151
152
153
# File 'lib/swarm_sdk/tools/registry.rb', line 151

def validate(names)
  names.reject { |name| exists?(name) }
end