Class: Legion::CLI::Chat::Tools::SearchContent
- Inherits:
-
Tools::Base
- Object
- Tools::Base
- Legion::CLI::Chat::Tools::SearchContent
- Defined in:
- lib/legion/cli/chat/tools/search_content.rb
Class Method Summary collapse
-
.call(pattern:, directory: nil, glob: nil) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity.
Methods inherited from Tools::Base
deferred, deferred?, description, error_response, extension, handle_exception, input_schema, log, mcp_category, mcp_tier, runner, sticky, tags, text_response, tool_name, trigger_words
Class Method Details
.call(pattern:, directory: nil, glob: nil) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity
22 23 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 55 56 57 58 |
# File 'lib/legion/cli/chat/tools/search_content.rb', line 22 def self.call(pattern:, directory: nil, glob: nil) # rubocop:disable Metrics/CyclomaticComplexity dir = File.(directory || Dir.pwd) return "Error: directory not found: #{dir}" unless Dir.exist?(dir) file_pattern = File.join(dir, glob || '**/*') files = Dir.glob(file_pattern).select { |f| File.file?(f) } regex = Regexp.new(pattern) results = [] files.each do |file| begin File.readlines(file, encoding: 'utf-8').each_with_index do |line, i| if line.match?(regex) relative = file.sub("#{dir}/", '') results << "#{relative}:#{i + 1}: #{line.rstrip}" end rescue ArgumentError => e Legion::Logging.debug("SearchContent#execute encoding error in #{file}: #{e.}") if defined?(Legion::Logging) next end rescue StandardError => e Legion::Logging.debug("SearchContent#execute skipping #{file}: #{e.}") if defined?(Legion::Logging) next end break if results.length >= 50 end return "No matches for /#{pattern}/ in #{dir}" if results.empty? "#{results.length} matches:\n#{results.join("\n")}" rescue RegexpError => e Legion::Logging.warn("SearchContent#execute invalid regex #{pattern}: #{e.}") if defined?(Legion::Logging) "Error: invalid regex: #{e.}" rescue StandardError => e Legion::Logging.warn("SearchContent#execute failed: #{e.}") if defined?(Legion::Logging) "Error searching: #{e.}" end |