'use client'; import React, { useState, useRef, useEffect } from 'react'; import { Send, Bot, User, AlertTriangle, Code, ChevronDown } from 'lucide-react'; const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000/api/v1/chat'; const USER_ID = 1; const ChatInterface = () => { const [messages, setMessages] = useState([]); const [input, setInput] = useState(''); const [isLoading, setIsLoading] = useState(false); const messagesEndRef = useRef(null); const scrollToBottom = () => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); }; useEffect(() => { scrollToBottom(); }, [messages]); const addMessage = (content, type = 'bot') => { setMessages((prev) => [...prev, { content, type }]); }; const handleSend = async () => { const text = input.trim(); if (!text) return; addMessage(text, 'user'); setInput(''); setIsLoading(true); try { const res = await fetch(API_URL, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message: text, user_id: USER_ID, }), }); if (!res.ok) throw new Error(`HTTP ${res.status}`); const data = await res.json(); renderResponse(data); } catch (e) { addMessage(`❌ خطا در ارسال پیام: ${e.message}`, 'bot'); } finally { setIsLoading(false); } }; const renderResponse = (data) => { let html = ''; // متن اصلی if (data.type === 'text') { html += `

${data.data.message}

`; } // نتیجه محصول if (data.type === 'product_list') { html += `

نتیجه: ${data.data.message}

`; html += `

تعداد: ${data.data.total_count}

`; data.data.products.forEach((p) => { html += `
${p.name}
قیمت: ${p.formatted_price} تومان
وضعیت: ${p.stock ? 'موجود' : 'ناموجود'}
برند: ${p.brand || '-'} | شهر: ${p.city || '-'}
`; }); } // 🔴 اگر ابهام وجود داشت if (data.debug && data.debug.is_ambiguous && data.debug.clarification_question) { html += `

❗ ابهام تشخیص داده شد: ${data.debug.clarification_question}

`; } // 🔍 JSON فیلترها (همیشه) if (data.debug) { html += `
🧪 فیلترهای استخراج‌شده توسط LLM
{JSON.stringify(data.debug, null, 2)}
`; } addMessage(html, 'bot'); }; return (

🧠 Enterprise Search Chat

Built with anycoder
{messages.length === 0 && (

چطور می‌توانم کمکتان کنم؟

)} {messages.map((msg, idx) => (
{msg.type === 'user' ? ( ) : ( )}
{msg.content}
))} {isLoading && (
)}
setInput(e.target.value)} onKeyPress={(e) => e.key === 'Enter' && handleSend()} placeholder="مثلاً: برند اپکس بالای 5 میلیون" disabled={isLoading} className="flex-1 px-4 py-3 rounded-xl border border-gray-300 focus:outline-none focus:ring-2 focus:ring-brand-500 disabled:bg-gray-100 disabled:cursor-not-allowed" />
); }; export default ChatInterface;