Class: RubyLLM::Toolbox::Tools::CsvRead

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

Overview

SAFE. Reads a CSV file within fs_root and returns its rows in a compact readable form. Read-only.

Constant Summary collapse

MAX_BYTES =
10 * 1024 * 1024
DEFAULT_LIMIT =
100

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:, headers: true, limit: DEFAULT_LIMIT) ⇒ Object



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

def execute(path:, headers: true, limit: DEFAULT_LIMIT)
  jail = Safety::PathJail.new(config.fs_root)
  real = jail.resolve(path)
  return error("not a file: #{path}", code: :not_a_file) unless File.file?(real)
  return error("file too large (> #{MAX_BYTES} bytes)", code: :too_large) if File.size(real) > MAX_BYTES

  rows = CSV.read(real)
  return "empty CSV" if rows.empty?

  max = limit.to_i
  max = DEFAULT_LIMIT if max <= 0
  truncate(render(rows, headers, max))
rescue Safety::PathJail::Jailbreak => e
  error(e.message, code: :path_denied)
rescue CSV::MalformedCSVError => e
  error("malformed CSV: #{e.message}", code: :bad_csv)
end