Class: RailsAiContext::Tools::SearchCode

Inherits:
BaseTool
  • Object
show all
Defined in:
lib/rails_ai_context/tools/search_code.rb

Constant Summary

Constants inherited from BaseTool

BaseTool::SESSION_CONTEXT, BaseTool::SHARED_CACHE

Class Method Summary collapse

Methods inherited from BaseTool

abstract!, abstract?, cache_key, cached_context, config, extract_method_source_from_file, extract_method_source_from_string, find_closest_match, fuzzy_find_key, inherited, not_found_response, paginate, rails_app, registered_tools, reset_all_caches!, reset_cache!, session_queries, session_record, session_reset!, set_call_params, text_response

Class Method Details

.call(pattern:, path: nil, file_type: nil, match_type: "any", exact_match: false, exclude_tests: false, group_by_file: false, offset: 0, limit: nil, context_lines: 2, server_context: nil) ⇒ Object

rubocop:disable Metrics



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/rails_ai_context/tools/search_code.rb', line 67

def self.call(pattern:, path: nil, file_type: nil, match_type: "any", exact_match: false, exclude_tests: false, group_by_file: false, offset: 0, limit: nil, context_lines: 2, server_context: nil) # rubocop:disable Metrics
  root = Rails.root.to_s
  original_pattern = pattern

  # Reject empty or whitespace-only patterns
  if pattern.nil? || pattern.strip.empty?
    return text_response("Pattern is required. Provide a search term or regex.")
  end

  # Trace mode — the game changer: full method picture in one call
  if match_type == "trace"
    return trace_method(pattern.strip, root, path, exclude_tests)
  end

  # Validate match_type
  valid_match_types = %w[any definition class call trace]
  unless valid_match_types.include?(match_type)
    return text_response("Unknown match_type: '#{match_type}'. Valid values: #{valid_match_types.join(', ')}")
  end

  # Apply match_type filter to pattern (exact_match word boundaries applied per-type)
  search_pattern = case match_type
  when "definition"
    cleaned = pattern.sub(/\A\s*def\s+/, "")
    escaped = Regexp.escape(cleaned)
    exact_match ? "^\\s*def\\s+(self\\.)?#{escaped}\\b" : "^\\s*def\\s+(self\\.)?#{escaped}"
  when "class"
    cleaned = pattern.sub(/\A\s*(class|module)\s+/, "")
    escaped = Regexp.escape(cleaned)
    exact_match ? "^\\s*(class|module)\\s+\\w*#{escaped}\\b" : "^\\s*(class|module)\\s+\\w*#{escaped}"
  when "call"
    exact_match ? "\\b#{pattern}\\b" : pattern
  else
    exact_match ? "\\b#{pattern}\\b" : pattern
  end

  # Validate regex syntax early
  begin
    Regexp.new(search_pattern, timeout: 1)
  rescue RegexpError => e
    return text_response("Invalid regex pattern: #{e.message}")
  end

  # Validate file_type to prevent injection
  if file_type && !file_type.match?(/\A[a-zA-Z0-9]+\z/)
    return text_response("Invalid file_type: must contain only alphanumeric characters.")
  end

  context_lines = [ [ context_lines.to_i, 0 ].max, 5 ].min
  offset = [ offset.to_i, 0 ].max

  search_path = path ? File.join(root, path) : root

  # Path traversal protection
  unless Dir.exist?(search_path)
    top_dirs = Dir.glob(File.join(root, "*")).select { |f| File.directory?(f) }.map { |f| File.basename(f) }.sort
    return text_response("Path not found: #{path}. Top-level directories: #{top_dirs.first(15).join(', ')}")
  end

  begin
    real_search = File.realpath(search_path)
    real_root = File.realpath(root)
    unless real_search.start_with?(real_root)
      return text_response("Path not allowed: #{path}")
    end
  rescue Errno::ENOENT
    return text_response("Path not found: #{path}")
  end

  # Fetch all results (capped at 200 for safety)
  all_results = if ripgrep_available?
    search_with_ripgrep(search_pattern, search_path, file_type, max_results_cap, root, context_lines, exclude_tests: exclude_tests)
  else
    search_with_ruby(search_pattern, search_path, file_type, max_results_cap, root, exclude_tests: exclude_tests)
  end

  # Filter out definitions for match_type:"call"
  all_results.reject! { |r| r[:content].match?(/\A\s*def\s/) } if match_type == "call"

  if all_results.empty?
    return text_response("No results found for '#{original_pattern}' in #{path || 'app'}.")
  end

  # Smart default limit: <10 → all, 10-100 → half, >100 → 100
  total = all_results.size
  default_limit = if total <= 10 then total
  elsif total <= 100 then (total / 2.0).ceil
  else 100
  end

  page = paginate(all_results, offset: offset, limit: limit, default_limit: [ default_limit, 1 ].max)
  paginated = page[:items]

  if paginated.empty? && total > 0
    return text_response(page[:hint])
  end

  pagination = page[:hint].empty? ? "" : "\n#{page[:hint]}"

  showing = paginated.size.to_s
  header = "# Search: `#{original_pattern}`\n**#{total} total results**#{" in #{path}" if path}, showing #{showing}\n"

  if group_by_file
    text_response(header + "\n" + format_grouped(paginated) + pagination)
  else
    output = paginated.map { |r| "#{r[:file]}:#{r[:line_number]}: #{r[:content].strip}" }.join("\n")
    text_response("#{header}\n```\n#{output}\n```#{pagination}")
  end
end

.max_results_capObject



14
15
16
# File 'lib/rails_ai_context/tools/search_code.rb', line 14

def self.max_results_cap
  RailsAiContext.configuration.max_search_results
end