Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete the message that was sent when the backend returned an error #105

Merged
merged 1 commit into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@
"last 1 safari version"
]
},
"proxy": "http://localhost:7860"
"proxy": "http://backend:7860"
}
73 changes: 52 additions & 21 deletions src/frontend/src/components/chatComponent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@ import {
PaperAirplaneIcon,
XMarkIcon,
} from "@heroicons/react/24/outline";
import { MouseEventHandler, useContext, useEffect, useRef, useState } from "react";
import {
MouseEventHandler,
useContext,
useEffect,
useRef,
useState,
} from "react";
import { sendAll } from "../../controllers/API";
import { alertContext } from "../../contexts/alertContext";
import { classNames, nodeColors } from "../../utils";
import { TabsContext } from "../../contexts/tabsContext";
import { ChatType } from "../../types/chat";
import ChatMessage from "./chatMessage";


const _ = require("lodash");

export default function Chat({ flow, reactFlowInstance }: ChatType) {
const { updateFlow,lockChat,setLockChat,flows,tabIndex } = useContext(TabsContext);
const { updateFlow, lockChat, setLockChat, flows, tabIndex } =
useContext(TabsContext);
const [saveChat, setSaveChat] = useState(false);
const [open, setOpen] = useState(true);
const [chatValue, setChatValue] = useState("");
Expand All @@ -26,14 +32,14 @@ export default function Chat({ flow, reactFlowInstance }: ChatType) {
const addChatHistory = (
message: string,
isSend: boolean,
thought?: string,
thought?: string
) => {
let tabsChange = false;
setChatHistory((old) => {
let newChat = _.cloneDeep(old);
if(JSON.stringify(flow.chat) !==JSON.stringify(old)){
tabsChange = true
return old
if (JSON.stringify(flow.chat) !== JSON.stringify(old)) {
tabsChange = true;
return old;
}
if (thought) {
newChat.push({ message, isSend, thought });
Expand All @@ -42,12 +48,17 @@ export default function Chat({ flow, reactFlowInstance }: ChatType) {
}
return newChat;
});
if(tabsChange){
if(thought){
updateFlow({..._.cloneDeep(flow),chat:[...flow.chat,{isSend,message,thought}]})
}
else{
updateFlow({..._.cloneDeep(flow),chat:[...flow.chat,{isSend,message}]})
if (tabsChange) {
if (thought) {
updateFlow({
..._.cloneDeep(flow),
chat: [...flow.chat, { isSend, message, thought }],
});
} else {
updateFlow({
..._.cloneDeep(flow),
chat: [...flow.chat, { isSend, message }],
});
}
}
setSaveChat((chat) => !chat);
Expand Down Expand Up @@ -98,19 +109,38 @@ export default function Chat({ flow, reactFlowInstance }: ChatType) {
setChatValue("");
addChatHistory(message, true);

sendAll({ ...reactFlowInstance.toObject(), message, chatHistory,name:flow.name,description:flow.description})
sendAll({
...reactFlowInstance.toObject(),
message,
chatHistory,
name: flow.name,
description: flow.description,
})
.then((r) => {
addChatHistory(r.data.result, false,r.data.thought);
addChatHistory(r.data.result, false, r.data.thought);
setLockChat(false);
})
.catch((error) => {
setErrorData({ title: error.message ?? "Unknown Error", list: [error.response.data.detail]});
setErrorData({
title: error.message ?? "Unknown Error",
list: [error.response.data.detail],
});
setLockChat(false);
let lastMessage;
setChatHistory((chatHistory) => {
let newChat = chatHistory;

lastMessage= newChat.pop().message;
return newChat;
});
setChatValue(lastMessage)
});
} else {
setErrorData({
title: "Error sending message",
list: [ "Oops! Looks like you missed some required information. Please fill in all the required fields before continuing."],
list: [
"Oops! Looks like you missed some required information. Please fill in all the required fields before continuing.",
],
});
}
} else {
Expand All @@ -121,8 +151,8 @@ export default function Chat({ flow, reactFlowInstance }: ChatType) {
}
}
function clearChat() {
setChatHistory([])
updateFlow({ ..._.cloneDeep(flow), chat: []});
setChatHistory([]);
updateFlow({ ..._.cloneDeep(flow), chat: [] });
}

return (
Expand Down Expand Up @@ -152,9 +182,10 @@ export default function Chat({ flow, reactFlowInstance }: ChatType) {
/>
Chat
</div>
<button className="hover:text-blue-500 dark:text-white"
<button
className="hover:text-blue-500 dark:text-white"
onClick={(e) => {
e.stopPropagation()
e.stopPropagation();
clearChat();
}}
>
Expand Down