Files
chatwoot/enterprise/app/fields/captain_model_overrides_field.rb
Sony MathewandGitHub b8b62ad0f1 feat: Harden model override preferences (5/6) (#14846)
## Description

Hardens the Captain model override preferences API so account-level
overrides follow the same feature-router contract used by runtime LLM
calls. The API now permits model and feature keys from `llm.yml`,
removes blank model overrides, rejects invalid saved model combinations,
and returns each feature's effective model, provider, and source for UI
clients.

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?

Verified the account preferences API and account model validation
behavior for valid overrides, invalid model values, unknown feature
keys, blank override removal, and effective model/provider/source
payload metadata.

- `eval "$(rbenv init -)" && bundle exec rspec
spec/controllers/api/v1/accounts/captain/preferences_controller_spec.rb
spec/models/account_spec.rb
spec/models/concerns/captain_featurable_spec.rb
spec/lib/llm/feature_router_spec.rb`
- `eval "$(rbenv init -)" && bundle exec rubocop
app/controllers/api/v1/accounts/captain/preferences_controller.rb
app/models/concerns/account_settings_schema.rb
app/models/concerns/captain_featurable.rb
spec/controllers/api/v1/accounts/captain/preferences_controller_spec.rb
spec/models/account_spec.rb
spec/models/concerns/captain_featurable_spec.rb
spec/lib/llm/feature_router_spec.rb`
- `git diff --check`

## Checklist:

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my code
- [x] I have commented on my code, particularly in hard-to-understand
areas
- [ ] I have made corresponding changes to the documentation
- [ ] 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
2026-06-25 17:38:11 +05:30

57 lines
1.6 KiB
Ruby

require 'administrate/field/base'
class CaptainModelOverridesField < Administrate::Field::Base
def feature_rows
Llm::Models.feature_keys.map do |feature_key|
route = Llm::FeatureRouter.resolve(feature: feature_key, account: resource)
{
key: feature_key,
name: feature_name(feature_key),
provider: provider_label(route[:provider]),
provider_id: route[:provider],
model: model_label(route[:model]),
model_id: route[:model],
default_model: model_label(default_model_id(feature_key)),
default_model_id: default_model_id(feature_key),
source: route[:source],
source_label: source_label(route[:source]),
selected_override: selected_override(feature_key),
options: model_options(feature_key)
}
end
end
private
def selected_override(feature_key)
resource.captain_models&.[](feature_key).presence
end
def default_model_id(feature_key)
Llm::Models.default_model_for(feature_key)
end
def model_options(feature_key)
Llm::Models.feature_config(feature_key)[:models].map do |model|
[model[:display_name] || model[:id], model[:id]]
end
end
def model_label(model_id)
Llm::Models.model_config(model_id)&.dig('display_name') || model_id
end
def provider_label(provider_id)
Llm::Models.providers.dig(provider_id, 'display_name') || provider_id
end
def feature_name(feature_key)
I18n.t("super_admin.captain_model_overrides.features.#{feature_key}", default: feature_key.humanize)
end
def source_label(source)
I18n.t("super_admin.captain_model_overrides.sources.#{source}")
end
end