Class: LLM::Tool::EditFile
- Defined in:
- lib/llm/tools/edit-file.rb
Overview
The LLM::Tool::EditFile class implements a tool that can substitute one piece of text for another piece of text in a given file.
Instance Method Summary collapse
Methods inherited from LLM::Tool
NotFound, a2a, a2a?, #a2a?, abstract!, abstract?, clear_registry!, description, find_by_name!, #function, function, inherited, mcp, #mcp?, mcp?, name, #on_cancel, #on_interrupt, params, register, registry, set, subclasses, unregister
Methods included from Param
Methods included from Function::Registry
#clear_registry!, #find_by_name, #lock, #register, #registry, #registry_key, #tool_name, #unregister
Instance Method Details
#call(path:, before:, after:, expected_count: 1) ⇒ Hash
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/llm/tools/edit-file.rb', line 23 def call(path:, before:, after:, expected_count: 1) content = File.read(path) re = Regexp.new(Regexp.escape(before)) count = content.scan(re).length raise "expected #{expected_count} match(es), found #{count}" unless count == expected_count.to_i ## # Block form keeps the replacement literal: # `sub(re, str)` would expand backreferences # like \1 and \& in the replacement text. File.write(path, content.sub(re) { after }) {ok: true, replaced: count} end |