2023-06-30 11:10:59 +03:00
|
|
|
/* eslint-disable max-lines */
|
2023-06-22 18:50:06 +03:00
|
|
|
import { AxiosError } from "axios";
|
|
|
|
import { useParams } from "next/navigation";
|
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
|
|
|
|
import { useBrainConfig } from "@/lib/context/BrainConfigProvider/hooks/useBrainConfig";
|
|
|
|
import { useToast } from "@/lib/hooks";
|
2023-06-26 13:54:07 +03:00
|
|
|
import { useEventTracking } from "@/services/analytics/useEventTracking";
|
2023-06-22 18:50:06 +03:00
|
|
|
|
|
|
|
import { useChatService } from "./useChatService";
|
|
|
|
import { useChatContext } from "../context/ChatContext";
|
|
|
|
import { ChatQuestion } from "../types";
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
export const useChat = () => {
|
2023-06-26 13:54:07 +03:00
|
|
|
const { track } = useEventTracking();
|
2023-06-22 18:50:06 +03:00
|
|
|
const params = useParams();
|
|
|
|
const [chatId, setChatId] = useState<string | undefined>(
|
|
|
|
params?.chatId as string | undefined
|
|
|
|
);
|
|
|
|
const [generatingAnswer, setGeneratingAnswer] = useState(false);
|
|
|
|
const {
|
|
|
|
config: { maxTokens, model, temperature },
|
|
|
|
} = useBrainConfig();
|
2023-06-30 11:10:59 +03:00
|
|
|
const { history, setHistory } = useChatContext();
|
2023-06-22 18:50:06 +03:00
|
|
|
const { publish } = useToast();
|
|
|
|
|
|
|
|
const {
|
|
|
|
createChat,
|
|
|
|
getChatHistory,
|
2023-06-30 11:10:59 +03:00
|
|
|
addStreamQuestion,
|
|
|
|
addQuestion: addQuestionToModel,
|
2023-06-22 18:50:06 +03:00
|
|
|
} = useChatService();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const fetchHistory = async () => {
|
2023-06-30 11:10:59 +03:00
|
|
|
const currentChatId = chatId;
|
|
|
|
const chatHistory = await getChatHistory(currentChatId);
|
|
|
|
|
|
|
|
if (chatId === currentChatId && chatHistory.length > 0) {
|
|
|
|
setHistory(chatHistory);
|
|
|
|
}
|
2023-06-22 18:50:06 +03:00
|
|
|
};
|
|
|
|
void fetchHistory();
|
2023-06-30 11:10:59 +03:00
|
|
|
}, [chatId, getChatHistory, setHistory]);
|
2023-06-22 18:50:06 +03:00
|
|
|
|
|
|
|
const generateNewChatIdFromName = async (
|
|
|
|
chatName: string
|
|
|
|
): Promise<string> => {
|
2023-06-30 11:10:59 +03:00
|
|
|
const chat = await createChat({ name: chatName });
|
2023-06-22 18:50:06 +03:00
|
|
|
|
2023-06-30 11:10:59 +03:00
|
|
|
return chat.chat_id;
|
2023-06-22 18:50:06 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const addQuestion = async (question: string, callback?: () => void) => {
|
|
|
|
const chatQuestion: ChatQuestion = {
|
|
|
|
model,
|
|
|
|
question,
|
|
|
|
temperature,
|
|
|
|
max_tokens: maxTokens,
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2023-06-26 13:54:07 +03:00
|
|
|
void track("QUESTION_ASKED");
|
2023-06-22 18:50:06 +03:00
|
|
|
setGeneratingAnswer(true);
|
|
|
|
const currentChatId =
|
|
|
|
chatId ??
|
|
|
|
// if chatId is undefined, we need to create a new chat on fly
|
|
|
|
(await generateNewChatIdFromName(
|
|
|
|
question.split(" ").slice(0, 3).join(" ")
|
|
|
|
));
|
2023-06-30 11:10:59 +03:00
|
|
|
|
|
|
|
setChatId(currentChatId);
|
|
|
|
|
|
|
|
if (chatQuestion.model === "gpt-3.5-turbo") {
|
|
|
|
await addStreamQuestion(currentChatId, chatQuestion);
|
|
|
|
} else {
|
|
|
|
await addQuestionToModel(currentChatId, chatQuestion);
|
|
|
|
}
|
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
callback?.();
|
|
|
|
} catch (error) {
|
|
|
|
console.error({ error });
|
|
|
|
|
|
|
|
if ((error as AxiosError).response?.status === 429) {
|
|
|
|
publish({
|
|
|
|
variant: "danger",
|
|
|
|
text: "You have reached the limit of requests, please try again later",
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
publish({
|
|
|
|
variant: "danger",
|
|
|
|
text: "Error occurred while getting answer",
|
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
setGeneratingAnswer(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-06-30 11:10:59 +03:00
|
|
|
return {
|
|
|
|
history,
|
|
|
|
addQuestion,
|
|
|
|
generatingAnswer,
|
|
|
|
};
|
2023-06-22 18:50:06 +03:00
|
|
|
};
|