diff --git a/components/AiButton.js b/components/AiButton.js index 0d8d1da..8d03d86 100644 --- a/components/AiButton.js +++ b/components/AiButton.js @@ -1,4 +1,3 @@ - import React, { useState } from "react"; import ChatComponent from "./chatbutton1"; // Adjust the path as needed @@ -13,29 +12,45 @@ function AiButton() { return (
$1
')
+ // Inline code
+ .replace(/`([^`]+)`/g, '$1
')
+ // Unordered lists
+ .replace(/^\* (.*$)/gm, '')
+ .replace(/\n/g, '
')
+ // Wrap in paragraph tags
+ .replace(/^(?!<[h1-6]|
$1
'); + + return html; + }; + + return ( + + ); +}; + function ChatComponent({ onClose }) { // holds the current user input and the chat history. const [inputMessage, setInputMessage] = useState(""); const [chatHistory, setChatHistory] = useState([]); const [isLoading, setIsLoading] = useState(false); + // New state to track whether to show suggested questions + const [showSuggestions, setShowSuggestions] = useState(true); + // New state to store the randomly selected questions + const [randomQuestions, setRandomQuestions] = useState([]); + + // List Predefined questions + const suggestedQuestions = [ + "What is the Allora Network?", + "What role do worker nodes play in the network?", + "What are the different layers of the network?", + "What are the main defining characteristics of Allora?", + "What are the differences between Reputers and Validators?", + "How do consumers access inferences within Allora?", + "What role does context awareness play in Allora's design, and how is it achieved through forecasting tasks?", + "How does Allora ensure the reliability and security of the network?", + "How does the tokenomics design of the Allora token (ALLO) ensure long-term network sustainability?", + ]; + + // Function to select 3 random questions + const getRandomQuestions = () => { + const shuffled = [...suggestedQuestions].sort(() => 0.5 - Math.random()); + return shuffled.slice(0, 3); + }; + + // Initialize with 3 random questions on component mount + useEffect(() => { + setRandomQuestions(getRandomQuestions()); + }, []); + + // this references the chat history container. const chatContainerRef = useRef(null); @@ -16,93 +86,286 @@ function ChatComponent({ onClose }) { } }, [chatHistory]); - // this is handler for form submission. - const handleSubmit = async (e) => { - e.preventDefault(); - - // Store the message and immediately clear the input field - const message = inputMessage; - setInputMessage(""); // Clear input immediately - - // Add user's message to the chat history. - const newUserEntry = { sender: "user", text: message }; - setChatHistory((prev) => [...prev, newUserEntry]); - - // Show loading indicator - setIsLoading(true); - - try { - // Send user's message to the FastAPI backend. - const response = await fetch("https://b832b91b8183b88b9c22eda604f1e09.testnet.allora.run/chat", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ message: message }), - }); - console.log("API went through"); - - if (!response.ok) { - throw new Error(`Server error: ${response.statusText}`); + // Hide suggestions when conversation starts + useEffect(() => { + if (chatHistory.length > 0) { + setShowSuggestions(false); } - - // Parse the JSON response. - const data = await response.json(); - - // Add the assistant's response to the chat history. - const newBotEntry = { - sender: "bot", - text: data.response, - sources: data.sources, + }, [chatHistory]); + // New handler for when a suggested question is clicked + const handleSuggestionClick = (question) => { + // Process the suggested question like a regular submission + handleMessageSubmit(question); + }; + + // Extracted logic to handle message submission + const handleMessageSubmit = async (message) => { + // Add user's message to the chat history. + const newUserEntry = { + sender: "user", + text: message.trim() // Trim any extra whitespace }; - setChatHistory((prev) => [...prev, newBotEntry]); - } catch (error) { - console.error("Error fetching chat response:", error); - // display an error message in the UI. - const errorEntry = { - sender: "bot", text: "Sorry, something went wrong." - }; - setChatHistory((prev) => [...prev, errorEntry]); - } finally { - // Hide loading indicator - setIsLoading(false); - } - }; - + setChatHistory((prev) => [...prev, newUserEntry]); + + // Show loading indicator + setIsLoading(true); + + try { + // Send user's message to the FastAPI backend. + const response = await fetch("https://b832b91b8183b88b9c22eda604f1e09.testnet.allora.run/chat", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ message: message.trim() }), + }); + console.log("API went through"); + + if (!response.ok) { + throw new Error(`Server error: ${response.statusText}`); + } + + // Parse the JSON response. + const data = await response.json(); + + // Add the assistant's response to the chat history. + const newBotEntry = { + sender: "bot", + text: data.response, + sources: data.sources, + }; + setChatHistory((prev) => [...prev, newBotEntry]); + } catch (error) { + console.error("Error fetching chat response:", error); + // display an error message in the UI. + const errorEntry = { + sender: "bot", + text: "Sorry, something went wrong. Please try again." + }; + setChatHistory((prev) => [...prev, errorEntry]); + } finally { + // Hide loading indicator + setIsLoading(false); + } + }; + + // this is handler for form submission. + const handleSubmit = async (e) => { + e.preventDefault(); + + // Store the message and immediately clear the input field + const message = inputMessage; + setInputMessage(""); // Clear input immediately + + // Process the message + await handleMessageSubmit(message); + }; return ({entry.text}
- {entry.sources && entry.sources.length > 0 } + {entry.sender === "bot" ? ( +