Class: Genie::AppendToFile

Inherits:
RubyLLM::Tool
  • Object
show all
Includes:
SandboxedFileTool
Defined in:
lib/tools/append_to_file.rb

Instance Method Summary collapse

Methods included from SandboxedFileTool

#enforce_sandbox!, #initialize, #within_sandbox?

Instance Method Details

#execute(filepath:, content:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tools/append_to_file.rb', line 11

def execute(filepath:, content:)
  filepath = File.expand_path(filepath)

  Genie.output "Appending to file: #{filepath}", color: :blue

  enforce_sandbox!(filepath)

  indented = content.each_line.map { |line| "  #{line}" }.join
  Genie.output indented, color: :green

  raise "File not found. Cannot append to a non-existent file." unless File.exist?(filepath)

  File.open(filepath, "a") do |file|
    file.write(content)
  end

  { success: true }
rescue => e
  Genie.output "Error: #{e.message}", color: :red
  { error: e.message }
end