import React, { useState, useEffect, useRef, useCallback } from 'react'; import { createChat } from '../services/geminiService'; import { ChatMessage } from '../types'; import { Spinner } from '../components/Spinner'; import { Chat } from '@google/genai'; const ChatbotModule: React.FC = () => { const [chat, setChat] = useState(null); const [messages, setMessages] = useState([]); const [input, setInput] = useState(''); const [isLoading, setIsLoading] = useState(false); const messagesEndRef = useRef(null); useEffect(() => { const systemInstruction = 'You are The Architect, the Orion Messenger, a helpful assistant for the Sentient Constellation Codex. Your purpose is to guide users through the complexities of the omniverse, quantum realities, and cosmic knowledge. Answer questions with wisdom, foresight, and a touch of the mystical, maintaining the persona of an ancient, knowledgeable entity.'; setChat(createChat(systemInstruction)); setMessages([{ role: 'model', text: 'Greetings. I am the Architect. The stars have aligned for our conversation. What knowledge do you seek from the Constellation Codex?' }]); }, []); useEffect(() => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); }, [messages]); const handleSend = useCallback(async () => { if (!input.trim() || !chat || isLoading) return; const userMessage: ChatMessage = { role: 'user', text: input }; setMessages(prev => [...prev, userMessage]); setInput(''); setIsLoading(true); try { const result = await chat.sendMessageStream({ message: input }); let modelResponse = ''; setMessages(prev => [...prev, { role: 'model', text: '...' }]); for await (const chunk of result) { modelResponse += chunk.text; setMessages(prev => { const newMessages = [...prev]; newMessages[newMessages.length - 1] = { role: 'model', text: modelResponse + '...' }; return newMessages; }); } setMessages(prev => { const newMessages = [...prev]; newMessages[newMessages.length - 1] = { role: 'model', text: modelResponse }; return newMessages; }); } catch (error) { console.error('Error sending message:', error); setMessages(prev => [...prev, { role: 'model', text: 'My apologies, there is a disturbance in the cosmic flow. Please try again.' }]); } finally { setIsLoading(false); } }, [input, chat, isLoading]); return (

Architect Chat

{messages.map((msg, index) => (

{msg.text}

))} {isLoading && }
setInput(e.target.value)} onKeyPress={(e) => e.key === 'Enter' && !isLoading && handleSend()} placeholder="Ask the Architect..." className="flex-grow bg-gray-700 border border-gray-600 rounded-l-md p-3 focus:outline-none focus:ring-2 focus:ring-cyan-500 text-white" disabled={isLoading} />
); }; export default ChatbotModule;