Class: Relay::Attachment::Session
- Inherits:
-
Object
- Object
- Relay::Attachment::Session
- Defined in:
- lib/relay/attachment/session.rb
Constant Summary collapse
- IMAGE_EXTENSIONS =
%w[.png .jpg .jpeg .gif .webp .bmp .tiff .tif .heic .heif].freeze
Instance Method Summary collapse
- #accept ⇒ String
- #attach(io:, filename:, type:) ⇒ Relay::Attachment
- #clear ⇒ void
- #clear_error ⇒ void
- #consume ⇒ Relay::Attachment?
- #error ⇒ String?
- #error=(message) ⇒ String
- #file ⇒ Relay::Attachment
-
#initialize(session:, root:, user: nil, provider: nil) ⇒ Session
constructor
A new instance of Session.
- #supported? ⇒ Boolean
- #type_supported?(filename:, type:) ⇒ Boolean
- #unsupported_message ⇒ String
Constructor Details
Instance Method Details
#accept ⇒ String
59 60 61 62 63 64 65 |
# File 'lib/relay/attachment/session.rb', line 59 def accept case provider when "anthropic" then "image/*,application/pdf,.pdf" when "openai", "google" then "*/*" else "" end end |
#attach(io:, filename:, type:) ⇒ Relay::Attachment
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/relay/attachment/session.rb', line 95 def attach(io:, filename:, type:) raise ArgumentError, "a file is required" unless io && filename FileUtils.mkdir_p() file = Relay::Attachment.new( name: sanitize_filename(filename), path: File.join(, "#{SecureRandom.hex(8)}-#{sanitize_filename(filename)}"), type: type.to_s ) io.rewind if io.respond_to?(:rewind) IO.copy_stream(io, file.path) @session["attachment"] = file.to_h clear_error @attachment = file end |
#clear ⇒ void
This method returns an undefined value.
125 126 127 128 129 |
# File 'lib/relay/attachment/session.rb', line 125 def clear @session.delete("attachment") clear_error @attachment = Relay::Attachment.new end |
#clear_error ⇒ void
This method returns an undefined value.
53 54 55 |
# File 'lib/relay/attachment/session.rb', line 53 def clear_error @session.delete("attachment_error") end |
#consume ⇒ Relay::Attachment?
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/relay/attachment/session.rb', line 112 def consume value = @session.delete("attachment") return unless Hash === value @attachment = Relay::Attachment.new( name: value["name"], path: value["path"], type: value["type"] ) @attachment.file? ? @attachment : nil end |
#error ⇒ String?
40 41 42 |
# File 'lib/relay/attachment/session.rb', line 40 def error @session["attachment_error"] end |
#error=(message) ⇒ String
47 48 49 |
# File 'lib/relay/attachment/session.rb', line 47 def error=() @session["attachment_error"] = .to_s end |
#file ⇒ Relay::Attachment
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/relay/attachment/session.rb', line 25 def file @attachment ||= begin value = @session["attachment"] = Relay::Attachment.new( name: value&.[]("name"), path: value&.[]("path"), type: value&.[]("type") ) clear unless .file? end end |
#supported? ⇒ Boolean
69 70 71 |
# File 'lib/relay/attachment/session.rb', line 69 def supported? %w[openai anthropic google].include?(provider) end |
#type_supported?(filename:, type:) ⇒ Boolean
77 78 79 80 81 |
# File 'lib/relay/attachment/session.rb', line 77 def type_supported?(filename:, type:) return false unless supported? return image_upload?(filename, type) || pdf_upload?(filename, type) if provider == "anthropic" true end |
#unsupported_message ⇒ String
85 86 87 88 |
# File 'lib/relay/attachment/session.rb', line 85 def return "#{provider} does not support attachments in Relay yet" unless supported? "#{provider} attachments must be images or PDFs" end |