Class: Girb::Tools::FindFile

Inherits:
Base
  • Object
show all
Defined in:
lib/girb/tools/find_file.rb

Constant Summary collapse

MAX_RESULTS =
20

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

available?, to_gemini_tool, tool_name

Class Method Details

.descriptionObject



11
12
13
14
15
# File 'lib/girb/tools/find_file.rb', line 11

def description
  "Find files in the application by name pattern. " \
  "Useful for locating models, controllers, views, or any files. " \
  "Supports glob patterns like '*.rb' or '**/*user*.rb'."
end

.parametersObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/girb/tools/find_file.rb', line 17

def parameters
  {
    type: "object",
    properties: {
      pattern: {
        type: "string",
        description: "File name pattern (glob). Examples: 'user.rb', '**/user*.rb', 'app/models/*.rb'"
      },
      directory: {
        type: "string",
        description: "Directory to search in (optional, defaults to app root)"
      }
    },
    required: ["pattern"]
  }
end

Instance Method Details

#execute(binding, pattern:, directory: nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/girb/tools/find_file.rb', line 35

def execute(binding, pattern:, directory: nil)
  base_dir = resolve_base_directory(directory)

  unless Dir.exist?(base_dir)
    return { error: "Directory not found: #{base_dir}" }
  end

  # パターンにディレクトリが含まれていなければ再帰検索
  search_pattern = if pattern.include?("/") || pattern.include?("**/")
                     File.join(base_dir, pattern)
                   else
                     File.join(base_dir, "**", pattern)
                   end

  files = Dir.glob(search_pattern).reject { |f| File.directory?(f) }

  # 結果を制限
  truncated = files.length > MAX_RESULTS
  files = files.first(MAX_RESULTS)

  # 相対パスに変換
  relative_files = files.map { |f| f.sub("#{base_dir}/", "") }

  {
    pattern: pattern,
    base_directory: base_dir,
    files: relative_files,
    count: relative_files.length,
    truncated: truncated
  }
rescue StandardError => e
  { error: "#{e.class}: #{e.message}" }
end