From fffb093df08fb806765df678ba0b0173b6ecbd41 Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Mon, 20 Jan 2025 17:44:18 +0530 Subject: [PATCH] feat: perform hand-off mid conversation if required --- .../message_templates/hook_execution_service.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/enterprise/app/services/enterprise/message_templates/hook_execution_service.rb b/enterprise/app/services/enterprise/message_templates/hook_execution_service.rb index 17515b628..92ccba553 100644 --- a/enterprise/app/services/enterprise/message_templates/hook_execution_service.rb +++ b/enterprise/app/services/enterprise/message_templates/hook_execution_service.rb @@ -2,6 +2,7 @@ module Enterprise::MessageTemplates::HookExecutionService def trigger_templates super return unless should_process_captain_response? + return perform_handoff unless inbox.captain_active? Captain::Conversation::ResponseBuilderJob.perform_later( conversation, @@ -10,6 +11,19 @@ module Enterprise::MessageTemplates::HookExecutionService end def should_process_captain_response? - conversation.pending? && message.incoming? && inbox.captain_active? + conversation.pending? && message.incoming? && inbox.captain_assistant.present? + end + + def perform_handoff + return unless conversation.pending? + + Rails.logger.info("Captain limit exceeded, performing handoff mid-conversation for conversation: #{conversation.id}") + conversation.messages.create!( + message_type: :outgoing, + account_id: conversation.account.id, + inbox_id: conversation.inbox.id, + content: 'Transferring to another agent for further assistance.' + ) + conversation.bot_handoff! end end