Class: PromptObjects::Primitives::ReadFile

Inherits:
PromptObjects::Primitive show all
Defined in:
lib/prompt_objects/primitives/read_file.rb

Overview

Primitive capability to read file contents.

Instance Attribute Summary

Attributes inherited from Capability

#state

Instance Method Summary collapse

Methods inherited from PromptObjects::Primitive

#initialize

Methods inherited from Capability

#descriptor, execution_policy, execution_policy_definition, #execution_policy_definition, #execution_policy_signature, #initialize, #resolved_execution_policy

Constructor Details

This class inherits a constructor from PromptObjects::Primitive

Instance Method Details

#descriptionObject



13
14
15
# File 'lib/prompt_objects/primitives/read_file.rb', line 13

def description
  "Read the contents of a text file"
end

#nameObject



9
10
11
# File 'lib/prompt_objects/primitives/read_file.rb', line 9

def name
  "read_file"
end

#parametersObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/prompt_objects/primitives/read_file.rb', line 17

def parameters
  {
    type: "object",
    properties: {
      path: {
        type: "string",
        description: "The path to the file to read"
      }
    },
    required: ["path"]
  }
end

#receive(message, context:) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/prompt_objects/primitives/read_file.rb', line 30

def receive(message, context:)
  path = extract_path(message)
  safe_path = validate_path(path)

  unless File.exist?(safe_path)
    return "Error: File not found: #{path}"
  end

  unless File.file?(safe_path)
    return "Error: Not a file: #{path}"
  end

  content = File.read(safe_path, encoding: "UTF-8")

  # Truncate very large files
  if content.length > 50_000
    content = content[0, 50_000] + "\n\n... [truncated, file is #{content.length} bytes]"
  end

  content
rescue Errno::EACCES
  "Error: Permission denied: #{path}"
rescue StandardError => e
  "Error reading file: #{e.message}"
end