Class: SharedTools::Tools::Doc::TextReaderTool

Inherits:
RubyLLM::Tool
  • Object
show all
Defined in:
lib/shared_tools/tools/doc/text_reader_tool.rb

Overview

Read and return the full contents of a plain text file.

Examples:

tool = SharedTools::Tools::Doc::TextReaderTool.new
tool.execute(doc_path: "./guide.txt")

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil) ⇒ TextReaderTool

Returns a new instance of TextReaderTool.

Parameters:

  • logger (Logger) (defaults to: nil)

    optional logger



21
22
23
# File 'lib/shared_tools/tools/doc/text_reader_tool.rb', line 21

def initialize(logger: nil)
  @logger = logger || RubyLLM.logger
end

Class Method Details

.nameObject



12
# File 'lib/shared_tools/tools/doc/text_reader_tool.rb', line 12

def self.name = 'doc_text_read'

Instance Method Details

#execute(doc_path:) ⇒ Hash

Returns file content and metadata.

Parameters:

  • doc_path (String)

    path to the text file

Returns:

  • (Hash)

    file content and metadata



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/shared_tools/tools/doc/text_reader_tool.rb', line 27

def execute(doc_path:)
  @logger.info("TextReaderTool#execute doc_path=#{doc_path.inspect}")

  raise ArgumentError, "doc_path is required" if doc_path.nil? || doc_path.strip.empty?
  raise ArgumentError, "File not found: #{doc_path}" unless File.exist?(doc_path)
  raise ArgumentError, "Not a file: #{doc_path}" unless File.file?(doc_path)

  content    = File.read(doc_path, encoding: 'utf-8')
  line_count = content.lines.size
  char_count = content.length
  word_count = content.split.size

  @logger.info("TextReaderTool read #{char_count} chars, #{line_count} lines from #{doc_path}")

  {
    doc_path:   doc_path,
    content:    content,
    line_count: line_count,
    word_count: word_count,
    char_count: char_count
  }
rescue ArgumentError
  raise
rescue => e
  @logger.error("TextReaderTool failed to read #{doc_path}: #{e.message}")
  { error: e.message, doc_path: doc_path }
end