import React, { useState, useRef, useEffect } from 'react'; import { motion } from 'framer-motion'; const AICommerceAgent = () => { const [messages, setMessages] = useState([ { role: 'agent', text: 'How can I assist your logistics operations today?' } ]); const [inputText, setInputText] = useState(''); const [loading, setLoading] = useState(false); const [activeTools, setActiveTools] = useState([]); const [checkoutStatus, setCheckoutStatus] = useState(null); // { status: 'processing' | 'success', orderId?: string } const messagesEndRef = useRef(null); const scrollToBottom = () => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); }; useEffect(() => { scrollToBottom(); }, [messages, loading, checkoutStatus]); const handleSend = async () => { if (!inputText.trim()) return; const userMessage = { role: 'user', text: inputText }; setMessages(prev => [...prev, userMessage]); setInputText(''); setLoading(true); setActiveTools([]); try { // Construct history for API (only user/model roles, mapping 'agent' to 'model') const history = messages.map(msg => ({ role: msg.role === 'agent' ? 'model' : 'user', text: msg.text })); const response = await fetch('/api/v1/chat', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message: userMessage.text, history: history }) }); if (!response.ok) throw new Error('Chat API failed'); const data = await response.json(); if (data.tools && data.tools.length > 0) { setActiveTools(data.tools); } const agentMessage = { role: 'agent', text: data.reply, hasCheckout: data.reply.toLowerCase().includes('order') || data.reply.toLowerCase().includes('checkout') || data.reply.toLowerCase().includes('book') || data.reply.toLowerCase().includes('restaurant') }; setMessages(prev => [...prev, agentMessage]); } catch (error) { console.error('Chat error:', error); setMessages(prev => [...prev, { role: 'agent', text: 'Connection to District Intelligence failed.' }]); } finally { setLoading(false); } }; const handleKeyDown = (e) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSend(); } }; const handleCheckout = async () => { setCheckoutStatus({ status: 'processing' }); try { const token = localStorage.getItem('demo_token'); const response = await fetch('/api/v1/orders/reserve', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` }, body: JSON.stringify({ items: [{ product_id: "prd_101", quantity: 1, price: 50.0 }], total_amount: 50.0 }) }); if (!response.ok) throw new Error('Checkout failed'); const data = await response.json(); setCheckoutStatus({ status: 'success', orderId: data.transaction_id }); setMessages(prev => [...prev, { role: 'agent', text: `Transaction complete. Order ID: ${data.transaction_id}. Logistics route verified.` }]); } catch (error) { console.error('Checkout error:', error); setCheckoutStatus({ status: 'error' }); setMessages(prev => [...prev, { role: 'agent', text: `Transaction failed. Error verifying inventory.` }]); } }; return ( <> {/* Top Navigation Bar */} District AI Agent Delivery Groceries location_on Downtown Hub {/* Left Pane: History & MCP Tools */} {/* Right Pane: Chat Window */} {/* Subtle atmospheric background */} {/* Chat Content */} {/* Agent Welcome */} smart_toy District Intelligence Powered by Gemini 2.0 & Swiggy MCP {messages.map((msg, index) => ( {msg.role === 'agent' && ( analytics System Output )} {msg.text} {msg.hasCheckout && checkoutStatus?.status !== 'success' && ( {checkoutStatus?.status === 'processing' ? 'Processing...' : 'Authorize Transaction'} )} ))} {loading && ( Processing )} {/* Input Dock */} setInputText(e.target.value)} onKeyDown={handleKeyDown} > send System Online MCP 1.4.2 Connected > ); }; export default AICommerceAgent;
Powered by Gemini 2.0 & Swiggy MCP
{msg.text}