se agrega wh
This commit is contained in:
@@ -8,6 +8,8 @@ import OpenAI from "openai";
|
||||
import { ResponseMetadata } from "../_shared/utils.ts";
|
||||
import { handlePlanesEstudioResponse } from "./planes_estudio/index.ts";
|
||||
import { handleAsignaturasResponse } from "./asignaturas/index.ts";
|
||||
import { handlePlanMensajesResponse } from "./planes_estudio/crear.ts";
|
||||
|
||||
|
||||
console.log("Starting OpenAI webhook responses function");
|
||||
const client = new OpenAI({
|
||||
@@ -20,7 +22,8 @@ async function handleCompletedResponse(
|
||||
const response_id = event.data.id;
|
||||
const response = await client.responses.retrieve(response_id);
|
||||
const metadata = response.metadata as ResponseMetadata | null;
|
||||
|
||||
console.log(("entre"));
|
||||
|
||||
if (!metadata || !metadata.tabla) {
|
||||
console.warn("No se recibió metadata o tabla en la respuesta");
|
||||
return;
|
||||
@@ -33,6 +36,11 @@ async function handleCompletedResponse(
|
||||
case "asignaturas":
|
||||
await handleAsignaturasResponse(response);
|
||||
break;
|
||||
case "plan_mensajes_ia": // <-- Nueva tabla añadida
|
||||
console.log("entre aqui");
|
||||
|
||||
await handlePlanMensajesResponse(response);
|
||||
break;
|
||||
default:
|
||||
console.warn("Tabla no reconocida:", metadata.tabla);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user