The personal finance app for everyone (by everyone)
Ruby
feat(ai): honor Setting.llm_provider for batch and PDF flows (#2265)
Guillem Arias Faustef25fe30a414a
diff --git a/app/models/assistant/function/import_bank_statement.rb b/app/models/assistant/function/import_bank_statement.rbindex d3bf86decc..8920ffad54 100644--- a/app/models/assistant/function/import_bank_statement.rb+++ b/app/models/assistant/function/import_bank_statement.rb@@ -92,15 +92,15 @@ class Assistant::Function::ImportBankStatement < Assistant::Function}end- # Extract transactions from the PDF using provider- # TODO(#2113): hardcoded to OpenAI. Provider::Anthropic implements- # extract_bank_statement (PR #1985); this should honor Setting.llm_provider.- provider = Provider::Registry.get_provider(:openai)+ # Extract transactions from the PDF using the configured LLM provider.+ # Honors Setting.llm_provider (issue #2113) — Provider::Anthropic implements+ # extract_bank_statement (PR #1985).+ provider = Provider::Registry.preferred_llm_providerunless providerreturn {success: false,error: "provider_not_configured",- message: "OpenAI provider is not configured"+ message: "AI provider is not configured"}enddiff --git a/app/models/family/auto_categorizer.rb b/app/models/family/auto_categorizer.rbindex c28cb7eb56..de56e18d50 100644--- a/app/models/family/auto_categorizer.rb+++ b/app/models/family/auto_categorizer.rb@@ -58,13 +58,11 @@ class Family::AutoCategorizerprivateattr_reader :family, :transaction_ids- # TODO(#2113): hardcoded to OpenAI. Provider::Anthropic now- # implements auto_categorize (PR #1984), so this should honor- # Setting.llm_provider the way chat does, instead of always routing batch- # categorization to OpenAI. Until then, Anthropic batch ops are only- # reachable directly / via the eval runner, not this family flow.+ # Honors Setting.llm_provider (issue #2113) — Provider::Anthropic implements+ # auto_categorize (PR #1984), so batch categorization routes to the configured+ # provider, with fallback handled by Provider::Registry.preferred_llm_provider.def llm_provider- Provider::Registry.get_provider(:openai)+ Provider::Registry.preferred_llm_providerenddef user_categories_inputdiff --git a/app/models/family/auto_merchant_detector.rb b/app/models/family/auto_merchant_detector.rbindex e93da64b4a..45ebabcbbb 100644--- a/app/models/family/auto_merchant_detector.rb+++ b/app/models/family/auto_merchant_detector.rb@@ -61,13 +61,12 @@ class Family::AutoMerchantDetectorprivateattr_reader :family, :transaction_ids- # TODO(#2113): hardcoded to OpenAI. Provider::Anthropic now- # implements auto_detect_merchants (PR #1984), so this should honor- # Setting.llm_provider the way chat does, instead of always routing batch- # merchant detection to OpenAI. Until then, Anthropic batch ops are only- # reachable directly / via the eval runner, not this family flow.+ # Honors Setting.llm_provider (issue #2113) — Provider::Anthropic implements+ # auto_detect_merchants (PR #1984), so batch merchant detection routes to the+ # configured provider, with fallback handled by+ # Provider::Registry.preferred_llm_provider.def llm_provider- Provider::Registry.get_provider(:openai)+ Provider::Registry.preferred_llm_providerenddef default_logo_provider_urldiff --git a/app/models/pdf_import.rb b/app/models/pdf_import.rbindex 69c74cb9e0..616b816446 100644--- a/app/models/pdf_import.rb+++ b/app/models/pdf_import.rb@@ -89,9 +89,9 @@ class PdfImport < Importenddef process_with_ai- # TODO(#2113): hardcoded to OpenAI. Provider::Anthropic implements- # process_pdf (PR #1985); this should honor Setting.llm_provider.- provider = Provider::Registry.get_provider(:openai)+ # Honors Setting.llm_provider (issue #2113) — Provider::Anthropic implements+ # process_pdf (PR #1985).+ provider = Provider::Registry.preferred_llm_providerraise "AI provider not configured" unless providerraise "AI provider does not support PDF processing" unless provider.supports_pdf_processing?@@ -117,9 +117,9 @@ class PdfImport < Importdef extract_transactionsreturn unless statement_with_transactions?- # TODO(#2113): hardcoded to OpenAI. Provider::Anthropic implements- # extract_bank_statement (PR #1985); this should honor Setting.llm_provider.- provider = Provider::Registry.get_provider(:openai)+ # Honors Setting.llm_provider (issue #2113) — Provider::Anthropic implements+ # extract_bank_statement (PR #1985).+ provider = Provider::Registry.preferred_llm_providerraise "AI provider not configured" unless providerresponse = provider.extract_bank_statement(diff --git a/app/models/provider/registry.rb b/app/models/provider/registry.rbindex 085c31b501..b573e33a7a 100644--- a/app/models/provider/registry.rb+++ b/app/models/provider/registry.rb@@ -18,6 +18,20 @@ class Provider::Registryraise Error.new("Provider '#{name}' not found in registry")end+ # Resolves the LLM provider for batch/PDF flows, honoring Setting.llm_provider+ # the way chat does: prefer the configured provider, but fall back to whichever+ # one actually has credentials so an install that swaps providers (or has only+ # 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|+ provider = get_provider(name)+ return provider if provider+ end+ nil+ end+def plaid_provider_for_region(region)region.to_sym == :us ? plaid_us : plaid_euenddiff --git a/app/models/provider_merchant/enhancer.rb b/app/models/provider_merchant/enhancer.rbindex e13d141a73..7114e66fd3 100644--- a/app/models/provider_merchant/enhancer.rb+++ b/app/models/provider_merchant/enhancer.rb@@ -70,13 +70,12 @@ class ProviderMerchant::Enhancercountend- # TODO(#2113): hardcoded to OpenAI. Provider::Anthropic now- # implements enhance_provider_merchants (PR #1984), so this should honor- # Setting.llm_provider the way chat does, instead of always routing merchant- # enhancement to OpenAI. Until then, Anthropic batch ops are only reachable- # directly / via the eval runner, not this family flow.+ # Honors Setting.llm_provider (issue #2113) — Provider::Anthropic implements+ # enhance_provider_merchants (PR #1984), so merchant enhancement routes to the+ # configured provider, with fallback handled by+ # Provider::Registry.preferred_llm_provider.def llm_provider- @llm_provider ||= Provider::Registry.get_provider(:openai)+ @llm_provider ||= Provider::Registry.preferred_llm_providerenddef unenhanced_merchantsdiff --git a/test/models/provider/registry_test.rb b/test/models/provider/registry_test.rbindex 2aa274662f..48c3d2fdf8 100644--- a/test/models/provider/registry_test.rb+++ b/test/models/provider/registry_test.rb@@ -107,4 +107,39 @@ class Provider::RegistryTest < ActiveSupport::TestCaseassert_instance_of Provider::Openai, providerendend++ test "preferred_llm_provider returns openai by default" do+ openai = mock("openai")+ Provider::Registry.stubs(:openai).returns(openai)+ Provider::Registry.stubs(:anthropic).returns(mock("anthropic"))+ Setting.stubs(:llm_provider).returns("openai")++ assert_same openai, Provider::Registry.preferred_llm_provider+ end++ test "preferred_llm_provider returns anthropic when selected" do+ anthropic = mock("anthropic")+ Provider::Registry.stubs(:openai).returns(mock("openai"))+ Provider::Registry.stubs(:anthropic).returns(anthropic)+ Setting.stubs(:llm_provider).returns("anthropic")++ assert_same anthropic, Provider::Registry.preferred_llm_provider+ end++ test "preferred_llm_provider falls back to the configured provider when the selected one is unconfigured" do+ openai = mock("openai")+ Provider::Registry.stubs(:openai).returns(openai)+ Provider::Registry.stubs(:anthropic).returns(nil)+ Setting.stubs(:llm_provider).returns("anthropic")++ assert_same openai, Provider::Registry.preferred_llm_provider+ end++ test "preferred_llm_provider returns nil when no provider is configured" do+ Provider::Registry.stubs(:openai).returns(nil)+ Provider::Registry.stubs(:anthropic).returns(nil)+ Setting.stubs(:llm_provider).returns("openai")++ assert_nil Provider::Registry.preferred_llm_provider+ endenddiff --git a/test/models/provider_merchant/enhancer_test.rb b/test/models/provider_merchant/enhancer_test.rbindex e3d717a385..abd92d7e38 100644--- a/test/models/provider_merchant/enhancer_test.rb+++ b/test/models/provider_merchant/enhancer_test.rb@@ -71,7 +71,9 @@ class ProviderMerchant::EnhancerTest < ActiveSupport::TestCaseendtest "returns zero counts when no LLM provider" do+ # No provider configured: preferred_llm_provider checks both before nil.Provider::Registry.stubs(:get_provider).with(:openai).returns(nil)+ Provider::Registry.stubs(:get_provider).with(:anthropic).returns(nil)result = ProviderMerchant::Enhancer.new(@family).enhance