Merge pull request 'main' (#130) from YasinOnm08/interstellar_ai:main into main

Reviewed-on: https://interstellardevelopment.org/code/code/React-Group/interstellar_ai/pulls/130
This commit is contained in:
Patrick 2024-10-09 15:57:39 +02:00
commit 12d7ac37fe
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 [timeZone, setTimeZone] = useState<string>("GMT");
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 [systemMessage, setSystemMessage] = useState<string>("You are a helpful assistant")
const apiURL = new URL("http://localhost:5000/interstellar_ai/api/ai_create")
@ -31,22 +31,26 @@ const InputOutputBackend: React.FC = () => {
apiURL.hostname = "localhost"
}
console.log(setSelectedIndex)
useEffect(() => {
useEffect(() => {
console.log("History", chatHistory);
console.log("Messages", 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 (currentMessages.length > 0 && JSON.stringify(currentMessages) !== JSON.stringify(messages)) {
// If the selected chat has messages, set them
if (currentMessages.length > 0) {
setMessages(currentMessages);
} else if (messages.length === 0) {
setMessages([{ role: "system", content: systemMessage }, { role: "assistant", content: "Hello! How can I help you?" }]);
} else if (currentMessages.length === 0) {
// 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
useEffect(() => {

View file

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