2023-06-30 11:10:59 +03:00
|
|
|
/* eslint-disable max-lines */
|
2023-08-22 11:05:52 +03:00
|
|
|
import { AxiosError } from "axios";
|
2023-08-30 10:45:11 +03:00
|
|
|
import { useParams, useRouter } from "next/navigation";
|
2023-08-22 11:05:52 +03:00
|
|
|
import { useState } from "react";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2023-06-22 18:50:06 +03:00
|
|
|
|
2023-08-22 11:05:52 +03:00
|
|
|
import { getChatConfigFromLocalStorage } from "@/lib/api/chat/chat.local";
|
|
|
|
import { useChatApi } from "@/lib/api/chat/useChatApi";
|
2023-09-12 18:44:15 +03:00
|
|
|
import { useChatContext } from "@/lib/context";
|
2023-08-22 11:05:52 +03:00
|
|
|
import { useBrainContext } from "@/lib/context/BrainProvider/hooks/useBrainContext";
|
|
|
|
import { useToast } from "@/lib/hooks";
|
|
|
|
import { useEventTracking } from "@/services/analytics/useEventTracking";
|
2023-06-22 18:50:06 +03:00
|
|
|
|
2023-08-22 11:05:52 +03:00
|
|
|
import { useQuestion } from "./useQuestion";
|
|
|
|
import { ChatQuestion } from "../types";
|
2023-08-01 02:14:16 +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-09-12 18:44:15 +03:00
|
|
|
|
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);
|
2023-08-30 10:45:11 +03:00
|
|
|
const router = useRouter();
|
2023-09-12 18:44:15 +03:00
|
|
|
const { messages } = useChatContext();
|
2023-08-29 13:26:08 +03:00
|
|
|
const { currentBrain, currentPromptId, currentBrainId } = useBrainContext();
|
2023-06-22 18:50:06 +03:00
|
|
|
const { publish } = useToast();
|
2023-08-11 11:06:20 +03:00
|
|
|
const { createChat } = useChatApi();
|
2023-06-22 18:50:06 +03:00
|
|
|
|
2023-08-01 02:14:16 +03:00
|
|
|
const { addStreamQuestion } = useQuestion();
|
2023-08-18 12:59:20 +03:00
|
|
|
const { t } = useTranslation(["chat"]);
|
2023-06-22 18:50:06 +03:00
|
|
|
|
|
|
|
const addQuestion = async (question: string, callback?: () => void) => {
|
2023-08-22 11:05:52 +03:00
|
|
|
if (question === "") {
|
|
|
|
publish({
|
|
|
|
variant: "danger",
|
|
|
|
text: t("ask"),
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
try {
|
|
|
|
setGeneratingAnswer(true);
|
2023-07-05 19:33:18 +03:00
|
|
|
|
|
|
|
let currentChatId = chatId;
|
|
|
|
|
2023-08-30 10:45:11 +03:00
|
|
|
let shouldUpdateUrl = false;
|
|
|
|
|
2023-07-05 19:33:18 +03:00
|
|
|
//if chatId is not set, create a new chat. Chat name is from the first question
|
|
|
|
if (currentChatId === undefined) {
|
2023-08-22 11:05:52 +03:00
|
|
|
const chatName = question.split(" ").slice(0, 3).join(" ");
|
2023-07-05 19:33:18 +03:00
|
|
|
const chat = await createChat(chatName);
|
|
|
|
currentChatId = chat.chat_id;
|
|
|
|
setChatId(currentChatId);
|
2023-08-30 10:45:11 +03:00
|
|
|
shouldUpdateUrl = true;
|
2023-08-18 12:59:20 +03:00
|
|
|
//TODO: update chat list here
|
2023-07-05 19:33:18 +03:00
|
|
|
}
|
|
|
|
|
2023-08-29 13:26:08 +03:00
|
|
|
void track("QUESTION_ASKED", {
|
|
|
|
brainId: currentBrainId,
|
|
|
|
promptId: currentPromptId,
|
|
|
|
});
|
|
|
|
|
2023-08-01 17:25:02 +03:00
|
|
|
const chatConfig = getChatConfigFromLocalStorage(currentChatId);
|
|
|
|
|
|
|
|
const chatQuestion: ChatQuestion = {
|
|
|
|
model: chatConfig?.model,
|
|
|
|
question,
|
|
|
|
temperature: chatConfig?.temperature,
|
|
|
|
max_tokens: chatConfig?.maxTokens,
|
2023-08-18 11:32:22 +03:00
|
|
|
brain_id: currentBrain?.id,
|
2023-08-29 11:50:36 +03:00
|
|
|
prompt_id: currentPromptId ?? undefined,
|
2023-08-01 17:25:02 +03:00
|
|
|
};
|
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?.();
|
2023-08-30 10:45:11 +03:00
|
|
|
|
|
|
|
if (shouldUpdateUrl) {
|
|
|
|
router.replace(`/chat/${currentChatId}`);
|
|
|
|
}
|
2023-06-22 18:50:06 +03:00
|
|
|
} catch (error) {
|
|
|
|
console.error({ error });
|
|
|
|
|
|
|
|
if ((error as AxiosError).response?.status === 429) {
|
|
|
|
publish({
|
2023-08-22 11:05:52 +03:00
|
|
|
variant: "danger",
|
|
|
|
text: t("limit_reached", { ns: "chat" }),
|
2023-06-22 18:50:06 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
publish({
|
2023-08-22 11:05:52 +03:00
|
|
|
variant: "danger",
|
|
|
|
text: t("error_occurred", { ns: "chat" }),
|
2023-06-22 18:50:06 +03:00
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
setGeneratingAnswer(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-06-30 11:10:59 +03:00
|
|
|
return {
|
2023-09-12 18:44:15 +03:00
|
|
|
messages,
|
2023-06-30 11:10:59 +03:00
|
|
|
addQuestion,
|
|
|
|
generatingAnswer,
|
2023-08-01 17:25:02 +03:00
|
|
|
chatId,
|
2023-06-30 11:10:59 +03:00
|
|
|
};
|
2023-06-22 18:50:06 +03:00
|
|
|
};
|