// FLUX Copilot — IA assistant via window.claude.complete function Copilot({open, onClose, onAction}){ const I = window.Icons; const [msgs, setMsgs] = React.useState([ { role:'ai', text:'¡Hola! Soy FLUX Copilot, tu asistente de inventario y analytics. Puedo analizar tus datos, sugerir transferencias, generar reportes o explicar tendencias. ¿En qué te ayudo?' } ]); const [input, setInput] = React.useState(''); const [thinking, setThinking] = React.useState(false); const bodyRef = React.useRef(null); const quick = [ '¿Qué SKUs tienen mayor riesgo hoy?', 'Resume el desempeño del mes', '¿Qué tienda necesita más atención?', 'Sugiere transferencias inter-tienda', ]; const send = async (textOverride) => { const text = textOverride ?? input; if(!text.trim() || thinking) return; setMsgs(m=>[...m,{role:'user',text}]); setInput(''); setThinking(true); const D = window.FLUXData; const ctx = `Eres FLUX Copilot, asistente de analytics e inventario para una cadena retail mexicana. Datos actuales (mayo 2025): - 8 tiendas, 6,842 SKUs, ventas mensuales $8.69M MXN, quiebres reducidos 78% YoY. - Tiendas top: ${D.stores.slice(0,3).map(s=>`${s.name} ($${(s.sales/1000000).toFixed(1)}M)`).join(', ')}. - SKUs en riesgo: ${D.skus.filter(s=>s.risk>0.6).map(s=>`${s.sku} (${s.name}, riesgo ${Math.round(s.risk*100)}%, stock ${s.stock})`).join('; ')}. - Alertas críticas: ${D.alerts.filter(a=>a.level==='critical').length}. - Modelo activo: Claude Forecast v2.4, MAPE 4.8%. Responde en español, conciso (2-4 frases), con números específicos cuando corresponda. Si el usuario pide una acción concreta (transferencia, reporte, promoción), termina sugiriendo "Ejecutar [acción]".`; try{ const res = await window.claude.complete({ messages: [ { role:'user', content: ctx + '\n\nPregunta: ' + text } ] }); setMsgs(m=>[...m,{role:'ai',text:res}]); }catch(e){ setMsgs(m=>[...m,{role:'ai',text:'Hubo un problema procesando tu pregunta. Intenta de nuevo.'}]); }finally{ setThinking(false); setTimeout(()=>{ if(bodyRef.current) bodyRef.current.scrollTop = bodyRef.current.scrollHeight; }, 50); } }; React.useEffect(()=>{ if(bodyRef.current) bodyRef.current.scrollTop = bodyRef.current.scrollHeight; },[msgs.length]); if(!open) return null; return (
FLUX Copilot
Powered by Claude · MAPE 4.8%
{msgs.map((m,i)=>(
$1')}}/>
))} {thinking && (
)} {msgs.length<=1 && !thinking && (
{quick.map((q,i)=>
send(q)}>{q}
)}
)}
setInput(e.target.value)} onKeyDown={e=>e.key==='Enter'&&send()} placeholder="Pregunta lo que sea sobre tu inventario..."/>
); } function CopilotFab({onClick, hidden}){ const I = window.Icons; if(hidden) return null; return ( ); } window.Copilot = Copilot; window.CopilotFab = CopilotFab;