Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Treat blank AI settings variables as unset (#5379)

jezmor12e17d0db3f3
diff --git a/app/models/setting.rb b/app/models/setting.rb
index d957be4c8a..6454ecdab5 100644
--- a/app/models/setting.rb
+++ b/app/models/setting.rb
@@ -4,13 +4,19 @@ class Setting < RailsSettings::Base
cache_prefix { "v1" }
+ # Compose files pass unset variables through as "", which `to_i` would turn
+ # into 0 and show in number fields whose minimum is above 0.
+ def self.integer_from_env(name)
+ ENV[name].to_s.strip.presence&.to_i
+ end
+
# Third-party API keys
field :twelve_data_api_key, type: :string, default: ENV["TWELVE_DATA_API_KEY"]
field :openai_access_token, type: :string, default: ENV["OPENAI_ACCESS_TOKEN"]
field :openai_uri_base, type: :string, default: ENV["OPENAI_URI_BASE"]
field :openai_model, type: :string, default: ENV["OPENAI_MODEL"]
field :openai_json_mode, type: :string, default: ENV["LLM_JSON_MODE"]
- field :openai_request_timeout, type: :integer, default: ENV["OPENAI_REQUEST_TIMEOUT"]&.to_i
+ field :openai_request_timeout, type: :integer, default: integer_from_env("OPENAI_REQUEST_TIMEOUT")
field :anthropic_access_token, type: :string, default: ENV["ANTHROPIC_ACCESS_TOKEN"].presence || ENV["ANTHROPIC_API_KEY"].presence
field :anthropic_model, type: :string, default: ENV["ANTHROPIC_MODEL"]
field :anthropic_base_url, type: :string, default: ENV["ANTHROPIC_BASE_URL"]
@@ -28,21 +34,21 @@ class Setting < RailsSettings::Base
# merchant detection, enhance-merchants, PDF processing). Defaults track
# Ollama's historical 2048-token baseline so local small-context models work
# out of the box. ENV overrides Setting at read time in Provider::Openai.
- field :llm_context_window, type: :integer, default: ENV["LLM_CONTEXT_WINDOW"]&.to_i
- field :llm_max_response_tokens, type: :integer, default: ENV["LLM_MAX_RESPONSE_TOKENS"]&.to_i
- field :llm_max_items_per_call, type: :integer, default: ENV["LLM_MAX_ITEMS_PER_CALL"]&.to_i
+ field :llm_context_window, type: :integer, default: integer_from_env("LLM_CONTEXT_WINDOW")
+ field :llm_max_response_tokens, type: :integer, default: integer_from_env("LLM_MAX_RESPONSE_TOKENS")
+ field :llm_max_items_per_call, type: :integer, default: integer_from_env("LLM_MAX_ITEMS_PER_CALL")
# How long the chat UI waits for an assistant response before treating it as
# undelivered. Self-hosted users running local models on slow hardware need
# this well above the 90s default — a local model that takes minutes to
# generate would otherwise always trip the watchdog. Read via
# `Chat.response_timeout`, which applies ENV > Setting > default precedence.
- field :ai_response_timeout, type: :integer, default: ENV["AI_RESPONSE_TIMEOUT"]&.to_i
+ field :ai_response_timeout, type: :integer, default: integer_from_env("AI_RESPONSE_TIMEOUT")
# Per-request bound for the admin "Run checks" AI probes on System Health.
# The PDF probes do a full extraction round trip, which cloud models often
# can't finish inside the 5s default. Read via `AiHealth::Probe.timeout`,
# which applies ENV > Setting > default precedence.
- field :ai_health_probe_timeout, type: :integer, default: ENV["AI_HEALTH_PROBE_TIMEOUT"]&.to_i
+ field :ai_health_probe_timeout, type: :integer, default: integer_from_env("AI_HEALTH_PROBE_TIMEOUT")
field :external_assistant_url, type: :string
field :external_assistant_token, type: :string
field :external_assistant_model, type: :string
diff --git a/test/models/setting_test.rb b/test/models/setting_test.rb
index 238d87a029..96c720eb45 100644
--- a/test/models/setting_test.rb
+++ b/test/models/setting_test.rb
@@ -195,6 +195,20 @@ class SettingTest < ActiveSupport::TestCase
assert_nil Setting.transform_brand_fetch_url(nil)
end
+ test "integer_from_env treats a blank variable as unset" do
+ with_env_overrides("LLM_CONTEXT_WINDOW" => "") do
+ assert_nil Setting.integer_from_env("LLM_CONTEXT_WINDOW")
+ end
+
+ with_env_overrides("LLM_CONTEXT_WINDOW" => " 8192 ") do
+ assert_equal 8192, Setting.integer_from_env("LLM_CONTEXT_WINDOW")
+ end
+
+ with_env_overrides("LLM_CONTEXT_WINDOW" => nil) do
+ assert_nil Setting.integer_from_env("LLM_CONTEXT_WINDOW")
+ end
+ end
+
test "LLM_PROVIDER env wins over a stored llm_provider" do
original = Setting.raw_llm_provider
Setting.llm_provider = "anthropic"