2023-06-30 11:10:59 +03:00
|
|
|
/* eslint-disable max-lines */
|
2023-08-18 11:32:22 +03:00
|
|
|
import { AxiosError } from 'axios';
|
|
|
|
import { useParams } from 'next/navigation';
|
|
|
|
import { useState } from 'react';
|
|
|
|
import { useTranslation } from 'react-i18next';
|
2023-06-22 18:50:06 +03:00
|
|
|
|
2023-08-18 11:32:22 +03:00
|
|
|
import { getChatConfigFromLocalStorage } from '@/lib/api/chat/chat.local';
|
|
|
|
import { useChatApi } from '@/lib/api/chat/useChatApi';
|
|
|
|
import { useBrainContext } from '@/lib/context/BrainProvider/hooks/useBrainContext';
|
|
|
|
import { useChatContext } from '@/lib/context/ChatProvider/hooks/useChatContext';
|
|
|
|
import { useToast } from '@/lib/hooks';
|
|
|
|
import { useEventTracking } from '@/services/analytics/useEventTracking';
|
2023-06-22 18:50:06 +03:00
|
|
|
|
2023-08-18 11:32:22 +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-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-01 17:25:02 +03:00
|
|
|
|
2023-08-11 11:06:20 +03:00
|
|
|
const { history } = useChatContext();
|
|
|
|
const { currentBrain } = 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 11:32:22 +03:00
|
|
|
const { t } = useTranslation(['chat']);
|
2023-06-22 18:50:06 +03:00
|
|
|
|
|
|
|
const addQuestion = async (question: string, callback?: () => void) => {
|
|
|
|
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) {
|
2023-08-18 11:32:22 +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-18 11:32:22 +03:00
|
|
|
void track('QUESTION_ASKED');
|
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-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?.();
|
|
|
|
} catch (error) {
|
|
|
|
console.error({ error });
|
|
|
|
|
|
|
|
if ((error as AxiosError).response?.status === 429) {
|
|
|
|
publish({
|
2023-08-18 11:32:22 +03:00
|
|
|
variant: 'danger',
|
|
|
|
text: t('limit_reached', { ns: 'chat' }),
|
2023-06-22 18:50:06 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
publish({
|
2023-08-18 11:32:22 +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 {
|
|
|
|
history,
|
|
|
|
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
|
|
|
};
|