|
|
@@ -1,14 +1,23 @@
|
|
|
+import fs from "node:fs";
|
|
|
+import path from "node:path";
|
|
|
+import { fileURLToPath } from "node:url";
|
|
|
import { config } from "../config/index.js";
|
|
|
import { Atendimento } from "../models/Atendimento.model.js";
|
|
|
import { AtendimentoMensagem } from "../models/AtendimentoMensagem.model.js";
|
|
|
import { AtendimentoRagIndex } from "../models/AtendimentoRagIndex.model.js";
|
|
|
-import { buildConversaTexto, calcularUltimaMensagemId } from "../utils/atendimentoFormat.js";
|
|
|
+import { buildConversaTextoParaRag, calcularUltimaMensagemId } from "../utils/atendimentoFormat.js";
|
|
|
import { ingestDocuments } from "./ingestService.js";
|
|
|
import { searchDocs } from "./searchService.js";
|
|
|
import { generateHydeDocument } from "./hydeService.js";
|
|
|
import { rerankHits } from "./rerankService.js";
|
|
|
import { NotFoundError } from "../shared/errors/index.js";
|
|
|
|
|
|
+const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
|
+const BOILERPLATE_PATH = path.join(__dirname, "..", "config", "atendimentoBoilerplate.json");
|
|
|
+const boilerplateSet = new Set(
|
|
|
+ (JSON.parse(fs.readFileSync(BOILERPLATE_PATH, "utf8")).candidatos ?? []).map((c) => c.texto)
|
|
|
+);
|
|
|
+
|
|
|
const ATENDIMENTO_HYDE_SYSTEM_PROMPT = [
|
|
|
"Você é um atendente experiente de suporte ao cliente. Dada a pergunta ou descrição",
|
|
|
"abaixo, escreva um parágrafo curto (3 a 6 frases) no estilo de um trecho real de",
|
|
|
@@ -45,10 +54,12 @@ export async function ingestarAtendimento(atendimentoId) {
|
|
|
|
|
|
const mensagens = atendimento.mensagens ?? [];
|
|
|
const ultimaMensagemId = calcularUltimaMensagemId(mensagens);
|
|
|
- const conversaTexto = buildConversaTexto({ atendimento, cliente: atendimento.cliente, mensagens });
|
|
|
+ const conversaTexto = buildConversaTextoParaRag(
|
|
|
+ { atendimento, cliente: atendimento.cliente, mensagens },
|
|
|
+ { boilerplateSet }
|
|
|
+ );
|
|
|
|
|
|
- // conversa sem conteúdo indexável (ex.: só mensagens de sistema) — grava com
|
|
|
- // ChunksCount:0 pra sair da fila de pendentes; volta a aparecer se chegarem mensagens novas
|
|
|
+
|
|
|
if (!conversaTexto) {
|
|
|
await upsertRagIndex(atendimento.Id, {
|
|
|
UltimaMensagemId: ultimaMensagemId,
|