Class: RubyLLM::Toolbox::Tools::ParseRuby

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

Overview

SAFE. Produces a structural outline of a Ruby file (classes, modules, methods, constants) with line numbers, or finds where a name is defined. In-process via Ripper — it parses, never executes, the code.

Constant Summary collapse

KINDS =
%w[class module method constant].freeze
MAX_BYTES =
5 * 1024 * 1024

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(path:, query: nil, kind: nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ruby_llm/toolbox/tools/parse_ruby.rb', line 31

def execute(path:, query: nil, kind: nil)
  kind = kind.to_s.strip.downcase
  kind = nil if kind.empty?
  return error("unknown kind: #{kind} (use #{KINDS.join(', ')})", code: :bad_kind) if kind && !KINDS.include?(kind)

  jail = Safety::PathJail.new(config.fs_root)
  real = jail.resolve(path)
  return error("not a file: #{path}", code: :not_a_file) unless File.file?(real)
  return error("file too large (> #{MAX_BYTES} bytes)", code: :too_large) if File.size(real) > MAX_BYTES

  entries = RubyOutline.extract(File.read(real).scrub)
  return "no definitions found in #{path}" if entries.empty?

  truncate(render(entries, path, query, kind))
rescue Safety::PathJail::Jailbreak => e
  error(e.message, code: :path_denied)
rescue RubyOutline::ParseError => e
  error("#{e.message} in #{path}", code: :parse_error)
end