Class: Aiko::Tools::Registry

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

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



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

def initialize
  @tools = {}
end

Instance Method Details

#dispatch(name, arguments, callbacks:) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/aiko/tools/registry.rb', line 29

def dispatch(name, arguments, callbacks:)
  tool = @tools[name]
  return "Error: unknown tool '#{name}'" unless tool

  if tool.requires_approval?(arguments) && !callbacks.confirm?(tool.approval_message(arguments))
    return "User declined to run this tool."
  end

  begin
    tool.call(arguments)
  rescue ToolError, SystemCallError => e
    "Error: #{e.message}"
  end
end

#register(tool) ⇒ Object

Raises:

  • (ArgumentError)


10
11
12
13
14
# File 'lib/aiko/tools/registry.rb', line 10

def register(tool)
  raise ArgumentError, "tool already registered: #{tool.name}" if @tools.key?(tool.name)

  @tools[tool.name] = tool
end

#schemasObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/aiko/tools/registry.rb', line 16

def schemas
  @tools.values.map do |tool|
    {
      "type" => "function",
      "function" => {
        "name" => tool.name,
        "description" => tool.description,
        "parameters" => tool.input_schema
      }
    }
  end
end