This commit is contained in:
YasinOnm08 2024-10-09 15:56:06 +02:00
parent 416dcb17ef
commit 299539dc86
2 changed files with 13 additions and 10 deletions

View file

@ -21,7 +21,7 @@ const InputOutputBackend: React.FC = () => {
const [preferredMeasurement, setPreferredMeasurement] = useState<string>("metric"); const [preferredMeasurement, setPreferredMeasurement] = useState<string>("metric");
const [timeZone, setTimeZone] = useState<string>("GMT"); const [timeZone, setTimeZone] = useState<string>("GMT");
const [dateFormat, setDateFormat] = useState<string>("DD-MM-YYYY"); const [dateFormat, setDateFormat] = useState<string>("DD-MM-YYYY");
const [messages, setMessages] = useState<Message[]>(chatHistory.chats[chatHistory.selectedIndex]?.messages || []); const [messages, setMessages] = useState<Message[]>(chatHistory.chats[chatHistory.selectedIndex].messages || []);
const [myBoolean, setMyBoolean] = useState<boolean>(false); const [myBoolean, setMyBoolean] = useState<boolean>(false);
const [systemMessage, setSystemMessage] = useState<string>("You are a helpful assistant") const [systemMessage, setSystemMessage] = useState<string>("You are a helpful assistant")
const apiURL = new URL("http://localhost:5000/interstellar_ai/api/ai_create") const apiURL = new URL("http://localhost:5000/interstellar_ai/api/ai_create")
@ -31,22 +31,26 @@ const InputOutputBackend: React.FC = () => {
apiURL.hostname = "localhost" apiURL.hostname = "localhost"
} }
console.log(setSelectedIndex)
useEffect(() => { useEffect(() => {
console.log("History", chatHistory); console.log("History", chatHistory);
console.log("Messages", messages); console.log("Messages", messages);
// Get the current chat's messages // Get the current chat's messages
const currentMessages = chatHistory.chats[chatHistory.selectedIndex]?.messages || []; const currentMessages = chatHistory.chats[chatHistory.selectedIndex].messages || [];
// If currentMessages is not empty, update messages only if it's not the same // If the selected chat has messages, set them
if (currentMessages.length > 0 && JSON.stringify(currentMessages) !== JSON.stringify(messages)) { if (currentMessages.length > 0) {
setMessages(currentMessages); setMessages(currentMessages);
} else if (messages.length === 0) { } else if (currentMessages.length === 0) {
setMessages([{ role: "system", content: systemMessage }, { role: "assistant", content: "Hello! How can I help you?" }]); // When creating a new chat and no messages exist yet, set default messages
addMessage("system", systemMessage)
addMessage("assistant", "Hello! How can I help you?")
console.log(systemMessage)
} }
}, [chatHistory, setSelectedIndex]); }, [chatHistory, chatHistory.selectedIndex, systemMessage]);
// Update messages when any of the settings change // Update messages when any of the settings change
useEffect(() => { useEffect(() => {

View file

@ -19,8 +19,7 @@ interface GlobalChatHistory {
let globalChatHistory: GlobalChatHistory = { let globalChatHistory: GlobalChatHistory = {
chats: [ chats: [
{ name: "Chat 1", messages: [{role:"system",content:"you are a helpful assistant"},{role:"assistant",content:"how can i help you"}], timestamp: 4 }, { name: "Welcome!", messages: [{role:"system",content:"you are a helpful assistant"},{role:"assistant",content:"Hello! How can I help you?"}], timestamp: 4 },
{ name: "Chat 2", messages: [{role:"system",content:"you are a helpful assistant"},{role:"assistant",content:"how can i help you"}], timestamp: 4 },
], ],
selectedIndex:0 selectedIndex:0
} }