mirror of
https://github.com/QuivrHQ/quivr.git
synced 2024-12-17 19:32:47 +03:00
80be40ad34
* feat: remove react-mentions * feat: add chat header * feat: remove v2/chat * feat: add fature flag * feat: add new chat UI * feat: add prompt and brain name to messages
93 lines
2.4 KiB
TypeScript
93 lines
2.4 KiB
TypeScript
/* eslint-disable max-lines */
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { useBrainContext } from "@/lib/context/BrainProvider/hooks/useBrainContext";
|
|
import { useChatContext } from "@/lib/context/ChatProvider/hooks/useChatContext";
|
|
import { useFetch } from "@/lib/hooks";
|
|
|
|
import { ChatHistory, ChatQuestion } from "../types";
|
|
|
|
interface UseChatService {
|
|
addStreamQuestion: (
|
|
chatId: string,
|
|
chatQuestion: ChatQuestion
|
|
) => Promise<void>;
|
|
}
|
|
|
|
export const useQuestion = (): UseChatService => {
|
|
const { fetchInstance } = useFetch();
|
|
const { updateStreamingHistory } = useChatContext();
|
|
const { currentBrain } = useBrainContext();
|
|
|
|
const { t } = useTranslation(["chat"]);
|
|
|
|
const handleStream = async (
|
|
reader: ReadableStreamDefaultReader<Uint8Array>
|
|
): Promise<void> => {
|
|
const decoder = new TextDecoder("utf-8");
|
|
|
|
const handleStreamRecursively = async () => {
|
|
const { done, value } = await reader.read();
|
|
|
|
if (done) {
|
|
return;
|
|
}
|
|
|
|
const dataStrings = decoder
|
|
.decode(value)
|
|
.trim()
|
|
.split("data: ")
|
|
.filter(Boolean);
|
|
|
|
dataStrings.forEach((data) => {
|
|
try {
|
|
const parsedData = JSON.parse(data) as ChatHistory;
|
|
updateStreamingHistory(parsedData);
|
|
} catch (error) {
|
|
console.error(t("errorParsingData", { ns: "chat" }), error);
|
|
}
|
|
});
|
|
|
|
await handleStreamRecursively();
|
|
};
|
|
|
|
await handleStreamRecursively();
|
|
};
|
|
|
|
const addStreamQuestion = async (
|
|
chatId: string,
|
|
chatQuestion: ChatQuestion
|
|
): Promise<void> => {
|
|
if (currentBrain?.id === undefined) {
|
|
throw new Error(t("noCurrentBrain", { ns: "chat" }));
|
|
}
|
|
const headers = {
|
|
"Content-Type": "application/json",
|
|
Accept: "text/event-stream",
|
|
};
|
|
const body = JSON.stringify(chatQuestion);
|
|
console.log("Calling API...");
|
|
try {
|
|
const response = await fetchInstance.post(
|
|
`/chat/${chatId}/question/stream?brain_id=${currentBrain.id}`,
|
|
body,
|
|
headers
|
|
);
|
|
|
|
if (response.body === null) {
|
|
throw new Error(t("resposeBodyNull", { ns: "chat" }));
|
|
}
|
|
|
|
console.log(t("receivedResponse"), response);
|
|
await handleStream(response.body.getReader());
|
|
} catch (error) {
|
|
console.error(t("errorCallingAPI", { ns: "chat" }), error);
|
|
}
|
|
};
|
|
|
|
return {
|
|
addStreamQuestion,
|
|
};
|
|
};
|