Class: ReadTool

Inherits:
LlmGateway::Tool show all
Defined in:
lib/llm_gateway/agents/tools/read_tool.rb

Constant Summary collapse

IMAGE_TYPE_SNIFF_BYTES =
4100
PNG_SIGNATURE =
[ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a ].freeze

Instance Method Summary collapse

Methods inherited from LlmGateway::Tool

cache, definition, description, #initialize, input_schema, name, tool_name, #tool_result

Constructor Details

This class inherits a constructor from LlmGateway::Tool

Instance Method Details

#execute(input, tool_use_id:) ⇒ Object



25
26
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
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
82
83
84
85
86
87
# File 'lib/llm_gateway/agents/tools/read_tool.rb', line 25

def execute(input, tool_use_id:)
  path = input[:path] || input["path"]
  offset = input[:offset] || input["offset"]
  limit = input[:limit] || input["limit"]

  absolute_path = ToolUtils.resolve_read_path(path)

  return tool_result("File not found: #{path}", tool_use_id: tool_use_id) unless File.exist?(absolute_path)
  return tool_result("Cannot read directory: #{path}", tool_use_id: tool_use_id) if File.directory?(absolute_path)
  return tool_result("File is not readable: #{path}", tool_use_id: tool_use_id) unless File.readable?(absolute_path)

  mime_type = detect_supported_image_mime_type_from_file(absolute_path)
  if mime_type
    data = Base64.strict_encode64(File.binread(absolute_path))
    return tool_result([
      { type: "text", text: "Read image file [#{mime_type}]" },
      { type: "image", data: data, media_type: mime_type }
    ], tool_use_id: tool_use_id)
  end

  content = File.read(absolute_path, mode: "r:bom|utf-8")
  all_lines = content.split("\n", -1)
  total_file_lines = all_lines.length

  start_line = [ 0, (offset || 1).to_i - 1 ].max
  return tool_result("Offset #{offset} is beyond end of file (#{all_lines.length} lines total)", tool_use_id: tool_use_id) if start_line >= all_lines.length

  selected_content = if limit
    end_line = [ start_line + limit.to_i, all_lines.length ].min
    all_lines[start_line...end_line].join("\n")
  else
    all_lines[start_line..].join("\n")
  end

  truncation = ToolUtils.truncate_head(selected_content)
  start_display = start_line + 1

  if truncation[:first_line_exceeds_limit]
    first_line_size = ToolUtils.format_size(all_lines[start_line].to_s.bytesize)
    return tool_result("[Line #{start_display} is #{first_line_size}, exceeds #{ToolUtils.format_size(ToolUtils::DEFAULT_MAX_BYTES)} limit. Use bash: sed -n '#{start_display}p' #{path} | head -c #{ToolUtils::DEFAULT_MAX_BYTES}]", tool_use_id: tool_use_id)
  end

  output = truncation[:content]

  if truncation[:truncated]
    end_display = start_display + truncation[:output_lines] - 1
    next_offset = end_display + 1
    suffix = if truncation[:truncated_by] == "lines"
      "[Showing lines #{start_display}-#{end_display} of #{total_file_lines}. Use offset=#{next_offset} to continue.]"
    else
      "[Showing lines #{start_display}-#{end_display} of #{total_file_lines} (#{ToolUtils.format_size(ToolUtils::DEFAULT_MAX_BYTES)} limit). Use offset=#{next_offset} to continue.]"
    end
    output = "#{output}\n\n#{suffix}"
  elsif limit && (start_line + limit.to_i) < all_lines.length
    next_offset = start_line + limit.to_i + 1
    remaining = all_lines.length - (start_line + limit.to_i)
    output = "#{output}\n\n[#{remaining} more lines in file. Use offset=#{next_offset} to continue.]"
  end

  tool_result(output, tool_use_id: tool_use_id)
rescue StandardError => e
  tool_result("Error reading file: #{e.message}", tool_use_id: tool_use_id)
end