Class: Genie::RenameFile

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

Instance Method Summary collapse

Methods included from SandboxedFileTool

#enforce_sandbox!, #initialize, #within_sandbox?

Instance Method Details

#execute(filepath:, new_path:) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tools/rename_file.rb', line 12

def execute(filepath:, new_path:)
  # Expand to absolute paths
  src = File.expand_path(filepath)
  dst = File.expand_path(new_path)

  Genie.output "Renaming file from: #{src} to #{dst}", color: :blue

  enforce_sandbox!(src)
  enforce_sandbox!(dst)

  # Check source exists
  unless File.exist?(src)
    raise "File not found. Cannot rename a non-existent file."
  end

  # Check destination does not exist
  if File.exist?(dst)
    raise "Destination already exists: #{dst}."
  end

  # Ensure destination directory exists
  dest_dir = File.dirname(dst)
  FileUtils.mkdir_p(dest_dir) unless Dir.exist?(dest_dir)

  # Perform rename
  FileUtils.mv(src, dst)

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