fix: el NLU respeta los modulos del perfil

Los menus se filtraban por modulo pero el texto libre no: un perfil limitado a
pluviometria podia escribir "registrar ausentismo" y el NLU lo llevaba igual.
El mismo agujero que ya cerramos con las fincas.

moduloDeFlow() decide por convencion de nombre a que modulo pertenece cada
flow —'modulo' explicito manda— y se aplica en dos puntos: el catalogo que ve
el modelo llega filtrado, y el ruteo verifica igual la clave devuelta por si
el modelo alucina una que no le ofrecieron.

Lo permitido sigue directo: "subir pluviometria" cae en la fecha con un solo
mensaje, "subir labores diarias" arranca el flujo. Cinco casos nuevos.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-08-19 22:39:45 -05:00
co-authored by Claude Sonnet 4.6
parent 9ae4b0dee3
commit 6c524a39ed
4 changed files with 95 additions and 6 deletions
+10 -3
View File
@@ -334,11 +334,11 @@ PROMPT;
* Dado un mensaje de texto, decide si enrutar a un flow existente o responder en chat libre.
* Devuelve: ['action'=>'route','key'=>'flow_key'] | ['action'=>'chat','text'=>'...']
*/
public static function routeOrChat(array $company, array $context, string $input): array
public static function routeOrChat(array $company, array $context, string $input, array $modulosPermitidos = []): array
{
$config = self::getConfig($company);
$permType = (int)($context['permission_type'] ?? 1);
$catalog = self::buildFlowCatalog($config, $permType);
$catalog = self::buildFlowCatalog($config, $permType, $modulosPermitidos);
if (empty($catalog)) {
return ['action' => 'chat', 'text' => ''];
@@ -405,7 +405,7 @@ PROMPT;
return $crudo;
}
private static function buildFlowCatalog(array $config, int $permType): array
private static function buildFlowCatalog(array $config, int $permType, array $modulosPermitidos = []): array
{
$flows = $config['flows'] ?? [];
$menus = $config['menus'] ?? [];
@@ -446,6 +446,13 @@ PROMPT;
// Flujos marcados como internos — no exponer al NLU
if (!empty($flow['nlu_skip'])) continue;
// El perfil del número acota qué módulos puede usar: lo que el menú
// no muestra, el NLU tampoco lo ofrece
if ($modulosPermitidos) {
$mod = NormalBot::moduloDeFlow($key, $flow);
if ($mod !== null && !in_array($mod, $modulosPermitidos, true)) continue;
}
// Skip internal/navigation flows unless they have an explicit NLU description
$hasNluDesc = isset($flow['nlu_description']) && $flow['nlu_description'] !== '';
if (in_array($type, ['text', 'image'], true) && !$isUpload && !$isDownload && !$hasNluDesc) continue;