class RubyLLM::FileStore::Directory
Keeps files in a folder on disk, created when missing. Files someone puts in the folder are the chatβs files too. Hidden files and folders, whose names start with a dot, are left out, and symbolic links are never followed. Code execution runs in the folder itself and can change anything in it, so give each store a folder of its own.
chat.with_file_store(RubyLLM.file_store.directory("tmp/chats/42"))
Attributes
The folder holding the files, as an absolute path.
Public Class Methods
Source
# File lib/ruby_llm/file_store/directory.rb, line 25 def initialize(root) super() FileUtils.mkdir_p(root) @root = File.realpath(root) end
Creates a store for the folder root.
Calls superclass method
Public Instance Methods
Source
# File lib/ruby_llm/file_store/directory.rb, line 82 def delete(path) file = find(path) File.delete(full_path(file.path)) if file file end
Deletes the file at path and returns its StoredFile, or nil.
Source
# File lib/ruby_llm/file_store/directory.rb, line 47 def find(path) full = full_path(FileStore.path(path)) stat = File.lstat(full) stored_file(full, stat) if stat.file? rescue SystemCallError nil end
Returns the StoredFile at path, or nil.
Source
# File lib/ruby_llm/file_store/directory.rb, line 37 def list(prefix = nil) start = prefix.to_s.empty? ? root : full_path(FileStore.path(prefix)) return [find(prefix)].compact unless File.lstat(start).directory? walk(start).sort_by(&:path) rescue SystemCallError [] end
Returns the files inside the folder prefix, or every file.
Source
# File lib/ruby_llm/file_store/directory.rb, line 32 def local_root root end
Returns root: code execution runs in this folder.
Source
# File lib/ruby_llm/file_store/directory.rb, line 56 def read(path) path = FileStore.path(path) File.open(full_path(path), OPEN_FLAGS, &:read).b rescue SystemCallError raise FileNotFoundError, "No file at #{path}" end
Returns the content of the file at path.
# File lib/ruby_llm/file_store/directory.rb, line 64 def write(path, content, content_type: nil) path = FileStore.path(path) full = full_path(path) make_room(full) content = FileStore.bytes(content) temporary = File.join(File.dirname(full), ".#{File.basename(full)}.#{Process.pid}.#{Thread.current.object_id}.tmp") File.binwrite(temporary, content) File.rename(temporary, full) file = stored_file(full, File.lstat(full)) return file unless content_type StoredFile.new(**file.to_h, content_type:) ensure FileUtils.rm_f(temporary) if temporary end
Writes content to path and returns its StoredFile.