Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(ai): route chat to the provider selected by LLM_PROVIDER first (#4244)

Josh Sandersf7d0be360f66
diff --git a/app/models/assistant/provided.rb b/app/models/assistant/provided.rb
index f33067c6ef..ba4ea649a6 100644
--- a/app/models/assistant/provided.rb
+++ b/app/models/assistant/provided.rb
@@ -2,7 +2,11 @@ module Assistant::Provided
extend ActiveSupport::Concern
def get_model_provider(ai_model)
- registry.providers.find { |provider| provider.supports_model?(ai_model) }
+ Provider::Registry.llm_provider_order.each do |name|
+ provider = Provider::Registry.get_provider(name)
+ return provider if provider&.supports_model?(ai_model)
+ end
+ nil
end
private
diff --git a/app/models/provider/registry.rb b/app/models/provider/registry.rb
index 6ffdcc730c..ec1a198bcf 100644
--- a/app/models/provider/registry.rb
+++ b/app/models/provider/registry.rb
@@ -24,14 +24,17 @@ class Provider::Registry
# one configured) keeps working. Returns nil when neither is configured —
# callers guard on that.
def preferred_llm_provider
- order = Setting.llm_provider == "anthropic" ? %i[anthropic openai] : %i[openai anthropic]
- order.each do |name|
+ llm_provider_order.each do |name|
provider = get_provider(name)
return provider if provider
end
nil
end
+ def llm_provider_order
+ Setting.llm_provider == "anthropic" ? %i[anthropic openai] : %i[openai anthropic]
+ end
+
def plaid_provider_for_region(region)
region.to_sym == :us ? plaid_us : plaid_eu
end
diff --git a/test/models/assistant/provided_test.rb b/test/models/assistant/provided_test.rb
new file mode 100644
index 0000000000..64340d60aa
--- /dev/null
+++ b/test/models/assistant/provided_test.rb
@@ -0,0 +1,47 @@
+require "test_helper"
+
+class Assistant::ProvidedTest < ActiveSupport::TestCase
+ setup do
+ @assistant = Assistant::Builtin.for_chat(chats(:two))
+ @custom_openai = mock("custom_openai")
+ @custom_openai.stubs(:supports_model?).returns(true)
+ @anthropic = mock("anthropic")
+ @anthropic.stubs(:supports_model?).with("claude-sonnet-4-6").returns(true)
+ @anthropic.stubs(:supports_model?).with("gpt-4.1").returns(false)
+ Provider::Registry.stubs(:openai).returns(@custom_openai)
+ Provider::Registry.stubs(:anthropic).returns(@anthropic)
+ end
+
+ test "routes to the selected provider before a custom OpenAI endpoint that accepts any model" do
+ Setting.stubs(:llm_provider).returns("anthropic")
+
+ assert_same @anthropic, @assistant.get_model_provider("claude-sonnet-4-6")
+ end
+
+ test "falls back to the other provider when the selected one doesn't support the model" do
+ Setting.stubs(:llm_provider).returns("anthropic")
+
+ assert_same @custom_openai, @assistant.get_model_provider("gpt-4.1")
+ end
+
+ test "builds each provider once when falling back" do
+ Setting.stubs(:llm_provider).returns("anthropic")
+ Provider::Registry.expects(:anthropic).once.returns(@anthropic)
+ Provider::Registry.expects(:openai).once.returns(@custom_openai)
+
+ assert_same @custom_openai, @assistant.get_model_provider("gpt-4.1")
+ end
+
+ test "keeps OpenAI first when it is selected" do
+ Setting.stubs(:llm_provider).returns("openai")
+
+ assert_same @custom_openai, @assistant.get_model_provider("claude-sonnet-4-6")
+ end
+
+ test "returns nil when no configured provider supports the model" do
+ Setting.stubs(:llm_provider).returns("anthropic")
+ Provider::Registry.stubs(:openai).returns(nil)
+
+ assert_nil @assistant.get_model_provider("gpt-4.1")
+ end
+end