Class: PromptObjects::Primitives::WriteFile

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

Overview

Primitive capability to write content to a file.

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



20
21
22
# File 'lib/prompt_objects/primitives/write_file.rb', line 20

def description
  "Write content to a file (creates or overwrites)"
end

#nameObject



16
17
18
# File 'lib/prompt_objects/primitives/write_file.rb', line 16

def name
  "write_file"
end

#parametersObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/prompt_objects/primitives/write_file.rb', line 24

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

#receive(message, context:) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/prompt_objects/primitives/write_file.rb', line 41

def receive(message, context:)
  path = extract_param(message, :path)
  content = extract_param(message, :content)

  if path.nil? || path.empty?
    return "Error: path is required"
  end

  if content.nil?
    return "Error: content is required"
  end

  safe_path = File.expand_path(path)

  # Create parent directories if they don't exist
  dir = File.dirname(safe_path)
  FileUtils.mkdir_p(dir) unless File.directory?(dir)

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

  "Successfully wrote #{content.length} bytes to #{path}"
rescue Errno::EACCES
  "Error: Permission denied: #{path}"
rescue StandardError => e
  "Error writing file: #{e.message}"
end