Class: OxAiWorkers::Tool::FileSystem

Inherits:
Object
  • Object
show all
Includes:
DependencyHelper, LoadI18n, OxAiWorkers::ToolDefinition
Defined in:
lib/oxaiworkers/tool/file_system.rb

Overview

A tool that wraps the Ruby file system classes.

Usage:

file_system = OxAiWorkers::Tool::FileSystem.new

Instance Attribute Summary collapse

Attributes included from LoadI18n

#locale

Attributes included from OxAiWorkers::ToolDefinition

#white_list

Instance Method Summary collapse

Methods included from LoadI18n

#store_locale, #with_locale

Methods included from DependencyHelper

#depends_on

Methods included from OxAiWorkers::ToolDefinition

#define_function, #full_function_name, #function_schemas, #init_white_list_with, #tool_name

Constructor Details

#initialize(current_dir: nil, only: nil) ⇒ FileSystem

Returns a new instance of FileSystem.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/oxaiworkers/tool/file_system.rb', line 20

def initialize(current_dir: nil, only: nil)
  depends_on 'ptools'

  store_locale

  init_white_list_with only

  define_function :list_directory,
                  description: I18n.t('oxaiworkers.tool.file_system.list_directory.description') do
    property :directory_path, type: 'string',
                              description: I18n.t('oxaiworkers.tool.file_system.list_directory.directory_path'),
                              required: true
  end

  define_function :read_file, description: I18n.t('oxaiworkers.tool.file_system.read_file.description') do
    property :file_path, type: 'string', description: I18n.t('oxaiworkers.tool.file_system.read_file.file_path'),
                         required: true
  end

  define_function :write_to_file, description: I18n.t('oxaiworkers.tool.file_system.write_to_file.description') do
    property :file_path, type: 'string',
                         description: I18n.t('oxaiworkers.tool.file_system.write_to_file.file_path'), required: true
    property :content, type: 'string', description: I18n.t('oxaiworkers.tool.file_system.write_to_file.content'),
                       required: true
  end

  @current_dir = current_dir
end

Instance Attribute Details

#current_dirObject

Returns the value of attribute current_dir.



18
19
20
# File 'lib/oxaiworkers/tool/file_system.rb', line 18

def current_dir
  @current_dir
end

Instance Method Details

#list_directory(directory_path:) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/oxaiworkers/tool/file_system.rb', line 49

def list_directory(directory_path:)
  path = full_path(directory_path)
  OxAiWorkers.logger.info("Listing directory: #{path}", for: self.class)
  list = Dir.entries(path)
  list.delete_if { |f| f.start_with?('.') }
  if list.present?
    "Contents of directory \"#{path}\":\n #{list.join("\n")}"
  else
    "Directory is empty: #{path}"
  end
rescue Errno::ENOENT
  "No such directory: #{path}"
end

#read_file(file_path:) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/oxaiworkers/tool/file_system.rb', line 63

def read_file(file_path:)
  path = full_path(file_path)
  OxAiWorkers.logger.info("Reading file: #{path}", for: self.class)
  if File.binary?(path)
    "File is binary: #{path}"
  else
    File.read(path).to_s
  end
rescue Errno::ENOENT
  "No such file: #{path}"
end

#write_to_file(file_path:, content:) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/oxaiworkers/tool/file_system.rb', line 75

def write_to_file(file_path:, content:)
  path = full_path(file_path)
  OxAiWorkers.logger.info("Writing to file: #{path}", for: self.class)
  # Ensure directory exists if using current_dir
  FileUtils.mkdir_p(File.dirname(path)) if @current_dir.present? && !Dir.exist?(File.dirname(path))
  File.write(path, content)
  "Content was successfully written to the file: #{path}"
rescue Errno::EACCES
  "Permission denied: #{path}"
end