forked from React-Group/interstellar_ai
Compare commits
2 commits
eed60ab282
...
82f59fb0d0
Author | SHA1 | Date | |
---|---|---|---|
82f59fb0d0 | |||
299539dc86 |
2 changed files with 13 additions and 10 deletions
|
@ -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(() => {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue