Class: OmniAI::Tools::DiskTool

Inherits:
OmniAI::Tool
  • Object
show all
Defined in:
lib/omniai/tools/disk_tool.rb

Overview

A tool for interacting with files and directories. Be careful using as it can perform actions on your computer!

Examples:

disk = OmniAI::Tools::DiskTool.new
disk.execute(action: OmniAI::Tools::DiskTool::Action::FILE_CREATE, path: "./demo.rb")
disk.execute(action: OmniAI::Tools::DiskTool::Action::FILE_WRITE, path: "./demo.rb", text: "puts 'Hello'")
disk.execute(action: OmniAI::Tools::DiskTool::Action::FILE_READ, path: "./demo.rb")
disk.execute(action: OmniAI::Tools::DiskTool::Action::FILE_DELETE, path: "./demo.rb")

Defined Under Namespace

Modules: Action

Constant Summary collapse

ACTIONS =
[
  Action::DIRECTORY_CREATE,
  Action::DIRECTORY_DELETE,
  Action::DIRECTORY_MOVE,
  Action::DIRECTORY_LIST,
  Action::FILE_CREATE,
  Action::FILE_DELETE,
  Action::FILE_MOVE,
  Action::FILE_READ,
  Action::FILE_WRITE,
  Action::FILE_REPLACE,
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(driver:, logger: Logger.new(IO::NULL)) ⇒ DiskTool

Returns a new instance of DiskTool.

Parameters:



92
93
94
95
96
# File 'lib/omniai/tools/disk_tool.rb', line 92

def initialize(driver:, logger: Logger.new(IO::NULL))
  @logger = logger
  @driver = driver
  super()
end

Instance Method Details

#execute(action:, path:, destination: nil, old_text: nil, new_text: nil, text: nil) ⇒ Object

Parameters:

  • action (String)
  • path (String)
  • destination (String) (defaults to: nil)

    optional

  • old_text (String) (defaults to: nil)

    optional

  • new_text (String) (defaults to: nil)

    optional

  • text (String) (defaults to: nil)

    optional



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/omniai/tools/disk_tool.rb', line 104

def execute(action:, path:, destination: nil, old_text: nil, new_text: nil, text: nil)
  @logger.info({
    action:,
    path:,
    destination:,
    old_text:,
    new_text:,
    text:,
  }.compact.map { |key, value| "#{key}=#{value.inspect}" }.join(" "))

  case action
  when Action::DIRECTORY_CREATE then @driver.directory_create(path:)
  when Action::DIRECTORY_DELETE then @driver.directory_delete(path:)
  when Action::DIRECTORY_MOVE then @driver.directory_move(path:, destination:)
  when Action::DIRECTORY_LIST then @driver.directory_list(path:)
  when Action::FILE_CREATE then @driver.file_create(path:)
  when Action::FILE_DELETE then @driver.file_delete(path:)
  when Action::FILE_MOVE then @driver.file_move(path:, destination:)
  when Action::FILE_READ then @driver.file_read(path:)
  when Action::FILE_WRITE then @driver.file_write(path:, text:)
  when Action::FILE_REPLACE then @driver.file_replace(old_text:, new_text:, path:)
  end
end