added stream thought

This commit is contained in:
anovazzi1 2023-05-05 19:06:07 -03:00
commit 88c5cd9147

View file

@ -26,7 +26,7 @@ export default function ChatModal({
const [chatHistory, setChatHistory] = useState<ChatMessageType[]>([]); const [chatHistory, setChatHistory] = useState<ChatMessageType[]>([]);
const { reactFlowInstance } = useContext(typesContext); const { reactFlowInstance } = useContext(typesContext);
const { setErrorData, setNoticeData } = useContext(alertContext); const { setErrorData, setNoticeData } = useContext(alertContext);
const ws = useRef<WebSocket|null>(null) const ws = useRef<WebSocket | null>(null);
const [lockChat, setLockChat] = useState(false); const [lockChat, setLockChat] = useState(false);
const isOpen = useRef(open); const isOpen = useRef(open);
@ -54,13 +54,18 @@ export default function ChatModal({
}); });
}; };
//add proper type signature for function
function updateLastMessage(str: string) { function updateLastMessage({str,thought}:{str?: string, thought?: string}) {
console.log(str);
setChatHistory((old) => { setChatHistory((old) => {
let newChat = [...old]; let newChat = [...old];
newChat[newChat.length - 1].message = if (str) {
newChat[newChat.length - 1].message + str; newChat[newChat.length - 1].message =
newChat[newChat.length - 1].message + str;
}
if(thought){
newChat[newChat.length - 1].thought = thought
}
return newChat; return newChat;
}); });
} }
@ -69,7 +74,7 @@ export default function ChatModal({
if (isOpen.current) { if (isOpen.current) {
setLockChat(false); setLockChat(false);
setTimeout(() => { setTimeout(() => {
connectWS() connectWS();
}, 1000); }, 1000);
} }
} }
@ -114,6 +119,9 @@ export default function ChatModal({
isStream = true; isStream = true;
} }
if (data.type === "end") { if (data.type === "end") {
if(data.intermediate_steps){
updateLastMessage({thought:data.intermediate_steps});
}
setLockChat(false); setLockChat(false);
isStream = false; isStream = false;
// if (data.files) { // if (data.files) {
@ -131,7 +139,7 @@ export default function ChatModal({
console.log(data); console.log(data);
} }
if (data.type === "stream" && isStream) { if (data.type === "stream" && isStream) {
updateLastMessage(data.message); updateLastMessage({str:data.message});
} }
} }
@ -169,7 +177,7 @@ export default function ChatModal({
], ],
}); });
}; };
ws.current=newWs ws.current = newWs;
} catch { } catch {
setErrorData({ setErrorData({
title: "There was an error on web connection, please: ", title: "There was an error on web connection, please: ",
@ -185,15 +193,14 @@ export default function ChatModal({
useEffect(() => { useEffect(() => {
connectWS(); connectWS();
return () => { return () => {
console.log("unmount") console.log("unmount");
console.log(ws) console.log(ws);
if (ws) { if (ws) {
ws.current.close(); ws.current.close();
} }
}; };
}, []); }, []);
async function sendAll(data: sendAllProps) { async function sendAll(data: sendAllProps) {
try { try {
if (ws) { if (ws) {