mirror of
https://github.com/we-promise/sure.git
synced 2026-04-19 03:54:08 +00:00
Add OpenAI token configuration to self-hosting settings (#122)
* feat: add OpenAI settings partial
This commit is contained in:
@@ -3,7 +3,7 @@ class Settings::HostingsController < ApplicationController
|
||||
|
||||
guard_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 < ApplicationController
|
||||
Setting.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 => error
|
||||
flash.now[:alert] = t(".failure")
|
||||
@@ -44,7 +55,7 @@ class Settings::HostingsController < ApplicationController
|
||||
|
||||
private
|
||||
def 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)
|
||||
end
|
||||
|
||||
def ensure_admin
|
||||
|
||||
@@ -88,7 +88,7 @@ class User < ApplicationRecord
|
||||
end
|
||||
|
||||
def 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?
|
||||
end
|
||||
|
||||
def ai_enabled?
|
||||
|
||||
@@ -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>
|
||||
|
||||
|
||||
29
app/views/settings/hostings/_openai_settings.html.erb
Normal file
29
app/views/settings/hostings/_openai_settings.html.erb
Normal file
@@ -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>
|
||||
@@ -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 %>
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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
|
||||
]
|
||||
|
||||
@@ -12,7 +12,7 @@ en:
|
||||
generated_tokens: Generated codes
|
||||
title: Require invite code for signup
|
||||
show:
|
||||
general: General Settings
|
||||
general: External Services
|
||||
invites: Invite Codes
|
||||
title: Self-Hosting
|
||||
danger_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 Fetch
|
||||
label: Client ID
|
||||
placeholder: 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: OpenAI
|
||||
twelve_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 Key
|
||||
placeholder: Enter your API key here
|
||||
plan: "%{plan} plan"
|
||||
title: Twelve Data Settings
|
||||
title: Twelve Data
|
||||
update:
|
||||
failure: Invalid setting value
|
||||
success: Settings updated
|
||||
|
||||
@@ -46,6 +46,14 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTest
|
||||
end
|
||||
end
|
||||
|
||||
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" do
|
||||
account = accounts(:investment)
|
||||
holding = account.holdings.first
|
||||
|
||||
@@ -138,4 +138,18 @@ class UserTest < ActiveSupport::TestCase
|
||||
assert_match %r{secret=#{user.otp_secret}}, user.provisioning_uri
|
||||
assert_match %r{issuer=Maybe}, user.provisioning_uri
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user