Class: Botiasloop::Tools::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/botiasloop/tools/registry.rb

Constant Summary collapse

EMPTY_PARAMETERS_SCHEMA =
{
  "type" => "object",
  "properties" => {},
  "required" => [],
  "additionalProperties" => false,
  "strict" => true
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



16
17
18
19
# File 'lib/botiasloop/tools/registry.rb', line 16

def initialize
  @tools = {}
  @tool_instances = {}
end

Instance Attribute Details

#toolsObject (readonly)

Returns the value of attribute tools.



6
7
8
# File 'lib/botiasloop/tools/registry.rb', line 6

def tools
  @tools
end

Instance Method Details

#deregister(name) ⇒ Object

Deregister a tool by name

Parameters:

  • name (String)

    Tool name to deregister



33
34
35
36
# File 'lib/botiasloop/tools/registry.rb', line 33

def deregister(name)
  @tools.delete(name)
  @tool_instances.delete(name)
end

#execute(name, arguments) ⇒ Hash

Execute a tool by name

Parameters:

  • name (String)

    Tool name

  • arguments (Hash)

    Tool arguments

Returns:

  • (Hash)

    Tool result

Raises:

  • (Error)

    If tool not found



58
59
60
61
62
63
64
65
# File 'lib/botiasloop/tools/registry.rb', line 58

def execute(name, arguments)
  tool_class = @tools[name]
  raise Error, "Unknown tool: #{name}" unless tool_class

  args = @tool_instances[name]
  tool = args ? tool_class.new(**args) : tool_class.new
  tool.execute(**arguments.transform_keys(&:to_sym))
end

#register(tool_class, **args) ⇒ Object

Register a tool class

Parameters:

  • tool_class (Class)

    Tool class to register

  • args (Hash)

    Arguments to pass to tool constructor



25
26
27
28
# File 'lib/botiasloop/tools/registry.rb', line 25

def register(tool_class, **args)
  @tools[tool_class.tool_name] = tool_class
  @tool_instances[tool_class.tool_name] = args
end

#schemasHash

Generate OpenAI-compatible tool schemas

Returns:

  • (Hash)

    Hash of tool instances keyed by tool name symbol



40
41
42
43
44
45
# File 'lib/botiasloop/tools/registry.rb', line 40

def schemas
  @tools.transform_values do |tool_class|
    args = @tool_instances[tool_class.tool_name]
    args ? tool_class.new(**args) : tool_class.new
  end
end

#tool_classesArray<Class>

Returns Array of registered tool classes.

Returns:

  • (Array<Class>)

    Array of registered tool classes



48
49
50
# File 'lib/botiasloop/tools/registry.rb', line 48

def tool_classes
  @tools.values
end