Class: Brute::Tools::FSPatch

Inherits:
LLM::Tool
  • Object
show all
Defined in:
lib/brute/tools/fs_patch.rb

Instance Method Summary collapse

Instance Method Details

#call(file_path:, old_string:, new_string:, replace_all: false) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/brute/tools/fs_patch.rb', line 20

def call(file_path:, old_string:, new_string:, replace_all: false)
  path = File.expand_path(file_path)
  Brute::FileMutationQueue.serialize(path) do
    raise "File not found: #{path}" unless File.exist?(path)

    original = File.read(path)
    raise "old_string not found in #{path}" unless original.include?(old_string)

    Brute::SnapshotStore.save(path)

    updated = if replace_all
                original.gsub(old_string, new_string)
              else
                original.sub(old_string, new_string)
              end

    File.write(path, updated)
    diff = Brute::Diff.unified(original, updated)
    count = replace_all ? original.scan(old_string).size : 1
    { success: true, file_path: path, replacements: count, diff: diff }
  end
end