Class: SharedTools::Tools::DocTool

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

Overview

A tool for reading and processing documents

Defined Under Namespace

Modules: Action

Constant Summary collapse

ACTIONS =
[
  Action::PDF_READ,
  Action::TEXT_READ,
  Action::DOCX_READ,
  Action::SPREADSHEET_READ,
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil) ⇒ DocTool

Returns a new instance of DocTool.

Parameters:

  • logger (Logger) (defaults to: nil)

    optional logger



138
139
140
# File 'lib/shared_tools/tools/doc_tool.rb', line 138

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

Class Method Details

.nameObject



9
# File 'lib/shared_tools/tools/doc_tool.rb', line 9

def self.name = 'doc_tool'

Instance Method Details

#execute(action:, doc_path: nil, page_numbers: nil, paragraph_range: nil, sheet: nil, row_range: nil, headers: true) ⇒ Hash

Returns execution result.

Parameters:

  • action (String)

    the action to perform

  • doc_path (String, nil) (defaults to: nil)

    path to document

  • page_numbers (String, nil) (defaults to: nil)

    page numbers to read

Returns:

  • (Hash)

    execution result



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/shared_tools/tools/doc_tool.rb', line 147

def execute(action:, doc_path: nil, page_numbers: nil, paragraph_range: nil, sheet: nil, row_range: nil, headers: true)
  @logger.info("DocTool#execute action=#{action}")

  case action.to_s.downcase
  when Action::TEXT_READ
    require_param!(:doc_path, doc_path)
    text_reader_tool.execute(doc_path: doc_path)
  when Action::PDF_READ
    require_param!(:doc_path, doc_path)
    require_param!(:page_numbers, page_numbers)
    pdf_reader_tool.execute(doc_path: doc_path, page_numbers: page_numbers)
  when Action::DOCX_READ
    require_param!(:doc_path, doc_path)
    docx_reader_tool.execute(doc_path: doc_path, paragraph_range: paragraph_range)
  when Action::SPREADSHEET_READ
    require_param!(:doc_path, doc_path)
    spreadsheet_reader_tool.execute(doc_path: doc_path, sheet: sheet, row_range: row_range, headers: headers)
  else
    { error: "Unsupported action: #{action}. Supported actions are: #{ACTIONS.join(', ')}" }
  end
rescue StandardError => e
  @logger.error("DocTool execution failed: #{e.message}")
  { error: e.message }
end