Class: Kreator::Tools::Read

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

Constant Summary collapse

DEFAULT_MAX_BYTES =
20_000
DEFAULT_MAX_LINES =
200

Instance Method Summary collapse

Instance Method Details

#call(args:, context:, signal:) ⇒ Object



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
# File 'lib/kreator/tools/read.rb', line 31

def call(args:, context:, signal:)
  path = resolve_path(args.fetch("path"), context)
  context.ensure_not_cancelled!(signal)
  start_line = args.fetch("start_line", 1)
  max_lines = args.fetch("max_lines", DEFAULT_MAX_LINES)
  max_bytes = args.fetch("max_bytes", DEFAULT_MAX_BYTES)

  bytes = File.binread(path, max_bytes + 1)
  byte_truncated = bytes.bytesize > max_bytes
  bytes = bytes.byteslice(0, max_bytes) if byte_truncated
  text = bytes.encode("UTF-8", invalid: :replace, undef: :replace, replace: "\uFFFD")
  all_lines = text.lines
  selected_lines = all_lines.drop(start_line - 1).first(max_lines)
  line_truncated = all_lines.length >= start_line + max_lines
  content = selected_lines.join
  notices = []
  notices << "[truncated after #{max_bytes} bytes]" if byte_truncated
  notices << "[truncated after #{max_lines} lines]" if line_truncated
  content = "#{content}\n#{notices.join("\n")}" unless notices.empty?

  ToolResult.new(
    tool_call_id: "",
    name: name,
    content: content,
    metadata: {
      "path" => path,
      "bytes_read" => [bytes.bytesize, max_bytes].min,
      "byte_truncated" => byte_truncated,
      "line_truncated" => line_truncated
    }
  )
end

#descriptionObject



13
14
15
# File 'lib/kreator/tools/read.rb', line 13

def description
  "Read a UTF-8 text file with optional line and byte truncation."
end

#nameObject



9
10
11
# File 'lib/kreator/tools/read.rb', line 9

def name
  "read"
end

#schemaObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/kreator/tools/read.rb', line 17

def schema
  {
    "type" => "object",
    "additionalProperties" => false,
    "required" => ["path"],
    "properties" => {
      "path" => { "type" => "string", "minLength" => 1 },
      "start_line" => { "type" => "integer", "minimum" => 1 },
      "max_lines" => { "type" => "integer", "minimum" => 1 },
      "max_bytes" => { "type" => "integer", "minimum" => 1 }
    }
  }
end