Class: RailsConsoleAi::Tools::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_console_ai/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 recall_memory execute_code execute_plan activate_skill save_skill delete_skill delegate_task explore_output].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(executor: nil, mode: :default, channel: nil, allowed_tools: nil) ⇒ Registry

Returns a new instance of Registry.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rails_console_ai/tools/registry.rb', line 11

def initialize(executor: nil, mode: :default, channel: nil, allowed_tools: nil)
  @executor = executor
  @mode = mode
  @channel = channel
  @allowed_tools = allowed_tools
  @definitions = []
  @handlers = {}
  @cache = {}
  @last_cached = false
  @last_sub_agent_usage = nil
  register_all
end

Instance Attribute Details

#definitionsObject (readonly)

Returns the value of attribute definitions.



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

def definitions
  @definitions
end

#last_sub_agent_usageObject (readonly)

Returns the value of attribute last_sub_agent_usage.



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

def last_sub_agent_usage
  @last_sub_agent_usage
end

Instance Method Details

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



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
55
56
57
58
# File 'lib/rails_console_ai/tools/registry.rb', line 28

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)


24
25
26
# File 'lib/rails_console_ai/tools/registry.rb', line 24

def last_cached?
  @last_cached
end

#to_anthropic_formatObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/rails_console_ai/tools/registry.rb', line 60

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

#to_bedrock_formatObject



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rails_console_ai/tools/registry.rb', line 71

def to_bedrock_format
  definitions.map do |d|
    {
      tool_spec: {
        name: d[:name],
        description: d[:description],
        input_schema: { json: d[:parameters] }
      }
    }
  end
end

#to_openai_formatObject



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rails_console_ai/tools/registry.rb', line 83

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