Class: SwarmSDK::V3::Tools::Glob

Inherits:
Base
  • Object
show all
Defined in:
lib/swarm_sdk/v3/tools/glob.rb

Overview

Glob tool for fast file pattern matching

Finds files and directories matching glob patterns, sorted by modification time.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#name

Constructor Details

#initialize(directory:) ⇒ Glob

Returns a new instance of Glob.

Parameters:

  • directory (String)

    Working directory for pattern matching



35
36
37
38
# File 'lib/swarm_sdk/v3/tools/glob.rb', line 35

def initialize(directory:)
  super()
  @directory = File.expand_path(directory)
end

Class Method Details

.creation_requirementsArray<Symbol>

Returns Constructor requirements.

Returns:

  • (Array<Symbol>)

    Constructor requirements



12
13
14
# File 'lib/swarm_sdk/v3/tools/glob.rb', line 12

def creation_requirements
  [:directory]
end

Instance Method Details

#execute(pattern:, path: nil) ⇒ String

Execute glob search

Parameters:

  • pattern (String)

    Glob pattern

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

    Search directory

Returns:

  • (String)

    Matching paths or error



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/swarm_sdk/v3/tools/glob.rb', line 45

def execute(pattern:, path: nil)
  return validation_error("pattern is required") if pattern.nil? || pattern.to_s.strip.empty?

  search_path = resolve_search_path(path)
  return search_path if search_path.start_with?("<tool_use_error>")

  full_pattern = pattern.start_with?("/") ? pattern : File.join(search_path, pattern)
  matches = Dir.glob(full_pattern, File::FNM_DOTMATCH)

  matches.reject! do |f|
    basename = File.basename(f.chomp("/"))
    basename == "." || basename == ".."
  end

  return "No matches found for pattern: #{pattern}" if matches.empty?

  matches.sort_by! { |f| -File.mtime(f).to_i }

  max_results = Configuration.instance.glob_result_limit
  truncated = matches.count > max_results
  matches = matches.take(max_results) if truncated

  output = matches.join("\n")
  output += "\n\n<system-reminder>Results limited to first #{max_results} matches.</system-reminder>" if truncated
  output
rescue Errno::EACCES => e
  error("Permission denied: #{e.message}")
rescue StandardError => e
  error("Failed to execute glob: #{e.class.name} - #{e.message}")
end