fix: auto fill chat input on new sessions (#5034)

* Show chatview only if messages are fetched

* Get value from chatInput node to put in new sessions and make New Flow not flash on the screen
This commit is contained in:
Lucas Oliveira 2024-12-03 18:33:18 -03:00 • committed by GitHub
commit 213d657b00
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 69 additions and 59 deletions

View file

@ -1,7 +1,6 @@
import LangflowLogo from "@/assets/LangflowLogo.svg?react"; import LangflowLogo from "@/assets/LangflowLogo.svg?react";
import ChainLogo from "@/assets/logo.svg?react"; import ChainLogo from "@/assets/logo.svg?react";
import { TextEffectPerChar } from "@/components/ui/textAnimation"; import { TextEffectPerChar } from "@/components/ui/textAnimation";
import { TextShimmer } from "@/components/ui/TextShimmer";
import { ENABLE_NEW_LOGO } from "@/customization/feature-flags"; import { ENABLE_NEW_LOGO } from "@/customization/feature-flags";
import { track } from "@/customization/utils/analytics"; import { track } from "@/customization/utils/analytics";
import { useMessagesStore } from "@/stores/messagesStore"; import { useMessagesStore } from "@/stores/messagesStore";
@ -14,7 +13,6 @@ import FlowRunningSqueleton from "../flowRunningSqueleton";
import useDragAndDrop from "./chatInput/hooks/use-drag-and-drop"; import useDragAndDrop from "./chatInput/hooks/use-drag-and-drop";
import { useFileHandler } from "./chatInput/hooks/use-file-handler"; import { useFileHandler } from "./chatInput/hooks/use-file-handler";
import ChatInput from "./chatInput/newChatInput"; import ChatInput from "./chatInput/newChatInput";
import LogoIcon from "./chatMessage/components/chatLogoIcon";
import ChatMessage from "./chatMessage/newChatMessage"; import ChatMessage from "./chatMessage/newChatMessage";
export default function ChatView({ export default function ChatView({
@ -27,11 +25,17 @@ export default function ChatView({
focusChat, focusChat,
closeChat, closeChat,
}: chatViewProps): JSX.Element { }: chatViewProps): JSX.Element {
const { flowPool, inputs, CleanFlowPool } = useFlowStore(); const flowPool = useFlowStore((state) => state.flowPool);
const inputs = useFlowStore((state) => state.inputs);
const currentFlowId = useFlowsManagerStore((state) => state.currentFlowId); const currentFlowId = useFlowsManagerStore((state) => state.currentFlowId);
const messagesRef = useRef<HTMLDivElement | null>(null); const messagesRef = useRef<HTMLDivElement | null>(null);
const [chatHistory, setChatHistory] = useState<ChatMessageType[]>([]); const [chatHistory, setChatHistory] = useState<ChatMessageType[] | undefined>(
undefined,
);
const messages = useMessagesStore((state) => state.messages); const messages = useMessagesStore((state) => state.messages);
const nodes = useFlowStore((state) => state.nodes);
const chatInput = inputs.find((input) => input.type === "ChatInput");
const chatInputNode = nodes.find((node) => node.id === chatInput?.id);
const inputTypes = inputs.map((obj) => obj.type); const inputTypes = inputs.map((obj) => obj.type);
const updateFlowPool = useFlowStore((state) => state.updateFlowPool); const updateFlowPool = useFlowStore((state) => state.updateFlowPool);
@ -79,6 +83,9 @@ export default function ChatView({
return new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime(); return new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime();
}); });
if (finalChatHistory.length === 0 && !lockChat && chatInputNode)
setChatValue(chatInputNode.data.node.template["input_value"].value ?? "");
setChatHistory(finalChatHistory); setChatHistory(finalChatHistory);
}, [flowPool, messages, visibleSession]); }, [flowPool, messages, visibleSession]);
useEffect(() => { useEffect(() => {
@ -135,45 +142,46 @@ export default function ChatView({
onDrop={onDrop} onDrop={onDrop}
> >
<div ref={messagesRef} className="chat-message-div"> <div ref={messagesRef} className="chat-message-div">
{lockChat || chatHistory?.length > 0 ? ( {chatHistory &&
chatHistory.map((chat, index) => ( (lockChat || chatHistory?.length > 0 ? (
<ChatMessage chatHistory.map((chat, index) => (
setLockChat={setLockChat} <ChatMessage
lockChat={lockChat} setLockChat={setLockChat}
chat={chat} lockChat={lockChat}
lastMessage={chatHistory.length - 1 === index ? true : false} chat={chat}
key={`${chat.id}-${index}`} lastMessage={chatHistory.length - 1 === index ? true : false}
updateChat={updateChat} key={`${chat.id}-${index}`}
closeChat={closeChat} updateChat={updateChat}
/> closeChat={closeChat}
)) />
) : ( ))
<div className="flex h-full w-full flex-col items-center justify-center"> ) : (
<div className="flex flex-col items-center justify-center gap-4 p-8"> <div className="flex h-full w-full flex-col items-center justify-center">
{ENABLE_NEW_LOGO ? ( <div className="flex flex-col items-center justify-center gap-4 p-8">
<LangflowLogo {ENABLE_NEW_LOGO ? (
title="Langflow logo" <LangflowLogo
className="h-10 w-10 scale-[1.5]" title="Langflow logo"
/> className="h-10 w-10 scale-[1.5]"
) : ( />
<ChainLogo ) : (
title="Langflow logo" <ChainLogo
className="h-10 w-10 scale-[1.5]" title="Langflow logo"
/> className="h-10 w-10 scale-[1.5]"
)} />
<div className="flex flex-col items-center justify-center"> )}
<h3 className="mt-2 pb-2 text-2xl font-semibold text-primary"> <div className="flex flex-col items-center justify-center">
New chat <h3 className="mt-2 pb-2 text-2xl font-semibold text-primary">
</h3> New chat
<p className="text-lg text-muted-foreground"> </h3>
<TextEffectPerChar> <p className="text-lg text-muted-foreground">
Test your flow with a chat prompt <TextEffectPerChar>
</TextEffectPerChar> Test your flow with a chat prompt
</p> </TextEffectPerChar>
</p>
</div>
</div> </div>
</div> </div>
</div> ))}
)}
<div <div
className={ className={
lockChat ? "w-5/6 max-w-[768px] py-4 word-break-break-word" : "" lockChat ? "w-5/6 max-w-[768px] py-4 word-break-break-word" : ""
@ -181,7 +189,7 @@ export default function ChatView({
ref={ref} ref={ref}
> >
{lockChat && {lockChat &&
!(chatHistory[chatHistory.length - 1]?.category === "error") && !(chatHistory?.[chatHistory.length - 1]?.category === "error") &&
flowRunningSkeletonMemo} flowRunningSkeletonMemo}
</div> </div>
</div> </div>

View file

@ -132,7 +132,7 @@ export default function IOModal({
); );
const flowPool = useFlowStore((state) => state.flowPool); const flowPool = useFlowStore((state) => state.flowPool);
const [sessionId, setSessionId] = useState<string>(currentFlowId); const [sessionId, setSessionId] = useState<string>(currentFlowId);
useGetMessagesQuery( const { isFetched: messagesFetched } = useGetMessagesQuery(
{ {
mode: "union", mode: "union",
id: currentFlowId, id: currentFlowId,
@ -475,22 +475,24 @@ export default function IOModal({
: "", : "",
)} )}
> >
<ChatView {messagesFetched && (
focusChat={sessionId} <ChatView
sendMessage={sendMessage} focusChat={sessionId}
chatValue={chatValue} sendMessage={sendMessage}
setChatValue={setChatValue} chatValue={chatValue}
lockChat={lockChat} setChatValue={setChatValue}
setLockChat={setLockChat} lockChat={lockChat}
visibleSession={visibleSession} setLockChat={setLockChat}
closeChat={ visibleSession={visibleSession}
!canvasOpen closeChat={
? undefined !canvasOpen
: () => { ? undefined
setOpen(false); : () => {
} setOpen(false);
} }
/> }
/>
)}
</div> </div>
) : ( ) : (
<span className="flex h-full w-full items-center justify-center font-thin text-muted-foreground"> <span className="flex h-full w-full items-center justify-center font-thin text-muted-foreground">