The personal finance app for everyone (by everyone)
Ruby
Add OpenAI token configuration to self-hosting settings (#122)
Juan José Mata5d6915a9943c
diff --git a/app/controllers/settings/hostings_controller.rb b/app/controllers/settings/hostings_controller.rbindex 066e318ef4..a3da5d0f43 100644--- a/app/controllers/settings/hostings_controller.rb+++ b/app/controllers/settings/hostings_controller.rb@@ -3,7 +3,7 @@ class Settings::HostingsController < ApplicationControllerguard_feature unless: -> { self_hosted? }- before_action :ensure_admin, only: :clear_cache+ before_action :ensure_admin, only: [ :update, :clear_cache ]def show@breadcrumbs = [@@ -31,6 +31,17 @@ class Settings::HostingsController < ApplicationControllerSetting.twelve_data_api_key = hosting_params[:twelve_data_api_key]end+ if hosting_params.key?(:openai_access_token)+ Setting.openai_access_token = hosting_params[:openai_access_token]+ end+ if hosting_params.key?(:openai_access_token)+ token_param = hosting_params[:openai_access_token].to_s.strip+ # Ignore blanks and redaction placeholders to prevent accidental overwrite+ unless token_param.blank? || token_param == "********"+ Setting.openai_access_token = token_param+ end+ end+redirect_to settings_hosting_path, notice: t(".success")rescue ActiveRecord::RecordInvalid => errorflash.now[:alert] = t(".failure")@@ -44,7 +55,7 @@ class Settings::HostingsController < ApplicationControllerprivatedef hosting_params- params.require(:setting).permit(:require_invite_for_signup, :require_email_confirmation, :brand_fetch_client_id, :twelve_data_api_key)+ params.require(:setting).permit(:require_invite_for_signup, :require_email_confirmation, :brand_fetch_client_id, :twelve_data_api_key, :openai_access_token)enddef ensure_admindiff --git a/app/models/user.rb b/app/models/user.rbindex 901a5eca6b..eaa7b08ebf 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -88,7 +88,7 @@ class User < ApplicationRecordenddef ai_available?- !Rails.application.config.app_mode.self_hosted? || ENV["OPENAI_ACCESS_TOKEN"].present?+ !Rails.application.config.app_mode.self_hosted? || ENV["OPENAI_ACCESS_TOKEN"].present? || Setting.openai_access_token.present?enddef ai_enabled?diff --git a/app/views/chats/_ai_consent.html.erb b/app/views/chats/_ai_consent.html.erbindex e96d257073..b19b8bc9fe 100644--- a/app/views/chats/_ai_consent.html.erb+++ b/app/views/chats/_ai_consent.html.erb@@ -10,7 +10,7 @@Maybe AI can answer financial questions and provide insights based on your data. To use this feature you'll need to explicitly enable it.<% else %>To use the AI assistant, you need to set the <code class="bg-surface-inset px-1 py-0.5 rounded font-mono text-xs">OPENAI_ACCESS_TOKEN</code>- environment variable in your self-hosted instance.+ environment variable or configure it in the Self-Hosting settings of your instance.<% end %></p>diff --git a/app/views/settings/hostings/_openai_settings.html.erb b/app/views/settings/hostings/_openai_settings.html.erbnew file mode 100644index 0000000000..0fcdfaedd4--- /dev/null+++ b/app/views/settings/hostings/_openai_settings.html.erb@@ -0,0 +1,29 @@+<div class="space-y-4">+ <div>+ <h2 class="font-medium mb-1"><%= t(".title") %></h2>+ <% if ENV["OPENAI_ACCESS_TOKEN"].present? %>+ <p class="text-sm text-secondary"><%= t(".env_configured_message") %></p>+ <% else %>+ <p class="text-secondary text-sm mb-4"><%= t(".description") %></p>+ <% end %>+ </div>++ <%= styled_form_with model: Setting.new,+ url: settings_hosting_path,+ method: :patch,+ data: {+ controller: "auto-submit-form",+ "auto-submit-form-trigger-event-value": "blur"+ } do |form| %>+ <%= form.password_field :openai_access_token,+ label: t(".label"),+ placeholder: t(".placeholder"),+ value: (Setting.openai_access_token.present? ? "********" : nil),+ autocomplete: "off",+ autocapitalize: "none",+ spellcheck: "false",+ inputmode: "text",+ disabled: ENV["OPENAI_ACCESS_TOKEN"].present?,+ data: { "auto-submit-form-target": "auto" } %>+ <% end %>+</div>diff --git a/app/views/settings/hostings/_twelve_data_settings.html.erb b/app/views/settings/hostings/_twelve_data_settings.html.erbindex 1bf4862c95..4559f65219 100644--- a/app/views/settings/hostings/_twelve_data_settings.html.erb+++ b/app/views/settings/hostings/_twelve_data_settings.html.erb@@ -2,7 +2,7 @@<div><h2 class="font-medium mb-1"><%= t(".title") %></h2><% if ENV["TWELVE_DATA_API_KEY"].present? %>- <p class="text-sm text-secondary">You have successfully configured your Twelve Data API key through the TWELVE_DATA_API_KEY environment variable.</p>+ <p class="text-sm text-secondary"><%= t(".env_configured_message") %></p><% else %><p class="text-secondary text-sm mb-4"><%= t(".description") %></p><% end %>diff --git a/app/views/settings/hostings/show.html.erb b/app/views/settings/hostings/show.html.erbindex 0511c46b1d..077cd445af 100644--- a/app/views/settings/hostings/show.html.erb+++ b/app/views/settings/hostings/show.html.erb@@ -1,7 +1,8 @@<%= content_for :page_title, t(".title") %><%= settings_section title: t(".general") do %>- <div class="space-y-6">+ <div class="space-y-6">+ <%= render "settings/hostings/openai_settings" %><%= render "settings/hostings/brand_fetch_settings" %><%= render "settings/hostings/twelve_data_settings" %></div>diff --git a/config/initializers/filter_parameter_logging.rb b/config/initializers/filter_parameter_logging.rbindex c010b83ddd..1392b6612d 100644--- a/config/initializers/filter_parameter_logging.rb+++ b/config/initializers/filter_parameter_logging.rb@@ -4,5 +4,5 @@# Use this to limit dissemination of sensitive information.# See the ActiveSupport::ParameterFilter documentation for supported notations and behaviors.Rails.application.config.filter_parameters += [- :passw, :email, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn+ :passw, :email, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn, :openai_access_token]diff --git a/config/locales/views/settings/hostings/en.yml b/config/locales/views/settings/hostings/en.ymlindex b7564928d1..1c3f8a0863 100644--- a/config/locales/views/settings/hostings/en.yml+++ b/config/locales/views/settings/hostings/en.yml@@ -12,7 +12,7 @@ en:generated_tokens: Generated codestitle: Require invite code for signupshow:- general: General Settings+ general: External Servicesinvites: Invite Codestitle: Self-Hostingdanger_zone: Danger Zone@@ -22,17 +22,24 @@ en:title: Clear data cache?body: Are you sure you want to clear the data cache? This will remove all exchange rates, security prices, account balances, and other data. This action cannot be undone.brand_fetch_settings:- description: Input the Client ID provided by Brand Fetch+ description: Enter the Client ID provided by Brand Fetchlabel: Client IDplaceholder: Enter your Client ID here- title: Brand Fetch Settings+ title: Brand Fetch+ openai_settings:+ description: Enter the access token provided by OpenAI+ env_configured_message: Successfully configured through the OPENAI_ACCESS_TOKEN environment variable.+ label: Access Token+ placeholder: Enter your access token here+ title: OpenAItwelve_data_settings:api_calls_used: "%{used} / %{limit} API daily calls used (%{percentage})"- description: Input the API key provided by Twelve Data+ description: Enter the API key provided by Twelve Data+ env_configured_message: Successfully configured through the TWELVE_DATA_API_KEY environment variable.label: API Keyplaceholder: Enter your API key hereplan: "%{plan} plan"- title: Twelve Data Settings+ title: Twelve Dataupdate:failure: Invalid setting valuesuccess: Settings updateddiff --git a/test/controllers/settings/hostings_controller_test.rb b/test/controllers/settings/hostings_controller_test.rbindex cb18866f8b..72d92847c7 100644--- a/test/controllers/settings/hostings_controller_test.rb+++ b/test/controllers/settings/hostings_controller_test.rb@@ -46,6 +46,14 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTestendend+ test "can update openai access token when self hosting is enabled" do+ with_self_hosting do+ patch settings_hosting_url, params: { setting: { openai_access_token: "token" } }++ assert_equal "token", Setting.openai_access_token+ end+ end+test "can clear data cache when self hosting is enabled" doaccount = accounts(:investment)holding = account.holdings.firstdiff --git a/test/models/user_test.rb b/test/models/user_test.rbindex 85e3c010e9..47c51895da 100644--- a/test/models/user_test.rb+++ b/test/models/user_test.rb@@ -138,4 +138,18 @@ class UserTest < ActiveSupport::TestCaseassert_match %r{secret=#{user.otp_secret}}, user.provisioning_uriassert_match %r{issuer=Maybe}, user.provisioning_uriend++ test "ai_available? returns true when openai access token set in settings" do+ Rails.application.config.app_mode.stubs(:self_hosted?).returns(true)+ previous = Setting.openai_access_token+ with_env_overrides OPENAI_ACCESS_TOKEN: nil do+ Setting.openai_access_token = nil+ assert_not @user.ai_available?++ Setting.openai_access_token = "token"+ assert @user.ai_available?+ end+ ensure+ Setting.openai_access_token = previous+ endend