Class: ActAsAgent::Tools::WriteFile

Inherits:
Object
  • Object
show all
Defined in:
lib/act_as_agent/tools/write_file.rb

Constant Summary collapse

ERROR =
ActAsAgent::Errors::ToolIncorrectArgsError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path: nil, base_folder: nil, return_content: false) ⇒ WriteFile



13
14
15
16
17
# File 'lib/act_as_agent/tools/write_file.rb', line 13

def initialize(file_path: nil, base_folder: nil, return_content: false)
  @file_path = file_path
  @base_folder = base_folder
  @return_content = return_content
end

Instance Attribute Details

#base_folderObject (readonly)

Returns the value of attribute base_folder.



11
12
13
# File 'lib/act_as_agent/tools/write_file.rb', line 11

def base_folder
  @base_folder
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



11
12
13
# File 'lib/act_as_agent/tools/write_file.rb', line 11

def file_path
  @file_path
end

#return_contentObject (readonly)

Returns the value of attribute return_content.



11
12
13
# File 'lib/act_as_agent/tools/write_file.rb', line 11

def return_content
  @return_content
end

Instance Method Details

#call(args = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/act_as_agent/tools/write_file.rb', line 44

def call(args = {})
  path = args.fetch("file_path", "")
  content = args.fetch("file_content", nil)

  return write(base_folder, path, content) unless path.chomp == ""
  return write(base_folder, file_path, content) unless file_path.nil?
  return write(nil, base_folder, content) unless base_folder.nil?

  ERROR.new("Incorrect params have been given to write file tool")
end

#descriptionObject



23
24
25
26
27
28
29
30
# File 'lib/act_as_agent/tools/write_file.rb', line 23

def description
  "
  It writes content into file. If file doesn't exist it will create a new one.
  It accepts two parameters file_path and file_content.
  But file_path is optional as by default it will write into the file
  user want it to be saved.
  "
end

#input_schemaObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/act_as_agent/tools/write_file.rb', line 32

def input_schema
  {
    type: "object",
    properties: {
      file_path: { type: "string",
                   description: "File path to write" },
      file_content: { type: "string", description: "File content" }
    },
    required: ["file_content"]
  }
end

#nameObject



19
20
21
# File 'lib/act_as_agent/tools/write_file.rb', line 19

def name
  self.class.to_s.gsub("::", "__")
end