feat(ee): Add reporting events for bots (#9027)

Added a new event conversation_bot_resolved and added a job to auto resolve the bot conversations if there was no activity for the last 1 hour.
This commit is contained in:
Sojan Jose
2024-02-28 04:23:28 +05:30
committed by GitHub
parent b7a83dcbcd
commit 41e269e873
20 changed files with 194 additions and 15 deletions

View File

@@ -8,9 +8,7 @@ RSpec.describe Enterprise::MessageTemplates::ResponseBotService, type: :service
let(:response_object) { instance_double(Response, id: 1, question: 'Q1', answer: 'A1') }
before do
# Uncomment if you want to run the spec in your local machine
# Features::ResponseBotService.new.enable_in_installation
skip('Skipping since vector is not enabled in this environment') unless Features::ResponseBotService.new.vector_extension_enabled?
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')
@@ -33,6 +31,19 @@ RSpec.describe Enterprise::MessageTemplates::ResponseBotService, type: :service
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
@@ -67,12 +78,13 @@ RSpec.describe Enterprise::MessageTemplates::ResponseBotService, type: :service
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('passing to an agent')
expect(conversation.messages.last.content).to eq('Transferring to another agent for further assistance.')
expect(conversation.status).to eq('open')
end
end
@@ -80,6 +92,7 @@ RSpec.describe Enterprise::MessageTemplates::ResponseBotService, type: :service
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
@@ -87,7 +100,7 @@ RSpec.describe Enterprise::MessageTemplates::ResponseBotService, type: :service
service.perform
end.to change { conversation.messages.where(message_type: :outgoing).count }.by(1)
expect(conversation.messages.last.content).to eq('passing to an agent')
expect(conversation.messages.last.content).to eq('Transferring to another agent for further assistance.')
expect(conversation.status).to eq('open')
end
end