2023-06-28 20:39:27 +03:00
|
|
|
import { useBrainContext } from "@/lib/context/BrainProvider/hooks/useBrainContext";
|
2023-06-22 18:50:06 +03:00
|
|
|
import { useAxios } from "@/lib/hooks";
|
|
|
|
|
|
|
|
import { ChatEntity, ChatHistory, ChatQuestion } from "../types";
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
export const useChatService = () => {
|
|
|
|
const { axiosInstance } = useAxios();
|
2023-06-28 20:39:27 +03:00
|
|
|
const { currentBrain } = useBrainContext();
|
2023-06-22 18:50:06 +03:00
|
|
|
const createChat = async ({ name }: { name: string }) => {
|
|
|
|
return axiosInstance.post<ChatEntity>(`/chat`, { name });
|
|
|
|
};
|
|
|
|
|
|
|
|
const getChatHistory = async (chatId: string | undefined) => {
|
|
|
|
if (chatId === undefined) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const rep = (
|
|
|
|
await axiosInstance.get<ChatHistory[]>(`/chat/${chatId}/history`)
|
|
|
|
).data;
|
|
|
|
|
|
|
|
return rep;
|
|
|
|
};
|
|
|
|
const addQuestion = async (
|
|
|
|
chatId: string,
|
|
|
|
chatQuestion: ChatQuestion
|
|
|
|
): Promise<ChatHistory> => {
|
2023-06-28 20:39:27 +03:00
|
|
|
if (currentBrain?.id === undefined) {
|
|
|
|
throw new Error("No current brain");
|
|
|
|
}
|
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
return (
|
|
|
|
await axiosInstance.post<ChatHistory>(
|
2023-06-29 08:33:19 +03:00
|
|
|
`/chat/${chatId}/question?brain_id=${currentBrain.id}`,
|
2023-06-22 18:50:06 +03:00
|
|
|
chatQuestion
|
|
|
|
)
|
|
|
|
).data;
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
createChat,
|
|
|
|
getChatHistory,
|
|
|
|
addQuestion,
|
|
|
|
};
|
|
|
|
};
|