diff --git a/modules/turnero/api/ai_chat.php b/modules/turnero/api/ai_chat.php index ba1035b..e63ccf2 100644 --- a/modules/turnero/api/ai_chat.php +++ b/modules/turnero/api/ai_chat.php @@ -1,7 +1,7 @@ 800) jsonError('Pregunta demasiado larga', 400); +// Historial de la conversación, para que LIA entienda preguntas de seguimiento +// ("¿y de esos cuántos fueron en la mañana?"). Se limita a los últimos turnos +// para no inflar el consumo de tokens en cada llamada. +$historial = []; +foreach (array_slice(is_array($body['historial'] ?? null) ? $body['historial'] : [], -8) as $h) { + $rol = ($h['rol'] ?? '') === 'model' ? 'model' : 'user'; + $texto = trim((string)($h['texto'] ?? '')); + if ($texto === '') continue; + $historial[] = ['role' => $rol, 'parts' => [['text' => mb_substr($texto, 0, 2000)]]]; +} + define('LIA_TOKENS_MAX', 1_000_000); $pdo = db(); @@ -173,9 +184,11 @@ if (!$ses) { // ── Prompt ──────────────────────────────────────────────────── $systemPrompt = << ['parts' => [['text' => $systemPrompt]]], - 'contents' => [ + 'contents' => array_merge($historial, [ ['role' => 'user', 'parts' => [['text' => $pregunta]]] - ], + ]), 'generationConfig' => [ 'temperature' => 0.3, // 600 cortaba a media frase las respuestas largas (listados de turnos, diff --git a/modules/turnero/views/dashboard.php b/modules/turnero/views/dashboard.php index e4afd7f..5a87e6e 100644 --- a/modules/turnero/views/dashboard.php +++ b/modules/turnero/views/dashboard.php @@ -393,6 +393,7 @@ const API = 'modules/turnero/api/'; const BASE_WA = ''; let _turnos = []; // cache para búsqueda local let _autoReloadId = null; +let _liaHistorial = []; // últimos intercambios con LIA (contexto de seguimiento) // ── Entrada ────────────────────────────────────────────────── document.addEventListener('DOMContentLoaded', () => { @@ -874,13 +875,17 @@ async function enviarIA() { const res = await fetch(`${API}ai_chat.php`, { method: 'POST', headers: {'Content-Type':'application/json'}, - body: JSON.stringify({ pregunta }), + body: JSON.stringify({ pregunta, historial: _liaHistorial }), }); const d = await res.json(); typing.className = 'ai-msg bot'; if (d.ok) { typing.innerHTML = mdToHtml(d.respuesta); hablarIA(d.respuesta); + // Guardar el intercambio para que las preguntas de seguimiento tengan contexto + _liaHistorial.push({ rol: 'user', texto: pregunta }); + _liaHistorial.push({ rol: 'model', texto: d.respuesta }); + if (_liaHistorial.length > 8) _liaHistorial = _liaHistorial.slice(-8); if (d.tokens_max) _actualizarTokens(d.tokens_usados, d.tokens_max); } else { typing.textContent = d.error || 'Error al conectar con la IA';