Class: RobotLab::ToolManifest

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/robot_lab/tool_manifest.rb

Overview

Registry of tools with lookup by name

ToolManifest provides a collection interface for managing multiple tools, with methods for lookup, iteration, and conversion to various formats.

Examples:

Creating a manifest

manifest = ToolManifest.new([weather_tool, calculator_tool])
manifest[:get_weather]  # => Tool
manifest.names          # => ["get_weather", "calculate"]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tools = []) ⇒ ToolManifest

Creates a new ToolManifest instance.

Examples:

manifest = ToolManifest.new([weather_tool, calculator_tool])

Parameters:

  • tools (Array<Tool>) (defaults to: [])

    initial tools to add to the manifest



23
24
25
26
# File 'lib/robot_lab/tool_manifest.rb', line 23

def initialize(tools = [])
  @tools = {}
  Array(tools).each { |tool| add(tool) }
end

Class Method Details

.from_hash(hash) ⇒ ToolManifest

Create manifest from hash of tool definitions

Parameters:

  • hash (Hash)

    Map of names to tool configs

Returns:



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/robot_lab/tool_manifest.rb', line 206

def self.from_hash(hash)
  tools = hash.map do |name, config|
    Tool.create(
      name: name,
      description: config[:description],
      parameters: config[:parameters],
      &config[:handler]
    )
  end
  new(tools)
end

Instance Method Details

#[](name) ⇒ Tool?

Get a tool by name

Parameters:

  • name (String, Symbol)

    The tool name

Returns:



58
59
60
# File 'lib/robot_lab/tool_manifest.rb', line 58

def [](name)
  @tools[name.to_s]
end

#add(tool) ⇒ self Also known as: <<

Add a tool to the manifest

Parameters:

  • tool (Tool)

    The tool to add

Returns:

  • (self)


33
34
35
36
# File 'lib/robot_lab/tool_manifest.rb', line 33

def add(tool)
  @tools[tool.name] = tool
  self
end

#clearself

Clear all tools

Returns:

  • (self)


153
154
155
156
# File 'lib/robot_lab/tool_manifest.rb', line 153

def clear
  @tools.clear
  self
end

#each {|Tool| ... } ⇒ Object

Iterate over tools

Yields:

  • (Tool)

    Each tool in the manifest



145
146
147
# File 'lib/robot_lab/tool_manifest.rb', line 145

def each(&block)
  @tools.values.each(&block)
end

#empty?Boolean

Check if manifest is empty

Returns:

  • (Boolean)


137
138
139
# File 'lib/robot_lab/tool_manifest.rb', line 137

def empty?
  @tools.empty?
end

#fetch(name) ⇒ Tool

Get a tool by name, raising if not found

Parameters:

  • name (String, Symbol)

    The tool name

Returns:

Raises:



68
69
70
71
72
# File 'lib/robot_lab/tool_manifest.rb', line 68

def fetch(name)
  @tools.fetch(name.to_s) do
    raise ToolNotFoundError, "Tool not found: #{name}. Available tools: #{names.join(', ')}"
  end
end

#include?(name) ⇒ Boolean Also known as: has?

Check if a tool exists

Parameters:

  • name (String, Symbol)

    The tool name

Returns:

  • (Boolean)


79
80
81
# File 'lib/robot_lab/tool_manifest.rb', line 79

def include?(name)
  @tools.key?(name.to_s)
end

#merge(other) ⇒ self

Merge another manifest or array of tools

Parameters:

Returns:

  • (self)


174
175
176
177
178
179
180
181
182
183
184
# File 'lib/robot_lab/tool_manifest.rb', line 174

def merge(other)
  case other
  when ToolManifest
    other.each { |tool| add(tool) }
  when Array
    other.each { |tool| add(tool) }
  when Tool
    add(other)
  end
  self
end

#namesArray<String>

Get all tool names

Returns:

  • (Array<String>)


93
94
95
# File 'lib/robot_lab/tool_manifest.rb', line 93

def names
  @tools.keys
end

#remove(name) ⇒ Tool?

Remove a tool from the manifest

Parameters:

  • name (String, Symbol)

    The tool name

Returns:

  • (Tool, nil)

    The removed tool



49
50
51
# File 'lib/robot_lab/tool_manifest.rb', line 49

def remove(name)
  @tools.delete(name.to_s)
end

#replace(tools) ⇒ self

Replace all tools

Parameters:

  • tools (Array<Tool>)

    New tools

Returns:

  • (self)


163
164
165
166
167
# File 'lib/robot_lab/tool_manifest.rb', line 163

def replace(tools)
  clear
  Array(tools).each { |tool| add(tool) }
  self
end

#sizeInteger Also known as: count, length

Number of tools

Returns:

  • (Integer)


119
120
121
# File 'lib/robot_lab/tool_manifest.rb', line 119

def size
  @tools.size
end

#to_hHash<String, Hash>

Converts the manifest to a hash representation.

Returns:

  • (Hash<String, Hash>)

    map of tool names to their hash representations



189
190
191
# File 'lib/robot_lab/tool_manifest.rb', line 189

def to_h
  @tools.transform_values(&:to_h)
end

#to_json(*args) ⇒ String

Converts the manifest to JSON.

Parameters:

  • args (Array)

    arguments passed to to_json

Returns:

  • (String)

    JSON representation



197
198
199
# File 'lib/robot_lab/tool_manifest.rb', line 197

def to_json(*args)
  to_h.to_json(*args)
end

#valuesArray<Tool> Also known as: all, to_a

Get all tools

Returns:



101
102
103
# File 'lib/robot_lab/tool_manifest.rb', line 101

def values
  @tools.values
end