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("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 27 def initialize(root) super() FileUtils.mkdir_p(root) @root = File.realpath(root) end
Creates a store for the folder root.
Public Instance Methods
Source
# File lib/ruby_llm/file_store/directory.rb, line 49 def [](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 74 def delete(path) path = FileStore.path(path) with_lock do refuse_folder(path) if folder?(path) file = self[path] File.delete(full_path(path)) if file file end end
Deletes the file at path and returns its StoredFile, or nil.
Source
# File lib/ruby_llm/file_store/directory.rb, line 88 def delete_folder(path) path = FileStore.path(path) with_lock do refuse_file(path) if self[path] next [] unless folder?(path) full = full_path(path) walk(full).sort_by(&:path).tap { FileUtils.rm_r(full) } end end
Deletes the folder path with everything in it, hidden files included, and returns the deleted StoredFile objects. Links are removed, never followed.
Source
# File lib/ruby_llm/file_store/directory.rb, line 39 def list(prefix = nil) start = prefix.to_s.empty? ? root : full_path(FileStore.path(prefix)) return [self[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 58 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.
Source
# File lib/ruby_llm/file_store/directory.rb, line 101 def with_lock super do next yield if @locked File.open(root, File::RDONLY) do |folder| folder.flock(File::LOCK_EX) @locked = true yield ensure @locked = false end end end
Runs the block with the store locked, against other threads and, through a lock on root, other processes, and returns its value.
RubyLLM::FileStore#with_lock
# File lib/ruby_llm/file_store/directory.rb, line 67 def write(path, content, content_type: nil) path = FileStore.path(path) file = with_lock { write_file(path, content) } content_type ? StoredFile.new(**file.to_h, content_type:) : file end
Writes content to path and returns its StoredFile. Raises ArgumentError when path is a folder.