class RubyLLM::ActiveRecord::FileStore
Keeps a persisted chat’s files in ruby_llm_chat_files, with their content in Active Storage, so they live on whatever service Active Storage uses: a disk now, a bucket later. A persisted chat uses one when ChatFiles#with_file_store is given no store, and RubyLLM.file_store.active_storage builds one.
chat.with_file_store chat.with_file_store(RubyLLM.file_store.active_storage(chat)) chat.file_store.list
With owner:, the store writes files for that owner and sees only their files and the files without an owner, so in a chat several people share, one person’s tool results stay theirs:
chat.with_file_store(owner: Current.user)
Converted text is kept in the row’s text column. Encrypt it, like anything else the model reads, with RubyLLM::ActiveRecord::ChatFile.encrypts :text.
Attributes
The chat record the files belong to.
The owner the store writes files for, or nil.
Public Class Methods
Source
# File lib/ruby_llm/active_record/file_store.rb, line 35 def initialize(chat, owner: nil) super() @chat = chat @owner = owner end
Creates a store for the files of the chat record, writing them for owner when given.
Public Instance Methods
Source
# File lib/ruby_llm/active_record/file_store.rb, line 82 def cache_text(file, text) record = row(file.path) record.update_columns(text:) if record&.digest == file.digest && record.has_attribute?(:text) end
Keeps text for file in its row.
Source
# File lib/ruby_llm/active_record/file_store.rb, line 76 def cached_text(file) record = row(file.path) record.text if record&.digest == file.digest && record.has_attribute?(:text) end
Returns the text kept for file, or nil.
Source
# File lib/ruby_llm/active_record/file_store.rb, line 69 def delete(path) record = owned.find_by(path: path(path)) record&.destroy record&.to_stored_file end
Deletes the file at path and returns its StoredFile, or nil.
Source
# File lib/ruby_llm/active_record/file_store.rb, line 48 def find(path) row(path)&.to_stored_file end
Returns the StoredFile at path, or nil.
Source
# File lib/ruby_llm/active_record/file_store.rb, line 42 def list(prefix = nil) scope = prefix.present? ? inside(path(prefix)) : visible preferred(scope.includes(:owner).order(:path, :id).to_a).map(&:to_stored_file) end
Returns the files inside the folder prefix, or every file.
Source
# File lib/ruby_llm/active_record/file_store.rb, line 53 def read(path) record = row(path) || raise(FileNotFoundError, "No file at #{path(path)}") record.file.download.b end
Returns the content of the file at path.
# File lib/ruby_llm/active_record/file_store.rb, line 59 def write(path, content, content_type: nil) content = RubyLLM::FileStore.bytes(content) path = path(path) type = content_type || RubyLLM::FileStore.content_type_for(path, content) save(path, content.bytesize, type, RubyLLM::FileStore.digest(content)) do |record| record.file.attach(io: StringIO.new(content), filename: File.basename(path), content_type: type) end end
Writes content to path and returns its StoredFile.