Class: RubyLLM::Toolbox::Tools::MultiEdit
- Defined in:
- lib/ruby_llm/toolbox/tools/multi_edit.rb
Overview
EXEC. Applies several string replacements to one file in a single, atomic operation. Edits are applied in order (a later edit sees the result of earlier ones), each following edit_file’s rules: old_string must match exactly once unless replace_all is set. If any edit can’t be applied, nothing is written and the failing edit is reported — so the file is never left half-edited.
Defined Under Namespace
Classes: BadEdit
Constant Summary collapse
- MAX_BYTES =
10 * 1024 * 1024
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
Methods inherited from Base
#call, exec_tool!, exec_tool?, #initialize, #name
Constructor Details
This class inherits a constructor from RubyLLM::Toolbox::Base
Instance Method Details
#execute(path:, edits:) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ruby_llm/toolbox/tools/multi_edit.rb', line 34 def execute(path:, edits:) return error("edits must be a non-empty array", code: :bad_edits) unless edits.is_a?(Array) && !edits.empty? jail = Safety::PathJail.new(config.fs_root) real = jail.resolve(path) return error("not a file: #{path}", code: :not_a_file) unless File.file?(real) return error("file too large (> #{MAX_BYTES} bytes)", code: :too_large) if File.size(real) > MAX_BYTES original = File.read(real).scrub content = original total = 0 edits.each_with_index do |edit, i| content, n = apply_edit(content, edit, i + 1, path) total += n end File.write(real, content) "Applied #{edits.size} edit#{edits.size == 1 ? '' : 's'} to #{path} " \ "(#{total} replacement#{total == 1 ? '' : 's'}, #{original.bytesize} -> #{content.bytesize} bytes)" rescue Safety::PathJail::Jailbreak => e error(e., code: :path_denied) rescue BadEdit => e error(e., code: :edit_failed) end |