Skip to content
Kward Search API index

Class: Kward::Tools::ReadFile

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

Overview

Tool wrapper for bounded workspace file reads.

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#schema

Constructor Details

#initialize(workspace:) ⇒ ReadFile

Builds the tool schema and stores the execution dependency.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/kward/tools/read_file.rb', line 10

def initialize(workspace:)
  @workspace = workspace
  super(
    "read_file",
    "Read a workspace text file. Output is capped; use offset/limit or mode to control context budget.",
    properties: {
      path: { type: "string", description: "Workspace-relative path." },
      limit: { type: "integer", description: "Maximum lines to return." },
      max_bytes: { type: "integer", description: "Optional byte budget for this read, capped by Kward's workspace read limit." },
      mode: { type: "string", enum: %w[preview outline range full], description: "Context mode. preview returns a short slice, outline returns source declarations, range reads offset/limit, full reads until Kward's cap." },
      offset: { type: "integer", description: "1-indexed start line." }
    },
    required: ["path"]
  )
end

Instance Method Details

#call(args, conversation, cancellation: nil) ⇒ Object

Executes the tool and returns model-facing output text.



27
28
29
30
31
32
33
34
35
36
# File 'lib/kward/tools/read_file.rb', line 27

def call(args, conversation, cancellation: nil)
  path = argument(args, :path, "")
  offset = argument(args, :offset)
  limit = argument(args, :limit)
  mode = argument(args, :mode)
  max_bytes = argument(args, :max_bytes)
  content = @workspace.read_file(path, offset: offset, limit: limit, mode: mode, max_bytes: max_bytes)
  conversation.mark_read(@workspace.resolved_path(path)) unless content.start_with?("Error:")
  content
end