# Pull Request Template ## Description Routes Enterprise assistant, copilot, FAQ, contact memory, action-classifier, and false-promise detector LLM paths through feature-specific model resolution. `Llm::BaseAiService` now accepts feature/account context and uses `Llm::FeatureRouter` when that context is present, while retaining the installation-model fallback for unmigrated callers. This also adds a `document_faq_generation` feature default for generative FAQ/document content. Linear: https://linear.app/chatwoot/issue/CW-7425/test-new-models Depends on #14840 ## 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/enterprise/services/llm/base_ai_service_spec.rb spec/enterprise/services/captain/copilot/chat_service_spec.rb spec/enterprise/services/captain/llm/assistant_chat_service_spec.rb spec/enterprise/services/captain/llm/faq_generator_service_spec.rb spec/enterprise/services/captain/llm/conversation_faq_service_spec.rb spec/enterprise/services/captain/llm/assistant_action_classifier_service_spec.rb spec/enterprise/services/captain/llm/assistant_false_promise_service_spec.rb spec/enterprise/jobs/captain/conversation/response_builder_job_spec.rb` passed with 112 examples, 0 failures. - `bundle exec rspec spec/models/concerns/captain_featurable_spec.rb spec/models/account_spec.rb spec/controllers/api/v1/accounts/captain/preferences_controller_spec.rb spec/lib/llm/feature_router_spec.rb` passed with 87 examples, 0 failures. - `bundle exec rubocop enterprise/app/services/llm/base_ai_service.rb enterprise/app/services/captain/copilot/chat_service.rb enterprise/app/services/captain/llm/assistant_chat_service.rb enterprise/app/services/captain/llm/faq_generator_service.rb enterprise/app/services/captain/llm/conversation_faq_service.rb enterprise/app/services/captain/llm/contact_notes_service.rb enterprise/app/services/captain/llm/contact_attributes_service.rb enterprise/app/services/captain/llm/assistant_action_classifier_service.rb enterprise/app/services/captain/llm/assistant_false_promise_service.rb spec/enterprise/services/llm/base_ai_service_spec.rb spec/enterprise/services/captain/copilot/chat_service_spec.rb spec/enterprise/services/captain/llm/assistant_chat_service_spec.rb spec/enterprise/services/captain/llm/faq_generator_service_spec.rb spec/enterprise/services/captain/llm/conversation_faq_service_spec.rb spec/enterprise/services/captain/llm/assistant_action_classifier_service_spec.rb spec/enterprise/services/captain/llm/assistant_false_promise_service_spec.rb spec/enterprise/jobs/captain/conversation/response_builder_job_spec.rb` passed with no offenses. - `bundle exec ruby -e "require 'yaml'; config = YAML.load_file('config/llm.yml'); abort('missing document_faq_generation') unless config.dig('features', 'document_faq_generation'); abort('missing default') unless config.dig('features', 'document_faq_generation', 'default'); puts 'llm.yml ok'"` passed. - `git diff --check` passed. ## 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 - [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
219 lines
6.3 KiB
Ruby
219 lines
6.3 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'rails_helper'
|
|
|
|
RSpec.describe Concerns::Agentable do
|
|
let(:dummy_class) do
|
|
Class.new do
|
|
include Concerns::Agentable
|
|
|
|
attr_reader :account
|
|
attr_accessor :temperature
|
|
|
|
def initialize(name: 'Test Agent', temperature: 0.8, account: nil)
|
|
@name = name
|
|
@temperature = temperature
|
|
@account = account
|
|
end
|
|
|
|
def self.name
|
|
'DummyClass'
|
|
end
|
|
|
|
private
|
|
|
|
def agent_name
|
|
@name
|
|
end
|
|
|
|
def prompt_context
|
|
{ base_key: 'base_value' }
|
|
end
|
|
end
|
|
end
|
|
|
|
let(:account) { create(:account) }
|
|
let(:dummy_instance) { dummy_class.new(account: account) }
|
|
let(:mock_agents_agent) { instance_double(Agents::Agent) }
|
|
|
|
before do
|
|
InstallationConfig.where(name: 'CAPTAIN_OPEN_AI_MODEL').destroy_all
|
|
allow(Agents::Agent).to receive(:new).and_return(mock_agents_agent)
|
|
allow(Captain::PromptRenderer).to receive(:render).and_return('rendered_template')
|
|
end
|
|
|
|
describe '#agent' do
|
|
it 'creates an Agents::Agent with correct parameters' do
|
|
expect(Agents::Agent).to receive(:new).with(
|
|
name: 'Test Agent',
|
|
instructions: instance_of(Proc),
|
|
tools: [],
|
|
model: Llm::Models.default_model_for('assistant'),
|
|
temperature: 0.8,
|
|
response_schema: Captain::ResponseSchema
|
|
)
|
|
|
|
dummy_instance.agent
|
|
end
|
|
|
|
it 'converts nil temperature to 0.0' do
|
|
dummy_instance.temperature = nil
|
|
|
|
expect(Agents::Agent).to receive(:new).with(
|
|
hash_including(temperature: 0.0)
|
|
)
|
|
|
|
dummy_instance.agent
|
|
end
|
|
|
|
it 'converts temperature to float' do
|
|
dummy_instance.temperature = '0.5'
|
|
|
|
expect(Agents::Agent).to receive(:new).with(
|
|
hash_including(temperature: 0.5)
|
|
)
|
|
|
|
dummy_instance.agent
|
|
end
|
|
end
|
|
|
|
describe '#agent_instructions' do
|
|
it 'calls Captain::PromptRenderer with base context' do
|
|
expect(Captain::PromptRenderer).to receive(:render).with(
|
|
'dummy_class',
|
|
hash_including(base_key: 'base_value')
|
|
)
|
|
|
|
dummy_instance.agent_instructions
|
|
end
|
|
|
|
it 'merges context state when provided' do
|
|
context_double = instance_double(Agents::RunContext,
|
|
context: {
|
|
state: {
|
|
assistant_config: { 'feature_contact_attributes' => true },
|
|
conversation: { id: 123 },
|
|
contact: { name: 'John' }
|
|
}
|
|
})
|
|
|
|
expected_context = {
|
|
base_key: 'base_value',
|
|
conversation: { id: 123 },
|
|
contact: { name: 'John' },
|
|
campaign: {}
|
|
}
|
|
|
|
expect(Captain::PromptRenderer).to receive(:render).with(
|
|
'dummy_class',
|
|
hash_including(expected_context)
|
|
)
|
|
|
|
dummy_instance.agent_instructions(context_double)
|
|
end
|
|
|
|
it 'merges campaign data from context state' do
|
|
context_double = instance_double(Agents::RunContext,
|
|
context: {
|
|
state: {
|
|
conversation: { id: 123 },
|
|
contact: { name: 'John' },
|
|
campaign: { id: 10, title: 'Summer Sale', message: 'Check it out' }
|
|
}
|
|
})
|
|
|
|
expect(Captain::PromptRenderer).to receive(:render).with(
|
|
'dummy_class',
|
|
hash_including(
|
|
campaign: { id: 10, title: 'Summer Sale', message: 'Check it out' }
|
|
)
|
|
)
|
|
|
|
dummy_instance.agent_instructions(context_double)
|
|
end
|
|
|
|
it 'handles context without state' do
|
|
context_double = instance_double(Agents::RunContext, context: {})
|
|
|
|
expect(Captain::PromptRenderer).to receive(:render).with(
|
|
'dummy_class',
|
|
hash_including(
|
|
base_key: 'base_value',
|
|
conversation: {},
|
|
contact: nil,
|
|
campaign: {}
|
|
)
|
|
)
|
|
|
|
dummy_instance.agent_instructions(context_double)
|
|
end
|
|
end
|
|
|
|
describe '#template_name' do
|
|
it 'returns underscored class name' do
|
|
expect(dummy_instance.send(:template_name)).to eq('dummy_class')
|
|
end
|
|
end
|
|
|
|
describe '#agent_tools' do
|
|
it 'returns empty array by default' do
|
|
expect(dummy_instance.send(:agent_tools)).to eq([])
|
|
end
|
|
end
|
|
|
|
describe '#agent_model' do
|
|
it 'returns the assistant feature default model' do
|
|
expect(dummy_instance.send(:agent_model)).to eq(Llm::Models.default_model_for('assistant'))
|
|
end
|
|
|
|
it 'returns account override model when present' do
|
|
create(:installation_config, name: 'CAPTAIN_OPEN_AI_MODEL', value: 'gpt-4.1-nano')
|
|
account.update!(captain_models: { 'assistant' => 'gpt-5.2' })
|
|
|
|
expect(dummy_instance.send(:agent_model)).to eq('gpt-5.2')
|
|
end
|
|
|
|
it 'returns the installation model when account override is absent' do
|
|
create(:installation_config, name: 'CAPTAIN_OPEN_AI_MODEL', value: 'gpt-4.1-nano')
|
|
|
|
expect(dummy_instance.send(:agent_model)).to eq('gpt-4.1-nano')
|
|
end
|
|
|
|
it 'returns the assistant feature default model when account is nil' do
|
|
agent = dummy_class.new(account: nil)
|
|
|
|
expect(agent.send(:agent_model)).to eq(Llm::Models.default_model_for('assistant'))
|
|
end
|
|
end
|
|
|
|
describe '#agent_response_schema' do
|
|
it 'returns Captain::ResponseSchema' do
|
|
expect(dummy_instance.send(:agent_response_schema)).to eq(Captain::ResponseSchema)
|
|
end
|
|
end
|
|
|
|
describe 'required methods' do
|
|
let(:incomplete_class) do
|
|
Class.new do
|
|
include Concerns::Agentable
|
|
end
|
|
end
|
|
|
|
let(:incomplete_instance) { incomplete_class.new }
|
|
|
|
describe '#agent_name' do
|
|
it 'raises NotImplementedError when not implemented' do
|
|
expect { incomplete_instance.send(:agent_name) }
|
|
.to raise_error(NotImplementedError, /must implement agent_name/)
|
|
end
|
|
end
|
|
|
|
describe '#prompt_context' do
|
|
it 'raises NotImplementedError when not implemented' do
|
|
expect { incomplete_instance.send(:prompt_context) }
|
|
.to raise_error(NotImplementedError, /must implement prompt_context/)
|
|
end
|
|
end
|
|
end
|
|
end
|