Files
DeskcommCRM/workers/ai-response-worker.handler.ts
melgarafaelandClaude Opus 5.5 f399c920ce fix(eventos): handler declara o que faz na organização parada
naOrgParada obrigatório no EventHandler; o dreno lê o status das orgs do
lote numa consulta só e o dispatcher consome como skipped quem custa ou
sai para fora. Leitura que falha adia o lote em vez de consumir às cegas.

Invariante alterado sem afrouxar invariante da main — checar-invariantes-so-crescem.sh verde.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-29 23:50:57 -03:00

34 lines
1.2 KiB
TypeScript

/**
* Adapter that exposes `ai-response-worker` to the event_log dispatcher.
*
* Kept separate from the worker pipeline file so unit tests can import the
* pipeline (`processMessageReceived`) without pulling in the dispatcher
* registry, and so the handler key (the source-of-truth string written into
* `event_log.consumed_by[]`) lives in one obvious place.
*/
import type { EventHandler, HandlerResult } from "@/lib/event-log/dispatcher";
import { processMessageReceived } from "@/workers/ai-response-worker";
export const AI_RESPONSE_HANDLER_KEY = "ai-response-worker.v1";
export const aiResponseHandler: EventHandler = {
key: AI_RESPONSE_HANDLER_KEY,
naOrgParada: "pula",
events: ["message.received"],
async handle(row): Promise<HandlerResult> {
const result = await processMessageReceived(row);
if (result.status === "sent_to_dispatch") {
return { consumer_key: AI_RESPONSE_HANDLER_KEY, status: "ok", detail: result.outbound_message_id };
}
if (result.status === "skipped") {
return {
consumer_key: AI_RESPONSE_HANDLER_KEY,
status: "skipped",
detail: result.reason,
};
}
return { consumer_key: AI_RESPONSE_HANDLER_KEY, status: "error", detail: result.detail };
},
};