fix: navegación — atras/cancelar/regresar + NLU desde menús activos

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-30 23:50:09 -05:00
co-authored by Claude Sonnet 4.6
parent 79d2399ddf
commit 09db466fa6
2 changed files with 23 additions and 8 deletions
+14 -2
View File
@@ -71,9 +71,13 @@ class NormalBot
} }
// 3. Active node — resume conversation // 3. Active node — resume conversation
// Menu-type nodes let NLU act on free text; only re-show if NLU doesn't handle it.
$sentinels = ['collecting', '__greeted']; $sentinels = ['collecting', '__greeted'];
if ($currentNode !== null && !in_array($currentNode, $sentinels, true) && isset($flows[$currentNode])) { if ($currentNode !== null && !in_array($currentNode, $sentinels, true) && isset($flows[$currentNode])) {
return self::handleFlow($flows[$currentNode], $context, $company, $ctxId, $menus); $isMenuNode = ($flows[$currentNode]['type'] ?? '') === 'menu';
if (!$isMenuNode) {
return self::handleFlow($flows[$currentNode], $context, $company, $ctxId, $menus);
}
} }
// 4. Welcome message — shown once for brand-new sessions before the category menu // 4. Welcome message — shown once for brand-new sessions before the category menu
@@ -132,11 +136,19 @@ class NormalBot
} }
} }
if ($route['action'] === 'chat' && ($route['text'] ?? '') !== '') { if ($route['action'] === 'chat' && ($route['text'] ?? '') !== '') {
ConversationContext::updateNode($ctxId, null); // Preserve node when inside a menu so state isn't lost
$inMenu = $currentNode !== null && !in_array($currentNode, $sentinels, true)
&& isset($flows[$currentNode]) && ($flows[$currentNode]['type'] ?? '') === 'menu';
if (!$inMenu) ConversationContext::updateNode($ctxId, null);
return self::sendText($route['text'], $context['from'], $company); return self::sendText($route['text'], $context['from'], $company);
} }
} }
// Re-show current menu when NLU didn't route anything
if ($currentNode !== null && !in_array($currentNode, $sentinels, true) && isset($flows[$currentNode])) {
return self::handleFlow($flows[$currentNode], $context, $company, $ctxId, $menus);
}
// 7. Fallback flow // 7. Fallback flow
$fallbackFlowId = $perType['fallback_flow'] ?? $config['fallback_flow'] ?? null; $fallbackFlowId = $perType['fallback_flow'] ?? $config['fallback_flow'] ?? null;
if ($fallbackFlowId !== null && isset($flows[$fallbackFlowId])) { if ($fallbackFlowId !== null && isset($flows[$fallbackFlowId])) {
+9 -6
View File
@@ -13,12 +13,15 @@ $configJson = [
'fallback' => 'No entendí. Escribe *menu* para ver las opciones disponibles.', 'fallback' => 'No entendí. Escribe *menu* para ver las opciones disponibles.',
'nlu_enabled' => true, 'nlu_enabled' => true,
'commands' => [ 'commands' => [
'menu' => 'show_main_menu', 'menu' => 'show_main_menu',
'informes' => 'descargar_informes', 'informes' => 'descargar_informes',
'info' => 'enviar_informacion', 'info' => 'enviar_informacion',
'inicio' => 'show_main_menu', 'inicio' => 'show_main_menu',
'volver' => 'show_main_menu', 'volver' => 'show_main_menu',
'salir' => 'show_main_menu', 'salir' => 'show_main_menu',
'atras' => 'show_main_menu',
'cancelar' => 'show_main_menu',
'regresar' => 'show_main_menu',
], ],
// ── Menus globales ────────────────────────────────────────────────────── // ── Menus globales ──────────────────────────────────────────────────────
'menus' => [ 'menus' => [