Spaces:
No application file
No application file
File size: 4,809 Bytes
a85c9b8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
import { useRouter } from "next/router";
import React, { useState, useEffect } from "react";
import BotWrapper from "@/components/chat/BotWrapper";
import HumanWrapper from "@/components/chat/HumanWrapper";
import SetSources from "@/containers/SetSources";
export default function ChatWindow({ embedding_model, app_type, setBotTitle }) {
const [bot, setBot] = useState(null);
const [chats, setChats] = useState([]);
const [isLoading, setIsLoading] = useState(false);
const [selectChat, setSelectChat] = useState(true);
const router = useRouter();
const { bot_slug } = router.query;
useEffect(() => {
if (bot_slug) {
const fetchBots = async () => {
const response = await fetch("/api/get_bots");
const data = await response.json();
const matchingBot = data.find((item) => item.slug === bot_slug);
setBot(matchingBot);
setBotTitle(matchingBot.name);
};
fetchBots();
}
}, [bot_slug]);
useEffect(() => {
const storedChats = localStorage.getItem(`chat_${bot_slug}_${app_type}`);
if (storedChats) {
const parsedChats = JSON.parse(storedChats);
setChats(parsedChats.chats);
}
}, [app_type, bot_slug]);
const handleChatResponse = async (e) => {
e.preventDefault();
setIsLoading(true);
const queryInput = e.target.query.value;
e.target.query.value = "";
const chatEntry = {
sender: "H",
message: queryInput,
};
setChats((prevChats) => [...prevChats, chatEntry]);
const response = await fetch("/api/get_answer", {
method: "POST",
body: JSON.stringify({
query: queryInput,
embedding_model,
app_type,
}),
headers: {
"Content-Type": "application/json",
},
});
const data = await response.json();
if (response.ok) {
const botResponse = data.response;
const botEntry = {
sender: "B",
message: botResponse,
};
setIsLoading(false);
setChats((prevChats) => [...prevChats, botEntry]);
const savedChats = {
chats: [...chats, chatEntry, botEntry],
};
localStorage.setItem(
`chat_${bot_slug}_${app_type}`,
JSON.stringify(savedChats)
);
} else {
router.reload();
}
};
return (
<>
<div className="flex flex-col justify-between h-full">
<div className="space-y-4 overflow-x-auto h-full pb-8">
{/* Greeting Message */}
<BotWrapper>
Hi, I am {bot?.name}. How can I help you today?
</BotWrapper>
{/* Chat Messages */}
{chats.map((chat, index) => (
<React.Fragment key={index}>
{chat.sender === "B" ? (
<BotWrapper>{chat.message}</BotWrapper>
) : (
<HumanWrapper>{chat.message}</HumanWrapper>
)}
</React.Fragment>
))}
{/* Loader */}
{isLoading && (
<BotWrapper>
<div className="flex items-center justify-center space-x-2 animate-pulse">
<div className="w-2 h-2 bg-black rounded-full"></div>
<div className="w-2 h-2 bg-black rounded-full"></div>
<div className="w-2 h-2 bg-black rounded-full"></div>
</div>
</BotWrapper>
)}
</div>
<div className="bg-white fixed bottom-0 left-0 right-0 h-28 sm:h-16"></div>
{/* Query Form */}
<div className="flex flex-row gap-x-2 sticky bottom-3">
<SetSources
setChats={setChats}
embedding_model={embedding_model}
setSelectChat={setSelectChat}
/>
{selectChat && (
<form
onSubmit={handleChatResponse}
className="w-full flex flex-col sm:flex-row gap-y-2 gap-x-2"
>
<div className="w-full">
<input
id="query"
name="query"
type="text"
placeholder="Enter your query..."
className="text-sm w-full border-2 border-black rounded-xl focus:outline-none focus:border-blue-800 sm:pl-4 h-11"
required
/>
</div>
<div className="w-full sm:w-fit">
<button
type="submit"
id="sender"
disabled={isLoading}
className={`${
isLoading ? "opacity-60" : ""
} w-full bg-black hover:bg-blue-800 rounded-xl text-lg text-white px-6 h-11`}
>
Send
</button>
</div>
</form>
)}
</div>
</div>
</>
);
}
|