feat: NLU routing + audio/image transcription via AI

- NluRouter in AiBot: builds permission-filtered flow catalog, asks AI
  to route user intent to exact flow key or return chat response
- MediaTranscriber: downloads WA media, transcribes audio (Whisper/Gemini),
  extracts image intent (OpenAI/Gemini/Claude vision)
- BotRouter hybrid: text → NormalBot → NLU fallback if no match;
  audio/image → transcribe → NLU route; sends transcript preview first
- Admin IA tab: NLU checkbox with description
- Per-company nlu flag saved in config_json

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-04 21:50:01 -05:00
co-authored by Claude Sonnet 4.6
parent 2b859e3dc0
commit 9e36a965c9
5 changed files with 488 additions and 6 deletions
+135
View File
@@ -302,6 +302,141 @@ PROMPT;
. "Responde de forma breve y apropiada según los permisos del usuario. Sin saludos largos.";
}
// ── NLU: enrutamiento inteligente ────────────────────────────────────────
/**
* 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
{
$config = self::getConfig($company);
$permType = (int)($context['permission_type'] ?? 1);
$catalog = self::buildFlowCatalog($config, $permType);
if (empty($catalog)) {
return ['action' => 'chat', 'text' => ''];
}
$systemPrompt = self::buildNluPrompt(
$company['display_name'] ?? $company['name'] ?? 'la empresa',
$catalog,
$permType
);
$raw = self::callLlmOnce($systemPrompt, $input, $company);
if ($raw === null) {
return ['action' => 'chat', 'text' => ''];
}
// Strip markdown fences if AI wrapped JSON
$clean = trim(preg_replace('/^```(?:json)?\s*/i', '', preg_replace('/\s*```$/m', '', $raw)));
$json = json_decode($clean, true);
if (!is_array($json) || !isset($json['action'])) {
return ['action' => 'chat', 'text' => $clean];
}
if ($json['action'] === 'route' && isset($json['key'])) {
// Validate key exists in flows
$allFlows = $config['flows'] ?? [];
foreach ($config['per_type'] ?? [] as $pt) {
$allFlows = array_merge($allFlows, $pt['flows'] ?? []);
}
if (isset($allFlows[$json['key']])) {
return ['action' => 'route', 'key' => $json['key']];
}
}
return ['action' => 'chat', 'text' => $json['text'] ?? $clean];
}
private static function buildFlowCatalog(array $config, int $permType): array
{
$flows = $config['flows'] ?? [];
$menus = $config['menus'] ?? [];
// Build human labels from menu rows/buttons
$labels = [];
foreach ($menus as $menu) {
foreach ($menu['sections'] ?? [] as $section) {
foreach ($section['rows'] ?? [] as $row) {
if (($row['id'] ?? '') !== '') $labels[$row['id']] = $row['title'] ?? $row['id'];
}
}
foreach ($menu['buttons'] ?? [] as $btn) {
if (($btn['id'] ?? '') !== '') $labels[$btn['id']] = $btn['title'] ?? $btn['id'];
}
}
$catalog = [];
foreach ($flows as $key => $flow) {
$type = $flow['type'] ?? 'text';
$fn = $flow['function'] ?? '';
$isUpload = in_array($type, ['collect_and_post', 'collect_for_each', 'submit_form'], true);
$isDownload = ($type === 'function' && $fn === 'api_report');
// Permission filter:
// type 1 = solo reporta (upload), type 2 = solo recibe (download), type 3 = ambos
if ($isUpload && !in_array($permType, [1, 3], true)) continue;
if ($isDownload && !in_array($permType, [2, 3], true)) continue;
// Skip purely internal/navigation flows
if (in_array($type, ['text', 'image'], true) && !$isUpload && !$isDownload) continue;
$label = $flow['nlu_description'] ?? $labels[$key] ?? $key;
$dir = $isUpload ? 'subida' : ($isDownload ? 'descarga' : '');
$catalog[] = ['key' => $key, 'label' => $label, 'dir' => $dir];
}
return $catalog;
}
private static function buildNluPrompt(string $company, array $catalog, int $permType): string
{
$permLabel = match ($permType) {
1 => 'puede subir/reportar datos al sistema',
2 => 'puede descargar reportes e informes',
3 => 'puede subir datos y descargar reportes',
default => 'acceso básico',
};
$lines = array_map(fn($item) =>
'- ' . $item['key'] . ': "' . $item['label'] . '"' . ($item['dir'] !== '' ? ' [' . $item['dir'] . ']' : ''),
$catalog
);
return "Eres el asistente de {$company}. El usuario {$permLabel}.\n\n"
. "Opciones disponibles:\n" . implode("\n", $lines) . "\n\n"
. "Analiza el mensaje y determina si se refiere claramente a una opción.\n\n"
. "Si sí → responde SOLO este JSON (sin markdown):\n"
. "{\"action\":\"route\",\"key\":\"<key_exacto>\"}\n\n"
. "Si no está claro o no hay opción correspondiente → responde SOLO:\n"
. "{\"action\":\"chat\",\"text\":\"<respuesta corta en español, máx 2 oraciones>\"}\n\n"
. "No inventes keys. Usa exactamente los keys de la lista.";
}
/** Llamada de 1 turno sin historial — para NLU routing. */
private static function callLlmOnce(string $systemPrompt, string $userMessage, array $company): ?string
{
$cfg = self::getConfig($company);
$provider = $cfg['ai_provider'] ?? env('AI_PROVIDER', 'openai');
$history = [['role' => 'user', 'content' => $userMessage]];
$result = match ($provider) {
'openai' => self::callOpenAI($systemPrompt, $history, $company),
'gemini' => self::callGemini($systemPrompt, $history, $company),
'claude' => self::callClaude($systemPrompt, $history, $company),
default => null,
};
return $result['content'] ?? null;
}
// ── Shared helpers ────────────────────────────────────────────────────────
private static function getConfig(array $company): array
{
$json = $company['config_json'] ?? '';