Class: RubyLLM::Toolbox::Tools::CsvWrite

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby_llm/toolbox/tools/csv_write.rb

Overview

EXEC. Writes rows to a CSV file within fs_root. Optional header row. Missing parent directories are created.

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#call, exec_tool!, exec_tool?, #initialize, #name

Constructor Details

This class inherits a constructor from RubyLLM::Toolbox::Base

Instance Method Details

#execute(path:, rows:, headers: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby_llm/toolbox/tools/csv_write.rb', line 30

def execute(path:, rows:, headers: nil)
  return error("rows must be an array of arrays", code: :bad_rows) unless rows.is_a?(Array)

  jail = Safety::PathJail.new(config.fs_root)
  real = jail.resolve(path)
  return error("path is a directory: #{path}", code: :is_a_directory) if File.directory?(real)

  all = []
  all << Array(headers) if headers && !Array(headers).empty?
  rows.each { |row| all << Array(row) }

  FileUtils.mkdir_p(File.dirname(real))
  CSV.open(real, "w") { |csv| all.each { |row| csv << row } }

  "Wrote #{rows.size} row#{rows.size == 1 ? '' : 's'} to #{path}"
rescue Safety::PathJail::Jailbreak => e
  error(e.message, code: :path_denied)
end