Class: PromptObjects::Primitives::WriteFile
Overview
Primitive capability to write content to a file.
Instance Attribute Summary
Attributes inherited from Capability
#state
Instance Method Summary
collapse
#initialize
Methods inherited from Capability
#descriptor, execution_policy, execution_policy_definition, #execution_policy_definition, #execution_policy_signature, #initialize, #resolved_execution_policy
Instance Method Details
#description ⇒ Object
20
21
22
|
# File 'lib/prompt_objects/primitives/write_file.rb', line 20
def description
"Write content to a file (creates or overwrites)"
end
|
#name ⇒ Object
16
17
18
|
# File 'lib/prompt_objects/primitives/write_file.rb', line 16
def name
"write_file"
end
|
#parameters ⇒ Object
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 = (message, :path)
content = (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)
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
|