## Description Adds the foundation for feature-specific LLM model routing so Captain AI features can resolve their effective provider/model from code defaults and account-level overrides. This fixes the provider metadata key in `config/llm.yml`, adds `Llm::FeatureRouter`, and routes existing `CaptainFeaturable` model defaults through the shared resolver. Fixes https://linear.app/chatwoot/issue/CW-7425/test-new-models ## Type of change - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality not to work as expected) - [ ] This change requires a documentation update ## How Has This Been Tested? - `bundle exec rspec spec/lib/llm/models_spec.rb spec/lib/llm/feature_router_spec.rb spec/models/concerns/captain_featurable_spec.rb` - 23 examples, 0 failures - `bundle exec rubocop lib/llm/models.rb lib/llm/feature_router.rb app/models/concerns/captain_featurable.rb spec/lib/llm/models_spec.rb spec/lib/llm/feature_router_spec.rb spec/models/concerns/captain_featurable_spec.rb` - no offenses - `bundle exec ruby -e "require 'yaml'; config = YAML.load_file('config/llm.yml'); abort('missing providers') unless config['providers']; abort('missing models') unless config['models']; abort('missing features') unless config['features']; puts 'llm.yml ok'"` - `git diff --check` ## Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my code - [ ] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules
61 lines
1.6 KiB
Ruby
61 lines
1.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'rails_helper'
|
|
|
|
RSpec.describe Llm::FeatureRouter do
|
|
let(:account) { create(:account) }
|
|
|
|
describe '.resolve' do
|
|
it 'returns the feature default without an account' do
|
|
resolved = described_class.resolve(feature: 'editor')
|
|
|
|
expect(resolved).to eq(
|
|
feature: 'editor',
|
|
provider: 'openai',
|
|
model: 'gpt-4.1-mini',
|
|
source: :default
|
|
)
|
|
end
|
|
|
|
it 'uses a valid account model override' do
|
|
account.update!(captain_models: { 'editor' => 'gpt-4.1' })
|
|
|
|
resolved = described_class.resolve(feature: 'editor', account: account)
|
|
|
|
expect(resolved).to include(
|
|
feature: 'editor',
|
|
provider: 'openai',
|
|
model: 'gpt-4.1',
|
|
source: :account_override
|
|
)
|
|
end
|
|
|
|
it 'falls back to the feature default when the account override is invalid' do
|
|
account.captain_models = { 'editor' => 'invalid-model' }
|
|
|
|
resolved = described_class.resolve(feature: 'editor', account: account)
|
|
|
|
expect(resolved).to include(
|
|
model: 'gpt-4.1-mini',
|
|
source: :default
|
|
)
|
|
end
|
|
|
|
it 'falls back to the feature default when the account override is blank' do
|
|
account.update!(captain_models: { 'editor' => '' })
|
|
|
|
resolved = described_class.resolve(feature: 'editor', account: account)
|
|
|
|
expect(resolved).to include(
|
|
model: 'gpt-4.1-mini',
|
|
source: :default
|
|
)
|
|
end
|
|
|
|
it 'raises for unknown features' do
|
|
expect { described_class.resolve(feature: 'unknown_feature') }
|
|
.to raise_error(described_class::UnknownFeatureError, 'Unknown LLM feature: unknown_feature')
|
|
end
|
|
end
|
|
end
|