Class: Rubino::Tools::ReadTool
- Inherits:
-
Base
- Object
- Base
- Rubino::Tools::ReadTool
show all
- Defined in:
- lib/rubino/tools/read_tool.rb
Overview
Reads a file with ‘cat -n` style line numbers, offset/limit windowing, and a hard cap on per-line length. Line numbers let the LLM cite or edit exact lines instead of “the second occurrence of X”; offset/limit let it page through files that would otherwise blow the context.
Constant Summary
collapse
- DEFAULT_LIMIT =
2000
- MAX_LINE_WIDTH =
2000
- MAX_OUTPUT_BYTES =
Hard cap on the bytes a single read returns (~25k tokens at 4 bytes/tok, matching Claude Code’s read gate). A window of 2000 lines × 2000 chars could otherwise build multiple MB in memory and blow up prefill/TTFT; past this we stop and tell the model to narrow the range or grep.
100_000
Instance Attribute Summary
Attributes inherited from Base
#cancel_token, #read_tracker, #stream_chunk
Instance Method Summary
collapse
Methods inherited from Base
#cancellation_requested?, #config_key, #emit_chunk, #risky?, #to_tool_definition, workspace_root, workspace_roots
Instance Method Details
#call(arguments) ⇒ Object
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
88
|
# File 'lib/rubino/tools/read_tool.rb', line 45
def call(arguments)
file_path = arguments["file_path"] || arguments[:file_path]
offset = (arguments["offset"] || arguments[:offset] || 1).to_i
limit = (arguments["limit"] || arguments[:limit] || DEFAULT_LIMIT).to_i
return "Error: file_path is required" if file_path.nil? || file_path.to_s.empty?
expanded = File.expand_path(file_path)
return "Error: File not found: #{file_path}" unless File.exist?(expanded)
return "Error: Not a regular file: #{file_path}" unless File.file?(expanded)
if binary?(expanded)
size = File.size(expanded)
return { output: "Error: #{file_path} appears to be a binary file (#{size} bytes). " \
"Reading it as text would corrupt the buffer. " \
"Use the shell tool with xxd/file/strings for inspection.",
error_code: :binary_file }
end
offset = 1 if offset < 1
limit = DEFAULT_LIMIT if limit <= 0
mtime = File.mtime(expanded)
@read_tracker&.register(expanded, mtime)
dup = @read_tracker&.register_window(expanded, offset, limit, mtime)
if dup && dup > 1
return { output: "[DUPLICATE READ] Exact repeat of an earlier read of #{file_path} " \
"(lines #{offset}-#{offset + limit - 1}) this turn — reuse that result " \
"instead of re-reading.",
metrics: "duplicate" }
end
render(expanded, file_path, offset, limit)
rescue StandardError => e
"Error reading #{file_path}: #{e.message}"
end
|
#description ⇒ Object
22
23
24
25
26
27
|
# File 'lib/rubino/tools/read_tool.rb', line 22
def description
"Read a text file from the filesystem with line numbers (cat -n style). " \
"Supports offset (1-based start line) and limit (max lines returned). " \
"Long lines are truncated at #{MAX_LINE_WIDTH} chars. " \
"Default window: first #{DEFAULT_LIMIT} lines."
end
|
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/rubino/tools/read_tool.rb', line 29
def input_schema
{
type: "object",
properties: {
file_path: { type: "string", description: "Absolute or relative file path" },
offset: { type: "integer", description: "1-based line to start at (default 1)" },
limit: { type: "integer", description: "Max lines to return (default #{DEFAULT_LIMIT})" }
},
required: %w[file_path]
}
end
|
#name ⇒ Object
18
19
20
|
# File 'lib/rubino/tools/read_tool.rb', line 18
def name
"read"
end
|
#risk_level ⇒ Object
41
42
43
|
# File 'lib/rubino/tools/read_tool.rb', line 41
def risk_level
:low
end
|