Class: Legate::ToolRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/legate/tool_registry.rb

Overview

Manages a collection of tool definitions for a specific agent instance.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeToolRegistry

Initialize an empty tool registry.



12
13
14
# File 'lib/legate/tool_registry.rb', line 12

def initialize
  @tools = {} # Stores { name_symbol => tool_class } for this instance
end

Instance Attribute Details

#toolsObject (readonly)

Make tools readable



9
10
11
# File 'lib/legate/tool_registry.rb', line 9

def tools
  @tools
end

Instance Method Details

#available_tool_namesArray<Symbol>

Get the set of registered tool name symbols.

Returns:

  • (Array<Symbol>)

    The registered tool names.



60
61
62
# File 'lib/legate/tool_registry.rb', line 60

def available_tool_names
  @tools.keys
end

#create_instance(name_symbol) ⇒ Legate::Tool?

Create an instance of a tool by its name symbol using the class registered here.

Parameters:

  • name_symbol (Symbol)

    The symbolic name of the tool.

Returns:

  • (Legate::Tool, nil)

    An instance of the tool or nil if instantiation fails or class not found.



52
53
54
55
56
# File 'lib/legate/tool_registry.rb', line 52

def create_instance(name_symbol)
  klass = find_class(name_symbol)
  Legate.logger.debug("[ToolRegistry #{@object_id}] create_instance(#{name_symbol.inspect}): Found class: #{klass.inspect}")
  klass&.new
end

#find_class(name_symbol) ⇒ Class?

Find a tool class by its name symbol within this registry.

Parameters:

  • name_symbol (Symbol)

    The symbolic name of the tool.

Returns:

  • (Class, nil)

    The tool class or nil if not found.



43
44
45
46
47
# File 'lib/legate/tool_registry.rb', line 43

def find_class(name_symbol)
  found_class = @tools[name_symbol.to_sym]
  Legate.logger.debug("[ToolRegistry #{@object_id}] find_class(#{name_symbol.inspect}): Found class in @tools: #{found_class.inspect}")
  found_class
end

#list_toolsArray<Hash>

Get a list of available tools registered in this instance with basic info.

Returns:

  • (Array<Hash>)

    An array of hashes, each with :name and :description.



66
67
68
69
70
71
# File 'lib/legate/tool_registry.rb', line 66

def list_tools
  @tools.values.map do |klass|
     = klass. # Get the full { name:, description:, parameters: } hash
     # Return the full hash
  end.sort_by { |t| t[:name].to_s }
end

#register(name, klass) ⇒ Object

Register a tool class with this registry instance.

Parameters:

  • name (Symbol)

    The symbolic name of the tool.

  • klass (Class)

    The tool class (must inherit from Legate::Tool).



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/legate/tool_registry.rb', line 19

def register(name, klass)
  logger = Legate.logger # Get the central logger instance

  unless klass < Legate::Tool
    logger.error("ToolRegistry: Attempted to register non-tool class: #{klass.inspect} for name '#{name}'.")
    return false
  end

  name_symbol = name.to_sym # Ensure it's a symbol

  if @tools.key?(name_symbol)
    # Use the local variable 'logger'
    logger.warn("ToolRegistry: Tool '#{name_symbol}' is already registered in this registry. Overwriting with class #{klass}.")
  else
    # Use the local variable 'logger'
    logger.info("ToolRegistry: Registering tool '#{name_symbol}' with class #{klass} in this registry.")
  end
  @tools[name_symbol] = klass
  true # Indicate success
end