Class: Chocomint::Tools::Grep

Inherits:
Base
  • Object
show all
Defined in:
lib/chocomint/tools/grep.rb

Overview

許可ディレクトリ内のファイル内容を正規表現で検索する (Claude Code の Grep 相当)。

Instance Method Summary collapse

Methods inherited from Base

#to_tool_definition

Constructor Details

#initialize(path_guard:, max_output_bytes:, base_dir: Dir.pwd) ⇒ Grep

Returns a new instance of Grep.



9
10
11
12
13
# File 'lib/chocomint/tools/grep.rb', line 9

def initialize(path_guard:, max_output_bytes:, base_dir: Dir.pwd)
  @path_guard = path_guard
  @max_output_bytes = max_output_bytes
  @base_dir = base_dir
end

Instance Method Details

#call(args) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/chocomint/tools/grep.rb', line 36

def call(args)
  measure do
    base_rel = args["base"] || @path_guard.allowed_roots.first
    base_abs = @path_guard.resolve(base_rel)
    next fail(stderr: "no such directory: #{base_rel}") unless File.directory?(base_abs)

    flags = args["ignore_case"] ? Regexp::IGNORECASE : 0
    re = Regexp.new(args["pattern"].to_s, flags)
    glob = args["glob"] || "**/*"

    lines = search(base_abs, glob, re)
    ok(stdout: truncate(lines.join("\n")))
  end
end

#descriptionObject



17
18
19
20
# File 'lib/chocomint/tools/grep.rb', line 17

def description
  "許可ディレクトリ内のファイルを正規表現で検索し、一致行を file:line:text 形式で返す。" \
    "glob (例: **/*.rb) で対象を絞れる。ignore_case で大文字小文字を無視。"
end

#nameObject



15
# File 'lib/chocomint/tools/grep.rb', line 15

def name = "grep"

#schemaObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/chocomint/tools/grep.rb', line 22

def schema
  {
    "type" => "object",
    "properties" => {
      "pattern" => { "type" => "string" },
      "base" => { "type" => "string" },
      "glob" => { "type" => "string" },
      "ignore_case" => { "type" => "boolean" }
    },
    "required" => %w[pattern],
    "additionalProperties" => false
  }
end