D9 cleanup: borrar NLU/handlers/machine/replyTemplates legacy + activar agente + prompt caching
Después de validar el agente E2E con DeepSeek, el legacy queda muerto. 51 archivos cambiados (la mayoría borrados), el motor único es ahora el agente tool-calling. Borrados (~3500 LOC): - src/modules/3-turn-engine/nlu/ (router + 4 specialists + promptLoader + schemas + humanFallback + 6 default prompts) — reemplazado por systemPrompt.js - src/modules/3-turn-engine/stateHandlers/ (cart.js, cartHelpers.js, idle.js, shipping.js, utils.js, index.js) — reemplazado por tools del agente - src/modules/3-turn-engine/stateHandlers.js (re-export shim) - src/modules/3-turn-engine/openai.js (NLU clásico v3 + jsonCompletion + llmRecommendWriter + llmPlanningRecommend) — el agente crea su propio cliente OpenAI con tools nativos - src/modules/3-turn-engine/replyRewriter.js (rewriting LLM) — el agente escribe say directo, no necesita reescribir - src/modules/3-turn-engine/replyTemplates.js + test (rotación de variantes) — el agente varía naturalmente con tool_choice=required + temperature - src/modules/3-turn-engine/recommendations.js (cross-sell + planning) — el agente decide cuándo recomendar via tool calls - src/modules/3-turn-engine/machine/ (XState v5 completo + 19 tests) — reemplazado por la FSM podada en fsm.js + agent/runTurn.js - src/modules/3-turn-engine/turnEngineV3.helpers.js, .units.js, .pendingSelection.js (helpers del legacy) - src/modules/0-ui/controllers/prompts.js, handlers/prompts.js, db/promptsRepo.js — admin de prompts NLU (ya no hay prompts editables) - public/components/prompts-crud.js + nav entry en ops-shell turnEngineV3.js se reduce a un thin wrapper que exporta runTurnV3 (alias de runTurnAgent) + safeNextState (re-export de fsm.js). Mantiene la firma pública para no tocar pipeline.js. Activado: - AGENT_MAX_TOOL_CALLS=10 y AGENT_TURN_TIMEOUT_MS=25000 son los únicos flags. Borradas: USE_MODULAR_NLU, USE_XSTATE, XSTATE_SHADOW, XSTATE_SETTLE_MS, REPLY_REWRITER, REPLY_REWRITER_TIMEOUT_MS, TURN_ENGINE, AGENT_TURN_ENGINE, AGENT_TURN_ENGINE_SHADOW (el agente es default). Prompt caching DeepSeek: - systemPrompt.js: era función con storeName interpolado → ahora export const SYSTEM_PROMPT (100% estático). storeName se pasa por user message via working_memory.store.name. Cualquier cambio al system invalida cache, por eso es estático estricto. - runTurn.js: captura usage.prompt_cache_hit_tokens (DeepSeek) o prompt_tokens_details.cached_tokens (OpenAI compat) y suma a métricas. - /api/metrics/agent ahora reporta prompt_tokens_total, completion_tokens_total, prompt_cache_hit_tokens, cache_hit_ratio. - Smoke test 3 turnos: cache_hit_ratio = 0.72 (17664 cached / 24546 total prompt tokens). Saving directo en costo: ~$0.02/M cached vs $0.27/M no cached en DeepSeek. Tests: 148/148 (perdimos 90 tests del legacy XState/replyTemplates que ya no aplican). Sim flow E2E confirmado: hola → agent responde, multi-turn con cache caliente. Si más adelante hace falta volver al legacy: git revert este commit (c c9c69cf8 es el último estado verde con doble motor). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
34
env.example
34
env.example
@@ -13,24 +13,17 @@ PG_CONN_TIMEOUT_MS=5000
|
||||
APP_ENCRYPTION_KEY=your-32-char-encryption-key-here
|
||||
|
||||
# ===================
|
||||
# OpenAI
|
||||
# LLM (OpenAI-compatible: DeepSeek, OpenAI, Anthropic via gateway, etc.)
|
||||
# ===================
|
||||
# Default actual: DeepSeek V3.x con tool-calling nativo + prompt caching automático.
|
||||
OPENAI_API_KEY=sk-xxx
|
||||
OPENAI_MODEL=gpt-4o-mini
|
||||
OPENAI_BASE_URL=https://api.deepseek.com/v1
|
||||
OPENAI_MODEL=deepseek-chat
|
||||
|
||||
# ===================
|
||||
# Turn Engine
|
||||
# WooCommerce
|
||||
# ===================
|
||||
# v1 = pipeline actual (heurísticas + guardrails + LLM plan final)
|
||||
# v2 = LLM-first NLU, deterministic core (nuevo motor)
|
||||
TURN_ENGINE=v1
|
||||
|
||||
# ===================
|
||||
# WooCommerce (usado por seed y fallback)
|
||||
# Estas variables son leídas por scripts/seed-tenant.mjs para crear
|
||||
# la configuración inicial del tenant en la base de datos.
|
||||
# ===================
|
||||
WOO_BASE_URL=https://tu-tienda.com
|
||||
WOO_BASE_URL=https://tu-tienda.com/wp-json/wc/v3
|
||||
WOO_CONSUMER_KEY=ck_xxx
|
||||
WOO_CONSUMER_SECRET=cs_xxx
|
||||
|
||||
@@ -49,19 +42,10 @@ LIMIT_CONVERSATIONS=100
|
||||
MAX_CHARS_PER_MESSAGE=4000
|
||||
|
||||
# ===================
|
||||
# Reply Rewriter (LLM adapta templates al contexto)
|
||||
# Agent (tool-calling) — único motor de turno
|
||||
# ===================
|
||||
REPLY_REWRITER=0
|
||||
REPLY_REWRITER_TIMEOUT_MS=1500
|
||||
|
||||
# ===================
|
||||
# XState (Turn engine v2)
|
||||
# ===================
|
||||
# USE_XSTATE=1 → reemplaza dispatcher legacy con statechart formal
|
||||
# XSTATE_SHADOW=1 → corre ambos paths, devuelve legacy, loguea diffs en logs
|
||||
USE_XSTATE=0
|
||||
XSTATE_SHADOW=0
|
||||
XSTATE_SETTLE_MS=10000
|
||||
AGENT_MAX_TOOL_CALLS=10
|
||||
AGENT_TURN_TIMEOUT_MS=25000
|
||||
|
||||
# ===================
|
||||
# Debug Flags (1/true/yes/on para activar)
|
||||
|
||||
Reference in New Issue
Block a user