Class: Clacky::ToolRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/clacky/agent/tool_registry.rb

Constant Summary collapse

TOOL_ALIASES =

Common aliases that LLMs frequently use instead of the registered tool names. Keys are downcased aliases; values are the canonical registered names.

{
  # file_reader aliases
  "read" => "file_reader",
  "read_file" => "file_reader",
  "filereader" => "file_reader",
  "file_read" => "file_reader",
  "cat" => "file_reader",
  # write aliases
  "write_file" => "write",
  "create_file" => "write",
  "file_write" => "write",
  # edit aliases
  "file_edit" => "edit",
  "replace" => "edit",
  "replace_in_file" => "edit",
  "str_replace" => "edit",
  # terminal aliases
  "shell" => "terminal",
  "bash" => "terminal",
  "exec" => "terminal",
  "execute" => "terminal",
  "run_command" => "terminal",
  "run" => "terminal",
  "command" => "terminal",
  # web_search aliases
  "search" => "web_search",
  "websearch" => "web_search",
  "internet_search" => "web_search",
  "online_search" => "web_search",
  # web_fetch aliases
  "fetch" => "web_fetch",
  "webfetch" => "web_fetch",
  "browse" => "web_fetch",
  "url_fetch" => "web_fetch",
  "http_get" => "web_fetch",
  # grep aliases
  "search_files" => "grep",
  "search_in_files" => "grep",
  "find_in_files" => "grep",
  "search_code" => "grep",
  # glob aliases
  "find_files" => "glob",
  "list_files" => "glob",
  "file_glob" => "glob",
  "search_filenames" => "glob",
  # invoke_skill aliases
  "skill" => "invoke_skill",
  "run_skill" => "invoke_skill",
  # todo_manager aliases
  "todo" => "todo_manager",
  "task_manager" => "todo_manager",
  # ask_user aliases
  "request_user_feedback" => "ask_user",
  "user_feedback" => "ask_user",
  "ask" => "ask_user",
  "ask_question" => "ask_user",
  "clarify" => "ask_user",
  # trash_manager aliases
  "trash" => "trash_manager",
  "delete" => "trash_manager",
  "rm" => "trash_manager",
  "remove" => "trash_manager",
}.freeze

Instance Method Summary collapse

Constructor Details

#initializeToolRegistry

Returns a new instance of ToolRegistry.



71
72
73
74
75
# File 'lib/clacky/agent/tool_registry.rb', line 71

def initialize
  @tools = {}
  # Downcased index for case-insensitive lookups
  @downcased_index = {}
end

Instance Method Details

#allObject



124
125
126
# File 'lib/clacky/agent/tool_registry.rb', line 124

def all
  @tools.values
end

#all_definitionsObject



128
129
130
# File 'lib/clacky/agent/tool_registry.rb', line 128

def all_definitions
  @tools.values.map(&:to_function_definition)
end

#allowed_definitions(allowed_tools = nil) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/clacky/agent/tool_registry.rb', line 132

def allowed_definitions(allowed_tools = nil)
  return all_definitions if allowed_tools.nil? || allowed_tools.include?("all")

  @tools.select { |name, _| allowed_tools.include?(name) }
         .values
         .map(&:to_function_definition)
end

#by_category(category) ⇒ Object



144
145
146
# File 'lib/clacky/agent/tool_registry.rb', line 144

def by_category(category)
  @tools.values.select { |tool| tool.category == category }
end

#get(name) ⇒ Object



82
83
84
# File 'lib/clacky/agent/tool_registry.rb', line 82

def get(name)
  @tools[name] || raise(Clacky::ToolCallError, "Tool not found: #{name}")
end

#register(tool) ⇒ Object



77
78
79
80
# File 'lib/clacky/agent/tool_registry.rb', line 77

def register(tool)
  @tools[tool.name] = tool
  @downcased_index[tool.name.downcase] = tool.name
end

#resolve(name) ⇒ Object

Resolve a tool name (possibly misspelt or aliased) to the canonical registered name. Resolution order:

1. Exact match in the registry
2. Case-insensitive match (e.g. "Read" → "file_reader")
3. Alias lookup (e.g. "read_file" → "file_reader")

Returns the canonical tool name, or nil if nothing matched.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/clacky/agent/tool_registry.rb', line 92

def resolve(name)
  return nil if name.nil?

  name = sanitize_name(name)
  return name if @tools.key?(name)

  downcased = name.downcase

  # Case-insensitive match
  if @downcased_index.key?(downcased)
    return @downcased_index[downcased]
  end

  # Alias lookup
  if TOOL_ALIASES.key?(downcased)
    return TOOL_ALIASES[downcased]
  end

  # Fuzzy: try underscore / hyphen normalisation (e.g. "file-reader" → "file_reader")
  normalized = downcased.tr("-", "_")
  if normalized != downcased
    if @downcased_index.key?(normalized)
      return @downcased_index[normalized]
    end
    if TOOL_ALIASES.key?(normalized)
      return TOOL_ALIASES[normalized]
    end
  end

  nil
end

#tool_namesObject



140
141
142
# File 'lib/clacky/agent/tool_registry.rb', line 140

def tool_names
  @tools.keys
end