Corregir Edge Function #36 #37
@@ -123,81 +123,7 @@ app.post(`${prefix}/conversations`, async (c) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
|
||||||
* GET /conversations/:conversation_plan_id/messages
|
|
||||||
* Lista mensajes (assistant/user) desde OpenAI
|
|
||||||
*/
|
|
||||||
app.get(`${prefix}/conversations/:id/messages`, async (c) => {
|
|
||||||
try {
|
|
||||||
/* const auth = c.req.header("authorization");
|
|
||||||
await requireUser(auth); */
|
|
||||||
|
|
||||||
const conversation_plan_id = c.req.param("id");
|
|
||||||
assertUuid(conversation_plan_id, "conversation_plan_id");
|
|
||||||
|
|
||||||
const supabase = getSupabaseServiceClient();
|
|
||||||
const openai = getOpenAI();
|
|
||||||
|
|
||||||
const { data: convRow, error } = await supabase
|
|
||||||
.from("conversaciones_plan")
|
|
||||||
.select("openai_conversation_id, estado")
|
|
||||||
.eq("id", conversation_plan_id)
|
|
||||||
.single();
|
|
||||||
|
|
||||||
if (error || !convRow) {
|
|
||||||
throw new HttpError(
|
|
||||||
404,
|
|
||||||
"conversation_not_found",
|
|
||||||
"Conversación no encontrada",
|
|
||||||
error,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (convRow.estado === "ARCHIVADA") {
|
|
||||||
// si ya está archivada, devolvemos lo guardado
|
|
||||||
const { data: archived } = await supabase
|
|
||||||
.from("conversaciones_plan")
|
|
||||||
.select("conversacion_json")
|
|
||||||
.eq("id", conversation_plan_id)
|
|
||||||
.single();
|
|
||||||
return withCors(
|
|
||||||
jsonResponse({
|
|
||||||
source: "supabase",
|
|
||||||
items: archived?.conversacion_json ?? null,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const items = await openai.conversations.items.list(
|
|
||||||
convRow.openai_conversation_id,
|
|
||||||
);
|
|
||||||
|
|
||||||
const conversacion = items.data
|
|
||||||
.filter((it: any) =>
|
|
||||||
it.type === "message" &&
|
|
||||||
(it.role === "assistant" || it.role === "user")
|
|
||||||
)
|
|
||||||
.map((it: any) => {
|
|
||||||
const rawText = it.content.map((c: any) => c.text).join("");
|
|
||||||
|
|
||||||
let parsedContent;
|
|
||||||
|
|
||||||
try {
|
|
||||||
parsedContent = JSON.parse(rawText); // 👈 aquí lo convertimos
|
|
||||||
} catch {
|
|
||||||
parsedContent = rawText; // si no es JSON, lo dejamos normal
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
role: it.role,
|
|
||||||
content: parsedContent,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
return withCors(jsonResponse({ source: "openai", items: conversacion }));
|
|
||||||
} catch (err) {
|
|
||||||
return withCors(handleErr(err));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* POST /conversations/:conversation_plan_id/messages
|
* POST /conversations/:conversation_plan_id/messages
|
||||||
@@ -428,91 +354,7 @@ app.post(`${prefix}/conversations/:id/messages`, async (c) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
|
||||||
* DELETE /conversations/:conversation_plan_id/archive
|
|
||||||
* Guarda items en Supabase y elimina la conversación de OpenAI
|
|
||||||
*/
|
|
||||||
app.delete(`${prefix}/conversations/:id/archive`, async (c) => {
|
|
||||||
try {
|
|
||||||
/* const auth = c.req.header("authorization");
|
|
||||||
await requireUser(auth); */
|
|
||||||
|
|
||||||
const conversation_plan_id = c.req.param("id");
|
|
||||||
assertUuid(conversation_plan_id, "conversation_plan_id");
|
|
||||||
|
|
||||||
const supabase = getSupabaseServiceClient();
|
|
||||||
const openai = getOpenAI();
|
|
||||||
|
|
||||||
const { data: row, error } = await supabase
|
|
||||||
.from("conversaciones_plan")
|
|
||||||
.select("id, openai_conversation_id, estado")
|
|
||||||
.eq("id", conversation_plan_id)
|
|
||||||
.single();
|
|
||||||
|
|
||||||
if (error || !row) {
|
|
||||||
throw new HttpError(
|
|
||||||
404,
|
|
||||||
"conversation_not_found",
|
|
||||||
"Conversación no encontrada",
|
|
||||||
error,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (row.estado === "ARCHIVADA") {
|
|
||||||
return withCors(jsonResponse({ ok: true, already: true }));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Marcar estado
|
|
||||||
await supabase.from("conversaciones_plan")
|
|
||||||
.update({ estado: "ARCHIVANDO" })
|
|
||||||
.eq("id", conversation_plan_id);
|
|
||||||
|
|
||||||
// Descargar items de OpenAI
|
|
||||||
const items = await openai.conversations.items.list(
|
|
||||||
row.openai_conversation_id,
|
|
||||||
);
|
|
||||||
|
|
||||||
const conversacion = items.data.filter((it: any) =>
|
|
||||||
it.type === "message" && (it.role === "assistant" || it.role === "user")
|
|
||||||
).map((it: any) => ({
|
|
||||||
role: it.role,
|
|
||||||
content: it.content.map((c: any) => c.text).join(""),
|
|
||||||
}));
|
|
||||||
|
|
||||||
// Guardar y marcar como ARCHIVADA
|
|
||||||
const { error: upErr } = await supabase.from("conversaciones_plan")
|
|
||||||
.update({
|
|
||||||
estado: "ARCHIVADA",
|
|
||||||
conversacion_json: conversacion,
|
|
||||||
})
|
|
||||||
.eq("id", conversation_plan_id);
|
|
||||||
|
|
||||||
if (upErr) {
|
|
||||||
throw new HttpError(
|
|
||||||
500,
|
|
||||||
"archive_save_failed",
|
|
||||||
"No se pudo guardar el archivo en Supabase",
|
|
||||||
upErr,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Borrar conversación en OpenAI (best effort)
|
|
||||||
try {
|
|
||||||
await openai.conversations.delete(row.openai_conversation_id);
|
|
||||||
} catch (delErr) {
|
|
||||||
// Queda archivada en Supabase, pero reportamos warning
|
|
||||||
return withCors(jsonResponse({
|
|
||||||
ok: true,
|
|
||||||
warning: "Archivada en Supabase, pero no se pudo borrar en OpenAI",
|
|
||||||
details: String(delErr),
|
|
||||||
}, 200));
|
|
||||||
}
|
|
||||||
|
|
||||||
return withCors(jsonResponse({ ok: true }));
|
|
||||||
} catch (err) {
|
|
||||||
return withCors(handleErr(err));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unknown routes
|
* Unknown routes
|
||||||
|
|||||||
Reference in New Issue
Block a user