Class: ConsoleAgent::Tools::Registry

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

Constant Summary collapse

NO_CACHE =

Tools that should never be cached (side effects or user interaction)

%w[ask_user save_memory delete_memory execute_plan].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(executor: nil) ⇒ Registry

Returns a new instance of Registry.



11
12
13
14
15
16
17
18
# File 'lib/console_agent/tools/registry.rb', line 11

def initialize(executor: nil)
  @executor = executor
  @definitions = []
  @handlers = {}
  @cache = {}
  @last_cached = false
  register_all
end

Instance Attribute Details

#definitionsObject (readonly)

Returns the value of attribute definitions.



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

def definitions
  @definitions
end

Instance Method Details

#execute(tool_name, arguments = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/console_agent/tools/registry.rb', line 24

def execute(tool_name, arguments = {})
  handler = @handlers[tool_name]
  unless handler
    return "Error: unknown tool '#{tool_name}'"
  end

  args = if arguments.is_a?(String)
           begin
             JSON.parse(arguments)
           rescue
             {}
           end
         else
           arguments || {}
         end

  unless NO_CACHE.include?(tool_name)
    cache_key = [tool_name, args].hash
    if @cache.key?(cache_key)
      @last_cached = true
      return @cache[cache_key]
    end
  end

  @last_cached = false
  result = handler.call(args)
  @cache[[tool_name, args].hash] = result unless NO_CACHE.include?(tool_name)
  result
rescue => e
  "Error executing #{tool_name}: #{e.message}"
end

#last_cached?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/console_agent/tools/registry.rb', line 20

def last_cached?
  @last_cached
end

#to_anthropic_formatObject



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

def to_anthropic_format
  definitions.map do |d|
    tool = {
      'name' => d[:name],
      'description' => d[:description],
      'input_schema' => d[:parameters]
    }
    tool
  end
end

#to_openai_formatObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/console_agent/tools/registry.rb', line 67

def to_openai_format
  definitions.map do |d|
    {
      'type' => 'function',
      'function' => {
        'name' => d[:name],
        'description' => d[:description],
        'parameters' => d[:parameters]
      }
    }
  end
end