Module: Crimson::Tools::SearchFiles

Defined in:
lib/crimson/tools/search_files.rb

Overview

Search files for regex patterns using ripgrep (preferred) or grep fallback.

Constant Summary collapse

TOOL_NAME =
"search_files"
PARAMS =

Tool parameter definitions.

{
  pattern: { type: "string", description: "The regex pattern to search for" },
  path: { type: "string", description: "The directory to search in. Defaults to current directory." },
  file_pattern: { type: "string", description: "Glob pattern to filter files (e.g. '*.rb'). Defaults to all files." },
  context_lines: { type: "integer", description: "Number of context lines to show around each match (default: 0)" }
}.freeze
RG_AVAILABLE =

Whether ripgrep is available on this system.

system("which rg > /dev/null 2>&1")

Class Method Summary collapse

Class Method Details

.anthropic_definitionHash

Returns Anthropic-compatible tool definition.

Returns:

  • (Hash)

    Anthropic-compatible tool definition



34
35
36
# File 'lib/crimson/tools/search_files.rb', line 34

def self.anthropic_definition
  Schema.build_anthropic(name: TOOL_NAME, description: "Search for a regex pattern in files. Returns matching file paths, line numbers, and context.", parameters: PARAMS, required: ["pattern"])
end

.call(pattern:, path: ".", file_pattern: nil, context_lines: 0) ⇒ String

Execute the tool.

Parameters:

  • pattern (String)

    regex pattern

  • path (String) (defaults to: ".")

    directory to search (default “.”)

  • file_pattern (String, nil) (defaults to: nil)

    glob to filter files

  • context_lines (Integer) (defaults to: 0)

    lines of context around matches

Returns:

  • (String)

    search results or error



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/crimson/tools/search_files.rb', line 44

def self.call(pattern:, path: ".", file_pattern: nil, context_lines: 0)
  return "Error: No pattern provided" if pattern.nil? || pattern.strip.empty?

  expanded = File.expand_path(path)
  context = [context_lines, 5].min

  if RG_AVAILABLE
    search_with_rg(pattern, expanded, file_pattern, context)
  else
    search_with_grep(pattern, expanded, file_pattern, context)
  end
rescue => e
  "Error searching files: #{e.message}"
end

.definitionHash

Returns OpenAI-compatible tool definition.

Returns:

  • (Hash)

    OpenAI-compatible tool definition



29
30
31
# File 'lib/crimson/tools/search_files.rb', line 29

def self.definition
  Schema.build(name: TOOL_NAME, description: "Search for a regex pattern in files. Returns matching file paths, line numbers, and context.", parameters: PARAMS, required: ["pattern"])
end

.prepare_arguments(args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



23
24
25
26
# File 'lib/crimson/tools/search_files.rb', line 23

def self.prepare_arguments(args)
  args["context_lines"] = args["context_lines"].to_i if args["context_lines"]
  args
end