Class: Relay::Attachment::Session

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(session:, root:, user: nil, provider: nil) ⇒ Session

Returns a new instance of Session.

Parameters:

  • session (Hash)
  • root (String)
  • user (Object, nil) (defaults to: nil)
  • provider (String, nil) (defaults to: nil)


16
17
18
19
20
21
# File 'lib/relay/attachment/session.rb', line 16

def initialize(session:, root:, user: nil, provider: nil)
  @session = session
  @root = root
  @user = user
  @provider = provider
end

Instance Method Details

#acceptString

Returns:

  • (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

Parameters:

  • io (#read)
  • filename (String)
  • type (String, nil)

Returns:

Raises:

  • (ArgumentError)


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(attachments_dir)
  file = Relay::Attachment.new(
    name: sanitize_filename(filename),
    path: File.join(attachments_dir, "#{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

#clearvoid

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_errorvoid

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

#consumeRelay::Attachment?

Returns:



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

#errorString?

Returns:

  • (String, nil)


40
41
42
# File 'lib/relay/attachment/session.rb', line 40

def error
  @session["attachment_error"]
end

#error=(message) ⇒ String

Parameters:

  • message (String)

Returns:

  • (String)


47
48
49
# File 'lib/relay/attachment/session.rb', line 47

def error=(message)
  @session["attachment_error"] = message.to_s
end

#fileRelay::Attachment

Returns:



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"]
    attachment = Relay::Attachment.new(
      name: value&.[]("name"),
      path: value&.[]("path"),
      type: value&.[]("type")
    )
    clear unless attachment.file?
    attachment
  end
end

#supported?Boolean

Returns:

  • (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

Parameters:

  • filename (String)
  • type (String, nil)

Returns:

  • (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_messageString

Returns:

  • (String)


85
86
87
88
# File 'lib/relay/attachment/session.rb', line 85

def unsupported_message
  return "#{provider} does not support attachments in Relay yet" unless supported?
  "#{provider} attachments must be images or PDFs"
end