class Riffer::Rig::Tools::Read
Constants
- DEFAULT_LIMIT
Public Instance Methods
(context: Riffer::Agent::Context?, path: String, ?offset: Integer?, ?limit: Integer) → Riffer::Tools::Response
Source
# File lib/riffer/rig/tools/read.rb, line 22 def call(context:, path:, offset: nil, limit: DEFAULT_LIMIT) resolved = File.expand_path(path, context&.[](:cwd)) return error("File not found: #{path}", type: :not_found) unless File.file?(resolved) return error('offset must be >= 1', type: :invalid_argument) if offset && offset < 1 start = offset ? offset - 1 : 0 lines = File.readlines(resolved, chomp: true, encoding: Encoding::UTF_8) slice = (lines[start, limit] || []).map(&:scrub) numbered = slice.each_with_index.map do |line, i| format("%<lineno>6d\t%<text>s", lineno: start + i + 1, text: line) end text(numbered.join("\n")) end
@rbs context: Riffer::Agent::Context? @rbs path: String @rbs offset: Integer? @rbs limit: Integer @rbs return: Riffer::Tools::Response