From 3bac319febd9744b9cde0514f1523b68398adedd Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Fri, 17 Jan 2025 16:44:22 +0530 Subject: [PATCH] feat: hand off if captain limits are reached --- .../app/jobs/captain/conversation/response_builder_job.rb | 4 ++++ .../enterprise/message_templates/hook_execution_service.rb | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/enterprise/app/jobs/captain/conversation/response_builder_job.rb b/enterprise/app/jobs/captain/conversation/response_builder_job.rb index d664fa49f..c61ec4f96 100644 --- a/enterprise/app/jobs/captain/conversation/response_builder_job.rb +++ b/enterprise/app/jobs/captain/conversation/response_builder_job.rb @@ -3,8 +3,12 @@ class Captain::Conversation::ResponseBuilderJob < ApplicationJob def perform(conversation, assistant) @conversation = conversation + @inbox = conversation.inbox @assistant = assistant + # this ensures that the conversation is not processed if the assistant limits are reached + return ActiveRecord::Base.transaction { process_action('handoff') } unless @inbox.captain_active? + ActiveRecord::Base.transaction do generate_and_process_response 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 17515b628..faefd3ef2 100644 --- a/enterprise/app/services/enterprise/message_templates/hook_execution_service.rb +++ b/enterprise/app/services/enterprise/message_templates/hook_execution_service.rb @@ -10,6 +10,9 @@ module Enterprise::MessageTemplates::HookExecutionService end def should_process_captain_response? - conversation.pending? && message.incoming? && inbox.captain_active? + # we don't check the captain usage limits here intentionally + # as the usage limits are checked in the job itself + # this is to ensure that the handoff can be done there if it's required + conversation.pending? && message.incoming? && inbox.captain_assistant.present? end end