iachat/enterprise/app/jobs/captain/hermes/outgoing_job.rb
Rodribm10 a2bb613e68 feat(captain/hermes): debounce — inbox.typing_delay como buffer + agrupar msgs
Antes: inbox.typing_delay funcionava só pro Captain interno
(schedule_internal_response). Hermes ignorava o campo e disparava
OutgoingJob na hora — campo da UI era cosmético pra inboxes Hermes.

Agora:
- schedule_hermes_response cancela jobs OutgoingJob pendentes pra mesma
  conversa e enfileira com wait=inbox.typing_delay (debounce window).
- OutgoingJob agrupa todas msgs incoming entre a última resposta real
  do agente (ignora reactions) e a msg âncora; dispatch envia o texto
  concatenado pro Hermes via novo content_override no Client#dispatch.

Resultado: cliente que digita "Oi" + "quero pernoite Master" em segundos
vê o agente esperar até o buffer vencer e responder UMA vez cobrindo
ambas as falas, em vez de 2 respostas atropelando o pensamento.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 08:46:24 -03:00

55 lines
2.5 KiB
Ruby

# Dispara o webhook do Hermes Agent assincronamente quando uma mensagem
# do cliente chega numa inbox marcada como Hermes-enabled.
#
# Acionado pelo Enterprise::MessageTemplates::HookExecutionService no lugar do
# Captain::Conversation::ResponseBuilderJob padrão, quando
# Captain::Hermes.enabled_for?(inbox) retorna true.
class Captain::Hermes::OutgoingJob < ApplicationJob
queue_as :default
retry_on Captain::Hermes::Client::DispatchError, attempts: 3, wait: 5.seconds
def perform(conversation_id, message_id)
conversation = Conversation.find_by(id: conversation_id)
message = Message.find_by(id: message_id)
return if conversation.blank? || message.blank?
return unless Captain::Hermes.enabled_for?(conversation.inbox)
# Auto-react ANTES do dispatch — gesto chega <1s sem esperar Codex.
# Não bloqueia fluxo: se falhar, dispatch normal continua.
Captain::Hermes::AutoReactService.maybe_react!(message)
# Debounce: agrupa msgs incoming desde a última resposta real do
# agente. Quando inbox.typing_delay>0, schedule_hermes_response
# cancela jobs pendentes e enfileira só o último — aqui pegamos o
# texto agrupado pra Hermes ver o pensamento completo do cliente.
combined = combined_incoming_content(conversation, message)
Captain::Hermes::Client.new(conversation.inbox).dispatch(
message: message, conversation: conversation, content_override: combined
)
end
private
# Concatena texto de todas as msgs incoming entre a última resposta real
# (não-reaction) do agente e a msg âncora. Retorna nil se só tem 1 msg
# (pra dispatch usar message.content normal).
def combined_incoming_content(conversation, anchor_message)
last_real_outgoing = conversation.messages
.where(message_type: :outgoing)
.where("(content_attributes ->> 'is_reaction') IS NULL OR (content_attributes ->> 'is_reaction') != 'true'")
.order(created_at: :desc)
.first
scope = conversation.messages.where(message_type: :incoming).where('created_at <= ?', anchor_message.created_at)
scope = scope.where('created_at > ?', last_real_outgoing.created_at) if last_real_outgoing
texts = scope.order(:created_at).pluck(:content).map(&:to_s).reject(&:blank?).uniq
return nil if texts.size <= 1
Rails.logger.info("[Captain::Hermes::Debounce] agrupando #{texts.size} msgs do cliente em conv #{conversation.id}")
texts.join("\n")
end
end