feat(ee): Add Captain features (#10665)

Migration Guide: https://chwt.app/v4/migration

This PR imports all the work related to Captain into the EE codebase. Captain represents the AI-based features in Chatwoot and includes the following key components:

- Assistant: An assistant has a persona, the product it would be trained on. At the moment, the data at which it is trained is from websites. Future integrations on Notion documents, PDF etc. This PR enables connecting an assistant to an inbox. The assistant would run the conversation every time before transferring it to an agent.
- Copilot for Agents: When an agent is supporting a customer, we will be able to offer additional help to lookup some data or fetch information from integrations etc via copilot.
- Conversation FAQ generator: When a conversation is resolved, the Captain integration would identify questions which were not in the knowledge base.
- CRM memory: Learns from the conversations and identifies important information about the contact.

---------

Co-authored-by: Vishnu Narayanan <vishnu@chatwoot.com>
Co-authored-by: Sojan <sojan@pepalo.com>
Co-authored-by: iamsivin <iamsivin@gmail.com>
Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>
This commit is contained in:
Pranav
2025-01-14 16:15:47 -08:00
committed by GitHub
parent 7b31b5ad6e
commit d070743383
184 changed files with 6666 additions and 2242 deletions

View File

@@ -0,0 +1,138 @@
require 'rails_helper'
RSpec.describe Captain::Llm::ConversationFaqService do
let(:captain_assistant) { create(:captain_assistant) }
let(:conversation) { create(:conversation) }
let(:service) { described_class.new(captain_assistant, conversation) }
let(:client) { instance_double(OpenAI::Client) }
let(:embedding_service) { instance_double(Captain::Llm::EmbeddingService) }
before do
create(:installation_config) { create(:installation_config, name: 'CAPTAIN_OPEN_AI_API_KEY', value: 'test-key') }
allow(OpenAI::Client).to receive(:new).and_return(client)
allow(Captain::Llm::EmbeddingService).to receive(:new).and_return(embedding_service)
end
describe '#generate_and_deduplicate' do
let(:sample_faqs) do
[
{ 'question' => 'What is the purpose?', 'answer' => 'To help users.' },
{ 'question' => 'How does it work?', 'answer' => 'Through AI.' }
]
end
let(:openai_response) do
{
'choices' => [
{
'message' => {
'content' => { faqs: sample_faqs }.to_json
}
}
]
}
end
context 'when successful' do
before do
allow(client).to receive(:chat).and_return(openai_response)
allow(embedding_service).to receive(:get_embedding).and_return([0.1, 0.2, 0.3])
allow(captain_assistant.responses).to receive(:nearest_neighbors).and_return([])
end
it 'creates new FAQs' do
expect do
service.generate_and_deduplicate
end.to change(captain_assistant.responses, :count).by(2)
end
it 'saves the correct FAQ content' do
service.generate_and_deduplicate
expect(captain_assistant.responses.pluck(:question,
:answer)).to contain_exactly(['What is the purpose?', 'To help users.'],
['How does it work?', 'Through AI.'])
end
end
context 'when finding duplicates' do
let(:existing_response) do
create(:captain_assistant_response, assistant: captain_assistant, question: 'Similar question', answer: 'Similar answer')
end
let(:similar_neighbor) do
# Using OpenStruct here to mock as the Captain:AssistantResponse does not implement
# neighbor_distance as a method or attribute rather it is returned directly
# from SQL query in neighbor gem
OpenStruct.new(
id: 1,
question: existing_response.question,
answer: existing_response.answer,
neighbor_distance: 0.1
)
end
before do
allow(client).to receive(:chat).and_return(openai_response)
allow(embedding_service).to receive(:get_embedding).and_return([0.1, 0.2, 0.3])
allow(captain_assistant.responses).to receive(:nearest_neighbors).and_return([similar_neighbor])
end
it 'filters out duplicate FAQs' do
expect do
service.generate_and_deduplicate
end.not_to change(captain_assistant.responses, :count)
end
end
context 'when OpenAI API fails' do
before do
allow(client).to receive(:chat).and_raise(OpenAI::Error.new('API Error'))
end
it 'handles the error and returns empty array' do
expect(Rails.logger).to receive(:error).with('OpenAI API Error: API Error')
expect(service.generate_and_deduplicate).to eq([])
end
end
context 'when JSON parsing fails' do
let(:invalid_response) do
{
'choices' => [
{
'message' => {
'content' => 'invalid json'
}
}
]
}
end
before do
allow(client).to receive(:chat).and_return(invalid_response)
end
it 'handles JSON parsing errors' do
expect(Rails.logger).to receive(:error).with(/Error in parsing GPT processed response:/)
expect(service.generate_and_deduplicate).to eq([])
end
end
end
describe '#chat_parameters' do
it 'includes correct model and response format' do
params = service.send(:chat_parameters)
expect(params[:model]).to eq('gpt-4o-mini')
expect(params[:response_format]).to eq({ type: 'json_object' })
end
it 'includes system prompt and conversation content' do
allow(Captain::Llm::SystemPromptsService).to receive(:conversation_faq_generator).and_return('system prompt')
params = service.send(:chat_parameters)
expect(params[:messages]).to include(
{ role: 'system', content: 'system prompt' },
{ role: 'user', content: conversation.to_llm_text }
)
end
end
end

View File

@@ -0,0 +1,128 @@
require 'rails_helper'
RSpec.describe Captain::Tools::SimplePageCrawlService do
let(:base_url) { 'https://example.com' }
let(:service) { described_class.new(base_url) }
before do
WebMock.disable_net_connect!
end
after do
WebMock.allow_net_connect!
end
describe '#page_title' do
context 'when title exists' do
before do
stub_request(:get, base_url)
.to_return(body: '<html><head><title>Example Page</title></head></html>')
end
it 'returns the page title' do
expect(service.page_title).to eq('Example Page')
end
end
context 'when title does not exist' do
before do
stub_request(:get, base_url)
.to_return(body: '<html><head></head></html>')
end
it 'returns nil' do
expect(service.page_title).to be_nil
end
end
end
describe '#page_links' do
context 'with HTML page' do
let(:html_content) do
<<~HTML
<html>
<body>
<a href="/relative">Relative Link</a>
<a href="https://external.com">External Link</a>
<a href="#anchor">Anchor Link</a>
</body>
</html>
HTML
end
before do
stub_request(:get, base_url).to_return(body: html_content)
end
it 'extracts and absolutizes all links' do
links = service.page_links
expect(links).to include(
'https://example.com/relative',
'https://external.com',
'https://example.com#anchor'
)
end
end
context 'with sitemap XML' do
let(:sitemap_url) { 'https://example.com/sitemap.xml' }
let(:sitemap_service) { described_class.new(sitemap_url) }
let(:sitemap_content) do
<<~XML
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://example.com/page1</loc>
</url>
<url>
<loc>https://example.com/page2</loc>
</url>
</urlset>
XML
end
before do
stub_request(:get, sitemap_url).to_return(body: sitemap_content)
end
it 'extracts links from sitemap' do
links = sitemap_service.page_links
expect(links).to contain_exactly(
'https://example.com/page1',
'https://example.com/page2'
)
end
end
end
describe '#body_text_content' do
let(:html_content) do
<<~HTML
<html>
<body>
<h1>Main Title</h1>
<p>Some <strong>formatted</strong> content.</p>
<ul>
<li>List item 1</li>
<li>List item 2</li>
</ul>
</body>
</html>
HTML
end
before do
stub_request(:get, base_url).to_return(body: html_content)
allow(ReverseMarkdown).to receive(:convert).and_return("# Main Title\n\nConverted markdown")
end
it 'converts body content to markdown' do
expect(service.body_text_content).to eq("# Main Title\n\nConverted markdown")
expect(ReverseMarkdown).to have_received(:convert).with(
kind_of(Nokogiri::XML::Element),
unknown_tags: :bypass,
github_flavored: true
)
end
end
end

View File

@@ -1,108 +0,0 @@
require 'rails_helper'
RSpec.describe Enterprise::MessageTemplates::ResponseBotService, type: :service do
let!(:conversation) { create(:conversation, status: :pending) }
let(:service) { described_class.new(conversation: conversation) }
let(:chat_gpt_double) { instance_double(ChatGpt) }
let(:response_source) { create(:response_source, account: conversation.account) }
let(:response_object) { instance_double(Response, id: 1, question: 'Q1', answer: 'A1') }
before do
skip_unless_response_bot_enabled_test_environment
stub_request(:post, 'https://api.openai.com/v1/embeddings').to_return(status: 200, body: {}.to_json,
headers: { Content_Type: 'application/json' })
create(:message, message_type: :incoming, conversation: conversation, content: 'Hi')
create(:message, message_type: :outgoing, conversation: conversation, content: 'Hello')
4.times { create(:response, response_source: response_source) }
allow(ChatGpt).to receive(:new).and_return(chat_gpt_double)
allow(chat_gpt_double).to receive(:generate_response).and_return({ 'response' => 'some_response', 'context_ids' => Response.all.map(&:id) })
allow(conversation.inbox).to receive(:get_responses).with('Hi').and_return([response_object])
end
describe '#perform' do
context 'when successful' do
it 'creates an outgoing message along with article references' do
expect do
service.perform
end.to change { conversation.messages.where(message_type: :outgoing).count }.by(1)
last_message = conversation.messages.last
expect(last_message.content).to include('some_response')
expect(last_message.content).to include(Response.first.question)
expect(last_message.content).to include('**Sources**')
end
it 'hands off the conversation if the response is handoff' do
allow(chat_gpt_double).to receive(:generate_response).and_return({ 'response' => 'conversation_handoff' })
expect(conversation).to receive(:bot_handoff!).and_call_original
expect do
service.perform
end.to change { conversation.messages.where(message_type: :outgoing).count }.by(1)
last_message = conversation.messages.last
expect(last_message.content).to eq('Transferring to another agent for further assistance.')
expect(conversation.status).to eq('open')
end
end
context 'when context_ids are not present' do
it 'creates an outgoing message without article references' do
allow(chat_gpt_double).to receive(:generate_response).and_return({ 'response' => 'some_response' })
expect do
service.perform
end.to change { conversation.messages.where(message_type: :outgoing).count }.by(1)
last_message = conversation.messages.last
expect(last_message.content).to include('some_response')
expect(last_message.content).not_to include('**Sources**')
end
end
context 'when response doesnt have response document' do
it 'creates an outgoing message without article references' do
response = create(:response, response_source: response_source, response_document: nil)
allow(chat_gpt_double).to receive(:generate_response).and_return({ 'response' => 'some_response', 'context_ids' => [response.id] })
expect do
service.perform
end.to change { conversation.messages.where(message_type: :outgoing).count }.by(1)
last_message = conversation.messages.last
expect(last_message.content).to include('some_response')
expect(last_message.content).not_to include('**Sources**')
end
end
context 'when JSON::ParserError is raised' do
it 'creates a handoff message' do
allow(chat_gpt_double).to receive(:generate_response).and_raise(JSON::ParserError)
expect(conversation).to receive(:bot_handoff!).and_call_original
expect do
service.perform
end.to change { conversation.messages.where(message_type: :outgoing).count }.by(1)
expect(conversation.messages.last.content).to eq('Transferring to another agent for further assistance.')
expect(conversation.status).to eq('open')
end
end
context 'when StandardError is raised' do
it 'captures the exception' do
allow(chat_gpt_double).to receive(:generate_response).and_raise(StandardError)
expect(conversation).to receive(:bot_handoff!).and_call_original
expect(ChatwootExceptionTracker).to receive(:new).and_call_original
expect do
service.perform
end.to change { conversation.messages.where(message_type: :outgoing).count }.by(1)
expect(conversation.messages.last.content).to eq('Transferring to another agent for further assistance.')
expect(conversation.status).to eq('open')
end
end
end
end

View File

@@ -11,14 +11,14 @@ RSpec.describe Internal::ReconcilePlanConfigService do
it 'disables the premium features for accounts' do
account = create(:account)
account.enable_features!('disable_branding', 'audit_logs', 'response_bot')
response_bot_account = create(:account)
response_bot_account.enable_features!('response_bot')
account.enable_features!('disable_branding', 'audit_logs', 'captain_integration')
account_with_captain = create(:account)
account_with_captain.enable_features!('captain_integration')
disable_branding_account = create(:account)
disable_branding_account.enable_features!('disable_branding')
service.perform
expect(account.reload.enabled_features.keys).not_to include('response_bot', 'disable_branding', 'audit_logs')
expect(response_bot_account.reload.enabled_features.keys).not_to include('response_bot')
expect(account.reload.enabled_features.keys).not_to include('captain_integration', 'disable_branding', 'audit_logs')
expect(account_with_captain.reload.enabled_features.keys).not_to include('captain_integration')
expect(disable_branding_account.reload.enabled_features.keys).not_to include('disable_branding')
end
@@ -56,14 +56,14 @@ RSpec.describe Internal::ReconcilePlanConfigService do
it 'does not disable the premium features for accounts' do
account = create(:account)
account.enable_features!('disable_branding', 'audit_logs', 'response_bot')
response_bot_account = create(:account)
response_bot_account.enable_features!('response_bot')
account.enable_features!('disable_branding', 'audit_logs', 'captain_integration')
account_with_captain = create(:account)
account_with_captain.enable_features!('captain_integration')
disable_branding_account = create(:account)
disable_branding_account.enable_features!('disable_branding')
service.perform
expect(account.reload.enabled_features.keys).to include('response_bot', 'disable_branding', 'audit_logs')
expect(response_bot_account.reload.enabled_features.keys).to include('response_bot')
expect(account.reload.enabled_features.keys).to include('captain_integration', 'disable_branding', 'audit_logs')
expect(account_with_captain.reload.enabled_features.keys).to include('captain_integration')
expect(disable_branding_account.reload.enabled_features.keys).to include('disable_branding')
end

View File

@@ -1,57 +0,0 @@
require 'rails_helper'
describe PageCrawlerService do
let(:html_link) { 'http://test.com' }
let(:sitemap_link) { 'http://test.com/sitemap.xml' }
let(:service_html) { described_class.new(html_link) }
let(:service_sitemap) { described_class.new(sitemap_link) }
let(:html_body) do
<<-HTML
<html>
<head><title>Test Title</title></head>
<body><a href="link1">Link 1</a><a href="link2">Link 2</a></body>
</html>
HTML
end
let(:sitemap_body) do
<<-XML
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url><loc>http://test.com/link1</loc></url>
<url><loc>http://test.com/link2</loc></url>
</urlset>
XML
end
before do
stub_request(:get, html_link).to_return(body: html_body, status: 200)
stub_request(:get, sitemap_link).to_return(body: sitemap_body, status: 200)
end
describe '#page_links' do
context 'when a HTML page is given' do
it 'returns all links on the page' do
expect(service_html.page_links).to eq(Set.new(['http://test.com/link1', 'http://test.com/link2']))
end
end
context 'when a sitemap is given' do
it 'returns all links in the sitemap' do
expect(service_sitemap.page_links).to eq(Set.new(['http://test.com/link1', 'http://test.com/link2']))
end
end
end
describe '#page_title' do
it 'returns the title of the page' do
expect(service_html.page_title).to eq('Test Title')
end
end
describe '#body_text_content' do
it 'returns the markdown converted body content of the page' do
expect(service_html.body_text_content.strip).to eq('[Link 1](link1)[Link 2](link2)')
end
end
end