Class: Zephira::Tools::ReadFile

Inherits:
BaseTool
  • Object
show all
Defined in:
lib/zephira/tools/read_file.rb

Constant Summary collapse

DEFAULT_MAX_BYTES =
20 * 1024

Instance Attribute Summary

Attributes inherited from BaseTool

#agent, #args

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseTool

announces_intent?, #arg, #error_result, #initialize, run, #success_result, #validate

Constructor Details

This class inherits a constructor from Zephira::Tools::BaseTool

Class Method Details

.descriptionObject



13
14
15
# File 'lib/zephira/tools/read_file.rb', line 13

def description
  "Read the contents of one or more files (up to a size threshold)."
end

.nameObject



9
10
11
# File 'lib/zephira/tools/read_file.rb', line 9

def name
  "read_file"
end

.parametersObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/zephira/tools/read_file.rb', line 21

def parameters
  {
    type: "object",
    properties: {
      intent: {
        type: "string",
        description: "Brief summary of intent of the operation, meant to be used for context compaction and presentation to the user. Use active voice (e.g., 'Reading X to do Y')."
      },
      file_paths: {
        type: "array",
        items: {type: "string"},
        description: "Paths to the files to read"
      }
    },
    required: ["file_paths", "intent"]
  }
end

.read_only?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/zephira/tools/read_file.rb', line 17

def read_only?
  true
end

Instance Method Details

#runObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/zephira/tools/read_file.rb', line 40

def run
  paths = validate(arg(:file_paths), arg_path: "file_paths", type: Array, allow_empty: false)

  results = paths.map do |file_path|
    if file_path.nil? || file_path.strip.empty?
      agent.status.warn("`file_path` was empty or missing for entry")
      {"path" => file_path, "content" => "", "error" => "`file_path` was empty or missing"}
    else
      expanded_path = ::File.expand_path(file_path)
      begin
        size = ::File.size(expanded_path)
        agent.status.verbose(" • Reading file: '#{file_path}' (max #{DEFAULT_MAX_BYTES} bytes)")

        data = if size > DEFAULT_MAX_BYTES
          agent.status.verbose(" • File size #{size} bytes exceeds limit of #{DEFAULT_MAX_BYTES} bytes, truncating content")
          ::File.open(expanded_path, "rb") { |file| file.read(DEFAULT_MAX_BYTES) }
        else
          ::File.binread(expanded_path)
        end

        content = normalize_content(data)
        agent.status.verbose(" • Read file: '#{file_path}'")
        agent.logger.info("Read file: '#{file_path}'")
        {"path" => file_path, "content" => content}
      rescue Errno::ENOENT
        agent.status.warn(" • File not found: '#{file_path}'")
        agent.logger.error("File not found: '#{file_path}'")
        {"path" => file_path, "content" => "", "error" => "No such file or directory: #{file_path}"}
      rescue Errno::EACCES
        agent.status.warn(" • Permission denied: '#{file_path}'")
        agent.logger.error("Permission denied: '#{file_path}'")
        {"path" => file_path, "content" => "", "error" => "Permission denied: #{file_path}"}
      rescue Errno::EISDIR
        agent.status.warn(" • Is a directory: '#{file_path}'")
        agent.logger.error("Is a directory: '#{file_path}'")
        {"path" => file_path, "content" => "", "error" => "Is a directory: #{file_path}"}
      end
    end
  end

  success_result(results)
end