Class: OxAiWorkers::Tool::FileSystem
- Inherits:
-
Object
- Object
- OxAiWorkers::Tool::FileSystem
- 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
Attributes included from LoadI18n
Attributes included from OxAiWorkers::ToolDefinition
Instance Method Summary collapse
-
#initialize(only: nil) ⇒ FileSystem
constructor
A new instance of FileSystem.
- #list_directory(directory_path:) ⇒ Object
- #read_file(file_path:) ⇒ Object
- #write_to_file(file_path:, content:) ⇒ Object
Methods included from LoadI18n
Methods included from DependencyHelper
Methods included from OxAiWorkers::ToolDefinition
#define_function, #full_function_name, #function_schemas, #init_white_list_with, #tool_name
Constructor Details
#initialize(only: nil) ⇒ FileSystem
Returns a new instance of FileSystem.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/oxaiworkers/tool/file_system.rb', line 18 def initialize(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 end |
Instance Method Details
#list_directory(directory_path:) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/oxaiworkers/tool/file_system.rb', line 45 def list_directory(directory_path:) OxAiWorkers.logger.info("Listing directory: #{directory_path}", for: self.class) list = Dir.entries(directory_path) list.delete_if { |f| f.start_with?('.') } if list.present? with_locale { "Contents of directory \"#{directory_path}\":\n #{list.join("\n")}" } else with_locale { "Directory is empty: #{directory_path}" } end rescue Errno::ENOENT with_locale { "No such directory: #{directory_path}" } end |
#read_file(file_path:) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/oxaiworkers/tool/file_system.rb', line 58 def read_file(file_path:) OxAiWorkers.logger.info("Reading file: #{file_path}", for: self.class) if File.binary?(file_path) with_locale { "File is binary: #{file_path}" } else File.read(file_path).to_s end rescue Errno::ENOENT with_locale { "No such file: #{file_path}" } end |
#write_to_file(file_path:, content:) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/oxaiworkers/tool/file_system.rb', line 69 def write_to_file(file_path:, content:) OxAiWorkers.logger.info("Writing to file: #{file_path}", for: self.class) File.write(file_path, content) with_locale { "Content was successfully written to the file: #{file_path}" } rescue Errno::EACCES with_locale { "Permission denied: #{file_path}" } end |