borrado de articulos del carrito

This commit is contained in:
Lucas Tettamanti
2026-01-25 23:43:00 -03:00
parent bd63d92c50
commit debad78781
4 changed files with 448 additions and 50 deletions

View File

@@ -128,9 +128,11 @@ const prev = await touchConversationState({ tenant_id: tenantId, wa_chat_id: cha
mark("start");
const stageDebug = dbg.perf;
mark("after_touchConversationState");
// Detectar conversación nueva (más de 24 horas sin actividad)
const staleThresholdMs = 24 * 60 * 60 * 1000; // 24 horas
const isStale =
prev?.state_updated_at &&
Date.now() - new Date(prev.state_updated_at).getTime() > 24 * 60 * 60 * 1000;
Date.now() - new Date(prev.state_updated_at).getTime() > staleThresholdMs;
const prev_state = isStale ? "IDLE" : prev?.state || "IDLE";
let externalCustomerId = await getExternalCustomerIdByChat({
tenant_id: tenantId,
@@ -161,7 +163,21 @@ let externalCustomerId = await getExternalCustomerIdByChat({
logStage(stageDebug, "history", { has_history: Array.isArray(conversation_history), state: prev_state });
// Cargar contexto previo, pero resetearlo si la conversación está "stale"
let reducedContext = prev?.context && typeof prev?.context === "object" ? { ...prev.context } : {};
if (isStale) {
// Conversación nueva: resetear carrito pero mantener datos del cliente
reducedContext = {
external_customer_id: reducedContext.external_customer_id,
// Resetear order y pending
order: null,
order_basket: null,
pending_items: null,
// Marcar que fue reseteado
_reset_reason: "stale",
_reset_at: new Date().toISOString(),
};
}
let decision;
let plan;
let llmMeta;