modularizado de prompts

This commit is contained in:
Lucas Tettamanti
2026-01-25 20:51:33 -03:00
parent b91ece867b
commit a489ec66a2
43 changed files with 5408 additions and 89 deletions

View File

@@ -3,9 +3,12 @@
*
* Flujo: IDLE → CART → SHIPPING → PAYMENT → WAITING_WEBHOOKS
* Regla universal: add_to_cart SIEMPRE vuelve a CART desde cualquier estado.
*
* Feature flag USE_MODULAR_NLU=true para usar el nuevo sistema NLU modular.
*/
import { llmNluV3 } from "./openai.js";
import { llmNluModular } from "./nlu/index.js";
import { ConversationState, shouldReturnToCart, safeNextState } from "./fsm.js";
import { migrateOldContext, createEmptyOrder } from "./orderModel.js";
import {
@@ -15,6 +18,10 @@ import {
handlePaymentState,
handleWaitingState,
} from "./stateHandlers.js";
import { getStoreConfig } from "../0-ui/db/settingsRepo.js";
// Feature flag para NLU modular
const USE_MODULAR_NLU = process.env.USE_MODULAR_NLU === "true";
/**
* Genera un resumen corto del historial para el NLU
@@ -58,7 +65,7 @@ export async function runTurnV3({
const normalizedState = normalizeState(prev_state);
// ─────────────────────────────────────────────────────────────
// NLU
// NLU (con feature flag para sistema modular)
// ─────────────────────────────────────────────────────────────
const nluInput = {
@@ -73,8 +80,37 @@ export async function runTurnV3({
locale: "es-AR",
};
const { nlu, raw_text, model, usage, validation } = await llmNluV3({ input: nluInput });
audit.nlu = { raw_text, model, usage, validation, parsed: nlu };
let nluResult;
if (USE_MODULAR_NLU) {
// Nuevo sistema NLU modular con prompts editables
// Cargar configuración del tenant desde la DB
const storeConfig = await getStoreConfig({ tenantId });
nluResult = await llmNluModular({ input: nluInput, tenantId, storeConfig });
audit.nlu = {
raw_text: nluResult.raw_text,
model: nluResult.model,
usage: nluResult.usage,
validation: nluResult.validation,
parsed: nluResult.nlu,
routing: nluResult.routing,
schema: "modular_v1",
};
} else {
// Sistema NLU clásico
nluResult = await llmNluV3({ input: nluInput });
audit.nlu = {
raw_text: nluResult.raw_text,
model: nluResult.model,
usage: nluResult.usage,
validation: nluResult.validation,
parsed: nluResult.nlu,
schema: "v3",
};
}
const nlu = nluResult.nlu;
// ─────────────────────────────────────────────────────────────
// Dispatcher por estado
@@ -90,7 +126,8 @@ export async function runTurnV3({
};
// Regla universal: si quiere agregar productos, volver a CART
if (shouldReturnToCart(normalizedState, nlu)) {
const returnToCart = shouldReturnToCart(normalizedState, nlu, text);
if (returnToCart) {
const result = await handleCartState({ ...handlerParams, fromIdle: false });
return formatResult(result, prev_context);
}