Skip to content

Commit fc7a482

Browse files
Add app_url to the config so workers can create OpenAI image URLs (#532)
Co-authored-by: Justin Vallelonga <[email protected]>
1 parent 19fc3cb commit fc7a482

File tree

21 files changed

+160
-74
lines changed

21 files changed

+160
-74
lines changed

.github/workflows/rubyonrails.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ jobs:
2828
env:
2929
RAILS_ENV: test
3030
DATABASE_URL: "postgres://rails:password@localhost:5432/hostedgpt_test"
31+
APP_URL_PROTOCOL: "http"
32+
APP_URL_HOST: "localhost"
33+
APP_URL_PORT: "3000"
3134
steps:
3235
- name: Checkout code
3336
uses: actions/checkout@v4
@@ -68,6 +71,9 @@ jobs:
6871
env:
6972
RAILS_ENV: test
7073
DATABASE_URL: "postgres://rails:password@localhost:5432/hostedgpt_test"
74+
APP_URL_PROTOCOL: "http"
75+
APP_URL_HOST: "localhost"
76+
APP_URL_PORT: "3000"
7177
DISPLAY: "=:99"
7278
CHROME_VERSION: "127.0.6533.119"
7379

Gemfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@ gem "ffi", "~> 1.15.5" # explicitly requiring 15.5 until this is resolved: https
4141
gem "amatch", "~> 0.4.1" # enables fuzzy comparison of strings, a tool uses this
4242
gem "rails_heroicon", "~> 2.2.0"
4343
gem "ruby-openai", "~> 7.0.1"
44-
gem "anthropic", "~> 0.1.0"
44+
gem "anthropic", "~> 0.1.0" # TODO update to the latest version
4545
gem "tiktoken_ruby", "~> 0.0.9"
4646
gem "solid_queue", "~> 1.0.0"
4747
gem "name_of_person"
4848
gem "actioncable-enhanced-postgresql-adapter" # longer paylaods w/ postgresql actioncable
4949
gem "aws-sdk-s3", require: false
5050
gem "postmark-rails"
51+
gem "ostruct"
5152

5253
gem "omniauth", "~> 2.1"
5354
gem "omniauth-google-oauth2", "~> 1.1"

app/models/document.rb

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,26 @@ class Document < ApplicationRecord
1919
validates :purpose, :filename, :bytes, presence: true
2020
validate :file_present
2121

22-
def file_data_url(variant = :large)
23-
return nil if !file.attached?
22+
def has_image?(variant = nil)
23+
if variant.present?
24+
return has_file_variant_processed?(variant)
25+
end
2426

25-
"data:#{file.blob.content_type};base64,#{file_base64(variant)}"
27+
file.attached?
2628
end
2729

28-
def file_base64(variant = :large)
29-
return nil if !file.attached?
30-
wait_for_file_variant_to_process!(variant.to_sym)
31-
32-
file_contents = file.variant(variant.to_sym).processed.download
33-
base64 = Base64.strict_encode64(file_contents)
30+
def image_url(variant, fallback: nil)
31+
return nil unless has_image?
32+
33+
if Rails.application.config.x.app_url.blank?
34+
file_data_url(variant)
35+
elsif has_file_variant_processed?(variant)
36+
fully_processed_url(variant)
37+
elsif fallback.nil?
38+
redirect_to_processed_path(variant)
39+
else
40+
fallback
41+
end
3442
end
3543

3644
def has_file_variant_processed?(variant)
@@ -57,6 +65,15 @@ def redirect_to_processed_path(variant)
5765

5866
private
5967

68+
def file_data_url(variant = :large)
69+
wait_for_file_variant_to_process!(variant)
70+
71+
file_contents = file.variant(variant.to_sym).processed.download
72+
base64_data = Base64.strict_encode64(file_contents)
73+
74+
"data:#{file.blob.content_type};base64,#{base64_data}"
75+
end
76+
6077
def set_default_user
6178
self.user ||= message.conversation.user
6279
end

app/models/message.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ def finished?
4343
(content_text.present? || content_tool_calls.present?)
4444
end
4545

46-
def not_finished?
47-
!finished?
48-
end
46+
def not_finished? = !finished?
4947

5048
private
5149

app/models/message/document_image.rb

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,12 @@ module Message::DocumentImage
88
end
99

1010
def has_document_image?(variant = nil)
11-
has_image = documents.present? && documents.first.file.attached?
12-
if has_image && variant
13-
has_image = documents.first.has_file_variant_processed?(variant)
14-
end
15-
16-
!!has_image
11+
documents.present? && documents.first.has_image?(variant)
1712
end
1813

19-
def document_image_path(variant, fallback: nil)
14+
def document_image_url(variant, fallback: nil)
2015
return nil unless has_document_image?
2116

22-
if documents.first.has_file_variant_processed?(variant)
23-
documents.first.fully_processed_url(variant)
24-
elsif fallback.nil?
25-
documents.first.redirect_to_processed_path(variant)
26-
else
27-
fallback
28-
end
17+
documents.first.image_url(variant, fallback: fallback)
2918
end
3019
end

app/models/setting.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,9 @@ def require_keys!(*keys)
2727
end
2828
end
2929
end
30+
31+
def key_set?(key)
32+
send(key).present?
33+
end
3034
end
3135
end

app/services/ai_backend/open_ai.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def preceding_conversation_messages
9898

9999
content_with_images = [{ type: "text", text: message.content_text }]
100100
content_with_images += message.documents.collect do |document|
101-
{ type: "image_url", image_url: { url: document.file_data_url(:large) }}
101+
{ type: "image_url", image_url: { url: document.image_url(:large) }}
102102
end
103103

104104
{

app/views/messages/_message.html.erb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ end %>
8282
role: "image-preview",
8383
controller: "image-loader",
8484
image_loader_message_scroller_outlet: "[data-role='inner-message']",
85-
image_loader_url_value: message.document_image_path(:small),
85+
image_loader_url_value: message.document_image_url(:small),
8686
action: "modal#open",
8787
} do %>
88-
<%= image_tag message.document_image_path(:small, fallback: ""),
88+
<%= image_tag message.document_image_url(:small, fallback: ""),
8989
class: %|
9090
my-0
9191
mx-auto
@@ -254,10 +254,10 @@ end %>
254254
class="flex flex-col md:flex-row justify-center"
255255
data-controller="image-loader"
256256
data-image-loader-message-scroller-outlet="[data-role='inner-message']"
257-
data-image-loader-url-value="<%= message.document_image_path(:large) %>"
257+
data-image-loader-url-value="<%= message.document_image_url(:large) %>"
258258
data-turbo-permanent
259259
>
260-
<%= image_tag message.document_image_path(:large, fallback: ""),
260+
<%= image_tag message.document_image_url(:large, fallback: ""),
261261
class: "w-full h-auto",
262262
data: {
263263
image_loader_target: "image",

config/application.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@ class Application < Rails::Application
3737
config.time_zone = "Central Time (US & Canada)"
3838
config.eager_load_paths << Rails.root.join("lib")
3939

40+
url_settings = [:app_url_protocol, :app_url_host]
41+
if url_settings.any?{|k| Setting.key_set?(k)}
42+
Setting.require_keys!(*url_settings)
43+
44+
config.x.app_url_protocol = Setting.app_url_protocol
45+
config.x.app_url_host = Setting.app_url_host
46+
47+
port = Setting.app_url_port.to_s
48+
if port.blank? || port == "80"
49+
config.x.app_url_port = nil
50+
else
51+
config.x.app_url_port = Setting.app_url_port
52+
end
53+
54+
port_str = config.x.app_url_port.present? ? ":#{port}" : ""
55+
config.x.app_url = "#{Setting.app_url_protocol}://#{Setting.app_url_host}#{port_str}"
56+
else
57+
config.x.app_url = nil
58+
end
59+
4060
# Active Storage
4161
if Feature.cloudflare_storage?
4262
config.active_storage.service = :cloudflare

config/environments/development.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,11 @@
8585
log_polling = ENV["SOLID_QUEUE_LOG_POLLING_ON"] != "false"
8686
config.solid_queue.silence_polling = log_polling # NOTE: this is backwards, true means silence
8787

88-
config.web_console.permissions = ["192.168.0.0/16", "172.17.0.0/16"]
88+
config.web_console.permissions = ["192.168.0.0/16", "172.17.0.0/16", "172.18.0.0/16"]
8989

90+
# TODO should we combine this with APP_URL_HOST?
9091
config.hosts << ENV["DEV_HOST"] if ENV["DEV_HOST"].present?
92+
config.hosts << Setting.app_url_host if Setting.key_set?(:app_url_host)
9193

9294
stdout_logger = ActiveSupport::Logger.new(STDOUT)
9395
tagged_logger = ActiveSupport::TaggedLogging.new(stdout_logger)

0 commit comments

Comments
 (0)