no more debu legacy
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import crypto from "crypto";
|
||||
import { parseEvolutionWebhook } from "../services/evolutionParser.js";
|
||||
import { resolveTenantId, processMessage } from "../services/pipeline.js";
|
||||
import { debug as dbg } from "../services/debug.js";
|
||||
|
||||
export async function handleEvolutionWebhook(body) {
|
||||
const t0 = Date.now();
|
||||
@@ -9,12 +10,14 @@ export async function handleEvolutionWebhook(body) {
|
||||
return { status: 200, payload: { ok: true, ignored: parsed.reason } };
|
||||
}
|
||||
|
||||
console.log("[perf] evolution.webhook.start", {
|
||||
tenant_key: parsed.tenant_key || null,
|
||||
chat_id: parsed.chat_id,
|
||||
message_id: parsed.message_id || null,
|
||||
ts: parsed.ts || null,
|
||||
});
|
||||
if (dbg.perf || dbg.evolution) {
|
||||
console.log("[perf] evolution.webhook.start", {
|
||||
tenant_key: parsed.tenant_key || null,
|
||||
chat_id: parsed.chat_id,
|
||||
message_id: parsed.message_id || null,
|
||||
ts: parsed.ts || null,
|
||||
});
|
||||
}
|
||||
|
||||
const tenantId = await resolveTenantId({
|
||||
chat_id: parsed.chat_id,
|
||||
@@ -33,13 +36,15 @@ export async function handleEvolutionWebhook(body) {
|
||||
meta: { pushName: parsed.from_name, ts: parsed.ts, instance: parsed.tenant_key, source: parsed.source },
|
||||
});
|
||||
|
||||
console.log("[perf] evolution.webhook.end", {
|
||||
tenantId,
|
||||
chat_id: parsed.chat_id,
|
||||
message_id: parsed.message_id || null,
|
||||
run_id: pm?.run_id || null,
|
||||
webhook_ms: Date.now() - t0,
|
||||
});
|
||||
if (dbg.perf || dbg.evolution) {
|
||||
console.log("[perf] evolution.webhook.end", {
|
||||
tenantId,
|
||||
chat_id: parsed.chat_id,
|
||||
message_id: parsed.message_id || null,
|
||||
run_id: pm?.run_id || null,
|
||||
webhook_ms: Date.now() - t0,
|
||||
});
|
||||
}
|
||||
|
||||
return { status: 200, payload: { ok: true } };
|
||||
}
|
||||
|
||||
42
src/services/debug.js
Normal file
42
src/services/debug.js
Normal file
@@ -0,0 +1,42 @@
|
||||
function envIsOn(v) {
|
||||
const s = String(v || "").trim().toLowerCase();
|
||||
return s === "1" || s === "true" || s === "yes" || s === "on";
|
||||
}
|
||||
|
||||
function envIsOff(v) {
|
||||
const s = String(v || "").trim().toLowerCase();
|
||||
return s === "0" || s === "false" || s === "no" || s === "off";
|
||||
}
|
||||
|
||||
/**
|
||||
* Debug flags (por temas)
|
||||
*
|
||||
* - DEBUG_PERF: performance/latencias
|
||||
* - DEBUG_WOO_HTTP: requests/responses a Woo (status, timings, tamaño)
|
||||
* - DEBUG_WOO_PRODUCTS: caching/queries de productos Woo
|
||||
* - DEBUG_LLM: requests/responses a OpenAI
|
||||
* - DEBUG_EVOLUTION: hook evolution + parse
|
||||
* - DEBUG_DB: queries/latencias DB (si se instrumenta)
|
||||
* - DEBUG_RESOLVE: debug de resolución/ambiguity (pipeline)
|
||||
*/
|
||||
export const debug = {
|
||||
perf: envIsOn(process.env.DEBUG_PERF),
|
||||
|
||||
wooHttp: envIsOn(process.env.DEBUG_WOO_HTTP),
|
||||
|
||||
wooProducts: envIsOn(process.env.DEBUG_WOO_PRODUCTS),
|
||||
|
||||
llm: envIsOn(process.env.DEBUG_LLM),
|
||||
|
||||
evolution: envIsOn(process.env.DEBUG_EVOLUTION),
|
||||
|
||||
db: envIsOn(process.env.DEBUG_DB),
|
||||
|
||||
resolve: envIsOn(process.env.DEBUG_RESOLVE),
|
||||
};
|
||||
|
||||
export function debugOn(flagName) {
|
||||
return Boolean(debug?.[flagName]);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import OpenAI from "openai";
|
||||
import { z } from "zod";
|
||||
import { debug as dbg } from "./debug.js";
|
||||
|
||||
let _client = null;
|
||||
let _clientKey = null;
|
||||
@@ -93,7 +94,7 @@ function extractJsonObject(text) {
|
||||
async function jsonCompletion({ system, user, model }) {
|
||||
const openai = getClient();
|
||||
const chosenModel = model || process.env.OPENAI_MODEL || "gpt-4o-mini";
|
||||
const debug = String(process.env.LLM_DEBUG || "") === "1";
|
||||
const debug = dbg.llm;
|
||||
if (debug) console.log("[llm] openai.request", { model: chosenModel });
|
||||
|
||||
const resp = await openai.chat.completions.create({
|
||||
|
||||
@@ -14,6 +14,7 @@ import { sseSend } from "./sse.js";
|
||||
import { createWooCustomer, getWooCustomerById } from "./woo.js";
|
||||
import { llmExtract, llmPlan } from "./openai.js";
|
||||
import { searchProducts } from "./wooProducts.js";
|
||||
import { debug as dbg } from "./debug.js";
|
||||
|
||||
|
||||
function nowIso() {
|
||||
@@ -895,7 +896,7 @@ export async function processMessage({
|
||||
await touchConversationState({ tenant_id: tenantId, wa_chat_id: chat_id });
|
||||
|
||||
mark("start");
|
||||
const stageDebug = String(process.env.PIPELINE_DEBUG || "") === "1";
|
||||
const stageDebug = dbg.perf;
|
||||
const prev = await getConversationState(tenantId, chat_id);
|
||||
mark("after_getConversationState");
|
||||
const isStale =
|
||||
@@ -939,7 +940,7 @@ export async function processMessage({
|
||||
|
||||
// Reducer de contexto: consolidar slots y evitar “olvidos”
|
||||
mark("before_reduceContext");
|
||||
const resolveDebug = String(process.env.RESOLVE_DEBUG || "") === "1";
|
||||
const resolveDebug = dbg.resolve;
|
||||
const { extracted, resolvedBasket, unresolved, products_context } = await extractProducts({
|
||||
tenantId,
|
||||
text,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import crypto from "crypto";
|
||||
import { getDecryptedTenantEcommerceConfig } from "../db/repo.js";
|
||||
import { debug } from "./debug.js";
|
||||
|
||||
// --- Simple in-memory lock to serialize work per key (e.g. wa_chat_id) ---
|
||||
const locks = new Map();
|
||||
@@ -76,9 +77,9 @@ async function fetchWoo({ url, method = "GET", body = null, timeout = 20000, hea
|
||||
body: body ? JSON.stringify(body) : undefined,
|
||||
signal: controller.signal,
|
||||
});
|
||||
console.log("woo headers in", Date.now() - t0, "ms", res.status);
|
||||
if (debug.wooHttp) console.log("woo headers in", Date.now() - t0, "ms", res.status);
|
||||
const text = await res.text();
|
||||
console.log("woo body in", Date.now() - t0, "ms", "len", text.length);
|
||||
if (debug.wooHttp) console.log("woo body in", Date.now() - t0, "ms", "len", text.length);
|
||||
let parsed;
|
||||
try {
|
||||
parsed = text ? JSON.parse(text) : null;
|
||||
@@ -89,7 +90,7 @@ async function fetchWoo({ url, method = "GET", body = null, timeout = 20000, hea
|
||||
const err = new Error(`Woo HTTP ${res.status}`);
|
||||
err.status = res.status;
|
||||
err.body = parsed;
|
||||
err.url = url;
|
||||
err.url = redactWooUrl(url);
|
||||
err.method = method;
|
||||
throw err;
|
||||
}
|
||||
@@ -97,7 +98,10 @@ async function fetchWoo({ url, method = "GET", body = null, timeout = 20000, hea
|
||||
} catch (e) {
|
||||
const err = new Error(`Woo request failed after ${Date.now() - t0}ms: ${e.message}`);
|
||||
err.cause = e;
|
||||
err.url = url;
|
||||
// Propagar status/body para que el caller pueda decidir retries/auth fallback
|
||||
err.status = e?.status || null;
|
||||
err.body = e?.body || null;
|
||||
err.url = redactWooUrl(url);
|
||||
err.method = method;
|
||||
throw err;
|
||||
} finally {
|
||||
@@ -105,6 +109,17 @@ async function fetchWoo({ url, method = "GET", body = null, timeout = 20000, hea
|
||||
}
|
||||
}
|
||||
|
||||
function redactWooUrl(url) {
|
||||
try {
|
||||
const u = new URL(url);
|
||||
if (u.searchParams.has("consumer_key")) u.searchParams.set("consumer_key", "REDACTED");
|
||||
if (u.searchParams.has("consumer_secret")) u.searchParams.set("consumer_secret", "REDACTED");
|
||||
return u.toString();
|
||||
} catch {
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
||||
async function searchWooCustomerByEmail({ base, consumerKey, consumerSecret, email, timeout }) {
|
||||
const url = `${base}/customers?email=${encodeURIComponent(email)}`;
|
||||
const auth = Buffer.from(`${consumerKey}:${consumerSecret}`).toString("base64");
|
||||
@@ -364,12 +379,16 @@ export async function getWooCustomerById({ tenantId, id }) {
|
||||
})();
|
||||
|
||||
const base = cfg.base_url.replace(/\/+$/, "");
|
||||
const url = `${base}/customers/${id}?consumer_key=${encodeURIComponent(
|
||||
consumerKey
|
||||
)}&consumer_secret=${encodeURIComponent(consumerSecret)}`;
|
||||
const url = `${base}/customers/${encodeURIComponent(id)}`;
|
||||
const auth = Buffer.from(`${consumerKey}:${consumerSecret}`).toString("base64");
|
||||
|
||||
try {
|
||||
const data = await fetchWoo({ url, method: "GET", timeout: cfg.timeout_ms });
|
||||
const data = await fetchWoo({
|
||||
url,
|
||||
method: "GET",
|
||||
timeout: cfg.timeout_ms,
|
||||
headers: { Authorization: `Basic ${auth}` },
|
||||
});
|
||||
return data;
|
||||
} catch (err) {
|
||||
if (err.status === 404) return null;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { getDecryptedTenantEcommerceConfig, getWooProductCacheById, searchWooProductCache, upsertWooProductCache } from "../db/repo.js";
|
||||
import { debug as dbg } from "./debug.js";
|
||||
|
||||
async function fetchWoo({ url, method = "GET", body = null, timeout = 8000, headers = {} }) {
|
||||
const controller = new AbortController();
|
||||
@@ -117,7 +118,7 @@ export async function searchProducts({
|
||||
maxAgeMs = 24 * 60 * 60 * 1000,
|
||||
forceWoo = false,
|
||||
}) {
|
||||
const debug = String(process.env.WOO_PRODUCTS_DEBUG || "") === "1";
|
||||
const debug = dbg.wooProducts;
|
||||
const lim = Math.max(1, Math.min(50, parseInt(limit, 10) || 10));
|
||||
const query = String(q || "").trim();
|
||||
if (!query) return { items: [], source: "none" };
|
||||
|
||||
Reference in New Issue
Block a user