Class: OmniAI::Tools::Disk::FileReplaceTool

Inherits:
BaseTool
  • Object
show all
Defined in:
lib/omniai/tools/disk/file_replace_tool.rb

Overview

Examples:

tool = OmniAI::Tools::Disk::FileReadTool.new(root: "./project")
tool.execute(
  old_text: 'puts "ABC"',
  new_text: 'puts "DEF"',
  path: "README.md"
)

Instance Method Summary collapse

Methods inherited from BaseTool

#initialize

Constructor Details

This class inherits a constructor from OmniAI::Tools::Disk::BaseTool

Instance Method Details

#execute(old_text:, new_text:, path:) ⇒ Object

Parameters:

  • path (String)
  • old_text (String)
  • new_text (String)


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/omniai/tools/disk/file_replace_tool.rb', line 25

def execute(old_text:, new_text:, path:)
  @logger.info %(#{self.class.name}#execute old_text="#{old_text}" new_text="#{new_text}" path="#{path}")

  resolved = resolve!(path:)
  contents = File.read(resolved)
  modified = contents.gsub(old_text, new_text)
  File.write(resolved, modified)
rescue StandardError => e
  @logger.error(e.message)
  raise e
end