diff --git a/enterprise/app/jobs/captain/conversation/response_builder_job.rb b/enterprise/app/jobs/captain/conversation/response_builder_job.rb index 9f8acc474..2967a8ce0 100644 --- a/enterprise/app/jobs/captain/conversation/response_builder_job.rb +++ b/enterprise/app/jobs/captain/conversation/response_builder_job.rb @@ -78,7 +78,7 @@ class Captain::Conversation::ResponseBuilderJob < ApplicationJob end def handoff_requested? - @response['response'] == 'conversation_handoff' + @response['action'] == 'handoff' end def process_action(action) @@ -97,9 +97,10 @@ class Captain::Conversation::ResponseBuilderJob < ApplicationJob end def create_handoff_message - create_outgoing_message( - @assistant.config['handoff_message'].presence || I18n.t('conversations.captain.handoff') - ) + handoff_message = @response['response'].presence || + @assistant.config['handoff_message'].presence || + I18n.t('conversations.captain.handoff') + create_outgoing_message(handoff_message) end def create_messages diff --git a/enterprise/app/services/captain/assistant/agent_runner_service.rb b/enterprise/app/services/captain/assistant/agent_runner_service.rb index 9c4e56841..b7eea59c7 100644 --- a/enterprise/app/services/captain/assistant/agent_runner_service.rb +++ b/enterprise/app/services/captain/assistant/agent_runner_service.rb @@ -95,7 +95,8 @@ class Captain::Assistant::AgentRunnerService def error_response(error_message) { 'response' => 'conversation_handoff', - 'reasoning' => "Error occurred: #{error_message}" + 'reasoning' => "Error occurred: #{error_message}", + 'action' => 'handoff' } end 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 5c9a8a184..1560ce5ed 100644 --- a/enterprise/app/services/enterprise/message_templates/hook_execution_service.rb +++ b/enterprise/app/services/enterprise/message_templates/hook_execution_service.rb @@ -57,11 +57,13 @@ module Enterprise::MessageTemplates::HookExecutionService return unless conversation.pending? Rails.logger.info("Captain limit exceeded, performing handoff mid-conversation for conversation: #{conversation.id}") + handoff_message = inbox.captain_assistant&.config&.dig('handoff_message').presence || + 'Transferring to another agent for further assistance.' conversation.messages.create!( message_type: :outgoing, account_id: conversation.account.id, inbox_id: conversation.inbox.id, - content: 'Transferring to another agent for further assistance.' + content: handoff_message ) conversation.bot_handoff! send_out_of_office_message_after_handoff