diff --git a/enterprise/app/services/enterprise/message_templates/response_bot_service.rb b/enterprise/app/services/enterprise/message_templates/response_bot_service.rb index a8a4d4907..0c1bcc220 100644 --- a/enterprise/app/services/enterprise/message_templates/response_bot_service.rb +++ b/enterprise/app/services/enterprise/message_templates/response_bot_service.rb @@ -63,23 +63,22 @@ class Enterprise::MessageTemplates::ResponseBotService def create_messages message_content = @response['response'] - - message_content = append_message_with_sources(message_content) + message_content += generate_sources_section if @response['context_ids'].present? create_outgoing_message(message_content) end - def append_message_with_sources(message_content) + def generate_sources_section article_ids = @response['context_ids'] - return message_content if article_ids.blank? + sources_content = '' - message_content += "\n \n \n **Sources** \n" articles_hash = get_article_hash(article_ids.uniq) articles_hash.first(3).each do |article_hash| - message_content += " - [#{article_hash[:response].question}](#{article_hash[:response_document].document_link}) \n" + sources_content += " - [#{article_hash[:response].question}](#{article_hash[:response_document].document_link}) \n" end - message_content + sources_content = "\n \n \n **Sources** \n#{sources_content}" if sources_content.present? + sources_content end def create_outgoing_message(message_content) diff --git a/spec/enterprise/services/enterprise/message_templates/response_bot_service_spec.rb b/spec/enterprise/services/enterprise/message_templates/response_bot_service_spec.rb index f8d8863b1..180fa4cef 100644 --- a/spec/enterprise/services/enterprise/message_templates/response_bot_service_spec.rb +++ b/spec/enterprise/services/enterprise/message_templates/response_bot_service_spec.rb @@ -30,6 +30,36 @@ RSpec.describe Enterprise::MessageTemplates::ResponseBotService, type: :service 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 + 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