Class: Yorishiro::Tools::WriteFile

Inherits:
Yorishiro::Tool show all
Defined in:
lib/yorishiro/tools/write_file.rb

Instance Method Summary collapse

Methods inherited from Yorishiro::Tool

#configure, #definition, #read_only?

Instance Method Details

#descriptionObject



10
11
12
# File 'lib/yorishiro/tools/write_file.rb', line 10

def description
  "Write content to a file at the specified path. Creates the file if it doesn't exist, overwrites if it does."
end

#execute(**params) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/yorishiro/tools/write_file.rb', line 25

def execute(**params)
  path = params[:path] || params["path"]
  content = params[:content] || params["content"]

  dir = File.dirname(path)
  FileUtils.mkdir_p(dir)

  File.write(path, content)
  "Successfully wrote #{content.bytesize} bytes to #{path}"
end

#nameObject



6
7
8
# File 'lib/yorishiro/tools/write_file.rb', line 6

def name
  "write_file"
end

#parametersObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/yorishiro/tools/write_file.rb', line 14

def parameters
  {
    type: "object",
    properties: {
      path: { type: "string", description: "The file path to write to" },
      content: { type: "string", description: "The content to write to the file" }
    },
    required: %w[path content]
  }
end

#permission_check(_arguments) ⇒ Object



36
37
38
# File 'lib/yorishiro/tools/write_file.rb', line 36

def permission_check(_arguments)
  :ask
end

#preview(arguments) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/yorishiro/tools/write_file.rb', line 40

def preview(arguments)
  path = arguments[:path] || arguments["path"]
  content = arguments[:content] || arguments["content"]
  return nil unless path && content

  old_content = File.file?(path) ? File.read(path) : ""
  Diff.unified(old_content, content, path: path)
end