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";
|
|
|
|
|
2023-08-01 02:14:16 +03:00
|
|
|
|
2023-07-05 14:33:26 +03:00
|
|
|
import { useChatApi } from "@/lib/api/chat/useChatApi";
|
2023-06-22 18:50:06 +03:00
|
|
|
import { useBrainConfig } from "@/lib/context/BrainConfigProvider/hooks/useBrainConfig";
|
2023-07-05 10:30:22 +03:00
|
|
|
import { useChatContext } from "@/lib/context/ChatProvider/hooks/useChatContext";
|
2023-06-22 18:50:06 +03:00
|
|
|
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
|
|
|
|
2023-08-01 02:14:16 +03:00
|
|
|
|
2023-07-31 22:34:34 +03:00
|
|
|
import { useQuestion } from "./useQuestion";
|
2023-08-01 02:14:16 +03:00
|
|
|
import { ChatQuestion } from "../types";
|
|
|
|
|
2023-07-31 22:34:34 +03:00
|
|
|
|
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
|
|
|
|
// 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();
|
2023-07-05 19:33:18 +03:00
|
|
|
const { createChat, getHistory } = useChatApi();
|
2023-06-22 18:50:06 +03:00
|
|
|
|
2023-08-01 02:14:16 +03:00
|
|
|
const { addStreamQuestion } = useQuestion();
|
2023-06-22 18:50:06 +03:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const fetchHistory = async () => {
|
2023-06-30 11:10:59 +03:00
|
|
|
const currentChatId = chatId;
|
2023-07-05 19:33:18 +03:00
|
|
|
if (currentChatId === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const chatHistory = await getHistory(currentChatId);
|
2023-06-30 11:10:59 +03:00
|
|
|
|
|
|
|
if (chatId === currentChatId && chatHistory.length > 0) {
|
|
|
|
setHistory(chatHistory);
|
|
|
|
}
|
2023-06-22 18:50:06 +03:00
|
|
|
};
|
|
|
|
void fetchHistory();
|
2023-07-05 19:33:18 +03:00
|
|
|
}, [chatId, setHistory]);
|
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 {
|
|
|
|
setGeneratingAnswer(true);
|
2023-07-05 19:33:18 +03:00
|
|
|
|
|
|
|
let currentChatId = chatId;
|
|
|
|
|
|
|
|
//if chatId is not set, create a new chat. Chat name is from the first question
|
|
|
|
if (currentChatId === undefined) {
|
|
|
|
const chatName = question.split(" ").slice(0, 3).join(" ");
|
|
|
|
const chat = await createChat(chatName);
|
|
|
|
currentChatId = chat.chat_id;
|
|
|
|
setChatId(currentChatId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void track("QUESTION_ASKED");
|
2023-06-30 11:10:59 +03:00
|
|
|
|
2023-07-31 22:34:34 +03:00
|
|
|
|
|
|
|
await addStreamQuestion(currentChatId, chatQuestion);
|
|
|
|
|
2023-06-30 11:10:59 +03:00
|
|
|
|
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
|
|
|
};
|