Class: Tools::Read

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

Overview

Reads file contents with smart truncation and offset/limit paging. Returns plain text without line numbers, normalized to LF line endings.

Truncation limits: ‘Anima::Settings.max_read_lines` lines or `Anima::Settings.max_read_bytes` bytes, whichever hits first. When truncated, appends a continuation hint with the next offset value so the agent can page through large files.

Examples:

Basic read

tool.execute("path" => "config/routes.rb")
# => "Rails.application.routes.draw do\n  ..."

Paging through a large file

tool.execute("path" => "large.log", "offset" => 2001, "limit" => 500)
# => "line 2001 content\n..."

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

schema

Constructor Details

#initialize(shell_session: nil) ⇒ Read

Returns a new instance of Read.

Parameters:

  • shell_session (ShellSession, nil) (defaults to: nil)

    provides working directory for resolving relative paths



36
37
38
# File 'lib/tools/read.rb', line 36

def initialize(shell_session: nil, **)
  @working_directory = shell_session&.pwd
end

Class Method Details

.descriptionObject



21
# File 'lib/tools/read.rb', line 21

def self.description = "Read file. Relative paths resolve against working directory."

.input_schemaObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tools/read.rb', line 23

def self.input_schema
  {
    type: "object",
    properties: {
      path: {type: "string"},
      offset: {type: "integer", description: "1-indexed line number (default: 1)."},
      limit: {type: "integer", description: "Max lines to return."}
    },
    required: ["path"]
  }
end

.tool_nameObject



19
# File 'lib/tools/read.rb', line 19

def self.tool_name = "read"

Instance Method Details

#execute(input) ⇒ String, Hash

Parameters:

  • input (Hash<String, Object>)

    string-keyed hash from the Anthropic API

Returns:

  • (String)

    file contents (possibly truncated with continuation hint)

  • (Hash)

    with :error key on failure



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/tools/read.rb', line 43

def execute(input)
  path, offset, limit = extract_params(input)
  return {error: "Path cannot be blank"} if path.empty?

  path = resolve_path(path)

  error = validate_file(path)
  return error if error

  read_file(path, offset, limit)
end