Class: SkillBench::Tools::ReadFile

Inherits:
Base
  • Object
show all
Defined in:
lib/skill_bench/tools/read_file.rb

Overview

Handles reading the contents of a file within the working directory.

Class Method Summary collapse

Class Method Details

.call(path, working_dir_path) ⇒ String

Reads the contents of a file.

Parameters:

  • path (String)

    The relative path to the file.

  • working_dir_path (Pathname)

    The working directory to resolve the path against.

Returns:

  • (String)

    The file contents, or an error message if not found.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/skill_bench/tools/read_file.rb', line 34

def self.call(path, working_dir_path)
  validation_error = validate_read_file_path(path)
  return validation_error if validation_error

  target = secure_path(path, working_dir_path)
  return 'Error: File not found' unless target.exist? && target.file?
  return 'Error: File is not readable' unless target.readable?

  target.read
rescue ArgumentError
  raise
rescue StandardError => e
  SkillBench::ErrorLogger.log_error(e, 'ReadFile Error')
  "Error reading file: #{e.message}"
end

.definitionHash

Returns The tool definition for the LLM API.

Returns:

  • (Hash)

    The tool definition for the LLM API.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/skill_bench/tools/read_file.rb', line 11

def self.definition
  {
    type: 'function',
    function: {
      name: 'read_file',
      description: 'Read the contents of a file.',
      parameters: {
        type: 'object',
        properties: {
          path: { type: 'string', description: 'Relative path to the file to read.' }
        },
        required: ['path'],
        additionalProperties: false
      }
    }
  }
end