se agrega wh

This commit is contained in:
2026-03-06 11:06:56 -06:00
parent d844698ffc
commit 749ec7a962
5 changed files with 159 additions and 131 deletions
@@ -106,3 +106,66 @@ export async function handleCrearPlanEstudio(
return;
}
}
export async function handlePlanMensajesResponse(
response: OpenAI.Responses.Response,
): Promise<void> {
const metadata = response.metadata as any;
const mensajeId = metadata?.mensaje_id;
console.log("ya entre aqui");
const isStructured = metadata?.is_structured === "true" || metadata?.is_structured === true;
if (!mensajeId) {
console.warn("No se recibió mensaje_id en la metadata del webhook");
return;
}
try {
const outputText = extractOutputText(response);
if (!outputText) {
throw new Error("La respuesta de OpenAI está vacía");
}
let respuestaJSON: any;
try {
respuestaJSON = JSON.parse(outputText);
} catch (e) {
throw new Error(`Error parseando JSON de OpenAI: ${e.message}`);
}
const is_refusal = !!respuestaJSON.is_refusal || respuestaJSON["is-refusal"] === true;
let recommendations = [];
if (isStructured && !is_refusal) {
recommendations = Object.entries(respuestaJSON)
.filter(([k]) => k !== "ai-message" && k !== "is-refusal" && k !== "is_refusal")
.map(([campo, valor]) => ({
campo_afectado: campo,
texto_mejora: valor,
aplicada: false,
}));
}
const { error } = await supabase
.from("plan_mensajes_ia")
.update({
respuesta: respuestaJSON["ai-message"] || "",
propuesta: { recommendations },
is_refusal,
estado: "COMPLETADO",
})
.eq("id", mensajeId);
if (error) {
throw error;
}
} catch (e) {
console.error("Error procesando handlePlanMensajesResponse:", { mensajeId, e });
// Opcional: Marcar el mensaje como fallido en la tabla si tienes ese estado
await supabase
.from("plan_mensajes_ia")
.update({ estado: "ERROR" })
.eq("id", mensajeId);
}
}