Fix useRef typo and update chat message reference

This commit is contained in:
anovazzi1 2024-02-27 20:50:22 -03:00
commit 42338a829b
2 changed files with 41 additions and 37 deletions

View file

@ -1,5 +1,5 @@
import Convert from "ansi-to-html"; import Convert from "ansi-to-html";
import { useEffect, useMemo, useState,useRef } from "react"; import { useEffect, useMemo, useState, useRef } from "react";
import Markdown from "react-markdown"; import Markdown from "react-markdown";
import rehypeMathjax from "rehype-mathjax"; import rehypeMathjax from "rehype-mathjax";
import remarkGfm from "remark-gfm"; import remarkGfm from "remark-gfm";
@ -32,6 +32,13 @@ export default function ChatMessage({
const [isStreaming, setIsStreaming] = useState(false); const [isStreaming, setIsStreaming] = useState(false);
const eventSource = useRef<EventSource | undefined>(undefined); const eventSource = useRef<EventSource | undefined>(undefined);
const updateFlowPool = useFlowStore((state) => state.updateFlowPool); const updateFlowPool = useFlowStore((state) => state.updateFlowPool);
const chatMessageRef = useRef(chatMessage);
// Sync ref with state
useEffect(() => {
chatMessageRef.current = chatMessage;
}, [chatMessage]);
// The idea now is that chat.stream_url MAY be a URL if we should stream the output of the chat // The idea now is that chat.stream_url MAY be a URL if we should stream the output of the chat
@ -63,28 +70,26 @@ export default function ChatMessage({
}; };
useEffect(() => { useEffect(() => {
console.log(streamUrl) console.log("chatMessage", chatMessage);
if (streamUrl&& !isStreaming) { if (streamUrl && !isStreaming) {
streamChunks(streamUrl) streamChunks(streamUrl)
.then(() => { .then(() => {
if (updateChat) { if (updateChat) {
console.log("rodou") updateChat(chat, chatMessageRef.current);
updateChat(chat, chatMessage);
} }
}) })
.catch((error) => { .catch((error) => {
console.error(error); console.error(error);
}); });
} }
}, [streamUrl,chatMessage]); }, [streamUrl, chatMessage]);
useEffect(()=>{ useEffect(() => {
return () => { return () => {
eventSource.current?.close(); eventSource.current?.close();
} }
},[]) }, [])
useEffect(() => { useEffect(() => {
const element = document.getElementById("last-chat-message"); const element = document.getElementById("last-chat-message");
@ -213,7 +218,7 @@ dark:prose-invert"
}, },
]} ]}
activeTab={"0"} activeTab={"0"}
setActiveTab={() => {}} setActiveTab={() => { }}
/> />
) : ( ) : (
<code className={className} {...props}> <code className={className} {...props}>
@ -270,33 +275,33 @@ dark:prose-invert"
<span className="prose text-primary word-break-break-word dark:prose-invert"> <span className="prose text-primary word-break-break-word dark:prose-invert">
{promptOpen {promptOpen
? template?.split("\n")?.map((line, index) => { ? template?.split("\n")?.map((line, index) => {
const regex = /{([^}]+)}/g; const regex = /{([^}]+)}/g;
let match; let match;
let parts: Array<JSX.Element | string> = []; let parts: Array<JSX.Element | string> = [];
let lastIndex = 0; let lastIndex = 0;
while ((match = regex.exec(line)) !== null) { while ((match = regex.exec(line)) !== null) {
// Push text up to the match // Push text up to the match
if (match.index !== lastIndex) { if (match.index !== lastIndex) {
parts.push(line.substring(lastIndex, match.index)); parts.push(line.substring(lastIndex, match.index));
} }
// Push div with matched text // Push div with matched text
if (chat.message[match[1]]) { if (chat.message[match[1]]) {
parts.push( parts.push(
<span className="chat-message-highlight"> <span className="chat-message-highlight">
{chat.message[match[1]]} {chat.message[match[1]]}
</span> </span>
); );
} }
// Update last index // Update last index
lastIndex = regex.lastIndex; lastIndex = regex.lastIndex;
} }
// Push text after the last match // Push text after the last match
if (lastIndex !== line.length) { if (lastIndex !== line.length) {
parts.push(line.substring(lastIndex)); parts.push(line.substring(lastIndex));
} }
return <p>{parts}</p>; return <p>{parts}</p>;
}) })
: chatMessage} : chatMessage}
</span> </span>
</> </>

View file

@ -123,7 +123,6 @@ export default function NewChatView({
) { ) {
if (message === "") return; if (message === "") return;
chat.message = message; chat.message = message;
console.log(message)
// chat is one of the chatHistory // chat is one of the chatHistory
updateFlowPool(chat.componentId,{message,sender_name:chat.sender_name??"Bot",sender:"Machine"}) updateFlowPool(chat.componentId,{message,sender_name:chat.sender_name??"Bot",sender:"Machine"})
// setChatHistory((oldChatHistory) => { // setChatHistory((oldChatHistory) => {