Class: RubyLLM::Toolbox::Tools::GemTool

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby_llm/toolbox/tools/gem_tool.rb

Overview

SAFE. Read-only RubyGems.org metadata lookup. The host is fixed and all user input is URL-encoded into the path/query (and gem names are validated), so there is no arbitrary-URL / SSRF surface here.

Tool name: “gem”.

Defined Under Namespace

Classes: HttpError, NotFound

Constant Summary collapse

ACTIONS =
%w[info version dependencies search].freeze
NAME_RE =
/\A[A-Za-z0-9_.-]+\z/
HOST =
"https://rubygems.org"

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#call, exec_tool!, exec_tool?, #initialize, #name

Constructor Details

This class inherits a constructor from RubyLLM::Toolbox::Base

Instance Method Details

#execute(name:, action: "info") ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ruby_llm/toolbox/tools/gem_tool.rb', line 35

def execute(name:, action: "info")
  action = normalize_action(action)
  return error("unknown action: #{action} (use #{ACTIONS.join(', ')})", code: :bad_action) unless ACTIONS.include?(action)
  return search(name) if action == "search"

  gem = name.to_s.strip
  return error("invalid gem name: #{gem.inspect}", code: :bad_name) unless gem.match?(NAME_RE)

  case action
  when "version"      then version(gem)
  when "dependencies" then dependencies(gem)
  else                     info(gem)
  end
rescue NotFound
  error("gem not found: #{name}", code: :not_found)
rescue HttpError => e
  error("rubygems.org request failed: #{e.message}", code: :http_error)
end